Skip to content

[select] Out of range defaultValue causes onChange to fire on mount #7

@DiegoAndai

Description

@DiegoAndai

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Link to live example: https://codesandbox.io/s/use-select-default-value-triggers-on-change-h5mmmh?file=/Demo.js

Steps:

  1. Create a Select component using the useSelect hook
  2. Provide an onChange callback to useSelect
  3. Provide a defaultValue to useSelect which doesn't correspond to any of the options' values

Current behavior 😯

When the component mounts, the onChange callback is fired with null values for the event and newValue parameters.

Expected behavior 🤔

  • The onChange callback shouldn't fire
  • There should be a warning about the value provided to useSelect being out of range

Context 🔦

Refactoring material-ui v5 Select to be based on the useSelect hook. Hopefully, we can make it so that the refactored version fails gracefully when value = "" or defaultValue = "" as that was the way to deselect all options in v5.

Your environment 🌎

npx @mui/envinfo
  System:
    OS: macOS 13.4.1
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.1/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
  Browsers:
    Chrome: 117.0.5938.149
    Edge: Not Found
    Safari: 16.5.1
  npmPackages:
    @emotion/react:  11.11.1 
    @emotion/styled:  11.11.0 
    @mui/base:  5.0.0-beta.17 
    @mui/codemod:  5.14.11 
    @mui/core-downloads-tracker:  5.14.11 
    @mui/docs:  5.14.11 
    @mui/envinfo:  2.0.10 
    @mui/icons-material:  5.14.11 
    @mui/internal-waterfall:  1.0.0 
    @mui/joy:  5.0.0-beta.8 
    @mui/lab:  5.0.0-alpha.146 
    @mui/markdown:  5.0.0 
    @mui/material:  5.14.11 
    @mui/material-next:  6.0.0-alpha.103 
    @mui/private-theming:  5.14.11 
    @mui/styled-engine:  5.14.11 
    @mui/styled-engine-sc:  5.14.11 
    @mui/styles:  5.14.11 
    @mui/system:  5.14.11 
    @mui/types:  7.2.4 
    @mui/utils:  5.14.11 
    @mui/x-charts:  6.0.0-alpha.12 
    @mui/x-data-grid:  6.15.0 
    @mui/x-data-grid-generator:  6.15.0 
    @mui/x-data-grid-premium:  6.15.0 
    @mui/x-data-grid-pro:  6.15.0 
    @mui/x-date-pickers:  6.15.0 
    @mui/x-date-pickers-pro:  6.15.0 
    @mui/x-license-pro:  6.10.2 
    @mui/x-tree-view:  6.0.0-alpha.1 
    @mui/zero-jest:  0.0.1-alpha.0 
    @mui/zero-next-plugin:  0.0.1-alpha.0 
    @mui/zero-runtime:  0.0.1-alpha.0 
    @mui/zero-tag-processor:  0.0.1-alpha.0 
    @mui/zero-vite-plugin:  0.0.1-alpha.0 
    @types/react: ^18.2.23 => 18.2.23 
    react:  18.2.0 
    react-dom:  18.2.0 
    styled-components:  5.3.11 
    typescript: ^5.1.6 => 5.1.6 

Metadata

Metadata

Labels

component: selectChanges related to the select component.type: bugIt doesn't behave as expected.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions