Search code examples
admin-on-rest

Select the first option of SelectInput by default in admin-on-rest


I've got a SelectInput with two choices:

<SelectInput source="existing_user" choices={[
        { id: 'existing', name: 'Existing'},
        { id: 'new', name: 'New User' },
  ]} validate={required} />

I also have a DependInput component that renders other fields, depending on the value of existing_user. I would like the first option ('existing') to be selected by default. However the SelectInput field is always empty when the form loads, so no other fields are visible: enter image description here

I tried setting the underlying SelectField's value property to 'existing' through options, but then I'm unable to edit the SelectInput (clicking the second option does nothing).

I'm probably missing something simple since this should be elementary, but I couldn't find anything helpful in the documentation about it.


Solution

  • Using defaultValue either on the SelectInput or on the form containing it should do the trick: