marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.89k stars 5.23k forks source link

TextInput or BooleanInput default value not dynamic. #9618

Closed david-hov closed 8 months ago

david-hov commented 8 months ago

What you were expecting: if component inside SimpleForm updated change children defaultValues

What happened instead: Keeps the first defaultValue

Related code:

<SimpleForm>
      <FormDataConsumer>
          {({ formData }) => formData.value === 'AAA' ?
           <AAA /> : <BBB />
          }
      </FormDataConsumer>
</SimpleForm>

/// AAA
<BooleanInput defaultValue={true} source='sameSource' label='AAA' />

/// BBB
<BooleanInput defaultValue={false} source='sameSource' label='BBB' />

https://codesandbox.io/p/devbox/fast-sun-33rf9w?file=%2Fsrc%2Ftags%2FTagCreate.tsx%3A25%2C20

Environment

fzaninotto commented 8 months ago

The default value is set on mount. That's a limitation of react-hook-form. If you need this feature, I suggest you open an issue on their repository. Otherwise, use a useWatch to update the current value (not the default) based on other values.