1

Is it possible to render SelectInput with default value selected? I need to load the page with a default language selected. thanks.

My code:


export const CT_SELECT_I18N = [
    {id: 'en', text: 'English'},
    {id: 'pt', text: 'Português'},
    {id: 'es', text: 'Español'},
];

const renderSelect = ({
                          meta: {touched, error} = {},
                          input: {...inputProps},
                          ...props
                      }) => (
    <SelectInput
        error={!!(touched && error)}
        helperText={touched && error}
        {...inputProps}
        {...props}
        fullWidth
        source="idioma"
        choices={Constants.CT_SELECT_I18N}
        translateChoice={false}
        optionValue="id"
        optionText="text"
    />
);

2 Answers 2

3

Use the prop initialValue for this.

Your selectinput would look like

     <SelectInput
        error={!!(touched && error)}
        helperText={touched && error}
        {...inputProps}
        {...props}
        fullWidth
        source="idioma"
        choices={Constants.CT_SELECT_I18N}
        translateChoice={false}
        optionValue="id"
        optionText="text"
        initialValue={"en"}
    />

More here: https://marmelab.com/react-admin/Inputs.html#common-input-props

Sign up to request clarification or add additional context in comments.

Comments

0

Since React Admin version 3.3.0 you can use the defaultValue prop.

<SelectInput
    error={!!(touched && error)}
    helperText={touched && error}
    {...inputProps}
    {...props}
    fullWidth
    source="idioma"
    choices={Constants.CT_SELECT_I18N}
    translateChoice={false}
    optionValue="id"
    optionText="text"
    defaultValue="en"
/>

In the React Admin documentation, the defaultValue prop is documented as a valid option for form inputs, and it's described as a prop that can be used to set the default value of a form field. See: https://marmelab.com/react-admin/Inputs.html#defaultvalue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.