- Notifications
You must be signed in to change notification settings - Fork16
🪄 Ant Design Form Simplified
License
nanxiaobei/ant-plus
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Link in bio towidgets,your onlinehome screen. ➫🔗 kee.so
antx provides a set ofantd mixed field components:
1. Say goodbye to cumbersome<Form.Item> andrules
Directly write on field components (e.g.Input) withForm.Item props and field props (fully TypeScript support), which greatly simplifies the code.
2. Stringrules (only enhanced, originalrules are also supported)rules in string, for examplerules={['required', 'max=10']} represents forrules={[{ required: true }, { max: 10 }]}.
3. Not adding any new props
All props areantd original props, without add any other new props, reducing mental burden.
In the same time,antx provides 2 helper components (WrapperCol,Watch), and a tool functioncreate() for easily enhancing existing field components.
Ant Plus and Ant Design form code comparison:
pnpm add antx# oryarn add antx# ornpm i antx
import{Button,Form}from'antd';import{Input,Select,WrapperCol}from'antx';constApp=()=>{return(<FormlabelCol={{span:8}}wrapperCol={{span:16}}><Inputlabel="Name"name="name"rules={['required','string']}/><Selectlabel="Gender"name="gender"rules={['required','number']}options={[{value:1,label:'Male'},{value:2,label:'Female'},]}/><InputNumberlabel="Age"name="age"rules={['required','number','min=0']}/><WrapperCol><Buttontype="primary"htmlType="submit"> Submit</Button></WrapperCol></Form>);};exportdefaultApp;
AutoCompleteCascaderCheckboxDatePickerInputInputNumberMentionsRadioRateSelectSliderSwitchTimePickerTransferTreeSelectUploadCheckboxGroup--- (Checkbox.Group)DateRange--- (DatePicker.RangePicker)TextArea--- (Input.TextArea)Search--- (Input.Search)Password--- (Input.Password)RadioGroup--- (Radio.Group)TimeRange--- (TimePicker.RangePicker)Dragger--- (Upload.Dragger)
For all the mixed components above,classNamestylenametooltip props will be passed toForm.Item.
To pass to the inner field component (likeInput), please useselfClassselfStyleselfNameselfTooltip.
- Watch: used to monitor the changes of form values, which can be only partial re-render, more refined and better performance
| Props | Description | Type | Default |
|---|---|---|---|
name | Field to monitor | NamePath | - |
list | List of fields to monitor (mutually exclusive withname) | NamePath[] | - |
children | Render props. Get the monitored value (or list), return UI | (next: any, prev: any, form: FormInstance) => ReactNode | - |
onlyValid | Only triggerchildren rendering when the monitored value is notundefined | boolean | false |
onChange | Get the monitored value (or list), handle side effects (mutually exclusive withchildren) | (next: any, prev: any, form: FormInstance) => void | - |
// Watch exampleimport{Watch}from'antx';<Form><Inputlabel="Song"name="song"/><Inputlabel="Artist"name="artist"/><Watchname="song">{(song)=>{return<div>Song:{song}</div>;}}</Watch><Watchlist={['song','artist']}>{([song,artist])=>{return(<div> Song:{song}, Artist:{artist}</div>);}}</Watch></Form>;
- WrapperCol: simplify the layout code, the same props as
Form.Item, used when the UI needs to be aligned with the input box.
// WrapperCol exampleimport{WrapperCol}from'antx';<Form><Inputlabel="Song"name="song"/><WrapperCol>This is a hint that aligns with the input box</WrapperCol></Form>;
- create(): convert existing custom field components into components that support
Form.Itemprops mix-in.
import{create}from'antx';// Before<Form><Form.Itemlabel="Song"name="song"rules={{required:true}}><MyCustomInput/></Form.Item></Form>;// enhancing with create()constMyCustomInputPlus=create(MyCustomInput);// After<Form><MyCustomInputPluslabel="Song"name="song"rules={['required']}/></Form>;
| String | Equals to | Description |
|---|---|---|
'required' | { required: true } | |
'required=xx' | { required: true, message: 'xx' } | |
'string' | { type: 'string', whitespace: true } | |
'pureString' | { type: 'string' } | |
'number' | { type: 'number' } | |
'array' | { type: 'array' } | |
'boolean' | { type: 'boolean' } | |
'url' | { type: 'url' } | |
'email' | { type: 'email' } | |
'len=20' | { len: 20 } | len === 20 |
'max=100' | { max: 100 } | max <= 100 |
'min=10' | { min: 10 } | min >= 10 |
// String rules example<Inputlabel="Song"name="song"rules={['required','min=0','max=50']}/>
About
🪄 Ant Design Form Simplified
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.
