diff --git a/.eslintrc b/.eslintrc index afdec9d..b5bd973 100644 --- a/.eslintrc +++ b/.eslintrc @@ -10,6 +10,7 @@ "parser": "babel-eslint", "rules": { "strict": 0, - "no-underscore-dangle": 0 + "no-underscore-dangle": 0, + "no-console": ["warn", { allow: ["warn", "error"] }] } } diff --git a/assets/scripts/src/actions/index.js b/assets/scripts/src/actions/index.js index c798c49..2a59e1c 100644 --- a/assets/scripts/src/actions/index.js +++ b/assets/scripts/src/actions/index.js @@ -1,4 +1,13 @@ -export const addItem = (value, label, id, choiceId, groupId, customProperties, placeholder, keyCode) => ({ +export const addItem = ( + value, + label, + id, + choiceId, + groupId, + customProperties, + placeholder, + keyCode, +) => ({ type: 'ADD_ITEM', value, label, @@ -22,7 +31,17 @@ export const highlightItem = (id, highlighted) => ({ highlighted, }); -export const addChoice = (value, label, id, groupId, disabled, elementId, customProperties, placeholder, keyCode) => ({ +export const addChoice = ( + value, + label, + id, + groupId, + disabled, + elementId, + customProperties, + placeholder, + keyCode, +) => ({ type: 'ADD_CHOICE', value, label, diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 86a4627..d0c6795 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -1763,7 +1763,10 @@ class Choices { // We are typing into a text input and have a value, we want to show a dropdown // notice. Otherwise hide the dropdown if (this.isTextElement) { - const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState); + const hasActiveDropdown = this.dropdown.classList.contains( + this.config.classNames.activeState, + ); + if (value) { if (canAddItem.notice) { const dropdownItem = this._getTemplate('notice', canAddItem.notice); @@ -1836,7 +1839,10 @@ class Choices { // If a user tapped within our container... if (this.wasTap === true && this.containerOuter.contains(target)) { // ...and we aren't dealing with a single select box, show dropdown/focus input - if ((target === this.containerOuter || target === this.containerInner) && !this.isSelectOneElement) { + if ( + (target === this.containerOuter || target === this.containerInner) && + !this.isSelectOneElement + ) { if (this.isTextElement) { // If text element, we only want to focus the input (if it isn't already) if (document.activeElement !== this.input) { @@ -1917,7 +1923,11 @@ class Choices { this.showDropdown(); this.containerOuter.focus(); } - } else if (this.isSelectOneElement && target !== this.input && !this.dropdown.contains(target)) { + } else if ( + this.isSelectOneElement && + target !== this.input && + !this.dropdown.contains(target) + ) { this.hideDropdown(true); } } else { @@ -1974,7 +1984,9 @@ class Choices { const target = e.target; // If target is something that concerns us if (this.containerOuter.contains(target)) { - const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState); + const hasActiveDropdown = this.dropdown.classList.contains( + this.config.classNames.activeState, + ); const focusActions = { text: () => { if (target === this.input) { @@ -2018,7 +2030,9 @@ class Choices { // If target is something that concerns us if (this.containerOuter.contains(target) && !this.isScrollingOnIe) { const activeItems = this.store.getItemsFilteredByActive(); - const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState); + const hasActiveDropdown = this.dropdown.classList.contains( + this.config.classNames.activeState, + ); const hasHighlightedItems = activeItems.some(item => item.highlighted); const blurActions = { text: () => { @@ -2188,7 +2202,15 @@ class Choices { * @return {Object} Class instance * @public */ - _addItem(value, label = null, choiceId = -1, groupId = -1, customProperties = null, placeholder = false, keyCode = null) { + _addItem( + value, + label = null, + choiceId = -1, + groupId = -1, + customProperties = null, + placeholder = false, + keyCode = null, + ) { let passedValue = isType('String', value) ? value.trim() : value; const passedKeyCode = keyCode; const items = this.store.getItems(); @@ -2300,7 +2322,16 @@ class Choices { * @return * @private */ - _addChoice(value, label = null, isSelected = false, isDisabled = false, groupId = -1, customProperties = null, placeholder = false, keyCode = null) { + _addChoice( + value, + label = null, + isSelected = false, + isDisabled = false, + groupId = -1, + customProperties = null, + placeholder = false, + keyCode = null, + ) { if (typeof value === 'undefined' || value === null) { return; } @@ -2420,25 +2451,30 @@ class Choices { _createTemplates() { const globalClasses = this.config.classNames; const templates = { - containerOuter: direction => strToEl(` - - `), + containerOuter: (direction) => { + const tabIndex = this.isSelectOneElement ? 'tabindex="0"' : ''; + let role = this.isSelectElement ? 'role="listbox"' : ''; + let ariaAutoComplete = ''; + + if (this.config.searchEnabled && this.isSelectElement) { + role = 'role="combobox"'; + ariaAutoComplete = 'aria-autocomplete="list"'; + } + + return strToEl(` + + `); + }, containerInner: () => strToEl(`
`), @@ -2460,6 +2496,9 @@ class Choices { `), item: (data) => { + const ariaSelected = data.active ? 'aria-selected="true"' : ''; + const ariaDisabled = data.disabled ? 'aria-disabled="true"' : ''; + let localClasses = classNames( globalClasses.item, { [globalClasses.highlightedState]: data.highlighted, @@ -2484,14 +2523,8 @@ class Choices { data-id="${data.id}" data-value="${data.value}" data-deletable - ${data.active ? - 'aria-selected="true"' : - '' - } - ${data.disabled ? - 'aria-disabled="true"' : - '' - } + ${ariaSelected} + ${ariaDisabled} > ${data.label}