This commit is contained in:
Travis Tidwell 2021-03-30 01:25:45 +00:00 committed by GitHub
commit 524df10efe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 2 deletions

View file

@ -372,6 +372,23 @@ Pass an array of objects:
**Usage:** The maximum amount of search results to show. **Usage:** The maximum amount of search results to show.
### shadowRoot
**Type:** Document Element **Default:** null
**Input types affected:** `select-one`, `select-multiple`
**Usage:** You can pass along the shadowRoot from your application like so.
```js
var shadowRoot = document
.getElementById('wrapper')
.attachShadow({ mode: 'open' });
var choices = new Choices({
shadowRoot: shadowRoot,
});
```
### position ### position
**Type:** `String` **Default:** `auto` **Type:** `String` **Default:** `auto`

View file

@ -128,6 +128,10 @@ class Choices {
// instead of concatenating with the default array // instead of concatenating with the default array
{ arrayMerge: (_, sourceArray) => [...sourceArray] }, { arrayMerge: (_, sourceArray) => [...sourceArray] },
); );
// Restore the shadowRoot if provided. deeperge converts it into an empty object.
if (userConfig.shadowRoot) {
this.config.shadowRoot = userConfig.shadowRoot;
}
const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG); const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG);
if (invalidConfigOptions.length) { if (invalidConfigOptions.length) {
@ -1308,7 +1312,7 @@ class Choices {
} }
_addEventListeners(): void { _addEventListeners(): void {
const { documentElement } = document; const documentElement = this.config.shadowRoot || document.documentElement;
// capture events - can cancel event processing or propagation // capture events - can cancel event processing or propagation
documentElement.addEventListener('touchend', this._onTouchEnd, true); documentElement.addEventListener('touchend', this._onTouchEnd, true);
@ -1362,7 +1366,7 @@ class Choices {
} }
_removeEventListeners(): void { _removeEventListeners(): void {
const { documentElement } = document; const documentElement = this.config.shadowRoot || document.documentElement;
documentElement.removeEventListener('touchend', this._onTouchEnd, true); documentElement.removeEventListener('touchend', this._onTouchEnd, true);
this.containerOuter.element.removeEventListener( this.containerOuter.element.removeEventListener(

View file

@ -60,6 +60,7 @@ export const DEFAULT_CONFIG: Options = {
shouldSort: true, shouldSort: true,
shouldSortItems: false, shouldSortItems: false,
sorter: sortByAlpha, sorter: sortByAlpha,
shadowRoot: null,
placeholder: true, placeholder: true,
placeholderValue: null, placeholderValue: null,
searchPlaceholderValue: null, searchPlaceholderValue: null,

View file

@ -503,6 +503,11 @@ export interface Options {
*/ */
resetScrollPosition: boolean; resetScrollPosition: boolean;
/**
* The shadow root for use within ShadowDom
*/
shadowRoot: any;
/** /**
* Whether choices and groups should be sorted. If false, choices/groups will appear in the order they were given. * Whether choices and groups should be sorted. If false, choices/groups will appear in the order they were given.
* *