Skip to content

Commit a33ae0f

Browse files
update the test, fix discovered issue
1 parent 93c3e04 commit a33ae0f

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,27 @@ describe('<Autocomplete />', () => {
7777
checkHighlightIs('one');
7878
});
7979

80-
it.only('should set the focus on selected item when dropdown is expanded', () => {
81-
const options = ['one', 'two', 'three'];
80+
it('should set the focus on selected item when dropdown is expanded', () => {
8281
const { getByRole, setProps } = render(
8382
<Autocomplete
8483
{...defaultProps}
8584
value="one"
86-
options={options}
85+
options={['one', 'two', 'three']}
8786
renderInput={params => <TextField autoFocus {...params} />}
88-
size="small"
8987
/>,
9088
);
9189

9290
function checkHighlightIs(expected) {
9391
expect(getByRole('listbox').querySelector('li[data-focus]')).to.have.text(expected);
9492
}
9593

96-
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
9794
checkHighlightIs('one');
98-
99-
// setProps({ value: 'two' })
100-
// checkHighlightIs('two');
101-
102-
// fireEvent.change(document.activeElement, { target: { value: 'two' } });
103-
// fireEvent.keyDown(document.activeElement, { key: 'Enter' });
104-
// fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
105-
// checkHighlightIs('two');
95+
setProps({ value: 'two' });
96+
checkHighlightIs('two');
10697
});
10798
});
10899

109-
describe.only('prop: filterSelectedOptions', () => {
100+
describe('prop: filterSelectedOptions', () => {
110101
it('when the last item is selected, highlights the new last item', () => {
111102
const { getByRole } = render(
112103
<Autocomplete

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default function useAutocomplete(props) {
133133
const defaultHighlighted = autoHighlight ? 0 : -1;
134134
const highlightedIndexRef = React.useRef(defaultHighlighted);
135135

136-
function setHighlightedIndex(index, mouse = false) {
136+
const setHighlightedIndex = useEventCallback((index, mouse = false) => {
137137
highlightedIndexRef.current = index;
138138
// does the index exist?
139139
if (index === -1) {
@@ -190,7 +190,7 @@ export default function useAutocomplete(props) {
190190
listboxNode.scrollTop = element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0);
191191
}
192192
}
193-
}
193+
});
194194

195195
const [value, setValue] = useControlled({
196196
controlled: valueProp,
@@ -323,7 +323,7 @@ export default function useAutocomplete(props) {
323323
}
324324
}
325325

326-
const changeHighlightedIndex = (diff, direction) => {
326+
const changeHighlightedIndex = useEventCallback((diff, direction) => {
327327
if (!popupOpen) {
328328
return;
329329
}
@@ -390,11 +390,7 @@ export default function useAutocomplete(props) {
390390
}
391391
}
392392
}
393-
};
394-
395-
React.useEffect(() => {
396-
changeHighlightedIndex('reset', 'next');
397-
}, [inputValue]); // eslint-disable-line react-hooks/exhaustive-deps
393+
});
398394

399395
React.useEffect(() => {
400396
if (!open) {
@@ -423,8 +419,18 @@ export default function useAutocomplete(props) {
423419
if (highlightedIndexRef.current >= filteredOptions.length - 1) {
424420
setHighlightedIndex(filteredOptions.length - 1);
425421
}
422+
423+
// Ignore filterOptions => options, getOptionSelected, getOptionLabel)
426424
// eslint-disable-next-line react-hooks/exhaustive-deps
427-
}, [value, open, filterSelectedOptions, multiple]);
425+
}, [
426+
value,
427+
open,
428+
filterSelectedOptions,
429+
changeHighlightedIndex,
430+
setHighlightedIndex,
431+
inputValue,
432+
multiple,
433+
]);
428434

429435
const handleOpen = event => {
430436
if (open) {

0 commit comments

Comments
 (0)