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] Provide prop to disable option equal to value warning #36275

Closed
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
4 changes: 4 additions & 0 deletions docs/pages/base/api/use-autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"type": { "name": "boolean", "description": "boolean" },
"default": "false"
},
"disableOptionEqualToValueWarning": {
"type": { "name": "boolean", "description": "boolean" },
"default": "false"
},
"filterOptions": {
"type": {
"name": "(options: T[], state: FilterOptionsState<T&gt) =&gt T[]",
Expand Down
1 change: 1 addition & 0 deletions docs/pages/material-ui/api/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"disabled": { "type": { "name": "bool" }, "default": "false" },
"disabledItemsFocusable": { "type": { "name": "bool" }, "default": "false" },
"disableListWrap": { "type": { "name": "bool" }, "default": "false" },
"disableOptionEqualToValueWarning": { "type": { "name": "bool" }, "default": "false" },
"disablePortal": { "type": { "name": "bool" }, "default": "false" },
"filterOptions": { "type": { "name": "func" } },
"filterSelectedOptions": { "type": { "name": "bool" }, "default": "false" },
Expand Down
1 change: 1 addition & 0 deletions docs/translations/api-docs/autocomplete/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"disabled": "If <code>true</code>, the component is disabled.",
"disabledItemsFocusable": "If <code>true</code>, will allow focus on disabled items.",
"disableListWrap": "If <code>true</code>, the list box in the popup will not wrap focus.",
"disableOptionEqualToValueWarning": "Disable warning when options are not equal to value",
"disablePortal": "If <code>true</code>, the <code>Popper</code> content will be under the DOM hierarchy of the parent component.",
"filterOptions": "A function that determines the filtered options to be rendered on search.<br><br><strong>Signature:</strong><br><code>function(options: Array&lt;T&gt;, state: object) =&gt; Array&lt;T&gt;</code><br><em>options:</em> The options to render.<br><em>state:</em> The state of the component.",
"filterSelectedOptions": "If <code>true</code>, hide the selected options from the list box.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"disabled": "If <code>true</code>, the component is disabled.",
"disabledItemsFocusable": "If <code>true</code>, will allow focus on disabled items.",
"disableListWrap": "If <code>true</code>, the list box in the popup will not wrap focus.",
"disableOptionEqualToValueWarning": "Disable warning when options are not equal to value",
"filterOptions": "A function that determines the filtered options to be rendered on search.",
"filterSelectedOptions": "If <code>true</code>, hide the selected options from the list box.",
"freeSolo": "If <code>true</code>, the Autocomplete is free solo, meaning that the user input is not bound to provided options.",
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ export interface UseAutocompleteProps<
* @returns {boolean}
*/
isOptionEqualToValue?: (option: T, value: T) => boolean;
/**
* Disable warning when options are not equal to value
* @default false
*/
disableOptionEqualToValueWarning?: boolean;
/**
* If `true`, `value` must be an array and the menu will support multiple selections.
* @default false
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default function useAutocomplete(props) {
includeInputInList = false,
inputValue: inputValueProp,
isOptionEqualToValue = (option, value) => option === value,
disableOptionEqualToValueWarning = false,
multiple = false,
onChange,
onClose,
Expand Down Expand Up @@ -253,7 +254,7 @@ export default function useAutocomplete(props) {
const listboxAvailable = open && filteredOptions.length > 0 && !readOnly;

if (process.env.NODE_ENV !== 'production') {
if (value !== null && !freeSolo && options.length > 0) {
if (value !== null && !freeSolo && options.length > 0 && !disableOptionEqualToValueWarning) {
const missingValue = (multiple ? value : [value]).filter(
(value2) => !options.some((option) => isOptionEqualToValue(option, value2)),
);
Expand Down
1 change: 1 addition & 0 deletions packages/mui-joy/src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ const excludeUseAutocompleteParams = <
disableCloseOnSelect,
disabledItemsFocusable,
disableListWrap,
disableOptionEqualToValueWarning,
filterSelectedOptions,
handleHomeEndKeys,
includeInputInList,
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
disabled = false,
disabledItemsFocusable = false,
disableListWrap = false,
disableOptionEqualToValueWarning = false,
disablePortal = false,
filterOptions,
filterSelectedOptions = false,
Expand Down Expand Up @@ -807,6 +808,11 @@ Autocomplete.propTypes /* remove-proptypes */ = {
* @default false
*/
disableListWrap: PropTypes.bool,
/**
* Disable warning when options are not equal to value
* @default false
*/
disableOptionEqualToValueWarning: PropTypes.bool,
/**
* If `true`, the `Popper` content will be under the DOM hierarchy of the parent component.
* @default false
Expand Down
16 changes: 16 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,22 @@ describe('<Autocomplete />', () => {
]);
});

it('skip warn if value does not exist in options list and disableOptionEqualToValueWarning is true', () => {
const value = 'value outside of options';
const options = ['val from previous server fetch'];

expect(() => {
render(
<Autocomplete
value={value}
options={options}
renderInput={(params) => <TextField {...params} />}
disableOptionEqualToValueWarning
/>,
);
}).not.toWarnDev();
});

it('warn if groups options are not sorted', () => {
const data = [
{ group: 1, value: 'A' },
Expand Down