-
-
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
[Docs] Autocomplete Multiple Values delete bug #18081
Comments
On the same page, but unrelated to the issue, the following is written:
I guess that's supposed to say |
@Studio384 Thank you for the report. I can think of two possible solutions:
diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index bc02f10f1..f43a99e08 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -11,6 +11,26 @@ function stripDiacritics(string) {
: string;
}
+// Support IE 11
+// https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
+function closest(element, string) {
+ let closestPonyfill = Element.prototype.closest;
+
+ if (!closestPonyfill) {
+ closestPonyfill = () => {
+ let el = element;
+
+ do {
+ if (el.matches(string)) return el;
+ el = el.parentNode;
+ } while (el !== null && el.nodeType === 1);
+ return null;
+ };
+ }
+
+ return closestPonyfill(string);
+}
+
export function createFilterOptions(config = {}) {
const {
ignoreAccents = true,
@@ -584,7 +604,8 @@ export default function useAutocomplete(props) {
};
const handleTagDelete = event => {
- const index = Number(event.currentTarget.getAttribute('data-tag-index'));
+ const tagRoot = closest(event.currentTarget, '[data-tag-index]');
+ const index = Number(tagRoot.getAttribute('data-tag-index'));
const newValue = [...value];
newValue.splice(index, 1);
handleValue(event, newValue);
diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.js
index 4c6aa5260..eddf2c0cb 100644
--- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.js
+++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.js
@@ -220,21 +220,20 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
let startAdornment;
if (multiple && value.length > 0) {
- const tagProps = {
- ...getTagProps(),
+ const getTagProps2 = params => ({
className: classes.tag,
- };
+ ...getTagProps(params),
+ });
if (renderTags) {
- startAdornment = renderTags(value, tagProps);
+ startAdornment = renderTags(value, getTagProps2);
} else {
startAdornment = value.map((option, index) => (
<Chip
key={index}
- data-tag-index={index}
tabIndex={-1}
label={getOptionLabel(option)}
- {...tagProps}
+ {...getTagProps2({ index })}
/>
));
}
diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index bc02f10f1..ea709e7f7 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -583,8 +583,7 @@ export default function useAutocomplete(props) {
selectNewValue(event, filteredOptions[highlightedIndexRef.current]);
};
- const handleTagDelete = event => {
- const index = Number(event.currentTarget.getAttribute('data-tag-index'));
+ const handleTagDelete = index => event => {
const newValue = [...value];
newValue.splice(index, 1);
handleValue(event, newValue);
@@ -672,8 +671,9 @@ export default function useAutocomplete(props) {
event.preventDefault();
},
}),
- getTagProps: () => ({
- onDelete: handleTagDelete,
+ getTagProps: ({ index }) => ({
+ 'data-tag-index': index,
+ onDelete: handleTagDelete(index),
}),
getPopupProps: () => ({
role: 'presentation', I might prefer 2. 🤔 |
2 looks better to me as well. |
@joshwooding Alright, then let's go for it. I like the mental model of it, it makes the |
Current Behavior 😯
Clicking the delete button for an option in the "Multiple Values" and "Fixed Options" examples removes whatever is first in the last.
Expected Behavior 🤔
Clicking the delete button for an option removes its own optoin.
Steps to Reproduce 🕹
Link:
Steps:
Your Environment 🌎
MUI Documentation
The text was updated successfully, but these errors were encountered: