-
-
Notifications
You must be signed in to change notification settings - Fork 49.9k
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
Tabbing to select active option in <Select> #26876
Tabbing to select active option in <Select> #26876
Comments
I would also like to see this! The current behavior is not consistent with the default HTML |
Adding my 2 cents here: both ARIA specification and |
Any comments on this? |
How do i take the value when we are using key down and key up with select option . I have to capture the value when i am using down and up arrow in options? |
Would love to see this implemented. |
So is this feature ever going to be supported? |
Is there an update here? |
Anything we can do to help @afc163? I would make a PR but I am not an expert in JS enough to do backend library updates. |
I'm chiming in here because I am also experiencing this issue. There is no good way to implement what standard html-based selects do with antd Select, which is a shortcoming. |
I've looked into this further, and it appears to be deliberately disabled functionality. Ant Design Select is a wrapper of rc-select, which has a 3rd mode, called combobox. This is a combination of searching and selecting, hence the combobox name. The mode combobox is disabled. (1) Next, is the backfill option, that is a property of rc-select, that when you use up/down arrows, will set the current value of the select to the option you are 'hovering' over. This option is also disabled. (2) In the code snippet below, you can see they omit the property 'backfill'. I'm not 100% sure, but I believe that if you enabled these 2 options and use them together, that we would get this lost functionality. I'm pinging a few recent contributors to this repo, because I believe this is a necessary change, along with the fact that this issue is still marked inactive when I believe that has been done in error. (3) (1) Found in components\select\index.tsx (2) Found in components\select\index.tsx
(3) Why is this change needed? This is a core functionality of standard html-only selects. There is a comment above going into more detail as well if further detail is needed. |
I have a temp fall back implementation, the idea is that find the active option dom node when blur: // libs
import { useRef } from 'react';
import { Select } from 'antd';
// utils
import isFunction from '@/utils/isFunction';
// this func is based on antd version 4, you can customized it on your antd version
const getActiveOpitonContentElement = (rootElement) => {
if (rootElement) {
const activeOptionElement = rootElement.getElementsByClassName(
'ant-select-item-option-active',
)?.[0];
if (activeOptionElement) {
const optionContentElement = activeOptionElement.getElementsByClassName(
'ant-select-item-option-content',
)?.[0];
return optionContentElement;
}
}
return void 0;
};
const MySelect = (props) => {
const {
options,
optionFilterProp,
dropdownRender,
onBlur,
onChange,
...restProps
} = props;
const dropdownWrapperElementRef = useRef();
return (
<Select
{...restProps}
onChange={onChange}
optionFilterProp={optionFilterProp}
options={options}
dropdownRender={(originNode) => {
if (isFunction(dropdownRender)) {
return (
<div ref={dropdownWrapperElementRef}>
{dropdownRender(originNode)}
</div>
)
}
return (
<div ref={dropdownWrapperElementRef}>
{originNode}
</div>
);
}}
onBlur={(...args) => {
// ---- [implementation of tabbing to select active option] start -----
const activeOptionContentElement = getActiveOpitonContentElement(
dropdownWrapperElementRef.current,
);
if (
activeOptionContentElement &&
Array.isArray(options) &&
options.length > 0
) {
const activeOption = options.find((el) => {
return (
el[optionFilterProp ?? 'value'] ===
activeOptionContentElement.innerText
);
});
if (activeOption && isFunction(onChange)) {
onChange(activeOption.value);
}
}
// ---- [implementation of tabbing to select active option] end -----
if (isFunction(onBlur)) {
onBlur(...args);
}
}}
/>
);
}; It is based on antd version 4, but I think developer can customized the implementation. |
Hey, I also agree this is a very common UX approach, if one has moved the cursor or focused on a selection or is using search and a result is highlighted, one would expect that pressing tab would select to item. Instead, we need to press Enter or click on it to actually select then press Tab... 😞 |
I have tried an alternative way , please check this out :
|
Submitted a PR in rc component/select to handle the case that we can tab to select active option |
EPIC! Thanks |
What problem does this feature solve?
Currently, when using a focused Select component, if you use the keyboard to move down the list of options and hit Tab, it will blur the menu without selecting the active option. I think the preferred default would be that if the dropdown is open, hitting Tab selects the active option in addition to closing the menu (this is what react-select does, for example).
I also don't think there is a way for a user to implement this on their own, because there's no event triggered by changing the active option in an open dropdown menu.
What does the proposed API look like?
Select the active option on tabbing by default, without any public API change.
An alternative could be exposing an
onActiveOptionChange
event, so that someone could do something like:The text was updated successfully, but these errors were encountered: