Working with Antd Form
Antd have built in form management and support complex usage. It might seem overwhelming and hard to work with it for beginner or antd first time user. Here are some notes I tried to collect for my own future reference.
Preparations / Basics
- Wrap the whole form with
Form
component. - Create form hook through
Form.useForm
:
- Make sure to wrap each fields with
Form.Item
and definename
(similar concept likereact-hook-form
orformik
).
Things to Aware with Form
- Attach
form
state toForm
. - Define
initialValues
with state / immutable, not normal object. Why? Turns out anything passed toinitialValues
is prone to be unstable / becomes mutable. I tried to work around it with making sure passing state into it as state is immutable. This must be awared especially after switching back and forth between pages. Form
provideonFinish
that will only be triggered when submit and validation is success.Form
provideonFailure
that will be triggered when submit and validation is not passing.Form.Item
providerules
to define field validation.- Form
initialValues
will only work or mapped toForm.Item
orForm.List
. If there's an attribute which doesn't shown / rendered usingForm.Item
/Form.List
, it won't be mapped / assigned. - To make attribute still exist but the UI hidden, don't use null render. Use
style.display: 'none'
Form Submit
For submit button which are not a descendant of the form
:
-
define
id
inForm
-
define
form
in the submit button with the value same asid
being defined inForm
-
examples:
Actually, it's a web standard 😄 References:
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#form
- https://www.w3schools.com/tags/att_button_form.asp
Things to Aware with Inputs
Input, Select, NumberInput
- For general purpose inputs, use
Input
component. - For numeric inputs and expecting form state to also receive number, use
NumberInput
component. - For formatted number input, use
NumericInput
ofreact-number-format
and set customInput prop asInput
. - For switch toggle input, use
valuePropName="checked"
on theForm.Item
wrapper of the switch.
⚠️ Do Not Put Multiple Children inside Form.Item
Working with Array fields
Form.List
can be used for array property / attribute of the form shape.Form.List
provide render function children:(fields, { add, remove, move }) => React.ReactNode
- We can combine
Form.List
withTable
and passfields
as Table'sdataSource
.
Useful References
- https://ant.design/components/form
- https://codesandbox.io/s/antd-rich-list-demo-v2-z7wjf?file=/index.js
Form Item Persistency
For cases where the form item value need to be persisted whether shown or not
Keep the Form Values Persistent
Use the preserve
property in Form.Item
or Form.List
. This ensures the form values persist even if the component is unmounted.
References: https://ant.design/components/form#form (preserve
)
Working with Collapsible
Set destroyInactivePanel
to false for the collapsible component. This keeps the panel content in the DOM even when collapsed, so no values are lost.
References: https://ant.design/components/collapse#collapse (destroyInactivePanel
)
Numeric Input Form Item Rules
For min and max validation, it must be always paired with type: 'number'