@simpleform/form 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +208 -3
- package/README_CN.md +216 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,10 +1,215 @@
|
|
|
1
1
|
# `@simpleform/form`
|
|
2
|
+
English | [中文说明](./README_CN.md)
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
[](https://www.npmjs.com/package/@simpleform/form)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
> The underlying form component automatically injects `value` and `onChange` into the target control to display and update the form values.
|
|
6
7
|
|
|
8
|
+
# Matters
|
|
9
|
+
- The css style file needs to be introduced before it can be used, for example: `import '@simpleform/form/lib/css/main.css'`;
|
|
10
|
+
|
|
11
|
+
# Form.Item
|
|
12
|
+
|
|
13
|
+
The smallest unit of a component in a form, and nodes as an object can be nested within each other.
|
|
14
|
+
|
|
15
|
+
- Provides styles, as well as `value` (or set via `valueProp`) and `onChange` two-way bindings.
|
|
16
|
+
- You can customize `onChange` in outside, but you can only set the form value via an instance method such as `form.setFieldValue`.
|
|
17
|
+
- Custom form validation rules can be provided with the form validation rules property `rules`.
|
|
18
|
+
- 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 setting `data-name` to mark the target input form.
|
|
19
|
+
|
|
20
|
+
# Form.List
|
|
21
|
+
|
|
22
|
+
The `Form.Item` component acts as an item in the `Form.List` array type and is combined to form an array
|
|
23
|
+
|
|
24
|
+
- Only `Form.Item` items are recognised in `Form.List`, The `name` field of `Form.Item`, if set, is the field property in the array, if not, it defaults to the array serial number.
|
|
25
|
+
- The `rules` provided by `Form.List` are valid for all input items in the array, but have a lower priority than the `rules` of the `Form.Item` in the array
|
|
26
|
+
|
|
27
|
+
## install
|
|
28
|
+
- [Node.js](https://nodejs.org/en/) Version >= 18.18.2
|
|
29
|
+
- [react](https://nodejs.org/en/) Version >= 16.8.0
|
|
30
|
+
```bash
|
|
31
|
+
npm install @simpleform/form --save
|
|
32
|
+
# or
|
|
33
|
+
yarn add @simpleform/form
|
|
7
34
|
```
|
|
8
35
|
|
|
9
|
-
|
|
36
|
+
## base
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import React from "react";
|
|
40
|
+
import { Form, useSimpleForm, useFormValues } from "@simpleform/form";
|
|
41
|
+
import '@simpleform/form/lib/css/main.css';
|
|
42
|
+
import { Input, Select } from "antd";
|
|
43
|
+
|
|
44
|
+
export default function Demo() {
|
|
45
|
+
|
|
46
|
+
const form = useSimpleForm();
|
|
47
|
+
const formvalues = useFormValues(form, ['name1', 'name2'])
|
|
48
|
+
console.log(formvalues, 'formvalues')
|
|
49
|
+
|
|
50
|
+
const onSubmit = async (e) => {
|
|
51
|
+
e.preventDefault();
|
|
52
|
+
const { error, values } = await form.validate();
|
|
53
|
+
console.log(error, values, 'error ang values');
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const validator = (value) => {
|
|
57
|
+
if (value?.length < 2) {
|
|
58
|
+
return Promise.reject(new Error('length is < 2'));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<Form initialValues={{ name1: 1111 }} form={form} onSubmit={onSubmit}>
|
|
64
|
+
<Form.Item label="Name1" name="name1" rules={[{ required: true, message: 'name1 is Empty' }, { validator: validator, message: 'validator error' }]}>
|
|
65
|
+
<div data-type="ignore">
|
|
66
|
+
<input />
|
|
67
|
+
</div>
|
|
68
|
+
</Form.Item>
|
|
69
|
+
<Form.Item label="object" name="name2.a" rules={[{ required: true, message: 'name2.a is empty' }]}>
|
|
70
|
+
<input />
|
|
71
|
+
</Form.Item>
|
|
72
|
+
<Form.Item label="list" name="name3[0]" rules={[{ required: true, message: 'name3[0] is empty' }]}>
|
|
73
|
+
<input />
|
|
74
|
+
</Form.Item>
|
|
75
|
+
<Form.Item label="">
|
|
76
|
+
<button>Submit</button>
|
|
77
|
+
</Form.Item>
|
|
78
|
+
</Form>
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
## Form.List
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
import React from "react";
|
|
86
|
+
import { Form, useSimpleForm } from "@simpleform/form";
|
|
87
|
+
import '@simpleform/form/lib/css/main.css';
|
|
88
|
+
import { Input, Select } from "antd";
|
|
89
|
+
|
|
90
|
+
export default function Demo() {
|
|
91
|
+
|
|
92
|
+
const form = useSimpleForm();
|
|
93
|
+
|
|
94
|
+
const onSubmit = async (e) => {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
const { error, values } = await form.validate();
|
|
97
|
+
console.log(error, values, 'error ang values');
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const validator = async (value) => {
|
|
101
|
+
if (value?.length < 2) {
|
|
102
|
+
return Promise.reject(new Error('length is < 2'));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<Form form={form} onSubmit={onSubmit}>
|
|
108
|
+
<Form.List name="list">
|
|
109
|
+
<Form.Item
|
|
110
|
+
rules={[
|
|
111
|
+
{ required: true, message: "list's one is Empty" },
|
|
112
|
+
{ validator: validator, message: "custome tips" },
|
|
113
|
+
]}
|
|
114
|
+
>
|
|
115
|
+
<Input />
|
|
116
|
+
</Form.Item>
|
|
117
|
+
<Form.Item
|
|
118
|
+
rules={[{ required: true, message: "list's two is Empty" }]}
|
|
119
|
+
>
|
|
120
|
+
<Input />
|
|
121
|
+
</Form.Item>
|
|
122
|
+
</Form.List>
|
|
123
|
+
<Form.Item label="">
|
|
124
|
+
<button>Submit</button>
|
|
125
|
+
</Form.Item>
|
|
126
|
+
</Form>
|
|
127
|
+
);
|
|
128
|
+
};
|
|
129
|
+
|
|
10
130
|
```
|
|
131
|
+
|
|
132
|
+
## APIs
|
|
133
|
+
|
|
134
|
+
### Default field display component
|
|
135
|
+
|
|
136
|
+
- `className` `string` class name, `optional`.
|
|
137
|
+
- `label` `string` label, `optional`.
|
|
138
|
+
- `labelStyle` `CSSProperties` custom label's style, `optional`.
|
|
139
|
+
- `labelWidth` `CSSProperties['width']`, the width of the label label.
|
|
140
|
+
- `labelAlign` `CSSProperties['textAlign']`, the label label's textAlign property.
|
|
141
|
+
- `inline` `boolea`, Whether or not field display components have inline layout.
|
|
142
|
+
- `layout` `'horizontal'|'vertical'` field’s display components set the layout type, the default value is `horizontal`.
|
|
143
|
+
- `colon` `boolean` whether add colon
|
|
144
|
+
- `required` `boolean` Indicates that the value of the field is required `optional`。
|
|
145
|
+
- `gutter` `number` The distance between field's display component custom labels and form components, `optional`.
|
|
146
|
+
- `error` `string` form field displays the component's error message field.
|
|
147
|
+
- `suffix` `React.ReactNode` Suffix node, `optional`.
|
|
148
|
+
- `footer` `React.ReactNode` bootom node, `optional`.
|
|
149
|
+
- `tooltip` `string` Tip icon to prompt for information. `optional`.
|
|
150
|
+
- `compact` `boolean` Whether or not compact, will remove the component's `margin-bottom`. `optional`.
|
|
151
|
+
|
|
152
|
+
### Form Props
|
|
153
|
+
Inherited field display component
|
|
154
|
+
|
|
155
|
+
- `className` The class name of the form element, `optional`.
|
|
156
|
+
- `form` The form data store, `required`.
|
|
157
|
+
- `tagName` Replace the element tag name of the form, default `form` tag
|
|
158
|
+
- `initialValues` The initial value of the form, which is overridden by the `initialValue` of the form field, Note that this value can only initialise the form `optional`.
|
|
159
|
+
- `onSubmit` `form` tag triggers the reset default event, only `button` tags that provide `htmlType=submit` can trigger `optional`.
|
|
160
|
+
- `onReset` `form` tag triggers the reset default event, only `button` tags that provide `htmlType=reset` can trigger `optional`.
|
|
161
|
+
- `onFieldsChange` The event function when a form changes onChange will only be triggered by the control's active `onChange`, not by `form.setFieldValue` and `form.setFieldsValue`, avoiding circular calls。`optional`.
|
|
162
|
+
- `onValuesChange` Listening for changes in form values.`optional`.
|
|
163
|
+
- `watch` Listens for changes in the value of any field.
|
|
164
|
+
|
|
165
|
+
### Form.Item Props
|
|
166
|
+
Inherited field display component
|
|
167
|
+
|
|
168
|
+
- `className` Form field class name, `optional`.
|
|
169
|
+
- `component` field display component.
|
|
170
|
+
- `name` Form field name, `optional`.
|
|
171
|
+
- `trigger` Sets the event name of the form field to collect form values, default `onChange`.
|
|
172
|
+
- `validateTrigger` Sets the event for trigger form field validation, default `onChange`.
|
|
173
|
+
- `valueProp` The field name of the value in the callback function object, the default value is `'value'`.
|
|
174
|
+
- `valueGetter` A function to format the output form value, used with `valueSetter`, `optional`.
|
|
175
|
+
- `valueSetter` function to format input form value, used with `valueGetter`, `optional`.
|
|
176
|
+
- `rules` Checksum rules for form fields `optional`.
|
|
177
|
+
- `initialValue` The initial value of the form field, note that this value is different from `value` when the form is rendered for the first time.
|
|
178
|
+
- `onFieldsChange` The event function when the value of the control changes will only be triggered by the control's active `onChange`, not by `form.setFieldValue` and `form.setFieldsValue`, avoiding circular calls. `optional`.
|
|
179
|
+
- `onValuesChange` Listening for changes in form values.`optional`。
|
|
180
|
+
- `errorClassName` add a custom class name when there is an error message, `optional`.
|
|
181
|
+
|
|
182
|
+
### Form.List Props
|
|
183
|
+
Inherited field display component
|
|
184
|
+
|
|
185
|
+
- `className` Form field class name, `optional`.
|
|
186
|
+
- `component` field display component.
|
|
187
|
+
- `name` Form field name, `optional`.
|
|
188
|
+
- `initialValue` Form field initial value, Note that this value can only initialise the form `optional`.
|
|
189
|
+
- `rules` Checksum rules for form fields `optional`.
|
|
190
|
+
|
|
191
|
+
### rules
|
|
192
|
+
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`.
|
|
193
|
+
- `validateTrigger` `string` Event to trigger validate form rules, default `onChange`.
|
|
194
|
+
- `message` `string` Default error message when a check rule reports an error `optional`。
|
|
195
|
+
- `required` `boolean` The required symbol is marked, and a `required` attribute of `true` in `rules` also automatically adds the required symbol `optional`。
|
|
196
|
+
- `validator` `(value) => void | boolean` Custom check function, `value` is the current control value `optional`.
|
|
197
|
+
- `pattern` `RegExp | string` Expression check, error if does not match `optional`.
|
|
198
|
+
- `whitespace` `boolean` space check `optional`.
|
|
199
|
+
- `max` `number` Maximum length for string type; maximum length for number type; maximum length for array type `optional`.
|
|
200
|
+
- `min` `number` minimum length for `string` type; minimum value for `number` type; minimum length for `array` type. `optional`.
|
|
201
|
+
|
|
202
|
+
### SimpleForm
|
|
203
|
+
To create an instance of `form` via `useSimpleForm`, use the following:
|
|
204
|
+
- `form.getFieldValue(path?: string)` Returns the value of the form field for which `path` is specified, or the value of the whole form without `name`.
|
|
205
|
+
- `form.setFieldValue(path, value)` Update the value of a form field
|
|
206
|
+
- `form.setFieldsValue(obj: Partial<T>)` Set the value of the form field (override).
|
|
207
|
+
- `form.reset(values?: Partial<T>)` Reset the form.The value can be passed to reset to the target value.
|
|
208
|
+
- `form.validate(path?: string)` Checks form and returns error messages and form values.
|
|
209
|
+
- `form.getFieldError(path?: string)` Returns the target's error message or all error messages.
|
|
210
|
+
|
|
211
|
+
### Hooks
|
|
212
|
+
|
|
213
|
+
- `useSimpleForm(defaultValues)` create `new SimpleForm()`
|
|
214
|
+
- `useFormError(form: SimpleForm, path?: string)` Use hooks to get the specified form error.
|
|
215
|
+
- 3.0.12 `useFormValues(form: SimpleForm, path?: string | string[])` Use hooks to get the specified form values.
|
package/README_CN.md
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# `@simpleform/form`
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 中文说明
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@simpleform/form)
|
|
6
|
+
|
|
7
|
+
> 表单底层组件,自动注入`value`和`onChange`到目标控件中,完成表单值的显示和更新.
|
|
8
|
+
|
|
9
|
+
# Matters
|
|
10
|
+
- 在使用之前需要先引入css样式文件,例:`import '@simpleform/form/lib/css/main.css'`;
|
|
11
|
+
|
|
12
|
+
# Form.Item
|
|
13
|
+
|
|
14
|
+
表单域组件,用于双向绑定目标控件。
|
|
15
|
+
|
|
16
|
+
- 双向绑定:`value`(或通过`valueProp`设置)和`onChange`双向绑定,`name`字段为目标属性。
|
|
17
|
+
- 更新表单值:可通过`form.setFieldValue`等实例方法设置表单值。
|
|
18
|
+
- 可以提供表单校验规则属性`rules`,进行自定义表单校验规则。
|
|
19
|
+
- 当输入表单控件外面添加了非表单组件或节点,通过添加`data-type="ignore"`过滤非目标节点或设置`data-name`标记目标节点来绑定目标控件。
|
|
20
|
+
|
|
21
|
+
# Form.List
|
|
22
|
+
|
|
23
|
+
`Form.Item`组件作为`Form.List`数组类型中的项,组合形成一个数组
|
|
24
|
+
|
|
25
|
+
- `Form.List`中只识别`Form.Item`项,`Form.Item`的`name`字段如果设置,则为数组中的字段属性,如果不设置,则默认为数组序号。
|
|
26
|
+
- `Form.List`提供的`rules`校验规则,对数组中的所有输入项都有效,但优先级低于数组中的`Form.Item`的`rules`规则
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
- [Node.js](https://nodejs.org/en/) Version >= 18.18.2
|
|
30
|
+
- [react](https://nodejs.org/en/) Version >= 16.8.0
|
|
31
|
+
```bash
|
|
32
|
+
npm install @simpleform/form --save
|
|
33
|
+
# 或者
|
|
34
|
+
yarn add @simpleform/form
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 基本使用
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
import React from "react";
|
|
41
|
+
import { Form, useSimpleForm, useFormValues } from "@simpleform/form";
|
|
42
|
+
import '@simpleform/form/lib/css/main.css';
|
|
43
|
+
import { Input, Select } from "antd";
|
|
44
|
+
|
|
45
|
+
export default function Demo() {
|
|
46
|
+
|
|
47
|
+
const form = useSimpleForm();
|
|
48
|
+
const formvalues = useFormValues(form, ['name1', 'name2'])
|
|
49
|
+
console.log(formvalues, 'formvalues')
|
|
50
|
+
|
|
51
|
+
const onSubmit = async (e) => {
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
const { error, values } = await form.validate();
|
|
54
|
+
console.log(error, values, 'error ang values');
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const validator = (value) => {
|
|
58
|
+
if (value?.length < 2) {
|
|
59
|
+
return Promise.reject(new Error('length is < 2'));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<Form initialValues={{ name1: 1111 }} form={form} onSubmit={onSubmit}>
|
|
65
|
+
<Form.Item label="Name1" name="name1" rules={[{ required: true, message: 'name1 is Empty' }, { validator: validator, message: 'validator error' }]}>
|
|
66
|
+
<div data-type="ignore">
|
|
67
|
+
<input />
|
|
68
|
+
</div>
|
|
69
|
+
</Form.Item>
|
|
70
|
+
<Form.Item label="object" name="name2.a" rules={[{ required: true, message: 'name2.a is empty' }]}>
|
|
71
|
+
<input />
|
|
72
|
+
</Form.Item>
|
|
73
|
+
<Form.Item label="list" name="name3[0]" rules={[{ required: true, message: 'name3[0] is empty' }]}>
|
|
74
|
+
<input />
|
|
75
|
+
</Form.Item>
|
|
76
|
+
<Form.Item label="">
|
|
77
|
+
<button>Submit</button>
|
|
78
|
+
</Form.Item>
|
|
79
|
+
</Form>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Form.List
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
import React from "react";
|
|
88
|
+
import { Form, useSimpleForm } from "@simpleform/form";
|
|
89
|
+
import '@simpleform/form/lib/css/main.css';
|
|
90
|
+
import { Input, Select } from "antd";
|
|
91
|
+
|
|
92
|
+
export default function Demo() {
|
|
93
|
+
|
|
94
|
+
const form = useSimpleForm();
|
|
95
|
+
|
|
96
|
+
const onSubmit = async (e) => {
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
const { error, values } = await form.validate();
|
|
99
|
+
console.log(error, values, 'error ang values');
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const validator = async (value) => {
|
|
103
|
+
if (value?.length < 2) {
|
|
104
|
+
return Promise.reject(new Error('length is < 2'));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<Form form={form} onSubmit={onSubmit}>
|
|
110
|
+
<Form.List name="list">
|
|
111
|
+
<Form.Item
|
|
112
|
+
rules={[
|
|
113
|
+
{ required: true, message: "list's one is Empty" },
|
|
114
|
+
{ validator: validator, message: "custome tips" },
|
|
115
|
+
]}
|
|
116
|
+
>
|
|
117
|
+
<Input />
|
|
118
|
+
</Form.Item>
|
|
119
|
+
<Form.Item
|
|
120
|
+
rules={[{ required: true, message: "list's two is Empty" }]}
|
|
121
|
+
>
|
|
122
|
+
<Input />
|
|
123
|
+
</Form.Item>
|
|
124
|
+
</Form.List>
|
|
125
|
+
<Form.Item label="">
|
|
126
|
+
<button>Submit</button>
|
|
127
|
+
</Form.Item>
|
|
128
|
+
</Form>
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## APIs
|
|
134
|
+
|
|
135
|
+
### 默认的表单域显示组件的属性
|
|
136
|
+
- `className` `string` 类名,`可选`。
|
|
137
|
+
- `label` `string` 标签,`可选`。
|
|
138
|
+
- `labelStyle` `CSSProperties` 自定义`label`样式,`可选`。
|
|
139
|
+
- `labelWidth` `CSSProperties['width']`, label标签的宽度。
|
|
140
|
+
- `labelAlign` `CSSProperties['textAlign']`, label标签的textAlign属性。
|
|
141
|
+
- `inline` `boolean`, 是否设置行内布局。
|
|
142
|
+
- `layout` `'horizontal'|'vertical'` 设置布局类型,默认值为`horizontal`。
|
|
143
|
+
- `colon` `boolean` 是否添加冒号
|
|
144
|
+
- `required` `boolean` 是否显示星号,不包含表单校验,仅用于显示,默认值为`false`。
|
|
145
|
+
- `gutter` `number` 自定义`label`标签和表单组件间的距离,`可选`。
|
|
146
|
+
- `error` `string` 表单域显示组件的报错信息字段。
|
|
147
|
+
- `suffix` `React.ReactNode` 后缀节点,`可选`。
|
|
148
|
+
- `footer` `React.ReactNode` 底部节点,`可选`。
|
|
149
|
+
- `tooltip` `string` 提示图标,可以提示信息。`可选`。
|
|
150
|
+
- `compact` `boolean` 是否紧凑,会去掉组件的`margin-bottom`。`可选`。
|
|
151
|
+
|
|
152
|
+
### Form Props
|
|
153
|
+
继承表单域显示组件(`component`)的props
|
|
154
|
+
|
|
155
|
+
- `className` 表单元素类名,`可选`。
|
|
156
|
+
- `form` 表单数据管理,`必须`。
|
|
157
|
+
- `tagName` 更换表单的元素标签名, 默认`form`标签
|
|
158
|
+
- `initialValues` 表单的初始值,会被表单域的`initialValue`覆盖, 注意此值只能初始化表单赋值`可选`。
|
|
159
|
+
- `onSubmit` `form`标签提交事件, 只有提供`htmlType=submit`的`button`标签才可以触发,`可选`。
|
|
160
|
+
- `onReset` `form`标签触发重置默认值触发事件, 只有`htmlType=reset`的`button`标签才可以触发 `可选`。
|
|
161
|
+
- `onFieldsChange` 表单域 `onChange` 变化时的事件函数,只会被控件主动`onChange`触发,不会被`form.setFieldValue`和`form.setFieldsValue`触发, 避免循环调用。`可选`。
|
|
162
|
+
- `onValuesChange` 监听表单值的变化。`可选`。
|
|
163
|
+
- `watch` 监听任意字段的值的变化。
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
### Form.Item Props
|
|
167
|
+
继承表单域显示组件(`component`)的props
|
|
168
|
+
|
|
169
|
+
- `className` 表单域类名,`可选`。
|
|
170
|
+
- `component` 表单域显示组件。
|
|
171
|
+
- `name` 表单域字段名,`可选`。
|
|
172
|
+
- `trigger` 设置表单域收集表单值的事件名,默认`onChange`.
|
|
173
|
+
- `validateTrigger` 设置表单域校验的触发事件, 默认`onChange`.
|
|
174
|
+
- `valueProp` 回调函数对象中值的字段名,默认值为`'value'`。
|
|
175
|
+
- `valueGetter` 格式化输出表单值的函数,配合`valueSetter`使用, `可选`。
|
|
176
|
+
- `valueSetter` 格式化输入表单值的函数,配合`valueGetter`使用, `可选`。
|
|
177
|
+
- `rules` 表单域的校验规则 `可选`。
|
|
178
|
+
- `initialValue` 表单域的初始值,注意此值和`value`不同,只能表单第一次渲染时赋值`可选`。
|
|
179
|
+
- `onFieldsChange` 控件的值变化时的事件函数,只会被控件主动`onChange`触发,不会被`form.setFieldValue`和`form.setFieldsValue`触发, 避免循环调用。`可选`。
|
|
180
|
+
- `onValuesChange` 监听表单值的变化。`可选`。
|
|
181
|
+
- `errorClassName` 控件当有错误信息时,添加一个自定义类名,`可选`。
|
|
182
|
+
|
|
183
|
+
### Form.List Props
|
|
184
|
+
继承表单域显示组件(`component`)的props
|
|
185
|
+
|
|
186
|
+
- `className` 表单域类名,`可选`。
|
|
187
|
+
- `component` 表单域显示组件。
|
|
188
|
+
- `name` 表单域字段名,`可选`。
|
|
189
|
+
- `initialValue` 表单域的初始值,注意此值和`value`不同,只能初始化表单赋值`可选`。
|
|
190
|
+
- `rules` 表单域的校验规则 `可选`。
|
|
191
|
+
|
|
192
|
+
### 表单的rules中的校验字段
|
|
193
|
+
`rules`中的值的字段中的规则会按照顺序执行校验,`rules`中每一项只能设置一种规则。
|
|
194
|
+
- `validateTrigger` `string` 校验表单规则的触发事件, 默认`onChange`.
|
|
195
|
+
- `message` `string` 校验规则报错时,默认的报错信息 `可选`。
|
|
196
|
+
- `required` `boolean` 标记必填符号, 同时`rules`中的`required`属性为`true`也自动添加必填标记 `可选`。
|
|
197
|
+
- `validator` `(value) => void | boolean` 自定义校验函数, `value`为当前控件值 `可选`。
|
|
198
|
+
- `pattern` `RegExp | string` 表达式校验,不符合则报错 `可选`。
|
|
199
|
+
- `whitespace` `boolean` 空格校验 `可选`。
|
|
200
|
+
- `max` `number` 表单值为`string`类型时字符串最大长度;`number` 类型时为最大值;`array` 类型时为数组最大长度 `可选`。
|
|
201
|
+
- `min` `number` 表单值为`string`类型时字符串最小长度;`number` 类型时为最小值;`array` 类型时为数组最小长度 `可选`。
|
|
202
|
+
|
|
203
|
+
### SimpleForm
|
|
204
|
+
通过`useSimpleForm`创建`form`实例, 可使用以下方法:
|
|
205
|
+
- `form.getFieldValue(path?: string)` 返回指定`path`的表单域的值,不指定`path`返回整个表单的值。
|
|
206
|
+
- `form.setFieldValue(path, value)` 更新表单域的值
|
|
207
|
+
- `form.setFieldsValue(obj: Partial<T>)` 设置表单域的值(覆盖)。
|
|
208
|
+
- `form.reset(values?: Partial<T>)` 重置表单, 可以传值重置为目标值。
|
|
209
|
+
- `form.validate(path?: string)` 校验表单,并返回错误信息和表单值。
|
|
210
|
+
- `form.getFieldError(path?: string)` 返回目标的错误信息或所有的错误信息。
|
|
211
|
+
|
|
212
|
+
### Hooks
|
|
213
|
+
|
|
214
|
+
- `useSimpleForm(defaultValues)`: 创建 `new SimpleForm()`
|
|
215
|
+
- `useFormError(form: SimpleForm, path?: string)`: 使用 hooks 获取指定的报错信息。
|
|
216
|
+
- 3.0.12 `useFormValues(form: SimpleForm, path?: string | string[])`: 使用 hooks 获取指定的表单值。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simpleform/form",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "simple form component",
|
|
5
5
|
"author": "mezhanglei <496623925@qq.com>",
|
|
6
6
|
"homepage": "https://github.com/mezhanglei/simpleform#readme",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
},
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public",
|
|
27
|
-
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
"registry": "https://registry.npmjs.org/",
|
|
28
|
+
"node-version": ">14.0.0",
|
|
29
|
+
"npm-version": ">6.0.0"
|
|
28
30
|
},
|
|
29
31
|
"scripts": {
|
|
30
32
|
"build": "cross-env NODE_ENV=prod webpack --progress --config ./webpack/webpack.prod.js && tsc -d",
|