Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Add ability to override key down events handlers #23487

Merged
merged 5 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions docs/src/pages/components/autocomplete/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ Search within 10,000 randomly generated options. The list is virtualized thanks

{{"demo": "pages/components/autocomplete/Virtualize.js"}}

## Events

If you would like to prevent the default key handler behavior, you can set the event's `defaultMuiPrevented` property to `true`:

```jsx
<Autocomplete
onKeyDown={(event) => {
if (event.key === 'Enter') {
// Prevent's default 'Enter' behavior.
event.defaultMuiPrevented = false;
// your handler code
}
}}
/>
```

## Limitations

### autocomplete/autofill
Expand Down
33 changes: 33 additions & 0 deletions packages/material-ui/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2120,4 +2120,37 @@ describe('<Autocomplete />', () => {

expect(getAllByRole('option')).to.have.length(1);
});

it('should prevent the default event handlers', () => {
const handleChange = spy();
const handleSubmit = spy();
const Test = () => (
<div
onKeyDown={(event) => {
if (!event.defaultPrevented && event.key === 'Enter') {
handleSubmit();
}
}}
>
<Autocomplete
options={['one', 'two']}
onChange={handleChange}
onKeyDown={(event) => {
if (event.key === 'Enter') {
event.persist();
event.defaultMuiPrevented = true;
}
}}
renderInput={(params) => <TextField autoFocus {...params} />}
/>
</div>
);
render(<Test />);
const textbox = screen.getByRole('textbox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleChange.callCount).to.equal(0);
expect(handleSubmit.callCount).to.equal(1);
});
});
4 changes: 2 additions & 2 deletions packages/material-ui/src/SwipeableDrawer/SwipeableDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
}

// We can only have one node at the time claiming ownership for handling the swipe.
if (event.muiHandled) {
if (event.defaultMuiPrevented) {
return;
}

Expand Down Expand Up @@ -466,7 +466,7 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
}
}

event.muiHandled = true;
event.defaultMuiPrevented = true;
nodeThatClaimedTheSwipe = null;
swipeInstance.current.startX = currentX;
swipeInstance.current.startY = currentY;
Expand Down
14 changes: 9 additions & 5 deletions packages/material-ui/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ export default function useAutocomplete(props) {
};

const handleKeyDown = (other) => (event) => {
if (other.onKeyDown) {
other.onKeyDown(event);
}

if (event.defaultMuiPrevented) {
return;
}

if (focusedTag !== -1 && ['ArrowLeft', 'ArrowRight'].indexOf(event.key) === -1) {
setFocusedTag(-1);
focusTag(-1);
Expand Down Expand Up @@ -723,7 +731,7 @@ export default function useAutocomplete(props) {
const option = filteredOptions[highlightedIndexRef.current];
const disabled = getOptionDisabled ? getOptionDisabled(option) : false;

// We don't want to validate the form.
// Avoid early form validation, let the end-users continue filling the form.
event.preventDefault();

if (disabled) {
Expand Down Expand Up @@ -775,10 +783,6 @@ export default function useAutocomplete(props) {
default:
}
}

if (other.onKeyDown) {
other.onKeyDown(event);
}
};

const handleFocus = (event) => {
Expand Down