Gathering detailed insights and metrics for formik-antd
Gathering detailed insights and metrics for formik-antd
Gathering detailed insights and metrics for formik-antd
Gathering detailed insights and metrics for formik-antd
npm install formik-antd
58.6
Supply Chain
82.7
Quality
83.1
Maintenance
100
Vulnerability
99.3
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
591 Stars
504 Commits
80 Forks
8 Watching
39 Branches
25 Contributors
Updated on 14 Nov 2024
Minified
Minified + Gzipped
TypeScript (98.74%)
JavaScript (1.26%)
Cumulative downloads
Total Downloads
Last day
-14.3%
1,496
Compared to previous day
Last week
-5.8%
8,068
Compared to previous week
Last month
-11.7%
35,670
Compared to previous month
Last year
-17%
421,897
Compared to previous year
Install for ant design 5 (beta)
npm i formik-antd@beta
Install for ant design 4
npm i formik-antd
Simple declarative bindings for Ant Design and Formik.
This library enriches several Ant Design components with a required name: string
prop that connects them to a Formik form field. It is quite simple. Instead of importing your form components from antd, you need to import them from 'formik-antd' and set their name
prop. Now the component is connected/synced with the corresponding Formik
field!
The Ant Design components are feature rich and provide a lot of props to customize their visual presentation. These features and also their apis stay exactly the same. Visit their documentation to learn more.
1import React from 'react' 2import { Form, Input, InputNumber, Checkbox } from 'formik-antd' 3import { Formik } from 'formik' 4 5function App() { 6 return ( 7 <Formik 8 {/* default/initial values are set on the Formik container: */ } 9 initialValues={{ firstName: '', age: 20, newsletter: false }} 10 render={() => ( 11 <Form> 12 {/* every formik-antd component must have the 'name' prop set: */} 13 <Input name='firstName' placeholder='First Name' /> 14 {/* the rest of the api stays as is */} 15 <InputNumber name='age' min={0} /> 16 <Checkbox name='newsletter'>Newsletter</Checkbox> 17 </Form> 18 )} 19 /> 20 ) 21}
npm install formik-antd
Add import "antd/dist/antd.css"
to your index.js
file or check the Advanced setup section
Name | Props | |
---|---|---|
:white_check_mark: | AutoComplete | { name, validate?, fast? } & AutoCompleteProps |
:white_check_mark: | Cascader | { name, validate?, fast? } & CascaderProps |
:white_check_mark: | Checkbox | { name, validate?, fast? } & CheckboxProps |
:white_check_mark: | Checkbox.Group | { name, validate?, fast? } & CheckboxGroupProps |
:white_check_mark: | DatePicker | { name, validate?, fast? } & DatePickerProps |
:white_check_mark: | DatePicker.WeekPicker | { name, validate?, fast? } & WeekPickerProps |
:white_check_mark: | DatePicker.RangePicker | { name, validate?, fast? } & RangePickerProps |
:white_check_mark: | DatePicker.MonthPicker | { name, validate?, fast? } & MonthPickerProps |
:white_check_mark: | Input | { name, validate?, fast? } & InputProps |
:white_check_mark: | InputNumber | { name, validate?, fast? } & InputNumberProps |
:white_check_mark: | Input.Password | { name, validate?, fast? } & InputPasswordProps |
:white_check_mark: | Input.TextArea | { name, validate?, fast? } & Input.TextAreaProps |
:white_check_mark: | Mentions | { name, validate?, fast? } & MentionsProps |
:white_check_mark: | Radio.Group | { name, validate?, fast? } & RadioGroupProps |
:white_check_mark: | Rate | { name, validate?, fast? } & RateProps |
:white_check_mark: | Select | { name, validate?, fast? } & SelectProps |
:white_check_mark: | Slider | { name, validate?, fast? } & SliderProps |
:white_check_mark: | Switch | { name, validate?, fast? } & SwitchProps |
:white_check_mark: | Table | { name, fast? } & TableProps |
:white_check_mark: | TimePicker | { name, validate?, fast? } & TimePickerProps |
:white_check_mark: | Transfer | { name, validate?, fast? } & TransferProps |
:white_check_mark: | TreeSelect | { name, validate?, fast? } & TreeSelectProps |
Directly under each <Formik>
container a <Form>
component should be placed. This component composes the functionality provided by Ant Designs (https://ant.design/components/form/) as well as Formiks (https://jaredpalmer.com/formik/docs/api/form) <Form>
components:
1import React from 'react' 2import { Form, SubmitButton, ResetButton /* ... */ } from 'formik-antd' 3import { Formik } from 'formik' 4 5function App() { 6 return ( 7 <Formik initialValues={/* ... */} onSubmit={/* ... */}> 8 <Form> 9 {/* ... */} 10 <SubmitButton /> 11 <ResetButton /> 12 </Form> 13 </Formik> 14 ) 15}
The SubmitButton
and ResetButton
will disable automatically depending on form state. The ResetButton
is enabled if the form is dirty. The SubmitButton
is enabled if the form is valid or if it is not dirty and the submit count is zero.
If you do want to control the disable behavior yourself you can provide the disable: boolean
prop.
I.e. <SubmitButton disabled={false} />
will make the button always be enabled.
Formik provides form- and field-level validation callbacks to provide validation logic. How to validate is neither part of formik nor of this library.
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.
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:
1<Form.Item name='firstName' validate={validator}> 2 <Input name='firstName' /> 3</Form.Item>
Showing validation messages can be accomplished with the Form.Item
component (or FormItem
which is the same). It
showValidateSuccess: boolean
prop is set to true, the field has been touched and the corresponding field does not have a validation error.1<Form.Item name='firstName'> 2 <Input name='firstName' /> 3</Form.Item>
Formik allows performance optimizations through the <FastField/>
component. Please read the formik docs on when to use such an optimization (usually you don't and maybe should not optimize, unless you encounter performance issues in production).
To opt-in to FastField support, all formik-antd
components provide an optional fast?: boolean
prop. Setting this to true
enables the optimization:
1<Input name='firstName' fast={true} />
The table components comes with additional helper buttons to add and delete rows. An example can be seen in the codesandbox.
Nested objects and arrays can be accessed with lodash-like bracket syntax as described in the Formik documentation.
1<Input name='friends[0].firstName' />
You can checkout this github template project get the following setup (and more).
If you do not want to import the full ant design library and its stylesheet (in order to reduce the bundle size) you can import specific components and their stylesheet by their path, as it is described in the antd documentation https://ant.design/docs/react/getting-started#Import-on-Demand
1import Input from 'formik-antd/es/input' 2import 'formik-antd/es/input/style'
Some build tools like webpack are now able to "tree shake", meaning unused code from ant design will not be bundled.
As writing imports like this is a little cumbersome there is a babel import helper: https://github.com/ant-design/babel-plugin-import. In create-react-app
projects babel plugins do not work out of the box. With third party projects like customize-cra
and react-app-rewired
we are able to change the webpack config. Be warned though, the team behind create-react-app
does not support this scenario, so if you run into problems you are on your own.
npm install babel-plugin-import customize-cra react-app-rewired --save-dev
config-overrides.js
1const path = require('path') 2const { override, fixBabelImports } = require('customize-cra') 3 4module.exports = override( 5 fixBabelImports('antd', { 6 libraryName: 'antd', 7 libraryDirectory: 'es', 8 style: 'css', 9 }), 10 fixBabelImports('formik-antd', { 11 libraryName: 'formik-antd', 12 libraryDirectory: 'es', 13 style: 'css', 14 }), 15)
package.json
1 "scripts": { 2 "start": "react-app-rewired start", 3 "build": "react-app-rewired build", 4 "test": "react-app-rewired test" 5 }
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).
Types are included.
Form values currently cannot be typechecked (at least to my knowledge). For example the following ideally would give a compile error:
1<Formik<{name:string}> initialValues={{name:""}}> 2 <Input name="naem" /> 3</Formik>
Typescript cannot (yet) enforce types of children. In the future this hopefully will be possible.
MIT
Special thanks to all contributors:
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
20 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More