Skip to content

Commit

Permalink
Autocomplete: allow "falsely" input values (#2154)
Browse files Browse the repository at this point in the history
* fix: allow autocomplete input value to be  falsely

* chore: added changeset

* fixes linting error

Co-authored-by: Erik Hughes <erik.hughes@outlook.com>
  • Loading branch information
mperrotti and Swiftwork authored Jun 28, 2022
1 parent ce45de3 commit 96ad635
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-bottles-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Allow "falsely/empty" Autocomplete.Input values
4 changes: 1 addition & 3 deletions src/Autocomplete/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ const AutocompleteInput = React.forwardRef(
}, [autocompleteSuggestion, inputValue, inputRef, isMenuDirectlyActivated])

useEffect(() => {
if (value) {
setInputValue(value.toString())
}
setInputValue(typeof value !== 'undefined' ? value.toString() : '')
}, [value, setInputValue])

return (
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ describe('Autocomplete', () => {
inputNode && userEvent.type(inputNode, '{esc}')
expect(inputNode?.getAttribute('aria-expanded')).not.toBe('true')
})

it('allows the value to be 0', () => {
const {getByDisplayValue} = HTMLRender(
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: []}} inputProps={{value: 0}} />
)

expect(getByDisplayValue('0')).toBeDefined()
})
})

describe('Autocomplete.Menu', () => {
Expand Down

0 comments on commit 96ad635

Please sign in to comment.