Skip to content
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
4 changes: 3 additions & 1 deletion src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const OptionList: React.RefForwardingComponent<
* `setActive` function will call root accessibility state update which makes re-render.
* So we need to delay to let Input component trigger onChange first.
*/
setTimeout(() => {
const timeoutId = setTimeout(() => {
if (!multiple && open && values.size === 1) {
const value: RawValueType = Array.from(values)[0];
const index = memoFlattenOptions.findIndex(
Expand All @@ -145,6 +145,8 @@ const OptionList: React.RefForwardingComponent<
scrollIntoView(index);
}
});

return () => clearTimeout(timeoutId);
}, [open]);

// ========================== Values ==========================
Expand Down
16 changes: 15 additions & 1 deletion src/generate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,33 @@ export default function generateSelector<
}
};

const activeTimeoutIds: number[] = [];
React.useEffect(() => () => {
activeTimeoutIds.forEach(timeoutId => clearTimeout(timeoutId));
activeTimeoutIds.splice(0, activeTimeoutIds.length);
}, []);

const onInternalMouseDown: React.MouseEventHandler<HTMLDivElement> = (event, ...restArgs) => {
const { target } = event;
const popupElement: HTMLDivElement =
triggerRef.current && triggerRef.current.getPopupElement();

// We should give focus back to selector if clicked item is not focusable
if (popupElement && popupElement.contains(target as HTMLElement)) {
setTimeout(() => {
const timeoutId = setTimeout(() => {
const index = activeTimeoutIds.indexOf(timeoutId);
if (index !== -1) {
activeTimeoutIds.splice(index, 1);
}

cancelSetMockFocused();

if (!popupElement.contains(document.activeElement)) {
selectorRef.current.focus();
}
});

activeTimeoutIds.push(timeoutId);
}

if (onMouseDown) {
Expand Down