Skip to content

Commit 85a4301

Browse files
fix: preserve user-provided id on Autocomplete input (#4682)
* fix: preserve user-provided id on Autocomplete input Pass user-provided id as inputId to downshift's useCombobox so it is used consistently for the input element, label htmlFor, and all related aria attributes. Closes #4588 * fix: for pull request finding 'Unused variable, import, function or class' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * fix: address review feedback on Autocomplete id test Remove unused variable and use getByRole instead of container querySelector to satisfy testing-library lint rules. * fix: restore lost changes and snapshot from rebase Re-apply inputId fix in useAutocomplete and test cases that were lost during rebase. Restore snapshot from main to match CI environment. --------- Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent cfd254c commit 85a4301

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

packages/eds-core-react/src/components/Autocomplete/Autocomplete.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ describe('Autocomplete', () => {
138138
expect(optionsList.nodeName).toBe('UL')
139139
})
140140

141+
it('Preserves user-provided id on the input element', () => {
142+
render(<Autocomplete id="my-custom-id" label={labelText} options={items} />)
143+
const input = screen.getAllByLabelText(labelText)[0]
144+
expect(input).toHaveAttribute('id', 'my-custom-id')
145+
})
146+
147+
it('Maintains label-input association when custom id is provided', () => {
148+
render(<Autocomplete id="my-custom-id" label={labelText} options={items} />)
149+
// getByRole finds the input via its associated label, confirming the for/id link works
150+
expect(screen.getByRole('combobox', { name: labelText })).toHaveAttribute(
151+
'id',
152+
'my-custom-id',
153+
)
154+
})
155+
141156
it('Has provided ReactNode label', async () => {
142157
render(<Autocomplete label={<div>{labelText}</div>} options={items} />)
143158

packages/eds-core-react/src/components/Autocomplete/useAutocomplete.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const useAutocomplete = <T>({
6565
variant,
6666
onClear,
6767
ref,
68+
id,
6869
...other
6970
}: AutocompleteProps<T> & { ref?: React.Ref<HTMLInputElement> }) => {
7071
const [lastScrollOffset, setLastScrollOffset] = useState<number>(0)
@@ -295,6 +296,7 @@ export const useAutocomplete = <T>({
295296

296297
// MARK: downshift state
297298
let comboBoxProps: UseComboboxProps<T> = {
299+
...(id !== undefined && { inputId: id }),
298300
items: availableItems as T[], //can not pass readonly type to downshift so we cast it to regular T[]
299301
initialSelectedItem: initialSelectedOptions[0],
300302
isItemDisabled(item) {

0 commit comments

Comments
 (0)