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

refactor: make _getAvailableIndex check item visibility by default #8519

Closed
Closed
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
2 changes: 1 addition & 1 deletion packages/a11y-base/src/keyboard-direction-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export declare class KeyboardDirectionMixinClass {
items: Element[],
index: number,
increment: number,
condition: (item: Element) => boolean,
condition?: (item: Element) => boolean,
): number;

/**
Expand Down
28 changes: 17 additions & 11 deletions packages/a11y-base/src/keyboard-direction-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const KeyboardDirectionMixin = (superclass) =>
focus() {
const items = this._getItems();
if (Array.isArray(items)) {
const idx = this._getAvailableIndex(items, 0, null, (item) => !isElementHidden(item));
const idx = this._getAvailableIndex(items, 0, null);
if (idx >= 0) {
this._focus(idx);
}
Expand Down Expand Up @@ -91,7 +91,7 @@ export const KeyboardDirectionMixin = (superclass) =>
idx = items.length - 1;
}

idx = this._getAvailableIndex(items, idx, increment, (item) => !isElementHidden(item));
idx = this._getAvailableIndex(items, idx, increment);

if (idx >= 0) {
event.preventDefault();
Expand Down Expand Up @@ -171,22 +171,28 @@ export const KeyboardDirectionMixin = (superclass) =>

const item = items[idx];

if (!item.hasAttribute('disabled') && this.__isMatchingItem(item, condition)) {
return idx;
if (!this._isItemFocusable(item)) {
continue;
}

if (typeof condition === 'function' && !condition(item)) {
continue;
}

return idx;
}
return -1;
}

/**
* Returns true if the item matches condition.
* Returns whether the item is focusable. By default,
* returns true if the item is visible and not disabled.
*
* @param {Element} item - item to check
* @param {Function} condition - function used to check the item
* @return {number}
* @private
* @param {Element} item
* @return {boolean}
* @protected
*/
__isMatchingItem(item, condition) {
return typeof condition === 'function' ? condition(item) : true;
_isItemFocusable(item) {
return !item.hasAttribute('disabled') && !isElementHidden(item);
}
};
14 changes: 5 additions & 9 deletions packages/a11y-base/src/list-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,6 @@ export const ListMixin = (superClass) =>
super._onKeyDown(event);
}

/**
* @param {!Element} item
* @return {boolean}
* @protected
*/
_isItemHidden(item) {
return getComputedStyle(item).display === 'none';
}

/**
* @param {number} idx
* @protected
Expand Down Expand Up @@ -346,6 +337,11 @@ export const ListMixin = (superClass) =>
}
}

/** @override */
_isItemFocusable(item) {
return !item.hasAttribute('disabled');
}

/**
* Fired when the selection is changed.
* Not fired when used in `multiple` selection mode.
Expand Down
4 changes: 2 additions & 2 deletions packages/menu-bar/src/vaadin-menu-bar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { DisabledMixin } from '@vaadin/a11y-base/src/disabled-mixin.js';
import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
import { isElementFocused, isElementHidden, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
import { isElementFocused, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
import { KeyboardDirectionMixin } from '@vaadin/a11y-base/src/keyboard-direction-mixin.js';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
Expand Down Expand Up @@ -880,7 +880,7 @@ export const MenuBarMixin = (superClass) =>
const currentIdx = items.indexOf(this.focused);
const increment = e.shiftKey ? -1 : 1;
let idx = currentIdx + increment;
idx = this._getAvailableIndex(items, idx, increment, (item) => !isElementHidden(item));
idx = this._getAvailableIndex(items, idx, increment);
this.__switchSubMenu(items[idx]);
}
}
Expand Down
Loading