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
23 changes: 15 additions & 8 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,21 @@ const OptionList: React.RefForwardingComponent<

// Auto scroll to item position in single mode
React.useEffect(() => {
if (!multiple && open && values.size === 1) {
const value: RawValueType = Array.from(values)[0];
const index = memoFlattenOptions.findIndex(
({ data }) => (data as OptionData).value === value,
);
setActive(index);
scrollIntoView(index);
}
/**
* React will skip `onChange` when component update.
* `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(() => {
if (!multiple && open && values.size === 1) {
const value: RawValueType = Array.from(values)[0];
const index = memoFlattenOptions.findIndex(
({ data }) => (data as OptionData).value === value,
);
setActive(index);
scrollIntoView(index);
}
});
}, [open]);

// ========================== Values ==========================
Expand Down
31 changes: 5 additions & 26 deletions src/Selector/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { composeRef } from 'rc-util/lib/ref';

type InputRef = HTMLInputElement | HTMLTextAreaElement;

Expand All @@ -14,26 +15,9 @@ interface InputProps {
open: boolean;
tabIndex: number;

onKeyDown: React.KeyboardEventHandler<
HTMLInputElement | HTMLTextAreaElement | HTMLElement
>;
onMouseDown: React.MouseEventHandler<
HTMLInputElement | HTMLTextAreaElement | HTMLElement
>;
onChange: React.ChangeEventHandler<
HTMLInputElement | HTMLTextAreaElement | HTMLElement
>;
}

function fillRef(
node: InputRef,
ref: React.LegacyRef<InputRef> | React.Ref<InputRef>,
) {
if (typeof ref === 'function') {
ref(node);
} else if (ref && typeof ref === 'object') {
(ref as any).current = node;
}
onKeyDown: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onMouseDown: React.MouseEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
}

const Input: React.RefForwardingComponent<InputRef, InputProps> = (
Expand Down Expand Up @@ -66,14 +50,9 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
},
} = inputNode;

function inputRef(node: InputRef) {
fillRef(node, ref);
fillRef(node, originRef);
}

inputNode = React.cloneElement(inputNode, {
id,
ref: inputRef,
ref: composeRef(ref, originRef as any),
disabled,
tabIndex,
autoComplete: 'off',
Expand Down