Skip to content

Commit c11afb4

Browse files
authored
[combobox] Preserve inline always highlight on blur (#4016)
1 parent c61ef4d commit c11afb4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/react/src/autocomplete/root/AutocompleteRoot.test.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,58 @@ describe('<Autocomplete.Root />', () => {
385385
expect(input).to.have.attribute('aria-activedescendant', firstOption.id);
386386
expect(firstOption).to.have.attribute('data-highlighted');
387387
});
388+
389+
it('keeps the latest pointer highlight on outside blur when behavior is "always"', async () => {
390+
const { user } = await render(
391+
<React.Fragment>
392+
<Autocomplete.Root
393+
items={['apple', 'banana', 'cherry']}
394+
autoHighlight="always"
395+
keepHighlight
396+
open
397+
inline
398+
>
399+
<Autocomplete.Input data-testid="input" />
400+
<Autocomplete.List>
401+
{(item: string) => (
402+
<Autocomplete.Item key={item} value={item}>
403+
{item}
404+
</Autocomplete.Item>
405+
)}
406+
</Autocomplete.List>
407+
</Autocomplete.Root>
408+
<button data-testid="outside">outside</button>
409+
</React.Fragment>,
410+
);
411+
412+
const input = screen.getByTestId<HTMLInputElement>('input');
413+
const banana = screen.getByRole('option', { name: 'banana' });
414+
415+
await act(async () => {
416+
input.focus();
417+
});
418+
419+
await user.hover(banana);
420+
421+
await waitFor(() => {
422+
expect(input).to.have.attribute('aria-activedescendant', banana.id);
423+
expect(banana).to.have.attribute('data-highlighted');
424+
});
425+
426+
const outside = screen.getByTestId('outside');
427+
fireEvent.pointerDown(outside);
428+
fireEvent.blur(input, { relatedTarget: outside });
429+
fireEvent.focus(outside);
430+
431+
await waitFor(() => {
432+
expect(input).to.have.attribute('aria-activedescendant', banana.id);
433+
expect(banana).to.have.attribute('data-highlighted');
434+
});
435+
436+
expect(screen.getByRole('option', { name: 'apple' })).not.to.have.attribute(
437+
'data-highlighted',
438+
);
439+
});
388440
});
389441

390442
describe('prop: keepHighlight', () => {

packages/react/src/combobox/input/ComboboxInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export const ComboboxInput = React.forwardRef(function ComboboxInput(
211211
setFocused(false);
212212

213213
const activeIndex = store.state.activeIndex;
214-
if (inline && activeIndex !== null) {
214+
if (inline && activeIndex !== null && autoHighlightMode !== 'always') {
215215
lastActiveIndexRef.current = activeIndex;
216216
shouldRestoreActiveIndexRef.current = true;
217217
store.state.setIndices({ activeIndex: null });

0 commit comments

Comments
 (0)