viclafouch / mui-color-input

📌 A color input designed for MUI (Material ui) V6 built with TinyColor
https://viclafouch.github.io/mui-color-input/
60 stars 20 forks source link

onChange is not called with invalid value #36

Closed Marv-elous closed 2 months ago

Marv-elous commented 3 months ago

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):