Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<Select> element cannot be forced uncontrolled #329

Closed
ruggi99 opened this issue May 14, 2021 · 5 comments
Closed

<Select> element cannot be forced uncontrolled #329

ruggi99 opened this issue May 14, 2021 · 5 comments

Comments

@ruggi99
Copy link

ruggi99 commented May 14, 2021

Hi all,
I'm creating a demo page where I show the various form elements in a modal card.
All these elements are uncontrolled inputs and it's wanted.
All except <Select> where options cannot be changed without making a controlled input.
EDIT: doesn't work in multiple mode or not

Looking at the code you are forcing the defaultValue if value isn't specified and the defaultValue cannot be specified.

I think forcing the value isn't good practise because the input will be controlled

const defaultValue = multiple ? [] : '';
const context = useFieldContext();
const calculatedSize = size || context.size;
return (
<Element
domRef={domRef}
className={classnames('select', className, {
[`is-${calculatedSize}`]: calculatedSize,
[`is-${color}`]: color,
'is-loading': loading,
'is-multiple': multiple,
'is-rounded': rounded,
})}
style={style}
>
<Element
{...props}
className={classnames({
[`is-${normalizeStatus(status)}`]: status,
})}
multiple={multiple}
value={value !== undefined ? value : defaultValue}

EDIT:
Using value={null} makes the Select uncontrolled but React prints a warning about this:

Warning: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
    at select
    at Element
@kennethnym
Copy link
Collaborator

Can you explain a bit for your use case of needing an uncontrolled input?

@ruggi99
Copy link
Author

ruggi99 commented May 20, 2021

No particular use case from my side but it's the only controlled input in your library.
In my demo page, as said above, I have to do state management for a value that I don't care

@kennethnym
Copy link
Collaborator

You should be able to use an empty string as the default value.

@ruggi99
Copy link
Author

ruggi99 commented May 20, 2021

<Form.Select defaultValue=""><options...></Form.Select> doesn't work

@kennethnym
Copy link
Collaborator

Will look into it.

@couds couds closed this as completed in 8201eca May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants