Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tab => direction keys handling in single-select mode #1242

Merged
merged 4 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@
score: 0,
rank: 0,
value: option.value,
label: option.innerHTML,
label: option.innerText, // HTML options do not support most html tags, but innerHtml will extract html comments...
element: option,
active: true,
// this returns true if nothing is selected on initial load, which will break placeholder support
Expand Down Expand Up @@ -3630,11 +3630,15 @@
if (this.dropdown.isActive) {
return this;
}
if (preventInputFocus === undefined) {
// eslint-disable-next-line no-param-reassign
preventInputFocus = !this._canSearch;
}
requestAnimationFrame(function () {
_this.dropdown.show();
var rect = _this.dropdown.element.getBoundingClientRect();
_this.containerOuter.open(rect.bottom, rect.height);
if (!preventInputFocus && _this._canSearch) {
if (!preventInputFocus) {
_this.input.focus();
}
_this.passedElement.triggerEvent(EventType.showDropdown);
Expand Down Expand Up @@ -3936,6 +3940,7 @@
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -4392,6 +4397,7 @@
var maxItemCount = config.maxItemCount, maxItemText = config.maxItemText;
if (!config.singleModeForMultiSelect && maxItemCount > 0 && maxItemCount <= this._store.items.length) {
this.choiceList.element.replaceChildren('');
this._notice = undefined;
this._displayNotice(typeof maxItemText === 'function' ? maxItemText(maxItemCount) : maxItemText, NoticeTypes.addChoice);
return false;
}
Expand Down Expand Up @@ -4704,6 +4710,7 @@
if (hasActiveDropdown) {
event.stopPropagation();
this.hideDropdown(true);
this._stopSearch();
this.containerOuter.element.focus();
}
};
Expand Down Expand Up @@ -4880,17 +4887,11 @@
var blurWasWithinContainer = target && containerOuter.element.contains(target);
if (blurWasWithinContainer && !this._isScrollingOnIe) {
var targetIsInput = target === this.input.element;
if (this._isTextElement || this._isSelectMultipleElement) {
if (targetIsInput) {
containerOuter.removeFocusState();
this.hideDropdown(true);
this.unhighlightAll();
}
}
else {
if (targetIsInput) {
containerOuter.removeFocusState();
if (targetIsInput || (target === containerOuter.element && !this._canSearch)) {
this.hideDropdown(true);
this.hideDropdown(true);
if (this._isTextElement || this._isSelectMultipleElement) {
this.unhighlightAll();
}
}
Comment on lines 4889 to 4896
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst this works for the search not being hidden, we now don't take into account to remove the focusState on the previous outerElement when tabbing through. The previous element won't lose their is-focused class.

We'd actually need to check for another condition afterwards to remove the previous class:

if (target === this.input.elemen) {
   // your logic
}
// Remove the focus state when the past outerContainer was the target
else if (target === this.containerOuter.element) {
  containerOuter.removeFocusState();
}

Copy link
Collaborator Author

@Xon Xon Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've restored that else branch, and compiled the js files. Want to give that a test to see if that encounters any other regressions or accessibility issues?

Copy link
Contributor

@zoglo zoglo Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already did (testing), the change looks good to me :)

}
Expand Down Expand Up @@ -4978,6 +4979,10 @@
return;
}
this._store.dispatch(removeItem$1(item));
var notice = this._notice;
if (notice && notice.type === NoticeTypes.noChoices) {
this._clearNotice();
}
this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(item));
};
Choices.prototype._addChoice = function (choice, withEvents, userTriggered) {
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions public/assets/scripts/choices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ var WrappedSelect = /** @class */ (function (_super) {
score: 0,
rank: 0,
value: option.value,
label: option.innerHTML,
label: option.innerText, // HTML options do not support most html tags, but innerHtml will extract html comments...
element: option,
active: true,
// this returns true if nothing is selected on initial load, which will break placeholder support
Expand Down Expand Up @@ -3624,11 +3624,15 @@ var Choices = /** @class */ (function () {
if (this.dropdown.isActive) {
return this;
}
if (preventInputFocus === undefined) {
// eslint-disable-next-line no-param-reassign
preventInputFocus = !this._canSearch;
}
requestAnimationFrame(function () {
_this.dropdown.show();
var rect = _this.dropdown.element.getBoundingClientRect();
_this.containerOuter.open(rect.bottom, rect.height);
if (!preventInputFocus && _this._canSearch) {
if (!preventInputFocus) {
_this.input.focus();
}
_this.passedElement.triggerEvent(EventType.showDropdown);
Expand Down Expand Up @@ -3930,6 +3934,7 @@ var Choices = /** @class */ (function () {
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -4386,6 +4391,7 @@ var Choices = /** @class */ (function () {
var maxItemCount = config.maxItemCount, maxItemText = config.maxItemText;
if (!config.singleModeForMultiSelect && maxItemCount > 0 && maxItemCount <= this._store.items.length) {
this.choiceList.element.replaceChildren('');
this._notice = undefined;
this._displayNotice(typeof maxItemText === 'function' ? maxItemText(maxItemCount) : maxItemText, NoticeTypes.addChoice);
return false;
}
Expand Down Expand Up @@ -4698,6 +4704,7 @@ var Choices = /** @class */ (function () {
if (hasActiveDropdown) {
event.stopPropagation();
this.hideDropdown(true);
this._stopSearch();
this.containerOuter.element.focus();
}
};
Expand Down Expand Up @@ -4874,17 +4881,11 @@ var Choices = /** @class */ (function () {
var blurWasWithinContainer = target && containerOuter.element.contains(target);
if (blurWasWithinContainer && !this._isScrollingOnIe) {
var targetIsInput = target === this.input.element;
if (this._isTextElement || this._isSelectMultipleElement) {
if (targetIsInput) {
containerOuter.removeFocusState();
this.hideDropdown(true);
this.unhighlightAll();
}
}
else {
if (targetIsInput) {
containerOuter.removeFocusState();
if (targetIsInput || (target === containerOuter.element && !this._canSearch)) {
this.hideDropdown(true);
this.hideDropdown(true);
if (this._isTextElement || this._isSelectMultipleElement) {
this.unhighlightAll();
}
}
}
Expand Down Expand Up @@ -4972,6 +4973,10 @@ var Choices = /** @class */ (function () {
return;
}
this._store.dispatch(removeItem$1(item));
var notice = this._notice;
if (notice && notice.type === NoticeTypes.noChoices) {
this._clearNotice();
}
this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(item));
};
Choices.prototype._addChoice = function (choice, withEvents, userTriggered) {
Expand Down
29 changes: 17 additions & 12 deletions public/assets/scripts/choices.search-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@
score: 0,
rank: 0,
value: option.value,
label: option.innerHTML,
label: option.innerText, // HTML options do not support most html tags, but innerHtml will extract html comments...
element: option,
active: true,
// this returns true if nothing is selected on initial load, which will break placeholder support
Expand Down Expand Up @@ -3148,11 +3148,15 @@
if (this.dropdown.isActive) {
return this;
}
if (preventInputFocus === undefined) {
// eslint-disable-next-line no-param-reassign
preventInputFocus = !this._canSearch;
}
requestAnimationFrame(function () {
_this.dropdown.show();
var rect = _this.dropdown.element.getBoundingClientRect();
_this.containerOuter.open(rect.bottom, rect.height);
if (!preventInputFocus && _this._canSearch) {
if (!preventInputFocus) {
_this.input.focus();
}
_this.passedElement.triggerEvent(EventType.showDropdown);
Expand Down Expand Up @@ -3454,6 +3458,7 @@
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -3910,6 +3915,7 @@
var maxItemCount = config.maxItemCount, maxItemText = config.maxItemText;
if (!config.singleModeForMultiSelect && maxItemCount > 0 && maxItemCount <= this._store.items.length) {
this.choiceList.element.replaceChildren('');
this._notice = undefined;
this._displayNotice(typeof maxItemText === 'function' ? maxItemText(maxItemCount) : maxItemText, NoticeTypes.addChoice);
return false;
}
Expand Down Expand Up @@ -4222,6 +4228,7 @@
if (hasActiveDropdown) {
event.stopPropagation();
this.hideDropdown(true);
this._stopSearch();
this.containerOuter.element.focus();
}
};
Expand Down Expand Up @@ -4398,17 +4405,11 @@
var blurWasWithinContainer = target && containerOuter.element.contains(target);
if (blurWasWithinContainer && !this._isScrollingOnIe) {
var targetIsInput = target === this.input.element;
if (this._isTextElement || this._isSelectMultipleElement) {
if (targetIsInput) {
containerOuter.removeFocusState();
this.hideDropdown(true);
this.unhighlightAll();
}
}
else {
if (targetIsInput) {
containerOuter.removeFocusState();
if (targetIsInput || (target === containerOuter.element && !this._canSearch)) {
this.hideDropdown(true);
this.hideDropdown(true);
if (this._isTextElement || this._isSelectMultipleElement) {
this.unhighlightAll();
}
}
}
Expand Down Expand Up @@ -4496,6 +4497,10 @@
return;
}
this._store.dispatch(removeItem$1(item));
var notice = this._notice;
if (notice && notice.type === NoticeTypes.noChoices) {
this._clearNotice();
}
this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(item));
};
Choices.prototype._addChoice = function (choice, withEvents, userTriggered) {
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.search-basic.min.js

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions public/assets/scripts/choices.search-basic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ var WrappedSelect = /** @class */ (function (_super) {
score: 0,
rank: 0,
value: option.value,
label: option.innerHTML,
label: option.innerText, // HTML options do not support most html tags, but innerHtml will extract html comments...
element: option,
active: true,
// this returns true if nothing is selected on initial load, which will break placeholder support
Expand Down Expand Up @@ -3142,11 +3142,15 @@ var Choices = /** @class */ (function () {
if (this.dropdown.isActive) {
return this;
}
if (preventInputFocus === undefined) {
// eslint-disable-next-line no-param-reassign
preventInputFocus = !this._canSearch;
}
requestAnimationFrame(function () {
_this.dropdown.show();
var rect = _this.dropdown.element.getBoundingClientRect();
_this.containerOuter.open(rect.bottom, rect.height);
if (!preventInputFocus && _this._canSearch) {
if (!preventInputFocus) {
_this.input.focus();
}
_this.passedElement.triggerEvent(EventType.showDropdown);
Expand Down Expand Up @@ -3448,6 +3452,7 @@ var Choices = /** @class */ (function () {
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._clearNotice();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -3904,6 +3909,7 @@ var Choices = /** @class */ (function () {
var maxItemCount = config.maxItemCount, maxItemText = config.maxItemText;
if (!config.singleModeForMultiSelect && maxItemCount > 0 && maxItemCount <= this._store.items.length) {
this.choiceList.element.replaceChildren('');
this._notice = undefined;
this._displayNotice(typeof maxItemText === 'function' ? maxItemText(maxItemCount) : maxItemText, NoticeTypes.addChoice);
return false;
}
Expand Down Expand Up @@ -4216,6 +4222,7 @@ var Choices = /** @class */ (function () {
if (hasActiveDropdown) {
event.stopPropagation();
this.hideDropdown(true);
this._stopSearch();
this.containerOuter.element.focus();
}
};
Expand Down Expand Up @@ -4392,17 +4399,11 @@ var Choices = /** @class */ (function () {
var blurWasWithinContainer = target && containerOuter.element.contains(target);
if (blurWasWithinContainer && !this._isScrollingOnIe) {
var targetIsInput = target === this.input.element;
if (this._isTextElement || this._isSelectMultipleElement) {
if (targetIsInput) {
containerOuter.removeFocusState();
this.hideDropdown(true);
this.unhighlightAll();
}
}
else {
if (targetIsInput) {
containerOuter.removeFocusState();
if (targetIsInput || (target === containerOuter.element && !this._canSearch)) {
this.hideDropdown(true);
this.hideDropdown(true);
if (this._isTextElement || this._isSelectMultipleElement) {
this.unhighlightAll();
}
}
}
Expand Down Expand Up @@ -4490,6 +4491,10 @@ var Choices = /** @class */ (function () {
return;
}
this._store.dispatch(removeItem$1(item));
var notice = this._notice;
if (notice && notice.type === NoticeTypes.noChoices) {
this._clearNotice();
}
this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(item));
};
Choices.prototype._addChoice = function (choice, withEvents, userTriggered) {
Expand Down
Loading
Loading