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

♻️ Bento Selector: Simplify logic #35758

Merged
merged 2 commits into from
Aug 25, 2021
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
2 changes: 1 addition & 1 deletion extensions/amp-selector/1.0/base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function getOptions(element, mu) {
option,
disabled,
index,
onFocus: () => tryFocus(child),
focus: () => tryFocus(child),
role: child.getAttribute('role') || 'option',
shimDomElement: child,
// TODO(wg-bento): This implementation causes infinite loops on DOM mutation.
Expand Down
92 changes: 38 additions & 54 deletions extensions/amp-selector/1.0/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function SelectorWithRef(
multiple,
name,
onChange,
onKeyDown: customOnKeyDown,
role = 'listbox',
tabIndex,
children,
Expand Down Expand Up @@ -209,9 +208,6 @@ function SelectorWithRef(

const onKeyDown = useCallback(
(e) => {
if (customOnKeyDown) {
customOnKeyDown(e);
}
const {key} = e;
let dir;
switch (key) {
Expand All @@ -234,7 +230,7 @@ function SelectorWithRef(
}
}
},
[customOnKeyDown, keyboardSelectMode, focusBy, selectBy]
[keyboardSelectMode, focusBy, selectBy]
);

return (
Expand Down Expand Up @@ -272,11 +268,10 @@ export {Selector};
*/
export function Option({
as: Comp = 'div',
'class': className = '',
disabled = false,
focus: customFocus,
index,
onClick: customOnClick,
onFocus: customOnFocus,
onKeyDown: customOnKeyDown,
option,
role = 'option',
tabIndex,
Expand All @@ -294,17 +289,12 @@ export function Option({
selected,
} = useContext(SelectorContext);

const focus = useCallback(
(e) => {
if (customOnFocus) {
customOnFocus(e);
}
if (ref.current) {
tryFocus(ref.current);
}
},
[customOnFocus]
);
const focus = useCallback(() => {
customFocus?.();
if (ref.current) {
tryFocus(ref.current);
}
}, [customFocus]);

// Element should be "registered" before it is visible.
useLayoutEffect(() => {
Expand Down Expand Up @@ -338,49 +328,43 @@ export function Option({
selectOption(option);
}, [disabled, option, selectOption, selectorDisabled]);

const onClick = useCallback(
(e) => {
trySelect();
if (customOnClick) {
customOnClick(e);
}
},
[customOnClick, trySelect]
);
const onClick = useCallback(() => {
trySelect();
}, [trySelect]);

const onKeyDown = useCallback(
(e) => {
if (e.key === Keys.ENTER || e.key === Keys.SPACE) {
trySelect();
}
if (customOnKeyDown) {
customOnKeyDown(e);
}
},
[customOnKeyDown, trySelect]
[trySelect]
);

const isSelected = /** @type {!Array} */ (selected).includes(option);
const optionProps = {
...rest,
className: objstr({
[classes.option]: true,
[classes.selected]: isSelected && !selectorMultiple,
[classes.multiselected]: isSelected && selectorMultiple,
[classes.disabled]: disabled || selectorDisabled,
}),
disabled,
'aria-disabled': String(disabled),
onClick,
onFocus: () => (focusRef.current.active = option),
onKeyDown,
option,
ref,
role,
selected: isSelected,
'aria-selected': String(isSelected),
tabIndex:
tabIndex ?? keyboardSelectMode === KEYBOARD_SELECT_MODE.SELECT ? -1 : 0,
};
return <Comp {...optionProps} />;
return (
<Comp
{...rest}
aria-disabled={String(disabled)}
aria-selected={String(isSelected)}
class={objstr({
[className]: !!className,
[classes.option]: true,
[classes.selected]: isSelected && !selectorMultiple,
[classes.multiselected]: isSelected && selectorMultiple,
[classes.disabled]: disabled || selectorDisabled,
})}
disabled={disabled}
onClick={onClick}
onFocus={() => (focusRef.current.active = option)}
onKeyDown={onKeyDown}
ref={ref}
role={role}
selected={isSelected}
tabIndex={
tabIndex ?? keyboardSelectMode === KEYBOARD_SELECT_MODE.SELECT ? -1 : 0
}
value={option}
/>
);
}