Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initialValue for dynamic form field (ant design)?

I am struggling with setting the initialValue for the dynamic form field. Lets say we have this example from AntDesign docs.

https://codesandbox.io/s/wonderful-lichterman-br63z?file=/index.js

and the point is, to set the initial value for every newly added fields, in this example i want the "last name" to be set to "kowalski".

following ant design docs :

"In most case, we always recommend to use Form initialValues. Use Item initialValue only when dynamic field usage"

so i was trying to set the initialValue but in result i only get this warning :

"React does not recognize the initialValue prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase initialvalue instead. If you accidentally passed it from a parent component, remove it from the DOM element."

thanks for any advice.

like image 826
Olo Avatar asked May 06 '20 15:05

Olo


1 Answers

I found an easier implementation. Turns out add method allows one to set field defaultValue.

<Form.Item>
  <Button
    type="dashed"
    onClick={() => {
      add({ lastName: 'mejia', firstName: 'cesar' })
    }}
    style={{ width: '100%' }}
  >
    <PlusOutlined /> Add field
  </Button>
</Form.Item>

See: https://codesandbox.io/s/fervent-morning-c9vhk

like image 100
Cesar Napoleon Mejia Leiva Avatar answered Sep 28 '22 10:09

Cesar Napoleon Mejia Leiva