All Projects → jannikbuschke → Formik Antd

jannikbuschke / Formik Antd

Licence: mit
Simple declarative bindings for Ant Design and Formik.

Programming Languages

typescript
32286 projects
declarative
70 projects

Projects that are alternatives of or similar to Formik Antd

redux-form-antd
Ant design bindings for redux form
Stars: ✭ 107 (-76.38%)
Mutual labels:  antd, ant-design
Ng Alain
NG-ZORRO admin panel front-end framework (surge mirror https://ng-alain-doc.surge.sh)
Stars: ✭ 4,287 (+846.36%)
Mutual labels:  antd, ant-design
react-drag
A drag and drop platform based on sortable.js front-end visualization. 一个基于sortable.js的前端可视化搭建的拖拽平台,ui组件采用antd-mobile.通过umi脚手架构建.技术栈采用dva+hooks+umi+antd-mobile+sortable.js+react-color.
Stars: ✭ 51 (-88.74%)
Mutual labels:  antd, ant-design
React Admin
基于 Ant Design React 的管理系统架构
Stars: ✭ 376 (-17%)
Mutual labels:  antd, ant-design
React Spa
React Redux,适合中大型规模应用开发,注释还算详细,配置有TypeScript、 CSS Modules、React-Router 4、koa接口mock请求等。接口埋点报错统一处理。
Stars: ✭ 327 (-27.81%)
Mutual labels:  antd, ant-design
antd-pro-toolkit
🐜ant design pro toolkit.
Stars: ✭ 13 (-97.13%)
Mutual labels:  antd, ant-design
GOSH-FHIRworks2020-React-Dashboard
🩺 Fully Responsive FHIR Dashboard written using @reactjs for NHS and GOSH hackathon
Stars: ✭ 21 (-95.36%)
Mutual labels:  antd, ant-design
tiny-qiniu-request
tiny-qiniu for rc-upload or antd upload component `customRequest` property
Stars: ✭ 13 (-97.13%)
Mutual labels:  antd, ant-design
Ant Design Blazor
🌈A set of enterprise-class UI components based on Ant Design and Blazor WebAssembly.
Stars: ✭ 3,890 (+758.72%)
Mutual labels:  antd, ant-design
Bird Front
bird前端项目,基于react、antd、antd-admin,封装常用数据组件,细粒度权限解决方案。
Stars: ✭ 272 (-39.96%)
Mutual labels:  antd, ant-design
antd-etable
Editable Table base on Ant Design
Stars: ✭ 43 (-90.51%)
Mutual labels:  antd, ant-design
Sunflower
🦹 Process components for antd4 & antd3 by alipay industry technology
Stars: ✭ 441 (-2.65%)
Mutual labels:  antd, ant-design
ant-table-extensions
Export, Search extensions to Ant Design's Table component.
Stars: ✭ 43 (-90.51%)
Mutual labels:  antd, ant-design
umi-plugin-antd-theme
🎨 Best theme plugin
Stars: ✭ 77 (-83%)
Mutual labels:  antd, ant-design
next-plugin-antd-less
🎩 Use Antd (Less) with Next.js v12, Zero Dependency on other Next-Plugins.
Stars: ✭ 338 (-25.39%)
Mutual labels:  antd, ant-design
gatsby-wordpress-typescript-scss-blog
Gatsby Wordpress Typescript Blog Boilerplate
Stars: ✭ 50 (-88.96%)
Mutual labels:  antd, ant-design
elm-antd
The official Ant Design UI Kit for Elm
Stars: ✭ 56 (-87.64%)
Mutual labels:  antd, ant-design
picker
📅 All Date Pickers you need.
Stars: ✭ 185 (-59.16%)
Mutual labels:  antd, ant-design
erp-crm
IDURAR is Open Source ERP/CRM Based on Mern Stack (Node.js / Express.js / MongoDb / React.js ) with Ant Design (AntD) and Redux
Stars: ✭ 18 (-96.03%)
Mutual labels:  antd, ant-design
Jeecg Boot
「企业级低代码平台」前后端分离架构SpringBoot 2.x,SpringCloud,Ant Design&Vue,Mybatis-plus,Shiro,JWT。强大的代码生成器让前后端代码一键生成,无需写任何代码! 引领新的开发模式OnlineCoding->代码生成->手工MERGE,帮助Java项目解决70%重复工作,让开发更关注业务,既能快速提高效率,帮助公司节省成本,同时又不失灵活性。
Stars: ✭ 26,432 (+5734.88%)
Mutual labels:  antd, ant-design

Build Status GitHub stars npm All Contributors license

Ant Design 4 ready

Ant Design v4 has landed and so has formik-antd v2. Version 1 of this library, which is now in maintenance mode, supports Ant Design v3. Version 2 of this library supports Ant Design 4. Both v1 and v2 of this library work with Formik v1 and v2.

formik-antd

Simple declarative bindings for Ant Design and Formik.

Core Concept

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.

Example:

import React from 'react'
import { Form, Input, InputNumber, Checkbox } from 'formik-antd'
import { Formik } from 'formik'

function App() {
  return (
    <Formik
      {/* default/initial values are set on the Formik container: */ }
      initialValues={{ firstName: '', age: 20, newsletter: false }}
      render={() => (
        <Form>
          {/* every formik-antd component must have the 'name' prop set: */}
          <Input name='firstName' placeholder='First Name' />
          {/* the rest of the api stays as is */}
          <InputNumber name='age' min={0} />
          <Checkbox name='newsletter'>Newsletter</Checkbox>
        </Form>
      )}
    />
  )
}

Getting started

npm install formik-antd

Add import "antd/dist/antd.css" to your index.js file or check the Advanced setup section

Core Components

Name Props
AutoComplete { name, validate?, fast? } & AutoCompleteProps
Cascader { name, validate?, fast? } & CascaderProps
Checkbox { name, validate?, fast? } & CheckboxProps
Checkbox.Group { name, validate?, fast? } & CheckboxGroupProps
DatePicker { name, validate?, fast? } & DatePickerProps
DatePicker.WeekPicker { name, validate?, fast? } & WeekPickerProps
DatePicker.RangePicker { name, validate?, fast? } & RangePickerProps
DatePicker.MonthPicker { name, validate?, fast? } & MonthPickerProps
Input { name, validate?, fast? } & InputProps
InputNumber { name, validate?, fast? } & InputNumberProps
Input.Password { name, validate?, fast? } & InputPasswordProps
Input.TextArea { name, validate?, fast? } & Input.TextAreaProps
Mentions { name, validate?, fast? } & MentionsProps
Radio.Group { name, validate?, fast? } & RadioGroupProps
Rate { name, validate?, fast? } & RateProps
Select { name, validate?, fast? } & SelectProps
Slider { name, validate?, fast? } & SliderProps
Switch { name, validate?, fast? } & SwitchProps
Table { name, fast? } & TableProps
TimePicker { name, validate?, fast? } & TimePickerProps
Transfer { name, validate?, fast? } & TransferProps
TreeSelect { name, validate?, fast? } & TreeSelectProps

Submitting and Resetting Forms

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:

import React from 'react'
import { Form, SubmitButton, ResetButton /* ... */ } from 'formik-antd'
import { Formik } from 'formik'

function App() {
  return <Formik initialValues={/* ... */} onSubmit={/* ... */}>
    <Form>
      {/* ... */}
      <SubmitButton />
      <ResetButton />
    </Form>
  </Formik>
}

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.

Form- and Field-level Validation

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:

<Form.Item name='firstName' validate={validator}>
  <Input name='firstName' />
</Form.Item>

Rendering Validation Feedback

Showing validation messages can be accomplished with the Form.Item component (or FormItem which is the same). It

  • 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).
  • 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.
  • exposes some layout features and a label (visit https://ant.design/components/form/ for the details).
<Form.Item name='firstName'>
  <Input name='firstName' />
</Form.Item>

FastField support

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:

<Input name='firstName' fast={true} />

Table

The table components comes with additional helper buttons to add and delete rows. An example can be seen in the codesandbox.

Lists and Nested objects

Nested objects and arrays can be accessed with lodash-like bracket syntax as described in the Formik documentation.

<Input name='friends[0].firstName' />

Advanced setup

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

import Input from 'formik-antd/es/input'
import '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

const path = require('path')
const { override, fixBabelImports } = require('customize-cra')

module.exports = override(
  fixBabelImports('antd', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: 'css',
  }),
  fixBabelImports('formik-antd', {
    libraryName: 'formik-antd',
    libraryDirectory: 'es',
    style: 'css',
  }),
)

package.json

  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test"
  }

Playground & Contributions

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).

TypeScript

Types are included.

Typechecking limitations

Form values currently cannot be typechecked (at least to my knowledge). For example the following ideally would give a compile error:

<Formik<{name:string}> initialValues={{name:""}}>
  <Input name="naem" />
</Formik>

Typescript cannot (yet) enforce types of children. In the future this hopefully will be possible.

Related projects

  • cra-antd-x a template project with react-app-rewired (babel imports, fash refresh, themeable), typescript and react-router.

License

MIT

Contributors

Special thanks to all contributors:


Nile Daley

💻

James W Mann

💻

Jannik Buschke

💻

Lars-Jørgen Kristiansen

💻

Adam

💻

jeessy2

💻

Pavan Agrawal

📖

Khartir

💻

Yury Kozhenov

💻

Tonye Jack

💻

Edon Gashi

🚇

Roman Tsegelskyi

💻

Daniel Schulz

💻

Harry Green

📖

Daniel Huth

💻

Gabriel Berlanda

💻 📖

Alexandre Giard

🤔

kissthom

🐛

Chris Higgins

💻

Felipe Lima

💻

Frédéric Barthelet

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].