Skip to content

Commit

Permalink
fix(Autocomplete): dont put cursor to the end on every change event
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoste committed Mar 23, 2022
1 parent c627b83 commit c75e798
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/components/autocomplete/src/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,23 @@ function _Autocomplete<ItemType>(
const [inputValue, setInputValue] = useState(defaultValue);

const handleInputValueChange = useCallback(
(value) => {
(value: string) => {
setInputValue(value);

onInputValueChange?.(value);
},
[onInputValueChange],
);

// Handle manually to avoid a jumping cursor, see https://github.com/downshift-js/downshift/issues/1108#issuecomment-842407759
const handleNativeChangeEvent = useCallback(
(event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const value = event.target.value;
handleInputValueChange(value);
},
[handleInputValueChange],
);

const flattenItems = isUsingGroups(isGrouped, items)
? items.reduce(
(acc: ItemType[], group: GroupType) => [...acc, ...group.options],
Expand All @@ -185,8 +194,10 @@ function _Autocomplete<ItemType>(
items: flattenItems,
inputValue,
itemToString,
onInputValueChange: ({ inputValue }) => {
handleInputValueChange(inputValue);
onInputValueChange: ({ type, inputValue }) => {
if (type !== '__input_change__') {
handleInputValueChange(inputValue);
}
},
onStateChange: ({ type, selectedItem }) => {
switch (type) {
Expand Down Expand Up @@ -249,6 +260,10 @@ function _Autocomplete<ItemType>(
ref={mergeRefs(inputProps.ref, inputRef)}
testId="cf-autocomplete-input"
placeholder={placeholder}
onChange={(event) => {
inputProps.onChange(event);
handleNativeChangeEvent(event);
}}
/>
<IconButton
{...toggleProps}
Expand Down

1 comment on commit c75e798

@vercel
Copy link

@vercel vercel bot commented on c75e798 Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.