react-easy-formcore v6.0.11
react-easy-formcore
English | 中文说明
Introduction?
Lightweight form container two-way binding component that automatically handles the value (or other) and onChange of the control to complete the display and update of the form values. Other operations can be implemented through the methods provided by the injected FormStore instance.
Version changelog
- Version 5.x
Major Updates
- Adjusted rendering, nesting between
Form.Itemis no longer allowed, detailed usage documentation has been updated. - The
Form.Itemoption inForm.Listsets thenamefield as an attribute field in the array. - 5.0.1
changed tostoreform
- Adjusted rendering, nesting between
- 4.x
- 4.0.12 optimize the routing system in forms,
changed tojoinPathjoinFormPath. - 4.0.11 Add
tagNameproperty to theFormcomponent, which can replace the defaultformtag - optimize the routing system in forms, fix the
useFormValueserror.
- 4.0.12 optimize the routing system in forms,
- 3.x
Important architecture update, you need to delete the old package and install the new version again
- add
componentproperty to dynamically replace the display components of theForm.ItemandForm.Listcomponents, and the display component's own props can be set either globally onFormor locally onForm.Itemor `Form. onFieldsChangeandonValuesChangechanged- 3.0.3
changed todata-type="fragment"data-type="ignore" - 3.1.2 Add
triggerproperty toForm.Itemto set the event that triggers the collection of values, andvalidateTriggerto set the event that triggers the form validation. Alsorulesproperty can setvalidateTriggerevent for each rule. - 3.1.3 Add
tooltipproperty to add tip.
- add
- 2.x
.colandcustomInnerdeprecated
- 1.3.x:
Addedinlineinline layout attribute, used withcolattributeAddcustomInnerproperty, you can customize the display container- Added
labelAlignandlabelWidthproperties - Added
valueSetterproperty, used in conjunction withvalueGetterproperty to format input and output items.
- 1.2.x:
- 1.2.9 add
footerprops - add
data-namesetting to mark controls that match thevalue,onChangerequirement Addcollayout attribute to allow for raster layout
- 1.2.9 add
- Version 1.0.x:
labelWidthandlayouthave been changed to labelStyle, allowing you to customize your own label label-related styles- inline changed to layout, with
'horizontal' | 'vertical'. - Changes to the form char rule in forms: where the path contained an array of items, for example
a.b.0, this has now been changed toa.b[0].
- 0.3.8 Initial release
Matters
- The css style file needs to be introduced before it can be used, for example:
import 'react-easy-formcore/lib/css/main.css';
Form.Item
The smallest unit of a component in a form, and nodes as an object can be nested within each other.
- Provides styles, as well as
value(or set viavalueProp) andonChangetwo-way bindings. - You can customize
onChangein outside, but you can only set the form value via an instance method such asform.setFieldValue. - Custom form validation rules can be provided with the form validation rules property
rules. - When a non-form component or node is added outside the input form control, bind the target control by adding
data-type="ignore"to filter the non-target node or by settingdata-nameto mark the target input form.
Form.List
The Form.Item component acts as an item in the Form.List array type and is combined to form an array
- Only
Form.Itemitems are recognised inForm.List, Thenamefield ofForm.Item, if set, is the field property in the array, if not, it defaults to the array serial number. - The
rulesprovided byForm.Listare valid for all input items in the array, but have a lower priority than therulesof theForm.Itemin the array
install
npm install react-easy-formcore --save
# or
yarn add react-easy-formcorebase
import React from "react";
import { Form, useFormStore, useFormValues } from "react-easy-formcore";
import 'react-easy-formcore/lib/css/main.css';
import { Input, Select } from "antd";
export default function Demo() {
const form = useFormStore();
const onSubmit = async (e) => {
e.preventDefault();
const { error, values } = await form.validate();
console.log(error, values, 'error ang values');
};
const validator = (value) => {
if (value?.length < 2) {
return Promise.reject(new Error('length is < 2'));
}
}
const formvalues = useFormValues(form, ['name1', 'name2'])
console.log(formvalues, 'formvalues')
return (
<Form initialValues={{ name1: 1111 }} form={form} onSubmit={onSubmit}>
<Form.Item label="Name1" name="name1" rules={[{ required: true, message: 'name1 is Empty' }, { validator: validator, message: 'validator error' }]}>
<div data-type="ignore">
<input />
</div>
</Form.Item>
<Form.Item label="object" name="name2.a" rules={[{ required: true, message: 'name2.a is empty' }]}>
<input />
</Form.Item>
<Form.Item label="list" name="name3[0]" rules={[{ required: true, message: 'name3[0] is empty' }]}>
<input />
</Form.Item>
<Form.Item label="">
<button>Submit</button>
</Form.Item>
</Form>
);
};Form.List
import React from "react";
import { Form, FormStore } from "react-easy-formcore";
import 'react-easy-formcore/lib/css/main.css';
import { Input, Select } from "antd";
export default function Demo() {
const form = useFormStore();
const onSubmit = async (e) => {
e.preventDefault();
const { error, values } = await form.validate();
console.log(error, values, 'error ang values');
};
const validator = (value) => {
if (value?.length < 2) {
return Promise.reject(new Error('length is < 2'));
}
}
return (
<Form form={form} onSubmit={onSubmit}>
<Form.List name="list">
<Form.Item
rules={[
{ required: true, message: "list's one is Empty" },
{ validator: validator, message: "custome tips" },
]}
>
<Input />
</Form.Item>
<Form.Item
rules={[{ required: true, message: "list's two is Empty" }]}
>
<Input />
</Form.Item>
</Form.List>
<Form.Item label="">
<button>Submit</button>
</Form.Item>
</Form>
);
};APIs
Default field display component
classNamestringclass name,optional.labelstringlabel,optional.labelStyleCSSPropertiescustom label's style,optional.labelWidthCSSProperties['width'], the width of the label label.labelAlignCSSProperties['textAlign'], the label label's textAlign property.inlineboolea, Whether or not field display components have inline layout.layout'horizontal'|'vertical'field’s display components set the layout type, the default value ishorizontal.colonbooleanwhether add colonrequiredbooleanIndicates that the value of the field is requiredoptional。gutternumberThe distance between field's display component custom labels and form components,optional.errorstringform field displays the component's error message field.suffixReact.ReactNodeSuffix node,optional.footerReact.ReactNodebootom node,optional.tooltipstringTip icon to prompt for information.optional.compactbooleanWhether or not compact, will remove the component'smargin-bottom.optional.
Form Props
Inherited field display component
classNameThe class name of the form element,optional.formThe form data store,required.tagNameReplace the element tag name of the form, defaultformtaginitialValuesThe initial value of the form, which is overridden by theinitialValueof the form field, Note that this value can only initialise the formoptional.onSubmitformtag triggers the reset default event, onlybuttontags that providehtmlType=submitcan triggeroptional.onResetformtag triggers the reset default event, onlybuttontags that providehtmlType=resetcan triggeroptional.onFieldsChangeThe event function when a form changes onChange will only be triggered by the control's activeonChange, not byform.setFieldValueandform.setFieldsValue, avoiding circular calls。optional.onValuesChangeListening for changes in form values.optional.
Form.Item Props
Inherited field display component
classNameForm field class name,optional.componentfield display component.nameForm field name,optional.triggerSets the event name of the form field to collect form values, defaultonChange.validateTriggerSets the event for trigger form field validation, defaultonChange.valuePropThe field name of the value in the callback function object, the default value is'value'.valueGetterA function to format the output form value, used withvalueSetter,optional.valueSetterfunction to format input form value, used withvalueGetter,optional.rulesChecksum rules for form fieldsoptional.initialValueThe initial value of the form field, note that this value is different fromvaluewhen the form is rendered for the first time.onFieldsChangeThe event function when the value of the control changes will only be triggered by the control's activeonChange, not byform.setFieldValueandform.setFieldsValue, avoiding circular calls.optional.onValuesChangeListening for changes in form values.optional。errorClassNameadd a custom class name when there is an error message,optional.
Form.List Props
Inherited field display component
classNameForm field class name,optional.componentfield display component.nameForm field name,optional.initialValueForm field initial value, Note that this value can only initialise the formoptional.rulesChecksum rules for form fieldsoptional.
rules
The rules in the fields of the values in rules perform the checks in order, and only one rule can be set for each item in rules.
validateTriggerstringEvent to trigger validate form rules, defaultonChange.messagestringDefault error message when a check rule reports an erroroptional。requiredbooleanThe required symbol is marked, and arequiredattribute oftrueinrulesalso automatically adds the required symboloptional。validator(value) => void | booleanCustom check function,valueis the current control valueoptional.patternRegExp | stringExpression check, error if does not matchoptional.whitespacebooleanspace checkoptional.maxnumberMaximum length for string type; maximum length for number type; maximum length for array typeoptional.minnumberminimum length forstringtype; minimum value fornumbertype; minimum length forarraytype.optional.
FormStore Methods
new FormStore(defaultValues)form manager.form.getFieldValue(path?: string)Returns the value of the form field for whichpathis specified, or the value of the whole form withoutname.form.setFieldValue(path, value)Update the value of a form fieldform.setFieldsValue(obj: Partial<T>)Set the value of the form field (override).form.reset(values?: Partial<T>)Reset the form.The value can be passed to reset to the target value.form.validate(path?: string)Checks form and returns error messages and form values.form.getFieldError(path?: string)Returns the target's error message or all error messages.
Hooks
useFormStore(defaultValues)createFormStoreuseFormError(form: FormStore, path?: string)Use hooks to get the specified form error.- 3.0.12
useFormValues(form: FormStore, path?: string | string[])Use hooks to get the specified form values. useValidator()createvalidator
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago