|
1 | | -const SELECT_MODIFIER_ACTIVE = 'ray-select--active'; |
2 | | -const SELECT_EL_INPUT = 'ray-select__input'; |
3 | | -const SELECT_MODIFIER_HAS_VALUE = 'ray-select--has-value'; |
4 | | -const SELECT_MODIFIER_PLACEHOLDER_MODE = 'ray-select--placeholder-mode'; |
| 1 | +import { CSS_CLASSES, STRINGS } from './constants'; |
| 2 | +import { validateNodeType, isTargetingItself } from './util'; |
5 | 3 |
|
6 | 4 | class Select { |
7 | | - constructor(element, options) { |
8 | | - this.element = element; |
9 | | - this.inputElement = element.querySelector(`.${SELECT_EL_INPUT}`); |
10 | | - this.bindEventListeners(); |
| 5 | + static instances = new WeakMap(); |
11 | 6 |
|
12 | | - const option = this.getCurrentValueOptionElement(); |
| 7 | + static get cssClasses() { |
| 8 | + return CSS_CLASSES; |
| 9 | + } |
| 10 | + |
| 11 | + static get strings() { |
| 12 | + return STRINGS; |
| 13 | + } |
| 14 | + |
| 15 | + static create(element, options) { |
| 16 | + return this.instances.get(element) || new this(element, options); |
| 17 | + } |
| 18 | + |
| 19 | + static createAll(target = document, _options = {}) { |
| 20 | + // Finds all instances of select on the document or within a given element and instantiates them. |
| 21 | + const options = { |
| 22 | + initSelector: this.strings.INIT_SELECTOR, |
| 23 | + ..._options |
| 24 | + }; |
| 25 | + |
| 26 | + validateNodeType(target); |
| 27 | + |
| 28 | + if (isTargetingItself(target, options)) { |
| 29 | + this.create(target, options); |
| 30 | + } else { |
| 31 | + const selects = [...target.querySelectorAll(options.initSelector)]; |
| 32 | + return selects.forEach(select => this.create(select, options)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + constructor(root, options) { |
| 37 | + this._root = root; |
| 38 | + this._inputElement = this._root.querySelector( |
| 39 | + `.${this.constructor.cssClasses.EL__INPUT}` |
| 40 | + ); |
| 41 | + |
| 42 | + if (!this._inputElement) { |
| 43 | + throw new Error( |
| 44 | + `Select must have an input element with a class of .${ |
| 45 | + this.constructor.cssClasses.EL__INPUT |
| 46 | + }` |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + this._bindEventListeners(); |
| 51 | + |
| 52 | + const option = this._getCurrentValueOptionElement(); |
13 | 53 |
|
14 | 54 | if (option && option.innerHTML) { |
15 | 55 | if (option.dataset.rayPlaceholder) { |
16 | | - this.element.classList.add(SELECT_MODIFIER_PLACEHOLDER_MODE); |
| 56 | + this._root.classList.add(this.constructor.cssClasses.PLACEHOLDER_MODE); |
17 | 57 | } else { |
18 | | - this.element.classList.add(SELECT_MODIFIER_HAS_VALUE); |
| 58 | + this._root.classList.add(this.constructor.cssClasses.HAS_VALUE); |
19 | 59 | } |
20 | 60 | } |
| 61 | + |
| 62 | + this.constructor.instances.set(this._root, this); |
21 | 63 | } |
22 | 64 |
|
23 | | - bindEventListeners() { |
24 | | - this.inputElement.addEventListener('focus', this.onFocus); |
25 | | - this.inputElement.addEventListener('blur', this.onBlur); |
26 | | - this.inputElement.addEventListener('change', this.onChange); |
| 65 | + _bindEventListeners() { |
| 66 | + this._inputElement.addEventListener('focus', this.onFocus); |
| 67 | + this._inputElement.addEventListener('blur', this.onBlur); |
| 68 | + this._inputElement.addEventListener('change', this.onChange); |
27 | 69 | } |
28 | 70 |
|
29 | 71 | value() { |
30 | | - return this.element.value; |
| 72 | + // Current value of the Select |
| 73 | + return this._inputElement.value; |
| 74 | + } |
| 75 | + |
| 76 | + set(value) { |
| 77 | + this._inputElement.value = value; |
| 78 | + this._inputElement.dispatchEvent(new Event('change')); |
31 | 79 | } |
32 | 80 |
|
33 | 81 | onFocus = () => { |
34 | | - this.element.classList.add(SELECT_MODIFIER_ACTIVE); |
| 82 | + this._root.classList.add(this.constructor.cssClasses.ACTIVE); |
35 | 83 | }; |
36 | 84 |
|
37 | 85 | onBlur = () => { |
38 | | - this.element.classList.remove(SELECT_MODIFIER_ACTIVE); |
| 86 | + this._root.classList.remove(this.constructor.cssClasses.ACTIVE); |
39 | 87 | }; |
40 | 88 |
|
41 | 89 | onChange = () => { |
42 | | - const option = this.getCurrentValueOptionElement(); |
| 90 | + const option = this._getCurrentValueOptionElement(); |
43 | 91 |
|
44 | 92 | if (option) { |
45 | 93 | if (option.dataset.rayPlaceholder) { |
46 | | - this.element.classList.add(SELECT_MODIFIER_PLACEHOLDER_MODE); |
47 | | - this.element.classList.remove(SELECT_MODIFIER_HAS_VALUE); |
| 94 | + this._root.classList.add(this.constructor.cssClasses.PLACEHOLDER_MODE); |
| 95 | + this._root.classList.remove(this.constructor.cssClasses.HAS_VALUE); |
48 | 96 | } else { |
49 | | - this.element.classList.add(SELECT_MODIFIER_HAS_VALUE); |
50 | | - this.element.classList.remove(SELECT_MODIFIER_PLACEHOLDER_MODE); |
| 97 | + this._root.classList.add(this.constructor.cssClasses.HAS_VALUE); |
| 98 | + this._root.classList.remove( |
| 99 | + this.constructor.cssClasses.PLACEHOLDER_MODE |
| 100 | + ); |
51 | 101 | } |
52 | 102 | } else { |
53 | | - this.element.classList.remove( |
54 | | - SELECT_MODIFIER_PLACEHOLDER_MODE, |
55 | | - SELECT_MODIFIER_HAS_VALUE |
| 103 | + this._root.classList.remove( |
| 104 | + this.constructor.cssClasses.PLACEHOLDER_MODE, |
| 105 | + this.constructor.cssClasses.HAS_VALUE |
56 | 106 | ); |
57 | 107 | } |
58 | 108 | }; |
59 | 109 |
|
60 | | - getCurrentValueOptionElement = () => { |
61 | | - return this.inputElement.options[this.inputElement.selectedIndex]; |
| 110 | + _getCurrentValueOptionElement = () => { |
| 111 | + return this._inputElement.options[this._inputElement.selectedIndex]; |
62 | 112 | }; |
63 | 113 |
|
64 | | - teardownEventListeners() {} |
| 114 | + destroy() { |
| 115 | + // Implement this method to release any resources / deregister any listeners they have |
| 116 | + // attached. An example of this might be deregistering a resize event from the window object. |
| 117 | + this._inputElement.removeEventListener('focus', this.onFocus); |
| 118 | + this._inputElement.removeEventListener('blur', this.onBlur); |
| 119 | + this._inputElement.removeEventListener('change', this.onChange); |
| 120 | + |
| 121 | + this.constructor.instances.delete(this._root); |
| 122 | + } |
65 | 123 | } |
66 | 124 |
|
67 | | -export { Select }; |
| 125 | +export default Select; |
0 commit comments