Skip to content

Commit 935b711

Browse files
author
叶文俊
committed
fix: wrong focus behavior when configuring menu selectability
1 parent 3ba3f98 commit 935b711

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/Menu.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,22 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {
394394
const keys = getKeys();
395395
const { elements, key2element, element2key } = refreshElements(keys, uuid);
396396
const focusableElements = getFocusableElements(containerRef.current, elements);
397-
397+
const defaultFocusKey = focusableElements[0]
398+
? element2key.get(focusableElements[0])
399+
: childList.find(node => !node.props.disabled)?.key;
398400
let shouldFocusKey: string;
399-
if (mergedActiveKey && keys.includes(mergedActiveKey)) {
400-
shouldFocusKey = mergedActiveKey;
401+
// find the item to focus on based on whether it is selectable.
402+
if (selectable) {
403+
// if there is already a selected item, do not apply the focus.
404+
if (!getMergedSelectKeys()?.length) {
405+
if (mergedActiveKey && keys.includes(mergedActiveKey)) {
406+
shouldFocusKey = mergedActiveKey;
407+
} else {
408+
shouldFocusKey = defaultFocusKey;
409+
}
410+
}
401411
} else {
402-
shouldFocusKey = focusableElements[0]
403-
? element2key.get(focusableElements[0])
404-
: childList.find(node => !node.props.disabled)?.key;
412+
shouldFocusKey = defaultFocusKey;
405413
}
406414
const elementToFocus = key2element.get(shouldFocusKey);
407415

@@ -434,6 +442,9 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {
434442

435443
return [internalSelectKeys];
436444
}, [internalSelectKeys]);
445+
function getMergedSelectKeys() {
446+
return mergedSelectKeys;
447+
}
437448

438449
// >>>>> Trigger select
439450
const triggerSelection = (info: MenuInfo) => {

0 commit comments

Comments
 (0)