2.0.2 • Published 2 years ago

form-bunch v2.0.2

Weekly downloads
5
License
MIT
Repository
github
Last release
2 years ago

form-bunch

NPM Status

Form-bunch is a component like plugin that make it easier to write form. You could add the most of components what you want to form-bunch for build various form. You could also easily change the settings to get the verification or layout you want. Hope you enjoy it, and if you like it, star it pllllllllllz. :)

Advantages:

  • Customizable. You can add whatever you want if it's possible, like components of Material-UI, Ant Design or customized components.
  • Small. it will give you performance and convenience for building form.
  • Easy. You can change the settings easily - see below

DEMO

Installation

  • YARN
yarn add form-bunch
  • NPM
npm install form-bunch

Usage

First of all, initialize form-bunch. Assume that you would like to use components of antd, the components should match two rules: 1. it has two fields —— value, onChange 2. the param(e) of onChange(e) => void must be corresponding to value. if one component has no value or onChange key, or it doesn't match form-bunch, you should replace them like following.

// index.tsx 
import { DatePicker, Input, Switch } from 'antd';

const extensions = {
  datePick: DatePicker,
  input: [
    Input,
    {
      // change the `e.target.value` to `value`
      // assume that original filed `onChange` is `onSelect`, 
      // then it should be `onChange: ['onSelect', 'e.target.value']`
      onChange: ['onChange', 'e.target.value'],
    },
  ],
  switch: [
    Switch,
    {
      // change the field `checked` to `value`
      value: 'checked'
    }
  ]
};

export type TExtensions = typeof extensions;

const MyFormBunch = formBunchInit<TExtensions>(extensions);

export default MyFormBunch;

And then, set style after verification failed in form-bunch-error-box className, then import it.

/* index.css */
.form-bunch-error-box .ant-input {
    border: 1px solid red;
    box-shadow: none
}

.form-bunch-error-box .ant-input:hover {
    border: 1px solid red;
}

.form-bunch-error-box .ant-input:active {
    border: 1px solid red;
}
// index.tsx
import './index.css'

...

export default MyFormBunch;

Finally, config form-bunch, and there are many apis for usage.

<MyFormBunch
  ref={formBunchRef}
  value={value}
  items={[
    {
      key: 'a',
      type: 'input',
    },
    {
      labelCol: '80px',
      offset: '40px',
      key: 'b',
      defaultValue: 'test',
      type: 'input',
      verify: (value) => value.length > 5,
    },
  ]}
  onChange={(value) => {
    setValue(value);
  }}
/>

Click DEMO for more details about usage.

API

Form

PropertyDescriptionTypeDefault
itemsconfig of each form item, see Items API for more detailsIFormItem-
onChangethe callback function when form data changes(form: IFormValue, item: any, key: string) => void-
settingglobal setting of form, Priority: items > setting, see Setting API for more detailsIFormSetting-
valueform dataIFormValue-

Items

PropertyDescriptionTypeDefault
classNameclass of the formItemstring-
collike flex-basic, the space that formItem takes upnumber | string'100%'
controlCollike flex-basic, the space that control of formItem takes upnumber | string'80%'
defaultValuedefault value, you can also change initial value of form API to set itany-
errorthe message after verification failsstring-
keyproperty of formItemstring-
labellabel of formItemstring-
labelAligntype of label alignment'left' | 'right' | 'center''right'
labelCollike flex-basic, the space that label of formItem takes upnumber | string20%
offsetlike margin-left, the space offset from the leftnumber | string0
renderrender type of formItem control, require that only one is provided between property type and render(value: any, setValue: (state: any) => void) => JSX.Element-
requiredset formItem value to be requiredbooleanfalse
typerender type of formItem control, require that only one is provided between property type and renderstring-
typePropswhen use type, then typeProps is its original propsobject-
verifyfunction that to verify the formItem value, it supports regex.when it return string, the string will replace corresponding error tipRegExp | ((value?: any, form?: IFormValue) => boolean | string)-

Setting

PropertyDescriptionTypeDefault
collike flex-basic, the space that formItem takes upnumber | string'100%
controlCollike flex-basic, the space that control of formItem takes upnumber | string'80%'
hasTipsdetermine if there is space left for error tipsbooleanfalse
labelAligntype of label alignment"left" | "right" | "center"'right'
labelCollike flex-basic, the space that label of formItem takes upnumber | string'20%'
offsetlike margin-left, the space offset from the leftnumber | string0

Ref

PropertyDescriptionTypeDefault
validatevalidate all values of form and return result() => boolean-
resetreset all value of form and result of verify() => void-

Bug tracker

If you find a bug, please report it here on Github!

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.1

3 years ago