-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(select): add javascripts * delete comment * delete file * cleans up code, adds specs * tweak animation
- Loading branch information
1 parent
546a8af
commit e9c1cfe
Showing
12 changed files
with
1,456 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: 'current' | ||
} | ||
} | ||
] | ||
], | ||
plugins: ['@babel/plugin-proposal-class-properties'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## JavaScript | ||
|
||
```javascript | ||
import { Select } from '@wework/ray'; | ||
|
||
Select.createAll(); | ||
// or | ||
Select.create(document.querySelector('.ray-select')); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const CSS_CLASSES = { | ||
ACTIVE: 'ray-select--active', | ||
HAS_VALUE: 'ray-select--has-value', | ||
PLACEHOLDER_MODE: 'ray-select--placeholder-mode', | ||
EL__INPUT: 'ray-select__input' | ||
}; | ||
|
||
export const STRINGS = { | ||
INIT_SELECTOR: 'ray-select' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,125 @@ | ||
const SELECT_MODIFIER_ACTIVE = 'ray-select--active'; | ||
const SELECT_EL_INPUT = 'ray-select__input'; | ||
const SELECT_MODIFIER_HAS_VALUE = 'ray-select--has-value'; | ||
const SELECT_MODIFIER_PLACEHOLDER_MODE = 'ray-select--placeholder-mode'; | ||
import { CSS_CLASSES, STRINGS } from './constants'; | ||
import { validateNodeType, isTargetingItself } from './util'; | ||
|
||
class Select { | ||
constructor(element, options) { | ||
this.element = element; | ||
this.inputElement = element.querySelector(`.${SELECT_EL_INPUT}`); | ||
this.bindEventListeners(); | ||
static instances = new WeakMap(); | ||
|
||
const option = this.getCurrentValueOptionElement(); | ||
static get cssClasses() { | ||
return CSS_CLASSES; | ||
} | ||
|
||
static get strings() { | ||
return STRINGS; | ||
} | ||
|
||
static create(element, options) { | ||
return this.instances.get(element) || new this(element, options); | ||
} | ||
|
||
static createAll(target = document, _options = {}) { | ||
// Finds all instances of select on the document or within a given element and instantiates them. | ||
const options = { | ||
initSelector: this.strings.INIT_SELECTOR, | ||
..._options | ||
}; | ||
|
||
validateNodeType(target); | ||
|
||
if (isTargetingItself(target, options)) { | ||
this.create(target, options); | ||
} else { | ||
const selects = [...target.querySelectorAll(options.initSelector)]; | ||
return selects.forEach(select => this.create(select, options)); | ||
} | ||
} | ||
|
||
constructor(root, options) { | ||
this._root = root; | ||
this._inputElement = this._root.querySelector( | ||
`.${this.constructor.cssClasses.EL__INPUT}` | ||
); | ||
|
||
if (!this._inputElement) { | ||
throw new Error( | ||
`Select must have an input element with a class of .${ | ||
this.constructor.cssClasses.EL__INPUT | ||
}` | ||
); | ||
} | ||
|
||
this._bindEventListeners(); | ||
|
||
const option = this._getCurrentValueOptionElement(); | ||
|
||
if (option && option.innerHTML) { | ||
if (option.dataset.rayPlaceholder) { | ||
this.element.classList.add(SELECT_MODIFIER_PLACEHOLDER_MODE); | ||
this._root.classList.add(this.constructor.cssClasses.PLACEHOLDER_MODE); | ||
} else { | ||
this.element.classList.add(SELECT_MODIFIER_HAS_VALUE); | ||
this._root.classList.add(this.constructor.cssClasses.HAS_VALUE); | ||
} | ||
} | ||
|
||
this.constructor.instances.set(this._root, this); | ||
} | ||
|
||
bindEventListeners() { | ||
this.inputElement.addEventListener('focus', this.onFocus); | ||
this.inputElement.addEventListener('blur', this.onBlur); | ||
this.inputElement.addEventListener('change', this.onChange); | ||
_bindEventListeners() { | ||
this._inputElement.addEventListener('focus', this.onFocus); | ||
this._inputElement.addEventListener('blur', this.onBlur); | ||
this._inputElement.addEventListener('change', this.onChange); | ||
} | ||
|
||
value() { | ||
return this.element.value; | ||
// Current value of the Select | ||
return this._inputElement.value; | ||
} | ||
|
||
set(value) { | ||
this._inputElement.value = value; | ||
this._inputElement.dispatchEvent(new Event('change')); | ||
} | ||
|
||
onFocus = () => { | ||
this.element.classList.add(SELECT_MODIFIER_ACTIVE); | ||
this._root.classList.add(this.constructor.cssClasses.ACTIVE); | ||
}; | ||
|
||
onBlur = () => { | ||
this.element.classList.remove(SELECT_MODIFIER_ACTIVE); | ||
this._root.classList.remove(this.constructor.cssClasses.ACTIVE); | ||
}; | ||
|
||
onChange = () => { | ||
const option = this.getCurrentValueOptionElement(); | ||
const option = this._getCurrentValueOptionElement(); | ||
|
||
if (option) { | ||
if (option.dataset.rayPlaceholder) { | ||
this.element.classList.add(SELECT_MODIFIER_PLACEHOLDER_MODE); | ||
this.element.classList.remove(SELECT_MODIFIER_HAS_VALUE); | ||
this._root.classList.add(this.constructor.cssClasses.PLACEHOLDER_MODE); | ||
this._root.classList.remove(this.constructor.cssClasses.HAS_VALUE); | ||
} else { | ||
this.element.classList.add(SELECT_MODIFIER_HAS_VALUE); | ||
this.element.classList.remove(SELECT_MODIFIER_PLACEHOLDER_MODE); | ||
this._root.classList.add(this.constructor.cssClasses.HAS_VALUE); | ||
this._root.classList.remove( | ||
this.constructor.cssClasses.PLACEHOLDER_MODE | ||
); | ||
} | ||
} else { | ||
this.element.classList.remove( | ||
SELECT_MODIFIER_PLACEHOLDER_MODE, | ||
SELECT_MODIFIER_HAS_VALUE | ||
this._root.classList.remove( | ||
this.constructor.cssClasses.PLACEHOLDER_MODE, | ||
this.constructor.cssClasses.HAS_VALUE | ||
); | ||
} | ||
}; | ||
|
||
getCurrentValueOptionElement = () => { | ||
return this.inputElement.options[this.inputElement.selectedIndex]; | ||
_getCurrentValueOptionElement = () => { | ||
return this._inputElement.options[this._inputElement.selectedIndex]; | ||
}; | ||
|
||
teardownEventListeners() {} | ||
destroy() { | ||
// Implement this method to release any resources / deregister any listeners they have | ||
// attached. An example of this might be deregistering a resize event from the window object. | ||
this._inputElement.removeEventListener('focus', this.onFocus); | ||
this._inputElement.removeEventListener('blur', this.onBlur); | ||
this._inputElement.removeEventListener('change', this.onChange); | ||
|
||
this.constructor.instances.delete(this._root); | ||
} | ||
} | ||
|
||
export { Select }; | ||
export default Select; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class="ray-select"> | ||
<select class="ray-select__input"> | ||
<option value="" disabled selected data-ray-placeholder /> | ||
<option value="Pikatchu">Pikatchu</option> | ||
<option value="Squirtle">Squirtle</option> | ||
<option value="Squirtle">Charmander</option> | ||
</select> | ||
|
||
<label class="ray-select__label"> | ||
What's your favorite Pokémon? | ||
</label> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function validateNodeType(target) { | ||
if ( | ||
target.nodeType !== Node.ELEMENT_NODE && | ||
target.nodeType !== Node.DOCUMENT_NODE | ||
) { | ||
throw new TypeError( | ||
'DOM document or DOM element should be given to search for and initialize this widget.' | ||
); | ||
} | ||
} | ||
|
||
function isTargetingItself(target, options) { | ||
return ( | ||
target.nodeType === Node.ELEMENT_NODE && | ||
target.matches(options.selectorInit) | ||
); | ||
} | ||
|
||
export { validateNodeType, isTargetingItself }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
prefix: 'ray-' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.