Skip to content

Commit 6e70255

Browse files
committed
feat(*): field and validate npm packages
- replace field with @alifd/field and add React support - replace validate with @alifd/validate
1 parent 589a079 commit 6e70255

18 files changed

+302
-1727
lines changed

docs/field/demo/topath.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ class App extends React.Component {
4444
b: 'b',
4545
c: 'c'
4646
},
47-
arr: ['first', 'second']
47+
arr: ['first', 'second'],
48+
objWithDefaults: {
49+
a: 100,
50+
b: 200
51+
}
4852
});
4953
}
5054

docs/field/demo/useField.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 带有反应挂钩的功能组件
2+
3+
- order: 12
4+
5+
`Field` 将反应挂钩 `useField` 公开为静态方法。接受与 `new Field(...)` 一起使用的 `options` 参数。
6+
7+
:::lang=en-us
8+
# Functional Component with React Hooks
9+
10+
- order: 12
11+
12+
`Field` exposes a React Hook `useField` as a static method. Takes in the `options` parameter used with `new Field(...)`.
13+
14+
15+
:::
16+
---
17+
18+
19+
20+
21+
````jsx
22+
import ReactDOM from 'react-dom';
23+
import React from 'react';
24+
import { Input, Button, Field } from '@alifd/next';
25+
26+
27+
function NewApp() {
28+
const field = Field.useField({ values: { input: 'abc' }});
29+
30+
const { init, setValue, reset } = field;
31+
32+
function onGetValue() {
33+
console.log(field.getValue('input'));
34+
}
35+
36+
function onSetValue() {
37+
field.setValue('input', 'xyz');
38+
}
39+
40+
return (
41+
<div className="demo">
42+
<Input {...init('input', {initValue: 'test'})} />
43+
<Button onClick={onSetValue}> setValue </Button>
44+
<Button onClick={onGetValue}> getValue </Button>
45+
<br/><br/>
46+
</div>);
47+
}
48+
49+
50+
ReactDOM.render(<NewApp/>, mountNode);
51+
52+
````

docs/field/demo/validator.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
`注意`:Form 和 Field 做了深度结合,在 Form 中使用Field,错误信息不需`getError`获取会自动展现。
88

9+
请参见 “验证方承诺” 演示,以使用承诺而不是回调。
10+
911
:::lang=en-us
1012
# validate
1113

1214
- order: 4
1315

14-
you can easily use validate in `Form`, or you can use `getError` to set errors where you want to put
16+
you can easily use validate in `Form`, or you can use `getError` to set errors where you want to put. See `validatorPromise` demo to use promises instead of callbacks.
1517
:::
1618
---
1719

