Skip to content

Commit

Permalink
[Autocomplete] Fix support for renderTags={() => null} (#21460)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthenschke committed Jun 16, 2020
1 parent a953f2e commit 35d11d3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
9 changes: 8 additions & 1 deletion docs/src/pages/components/autocomplete/GitHubLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ export default function GitHubLabel() {
popperDisablePortal: classes.popperDisablePortal,
}}
value={pendingValue}
onChange={(event, newValue) => {
onChange={(event, newValue, reason) => {
if (
event.type === 'keydown' &&
event.key === 'Backspace' &&
reason === 'remove-option'
) {
return;
}
setPendingValue(newValue);
}}
disableCloseOnSelect
Expand Down
9 changes: 8 additions & 1 deletion docs/src/pages/components/autocomplete/GitHubLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ export default function GitHubLabel() {
popperDisablePortal: classes.popperDisablePortal,
}}
value={pendingValue}
onChange={(event, newValue) => {
onChange={(event, newValue, reason) => {
if (
event.type === 'keydown' &&
(event as React.KeyboardEvent).key === 'Backspace' &&
reason === 'remove-option'
) {
return;
}
setPendingValue(newValue);
}}
disableCloseOnSelect
Expand Down
31 changes: 31 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { act, createClientRender, fireEvent, screen } from 'test/utils/createCli
import { createFilterOptions } from '../useAutocomplete/useAutocomplete';
import Autocomplete from './Autocomplete';
import TextField from '@material-ui/core/TextField';
import Chip from '@material-ui/core/Chip';

describe('<Autocomplete />', () => {
const mount = createMount();
Expand Down Expand Up @@ -399,6 +400,36 @@ describe('<Autocomplete />', () => {
expect(handleChange.args[0][1]).to.deep.equal([options[1]]);
expect(textbox).toHaveFocus();
});

it('should not crash if a tag is missing', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
{...defaultProps}
defaultValue={options}
options={options}
value={options}
renderTags={(value, getTagProps) =>
value
.filter((x, index) => index === 1)
.map((option, index) => <Chip label={option.title} {...getTagProps({ index })} />)
}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('textbox');
const [firstSelectedValue] = screen.getAllByRole('button');

fireEvent.keyDown(textbox, { key: 'ArrowLeft' });
// skip value "two"
expect(firstSelectedValue).toHaveFocus();

fireEvent.keyDown(firstSelectedValue, { key: 'ArrowRight' });
expect(textbox).toHaveFocus();
});
});

it('should trigger a form expectedly', () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default function useAutocomplete(props) {
// Same logic as MenuList.js
const nextFocusDisabled = disabledItemsFocusable
? false
: option && (option.disabled || option.getAttribute('aria-disabled') === 'true');
: !option || option.disabled || option.getAttribute('aria-disabled') === 'true';

if ((option && !option.hasAttribute('tabindex')) || nextFocusDisabled) {
// Move to the next element.
Expand Down Expand Up @@ -596,10 +596,10 @@ export default function useAutocomplete(props) {

// Same logic as MenuList.js
if (
option &&
(!option.hasAttribute('tabindex') ||
option.disabled ||
option.getAttribute('aria-disabled') === 'true')
!option ||
!option.hasAttribute('tabindex') ||
option.disabled ||
option.getAttribute('aria-disabled') === 'true'
) {
nextFocus += direction === 'next' ? 1 : -1;
} else {
Expand Down

0 comments on commit 35d11d3

Please sign in to comment.