1 | [![Build Status](https://dev.azure.com/jannikb/glue/_apis/build/status/jannikb%20Formik-antd?branchName=master)](https://dev.azure.com/jannikb/glue/_build/latest?definitionId=4?branchName=master)
|
2 | [![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
|
3 | [![license](https://badgen.now.sh/badge/license/MIT)](./LICENSE)
|
4 |
|
5 | [CodeSandbox](https://codesandbox.io/s/x2941k7vpz)
|
6 |
|
7 | # formik-antd
|
8 |
|
9 | Simple declarative bindings for [Ant Design](https://ant.design/docs/react/introduce) and [Formik](https://github.com/jaredpalmer/Formik).
|
10 |
|
11 | ## Example
|
12 |
|
13 | ```jsx
|
14 | import React from "react";
|
15 | import { Form, Input, InputNumber, Checkbox } from "@jbuschke/formik-antd";
|
16 | import { Formik } from "formik";
|
17 |
|
18 | <Formik
|
19 | initialValues={{ firstName: "", age: 20, newsletter: false }}
|
20 | render={()=> (
|
21 | <Form>
|
22 | <Input name="firstName" placeholder="First Name" />
|
23 | <InputNumber name="age" min={0} />
|
24 | <Checkbox name="newsletter">Newsletter</Checkbox>
|
25 | </Form>
|
26 | )}
|
27 | />
|
28 | ```
|
29 |
|
30 | ## Getting started
|
31 |
|
32 | ```
|
33 | npx create-react-app my-app
|
34 | cd my-app
|
35 | npm install formik antd @jbuschke/formik-antd
|
36 | npm run start
|
37 | ```
|
38 |
|
39 | Add `import "antd/dist/antd.css` to your `index.js` file (or look at https://ant.design/docs/react/getting-started for other options).
|
40 |
|
41 | ## Core Concept
|
42 |
|
43 | This library enriches several Ant Design components with a `name: string` property that connects them to a Formik form field. It is pretty simple:
|
44 |
|
45 | 1. Import a supported Ant Design component from `formik-antd` (i.e. `import { Input } from "@jbuschke/formik-antd"`.
|
46 | 2. Declare an instance of the component inside a `<Formik>` component.
|
47 | 3. Provide the `name` property (i.e. `"firstName"`).
|
48 |
|
49 | Your components input/value state is now connected/synced with the corresponding `Formik` field!
|
50 |
|
51 | The Ant Design components are feature rich and provide a lot of props to customize their vizual presentation. These features and also their apis stay the same. Visit their documentation to learn more.
|
52 |
|
53 | ## Core Components
|
54 |
|
55 | To learn about Antd components just visit the official docs. Most supported components are found in the [Data Entry](https://ant.design/components/auto-complete/) section.
|
56 |
|
57 | | | Name | Props |
|
58 | | --------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
59 | | :white_check_mark: | AutoComplete | { name, validate? } & [AutoCompleteProps](https://ant.design/components/auto-complete/) |
|
60 | | :white_check_mark: | Cascader | { name, validate? } & [CascaderProps](https://ant.design/components/cascader/) |
|
61 | | :white_check_mark: | Checkbox | { name, validate? } & [CheckboxProps](https://ant.design/components/checkbox/) |
|
62 | | :white_check_mark: | Checkbox.Group | { name, validate? } & [CheckboxGroupProps](https://ant.design/components/checkbox/#Checkbox-Group) |
|
63 | | :white_check_mark: | DatePicker | { name, validate? } & [DatePickerProps](https://ant.design/components/date-picker/) |
|
64 | | :white_check_mark: | DatePicker.WeekPicker | { name, validate? } & [WeekPickerProps](https://ant.design/components/date-picker/#WeekPicker) |
|
65 | | :white_check_mark: | DatePicker.RangePicker | { name, validate? } & [RangePickerProps](https://ant.design/components/date-picker/#RangePicker) |
|
66 | | :white_check_mark: | DatePicker.MonthPicker | { name, validate? } & [MonthPickerProps](https://ant.design/components/date-picker/#MonthPicker) |
|
67 | | :white_check_mark: | Input | { name, validate? } & [InputProps](https://ant.design/components/input/) |
|
68 | | :white_check_mark: | InputNumber | { name, validate? } & [InputNumberProps](https://ant.design/components/input-number/) |
|
69 | | :white_check_mark: | Input.Password | { name, validate? } & [InputPasswordProps](https://ant.design/components/input/) |
|
70 | | :white_check_mark: | Input.TextArea | { name, validate? } & [Input.TextAreaProps](https://ant.design/components/input/#components-input-demo-textarea) |
|
71 | | :white_check_mark: | Mention | { name, validate? } & [MentionProps](https://ant.design/components/mention/) |
|
72 | | :white_check_mark: | Radio.Group | { name, validate? } & [RadioGroupProps](https://ant.design/components/radio/#RadioGroup) |
|
73 | | :white_check_mark: | Rate | { name, validate? } & [RateProps](https://ant.design/components/rate/) |
|
74 | | :white_check_mark: | Select | { name, validate? } & [SelectProps](https://ant.design/components/select/) |
|
75 | | :white_check_mark: | Slider | { name, validate? } & [SliderProps](https://ant.design/components/slider/) |
|
76 | | :white_check_mark: | Switch | { name, validate? } & [SwitchProps](https://ant.design/components/switch/) |
|
77 | | :white_check_mark: | TimePicker | { name, validate? } & [TimePickerProps](https://ant.design/components/input/#components-input-demo-textarea) |
|
78 | | :white_check_mark: | Transfer | { name, validate? } & [TransferProps](https://ant.design/components/transfer/) |
|
79 | | :white_check_mark: | TreeSelect | { name, validate? } & [TreeSelectProps](https://ant.design/components/tree-select/) |
|
80 |
|
81 | ## Form- and Field-level Validation
|
82 |
|
83 | Formik provides form- and field-level [validation callbacks](https://jaredpalmer.com/formik/docs/guides/validation) to provide validation logic. How to validate is neither part of formik nor of this library.
|
84 |
|
85 | Form-level validation is done by setting formiks `validate` prop. Field-level validation is optional available on the components. Additional to the `name` prop formiks optional `validate?: (value: any) => undefined | string | Promise<any>` is added to all core components to allow field-level validation.
|
86 | There is one special case to be aware of when using field-level validation: When using the `Form.Item` component with another Antd-field component, the `validate` prop has to be added to the `Form.Item`, not the input component:
|
87 |
|
88 | ```jsx
|
89 | <Form.Item name="firstName" validate={validator}>
|
90 | <Input name="firstName" />
|
91 | </Form.Item>
|
92 | ```
|
93 |
|
94 | ## Rendering Validation Feedback
|
95 |
|
96 | Showing validation messages can be accomplished with the `Form.Item` component (or `FormItem` which is the same). It
|
97 | - renders *error* messages if the field has been touched and the corresponding field has a validation error (and changes the border color of enclosed input component to red).
|
98 | - renders a green *success* icon messages if it's `showValidateSuccess: boolean` prop is set to true, the field has been touched and the corresponding field does not have a validation error.
|
99 | - exposes some layout features and a label (visit https://ant.design/components/form/ for the details).
|
100 |
|
101 | ```jsx
|
102 | <Form.Item name="firstName" >
|
103 | <Input name="firstName" />
|
104 | </Form.Item>
|
105 | ```
|
106 |
|
107 | ## Submitting and Resetting Forms
|
108 |
|
109 | Directly under each `<Formik>` container a `<Form>` component _should_ be placed (unless you do not need it). This component composes the functionality provided by Ant Designs `<Form>` https://ant.design/components/form/ as well as Formiks (https://jaredpalmer.com/formik/docs/api/form):
|
110 |
|
111 |
|
112 | ```jsx
|
113 | import React from "react";
|
114 | import { Form, SubmitButton, /* ... */ } from "@jbuschke/formik-antd";
|
115 | import { Formik } from "formik";
|
116 |
|
117 | <Formik>
|
118 | <Form>
|
119 | {/* ... */}
|
120 | <SubmitButton />
|
121 | </Form>
|
122 | </Formik>
|
123 | ```
|
124 |
|
125 | ## Submitting & Resetting
|
126 |
|
127 | | Name | Props | Description |
|
128 | | ------------ | ----------------------------------------------- | ---------------------------------------------------- |
|
129 | | SubmitButton | [Button](https://ant.design/components/button/) | triggers form submission, is enabled when form valid |
|
130 | | ResetButton | [Button](https://ant.design/components/button/) | resets the form, is enabled when form dirty |
|
131 |
|
132 | The SubmitButton must be placed inside a `Form` component.
|
133 |
|
134 | ## Lists and Nested objects
|
135 |
|
136 | Nested objects and arrays can be accessed with lodash-like bracket syntax as described in the [Formik documentation](https://jaredpalmer.com/Formik/docs/guides/arrays).
|
137 |
|
138 | ```jsx
|
139 | <InputField name="friends[0].firstName" />
|
140 | ```
|
141 |
|
142 | ## ES imports
|
143 |
|
144 | ```
|
145 | npm install babel-plugin-import customize-cra react-app-rewired --save-dev
|
146 | ```
|
147 | `config-overrides.js`
|
148 | ```js
|
149 | const path = require('path')
|
150 | const { override, fixBabelImports } = require('customize-cra')
|
151 |
|
152 | module.exports = override(
|
153 | fixBabelImports('antd', {
|
154 | libraryName: 'antd',
|
155 | libraryDirectory: 'es',
|
156 | style: 'css',
|
157 | }),
|
158 | fixBabelImports('formik-antd',
|
159 | {
|
160 | libraryName: '@jbuschke/formik-antd',
|
161 | libraryDirectory: 'es'
|
162 | style: "css",
|
163 | },
|
164 | )
|
165 | );
|
166 | );
|
167 | ```
|
168 |
|
169 | `package.json`
|
170 |
|
171 | ```json
|
172 | "scripts": {
|
173 | "start": "react-app-rewired start",
|
174 | "build": "react-app-rewired build"
|
175 | }
|
176 | ```
|
177 |
|
178 | ## Treeshaking
|
179 |
|
180 | Treeshaking with ant design is currently kind of broken, as generally all icons are imported. This will be fixed as of ant design v4 (might be ready in 2019).
|
181 |
|
182 | ## Playground & Contributions
|
183 |
|
184 | If you want to dig into the source code and test locally you can use https://github.com/jannikbuschke/Formik-antd-playground (clone with the --recursive flag and follow the README, its pretty simple).
|
185 |
|
186 | ## TypeScript
|
187 |
|
188 | Types are included.
|
189 |
|
190 | ### Typechecking limitations
|
191 | Form values currently cannot be typechecked (at least to my knowledge). For example the following ideally would give a compile error:
|
192 |
|
193 | ```jsx
|
194 | <Formik<{name:string}> initialValues={{name:""}}>
|
195 | <Input name="naem" />
|
196 | </Formik>
|
197 | ```
|
198 |
|
199 | Typescript cannot (yet) enforce types of children. In the future this hopefully will be possible.
|
200 |
|
201 | ## License
|
202 |
|
203 | MIT
|
204 |
|
205 | ## Contributors
|
206 |
|
207 | Special thanks to all contributors:
|
208 |
|
209 |
|
210 |
|
211 | <table>
|
212 | <tr>
|
213 | <td align="center"><a href="https://github.com/NileDaley"><img src="https://avatars3.githubusercontent.com/u/15862011?v=4" width="100px;" alt="Nile Daley"/><br /><sub><b>Nile Daley</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=NileDaley" title="Code">💻</a></td>
|
214 | <td align="center"><a href="http://www.jameswmann.com"><img src="https://avatars2.githubusercontent.com/u/436270?v=4" width="100px;" alt="James W Mann"/><br /><sub><b>James W Mann</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=jwmann" title="Code">💻</a></td>
|
215 | <td align="center"><a href="https://www.jannikbuschke.de/blog"><img src="https://avatars2.githubusercontent.com/u/5894881?v=4" width="100px;" alt="Jannik Buschke"/><br /><sub><b>Jannik Buschke</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=jannikbuschke" title="Code">💻</a></td>
|
216 | <td align="center"><a href="https://github.com/LarsJK"><img src="https://avatars2.githubusercontent.com/u/1528255?v=4" width="100px;" alt="Lars-Jørgen Kristiansen"/><br /><sub><b>Lars-Jørgen Kristiansen</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=LarsJK" title="Code">💻</a></td>
|
217 | <td align="center"><a href="https://github.com/voxtex"><img src="https://avatars3.githubusercontent.com/u/735455?v=4" width="100px;" alt="Adam"/><br /><sub><b>Adam</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=voxtex" title="Code">💻</a></td>
|
218 | <td align="center"><a href="https://github.com/jeessy2"><img src="https://avatars2.githubusercontent.com/u/6205259?v=4" width="100px;" alt="jeessy2"/><br /><sub><b>jeessy2</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=jeessy2" title="Code">💻</a></td>
|
219 | <td align="center"><a href="https://twitter.com/pavan_agrawal_1"><img src="https://avatars3.githubusercontent.com/u/17802917?v=4" width="100px;" alt="Pavan Agrawal"/><br /><sub><b>Pavan Agrawal</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=pavanagrawal123" title="Documentation">📖</a></td>
|
220 | </tr>
|
221 | <tr>
|
222 | <td align="center"><a href="https://github.com/Khartir"><img src="https://avatars3.githubusercontent.com/u/5592420?v=4" width="100px;" alt="Khartir"/><br /><sub><b>Khartir</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=Khartir" title="Code">💻</a></td>
|
223 | <td align="center"><a href="https://github.com/yurykozhenov"><img src="https://avatars3.githubusercontent.com/u/18726837?v=4" width="100px;" alt="Yury Kozhenov"/><br /><sub><b>Yury Kozhenov</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=yurykozhenov" title="Code">💻</a></td>
|
224 | <td align="center"><a href="https://ca.linkedin.com/in/jacktonye"><img src="https://avatars2.githubusercontent.com/u/17484350?v=4" width="100px;" alt="Tonye Jack"/><br /><sub><b>Tonye Jack</b></sub></a><br /><a href="https://github.com/jannikbuschke/formik-antd/commits?author=jackton1" title="Code">💻</a></td>
|
225 | </tr>
|
226 | </table>
|
227 |
|
228 |
|
229 |
|
230 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|