Skip to content

Commit

Permalink
feat: clear input field on blur if the user has not selected any option
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikMWold committed Jan 5, 2024
1 parent cd3de72 commit c4f1442
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ describe('Autocomplete', () => {
expect(within(filteredOptions[0]).getByText('Three')).toBeDefined()
})

it('Clears the input text on blur when no option is selected', async () => {
render(<Autocomplete disablePortal options={items} label={labelText} />)
const labeledNodes = await screen.findAllByLabelText(labelText)
const input = labeledNodes[0]

fireEvent.change(input, {
target: { value: 'ree' },
})

fireEvent.blur(input)
expect(input).toHaveValue('')
})

it('Second option is first when first option is disabled', async () => {
render(
<Autocomplete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ function AutocompleteInner<T>(
getItemProps,
inputValue,
reset: resetCombobox,
setInputValue,
} = useCombobox(comboBoxProps)

useEffect(() => {
Expand Down Expand Up @@ -731,6 +732,16 @@ function AutocompleteInner<T>(
disabled,
onChange: (e: ChangeEvent<HTMLInputElement>) =>
setTypedInputValue(e?.currentTarget?.value),
onBlur: () => {
if (multiple) return
if (selectedItems[0]) {
const inputValue = getLabel(selectedItems[0])
setTypedInputValue(inputValue)
setInputValue(inputValue)
return
}
clear()
},
}),
)
const consolidatedEvents = mergeEventsFromRight(other, inputProps)
Expand Down

0 comments on commit c4f1442

Please sign in to comment.