Skip to content

onChange is not called with invalid value #36

@Marv-elous

Description

@Marv-elous

Describe the bug
When setting a function for the onChange prop it never get's called with invalid values. If the user enters an invalid value the fallback value is used. From the outside there is no way to know if the user entered an invalid value and e.g. show an error message instead of submitting the fallback value.
To Reproduce
(Non)Pseudocode to reproduce

const MyComponent = () => {
    const [color, setColor] = React.useState('#ffffff')
    let error = ''

    const handleChange = (color) => {
        if(!matchIsValidColor(color)) {
            error = 'Invalid color!'
            return
        }
        setColor(color)
    }

    return (
        <MuiColorInput value={color} onChange={handleChange} />
)

Expected behavior
If the user enters an invalid value error gets set to 'Invalid color'.

Actual behavior
error never gets set to 'Invalid color'

Desktop (please complete the following information):

  • OS: MacOS Sonoma
  • Browser: Edge
  • Version: 125.0.2535.79

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions