Skip to content

Commit

Permalink
[material-ui][Autocomplete] Fix default value for multiple mode getti…
Browse files Browse the repository at this point in the history
…ng redefined with React 19 (#43189)
  • Loading branch information
DiegoAndai committed Nov 26, 2024
1 parent e682861 commit 277246d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const pageSize = 5;
const defaultIsActiveElementInListbox = (listboxRef) =>
listboxRef.current !== null && listboxRef.current.parentElement?.contains(document.activeElement);

const MULTIPLE_DEFAULT_VALUE = [];

export function useAutocomplete(props) {
const {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -88,7 +90,7 @@ export function useAutocomplete(props) {
clearOnBlur = !props.freeSolo,
clearOnEscape = false,
componentName = 'useAutocomplete',
defaultValue = props.multiple ? [] : null,
defaultValue = props.multiple ? MULTIPLE_DEFAULT_VALUE : null,
disableClearable = false,
disableCloseOnSelect = false,
disabled: disabledProp,
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ describe('useAutocomplete', () => {
});

it('should allow tuples or arrays as value when multiple=false', () => {
function Test() {
const defaultValue = ['bar'];
const defaultValue = ['bar'];

function Test() {
const { getClearProps, getInputProps } = useAutocomplete({
defaultValue,
disableClearable: false,
Expand Down
4 changes: 3 additions & 1 deletion packages/mui-material/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const pageSize = 5;
const defaultIsActiveElementInListbox = (listboxRef) =>
listboxRef.current !== null && listboxRef.current.parentElement?.contains(document.activeElement);

const MULTIPLE_DEFAULT_VALUE = [];

function useAutocomplete(props) {
const {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -88,7 +90,7 @@ function useAutocomplete(props) {
clearOnBlur = !props.freeSolo,
clearOnEscape = false,
componentName = 'useAutocomplete',
defaultValue = props.multiple ? [] : null,
defaultValue = props.multiple ? MULTIPLE_DEFAULT_VALUE : null,
disableClearable = false,
disableCloseOnSelect = false,
disabled: disabledProp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ describe('useAutocomplete', () => {
});

it('should allow tuples or arrays as value when multiple=false', () => {
function Test() {
const defaultValue = ['bar'];
const defaultValue = ['bar'];

function Test() {
const { getClearProps, getInputProps } = useAutocomplete({
defaultValue,
disableClearable: false,
Expand Down

0 comments on commit 277246d

Please sign in to comment.