docs/field/demo/validatorPromise.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# 验证承诺
2+
3+
- order: 4
4+
5+
验证表单值,并在承诺中返回错误。也支持回调。如果回调返回承诺,承诺的结果将在 “验证” 函数的承诺中返回。
6+
7+
:::lang=en-us
8+
# validate Promise
9+
10+
- order: 4
11+
12+
validate form values with errors returned wrapped in a promise. Also supports callbacks. If the callback returns a promise, the results of the promise will be returned in a promise from the `validate` function.
13+
14+
:::
15+
---
16+
17+
````jsx
18+
import ReactDOM from 'react-dom';
19+
import React from 'react';
20+
import { Input, Button, Checkbox } from '@alifd/next';
21+
import Field from '@alifd/field';
22+
23+
24+
25+
const CheckboxGroup = Checkbox.Group;
26+
27+
const list = [
28+
{
29+
value: 'apple',
30+
label: 'apple'
31+
}, {
32+
value: 'pear',
33+
label: 'pear'
34+
}, {
35+
value: 'orange',
36+
label: 'orange'
37+
}
38+
];
39+
40+
class App extends React.Component {
41+
state = {
42+
checkboxStatus: true
43+
}
44+
field = new Field(this, {scrollToFirstError: -10});
45+
46+
isChecked(rule, value) {
47+
if (!value) {
48+
return Promise.reject('consent agreement not checked ')
49+
} else {
50+
return Promise.resolve(null);
51+
}
52+
}
53+
54+
userName(rule, value) {
55+
if (value === 'frank') {
56+
return new Promise((resolve, reject) => {
57+
setTimeout(() => resolve('name existed'), 200);
58+
})
59+
} else {
60+
return new Promise((resolve) => {
61+
setTimeout(() => resolve(null), 200);
62+
})
63+
}
64+
}
65+
66+
render() {
67+
const init = this.field.init;
68+
69+
return (<div className="demo">
70+
<Input {...init('input', {initValue: 'delete all', rules: {required: true}})} />
71+
{this.field.getError('input') ?
72+
<span style={{color: 'red'}}>{this.field.getError('input').join(',')}</span> : ''}
73+
74+
<br/>
75+
<br/>
76+
77+
<Input placeholder="try onBlur" {...init('input1', {
78+
rules: [{
79+
required: true,
80+
message: 'can not be empty',
81+
trigger: ['onBlur', 'onChange']
82+
}]
83+
})} />
84+
{this.field.getError('input1') ?
85+
<span style={{color: 'red'}}>{this.field.getError('input1').join(',')}</span> : ''}
86+
87+
<br/>
88+
<br/>
89+
90+
<Input defaultValue="" placeholder="try frank" {...init('username', {
91+
rules: [{
92+
validator: this.userName,
93+
trigger: ['onBlur', 'onChange']
94+
}]
95+
})} />
96+
{this.field.getState('username') === 'loading' ? 'validating...' : ''}
97+
{this.field.getError('username') ?
98+
<span style={{color: 'red'}}>{this.field.getError('username').join(',')}</span> : ''}
99+
100+
<br/>
101+
<br/>
102+
103+
agreement:
104+
<Checkbox {...init('checkbox', {
105+
valueName: 'checked',
106+
rules: [{validator: this.isChecked}]
107+
})} />
108+
{this.field.getError('checkbox') ?
109+
<span style={{color: 'red'}}>{this.field.getError('checkbox').join(',')}</span> : ''}
110+
111+
<br/>
112+
<br/>
113+
114+
<Input.TextArea placeholder=">3 and <10" {...init('textarea', {
115+
rules: [{
116+
required: true,
117+
minLength: 3,
118+
maxLength: 10
119+
}]
120+
})} />
121+
{this.field.getError('textarea') ?
122+
<span style={{color: 'red'}}>{this.field.getError('textarea').join(',')}</span> : ''}
123+
124+
<br/>
125+
<br/>
126+
127+
{this.state.checkboxStatus ? <div>
128+
Array validate:
129+
<CheckboxGroup dataSource={list} {...init('checkboxgroup', {
130+
rules: [{
131+
required: true,
132+
type: 'array',
133+
message: 'choose one please'
134+
}]
135+
})} style={{marginBottom: 10}}/>
136+
{this.field.getError('checkboxgroup') ?
137+
<span style={{color: 'red'}}>{this.field.getError('checkboxgroup').join(',')}</span> : ''}
138+
</div> : null}
139+
140+
<br/>
141+
<br/>
142+
143+
<Button type="primary" onClick={() => {
144+
this.field.validatePromise().then(({errors, values}) => {
145+
console.log(errors, values);
146+
});
147+
}}>validate</Button>
148+
<Button onClick={() => {
149+
this.field.reset();
150+
}}>reset</Button>
151+
152+
<Button onClick={() => {
153+
if (this.state.checkboxStatus) {
154+
this.setState({checkboxStatus: false});
155+
this.field.remove('checkboxgroup');
156+
} else {
157+
this.setState({checkboxStatus: true});
158+
}
159+
160+
}}>{this.state.checkboxStatus ? 'delete' : 'restore'}</Button>
161+
</div>);
162+
}
163+
}
164+
165+
ReactDOM.render(<App/>, mountNode);
166+
````
167+
168+
````css
169+
.demo .next-btn {
170+
margin-right: 5px;
171+
}
172+
````

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
]
7373
},
7474
"dependencies": {
75+
"@alifd/field": "^1.1.6",
76+
"@alifd/validate": "^1.1.3",
7577
"babel-runtime": "^6.26.0",
7678
"classnames": "^2.2.3",
7779
"hoist-non-react-statics": "^2.1.0",

0 commit comments

Comments
 (0)