-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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] Provide options to control behaviour when cleared #18873
Comments
|
I'm not really sure how to respond to my "motivation" other than to say that clearing a value doesn't always imply the user wants to select another one - if they did, I would think they'd be less likely to focus specifically on the clear button, and would just open the select anywhere. Regarding The |
Thanks for the extra information, I better understand the motivation now. My previous suggestions were a bit off the target. I can see two different levels of solution.
|
What do you think of the following diff? diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index ca8afabdf..4466e4d50 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -108,6 +108,7 @@ export default function useAutocomplete(props) {
setDefaultId(`mui-autocomplete-${Math.round(Math.random() * 1e5)}`);
}, []);
+ const ignoreFocus = React.useRef(false);
const firstFocus = React.useRef(true);
const inputRef = React.useRef(null);
const listboxRef = React.useRef(null);
@@ -509,10 +510,8 @@ export default function useAutocomplete(props) {
};
const handleClear = event => {
+ ignoreFocus.current = true;
handleValue(event, multiple ? [] : null);
- if (disableOpenOnFocus) {
- handleClose();
- }
setInputValue('');
};
@@ -614,14 +613,17 @@ export default function useAutocomplete(props) {
const handleFocus = event => {
setFocused(true);
- if (!disableOpenOnFocus) {
+ if (!disableOpenOnFocus && !ignoreFocus.current) {
handleOpen(event);
}
};
const handleBlur = event => {
setFocused(false);
firstFocus.current = true;
+ ignoreFocus.current = false;
if (debug && inputValue !== '') {
return;
@@ -706,8 +708,6 @@ export default function useAutocomplete(props) {
});
const handlePopupIndicator = event => {
- inputRef.current.focus();
-
if (open) {
handleClose(event);
} else {
@@ -715,13 +715,14 @@ export default function useAutocomplete(props) {
}
};
+ // Prevent input blur when interacting with the combobox
const handleMouseDown = event => {
if (event.target.nodeName !== 'INPUT') {
- // Prevent blur
event.preventDefault();
}
};
+ // Focus the input when first interacting with the combobox
const handleClick = () => {
if (
firstFocus.current && Do you want to submit a pull request? It would also be great to add a new test case :). |
Look interesting - if I'm reading it correctly it appears to be changing the default behaviour as you suggested. I'll try this out then have a go at doing the PR myself - I don't have a lot of experience writing test cases for TS/JS but it never hurts to learn. |
Is there any particular reason that Mui enforce focus on clear? What if the end user wishes to have a clear state and no focus after clear? |
@MartinSkovsen The focus comes from: The clear button receives the focus on mouseDown, and is unmounted on mouseUp, so if we didn't focus the input, the focus would move to the outermost element that can receive a focus, often the Where do you expect the |
Good point. I don't have any specific expectation for the activeElement; Keeping the current functionality is certainly a sensible default. However, I would like to optionally provide an alternative activeElement. Think of this scenario: I have an optional entry in my form. The user uses a combobox for providing the desired value. The user might decide to delete the selected value for which ever reason, and then want to be out of focus. I currently have this case, and ideally I don't interfere with how the component works, particularly since a work around will be devious (and surely confusing). -- Perhaps this is just an anti pattern suggestion... |
@MartinSkovsen Thanks for the extra details, feel free to override this behavior in your application. I assume you would need to call prevent default on mouse down to keep the focus where it was, and extend the component to support https://next.material-ui.com/components/autocomplete/#events. |
Summary 💡
While this issue is similar to #18280 I don't believe its a duplicate.
It would be good if developers had the ability to customize the behavior of the autocomplete when the selection is cleared via the Clear button.
Options:
openOnClear={false}
- revert the Autocomplete to it's null/placeholder stateonClear
event handlerExamples 🌈
The text was updated successfully, but these errors were encountered: