Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
expect(optionsList.nodeName).toBe('UL')
})

it('Preserves user-provided id on the input element', () => {
render(<Autocomplete id="my-custom-id" label={labelText} options={items} />)
const input = screen.getAllByLabelText(labelText)[0]
expect(input).toHaveAttribute('id', 'my-custom-id')
})

it('Maintains label-input association when custom id is provided', () => {
const { container } = render(
<Autocomplete id="my-custom-id" label={labelText} options={items} />,
)
const input = screen.getAllByLabelText(labelText)[0]

Check failure on line 151 in packages/eds-core-react/src/components/Autocomplete/Autocomplete.test.tsx

View workflow job for this annotation

GitHub Actions / Process packages

'input' is assigned a value but never used
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
const label = container.querySelector('label')

Check failure on line 152 in packages/eds-core-react/src/components/Autocomplete/Autocomplete.test.tsx

View workflow job for this annotation

GitHub Actions / Process packages

Avoid direct Node access. Prefer using the methods from Testing Library

Check failure on line 152 in packages/eds-core-react/src/components/Autocomplete/Autocomplete.test.tsx

View workflow job for this annotation

GitHub Actions / Process packages

Avoid direct Node access. Prefer using the methods from Testing Library

Check failure on line 152 in packages/eds-core-react/src/components/Autocomplete/Autocomplete.test.tsx

View workflow job for this annotation

GitHub Actions / Process packages

Avoid using container methods. Prefer using the methods from Testing Library, such as "getByRole()"
expect(label).toHaveAttribute('for', 'my-custom-id')
})

it('Has provided ReactNode label', async () => {
render(<Autocomplete label={<div>{labelText}</div>} options={items} />)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const useAutocomplete = <T>({
variant,
onClear,
ref,
id,
...other
}: AutocompleteProps<T> & { ref?: React.Ref<HTMLInputElement> }) => {
const [lastScrollOffset, setLastScrollOffset] = useState<number>(0)
Expand Down Expand Up @@ -295,6 +296,7 @@ export const useAutocomplete = <T>({

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