Skip to content

Commit 7a80404

Browse files
authored
fix: AutoComplete typing to open input value (#465)
* Use ref * Use class * Revert "Use class" This reverts commit 99e505c. * Revert "Revert "Use class"" This reverts commit 1e72c1f. * Revert "Revert "Revert "Use class""" This reverts commit ba14e48. * delay for update status
1 parent 775399e commit 7a80404

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

src/OptionList.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,21 @@ const OptionList: React.RefForwardingComponent<
129129

130130
// Auto scroll to item position in single mode
131131
React.useEffect(() => {
132-
if (!multiple && open && values.size === 1) {
133-
const value: RawValueType = Array.from(values)[0];
134-
const index = memoFlattenOptions.findIndex(
135-
({ data }) => (data as OptionData).value === value,
136-
);
137-
setActive(index);
138-
scrollIntoView(index);
139-
}
132+
/**
133+
* React will skip `onChange` when component update.
134+
* `setActive` function will call root accessibility state update which makes re-render.
135+
* So we need to delay to let Input component trigger onChange first.
136+
*/
137+
setTimeout(() => {
138+
if (!multiple && open && values.size === 1) {
139+
const value: RawValueType = Array.from(values)[0];
140+
const index = memoFlattenOptions.findIndex(
141+
({ data }) => (data as OptionData).value === value,
142+
);
143+
setActive(index);
144+
scrollIntoView(index);
145+
}
146+
});
140147
}, [open]);
141148

142149
// ========================== Values ==========================

src/Selector/Input.tsx

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { composeRef } from 'rc-util/lib/ref';
23

34
type InputRef = HTMLInputElement | HTMLTextAreaElement;
45

@@ -14,26 +15,9 @@ interface InputProps {
1415
open: boolean;
1516
tabIndex: number;
1617

17-
onKeyDown: React.KeyboardEventHandler<
18-
HTMLInputElement | HTMLTextAreaElement | HTMLElement
19-
>;
20-
onMouseDown: React.MouseEventHandler<
21-
HTMLInputElement | HTMLTextAreaElement | HTMLElement
22-
>;
23-
onChange: React.ChangeEventHandler<
24-
HTMLInputElement | HTMLTextAreaElement | HTMLElement
25-
>;
26-
}
27-
28-
function fillRef(
29-
node: InputRef,
30-
ref: React.LegacyRef<InputRef> | React.Ref<InputRef>,
31-
) {
32-
if (typeof ref === 'function') {
33-
ref(node);
34-
} else if (ref && typeof ref === 'object') {
35-
(ref as any).current = node;
36-
}
18+
onKeyDown: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
19+
onMouseDown: React.MouseEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
20+
onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
3721
}
3822

3923
const Input: React.RefForwardingComponent<InputRef, InputProps> = (
@@ -66,14 +50,9 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
6650
},
6751
} = inputNode;
6852

69-
function inputRef(node: InputRef) {
70-
fillRef(node, ref);
71-
fillRef(node, originRef);
72-
}
73-
7453
inputNode = React.cloneElement(inputNode, {
7554
id,
76-
ref: inputRef,
55+
ref: composeRef(ref, originRef as any),
7756
disabled,
7857
tabIndex,
7958
autoComplete: 'off',

0 commit comments

Comments
 (0)