viclafouch / mui-chips-input

📌 A chips input designed for MUI (Material ui) V6
https://viclafouch.github.io/mui-chips-input/
MIT License
83 stars 19 forks source link

Move required to TextField only #23

Closed BennyAlex closed 1 year ago

BennyAlex commented 1 year ago

Hello,

currently, the required prop got passed to the input itself. This is not neccessary, as stated in the MUI docs, required has only to be set on the TextField. Currently this causes a problem with forms, since setting the element to required triggers browser validation (saying this field is required) and the input field itself is empty even chips are shown. This should fix this error.

viclafouch commented 1 year ago

Hey ! There is no difference with the current version because the required prop is only applied to the TextField (restTextFieldProps which contains the required prop is already spread to the TextFieldcomponent).

See: https://github.com/viclafouch/mui-chips-input/blob/048445eb498e454c4ab67976dd4fc82fbdb78edd/src/components/TextFieldChip/TextFieldChips.tsx#L342

I mean, here, I don't see any changes.

BennyAlex commented 1 year ago

But its also spread to InputProps I think.

viclafouch commented 1 year ago

Nop, we only spread the InputProps prop to the InputProps of TextField.

BennyAlex commented 1 year ago

https://codesandbox.io/s/mui-chips-input-forked-kju5uw Ah okay, than we need another workaround? As you can see, the browser throws an error even chips have been set.

It works if setting clearInputOnBlur={false} and you type someting inside the field, buts this is of course not really a solution.

viclafouch commented 1 year ago

Nah, you have to remove the required prop, and create your own validation rule.

viclafouch commented 1 year ago

before submitting your form, you have to check if value.length > 0, if not, display your own error message

BennyAlex commented 1 year ago

@viclafouch Hey, thanks for answer. This of course remove the browser validation, but it also removes the MUI styling for required fields... So maybe the fix is to set required for inputProps (written in lowercase for the element props) to false, so there is no browser check going on?

BennyAlex commented 1 year ago

https://codesandbox.io/s/mui-chips-input-forked-kju5uw?file=/src/App.js

looks like this will work.

BennyAlex commented 1 year ago

@viclafouch I think if this should be included by default, since browser validation will always fail with chips input, so this is more or less a bug. The developer always has to do his own validation anyways and by just overwriting required in inputProps, he stil can make it in required style by setting required true on the chips input component itself.