Skip to content

Commit bbc8325

Browse files
committed
fix(Form): 新增验证功能
1 parent 75e77d8 commit bbc8325

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

example/examples/src/routes/Form/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ import Layout, {Container} from '../../Layout';
44
const {Body, Footer} = Layout;
55

66
const FormDemo = () => {
7-
const items = [{type: 'input', label: 'name'}];
7+
const items = [
8+
{
9+
type: 'input',
10+
field: 'name',
11+
validate: (val: any) => (!val ? `请输入name` : ''),
12+
},
13+
{
14+
type: 'input',
15+
field: 'age',
16+
},
17+
];
818

919
const initialValues = {name: '王滴滴'};
1020

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"prop-types": "15.7.2",
6262
"react-native-gesture-handler": "2.0.0",
6363
"react-native-root-siblings": "4.1.1",
64-
"react-native-svg": "12.1.1"
64+
"react-native-svg": "12.1.1",
65+
"@validator.tool/hook":"2.2.4"
6566
},
6667
"peerDependencies": {
6768
"react": ">=16.9.0",

packages/core/src/Form/formItems.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import React, { FC, useContext } from 'react';
2-
import { FormProps } from './types';
2+
import { FormProps, KeyType, FormItemsProps } from './types';
33
import { Context } from './hooks/context';
44
import Input from '../Input';
5+
import { View, Text } from 'react-native';
6+
import { useValidator } from '@validator.tool/hook';
57

6-
const FormItems: FC<FormProps & any> = ({ formDatas = [], initialValues = {} }) => {
8+
const FormItems: FC<FormProps> = ({ formDatas = [] }) => {
79
const { innerMethods } = useContext(Context);
810

911
const formValues = innerMethods.innerGetStore();
1012

11-
const change = (field: string, value: any) => innerMethods?.innerUpdateStore(field, value);
13+
const { validator, forceUpdate } = useValidator({
14+
initValues: { ...formValues },
15+
});
16+
17+
const change = (field: KeyType, value: any) => innerMethods?.innerUpdateStore(field, value);
1218

1319
const _render = () => {
14-
return formDatas.map((v: any, i: number) => {
20+
return formDatas.map((v: FormItemsProps, i: number) => {
21+
let _render;
1522
if (v.type === 'input') {
16-
return <Input key={i} value={formValues[v.label]} onChangeText={(value) => change(v.label, value)} />;
23+
_render = (
24+
<Input
25+
value={formValues[v.field]}
26+
onChangeText={(value) => change(v.field, value)}
27+
onBlur={() => {
28+
validator.showMessages();
29+
forceUpdate();
30+
console.log('validator', validator);
31+
}}
32+
/>
33+
);
1734
}
35+
return (
36+
<View key={i}>
37+
{_render}
38+
<Text style={{ color: 'red' }}>
39+
{validator.message(v.field, formValues[v.field], {
40+
validate: v?.validate,
41+
})}
42+
</Text>
43+
</View>
44+
);
1845
});
1946
};
2047

packages/core/src/Form/hooks/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Store<FormData = any, FieldValue = FormData[keyof FormData], FieldKey exte
2727
return cloneDeep(get(this.store, field));
2828
};
2929

30-
public getStore = () => {
30+
public getStore = (): Partial<FormData> => {
3131
return this.store;
3232
};
3333
}

packages/core/src/Form/types/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Store from '../hooks/store';
2+
import { RulesOption } from '@validator.tool/hook';
23

34
type KeyType = string | number | symbol;
45

@@ -23,8 +24,9 @@ interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], Field
2324
}
2425

2526
interface FormItemsProps {
26-
label: string;
27+
field: string;
2728
type: string;
29+
validate?: RulesOption['validate'];
2830
}
2931

3032
export type { FormProps, FormItemsProps, KeyType, FormInstance };

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5040,6 +5040,13 @@
50405040
resolved "https://registry.yarnpkg.com/@uiw/utils/-/utils-4.21.14.tgz#e4f836bc52c386784c508e401391040b85de6ded"
50415041
integrity sha512-0A9pEbUqMFjccpJTOR2Tv6DlhSSmiNpJ2gSTlU0IHjDA6Phatl7Eh65fSxZyZiiigjH6EFhCj/hqmwOOIbF5hg==
50425042

5043+
"@validator.tool/hook@2.2.4":
5044+
version "2.2.4"
5045+
resolved "https://registry.npmjs.org/@validator.tool/hook/-/hook-2.2.4.tgz#e46262e3f2e7159ab069769a3b425a23e2dcb228"
5046+
integrity sha512-hW8nWhL33+wSZXYeqm2RI7E6kpaQxg883kcJdQo3XHgO6++xeVlUmoDLov7Dz6Sb0+S4iziR8JatE6EiVV6N+g==
5047+
dependencies:
5048+
validator.tool "2.2.4"
5049+
50435050
"@vue/babel-helper-vue-transform-on@^1.0.2":
50445051
version "1.0.2"
50455052
resolved "https://registry.yarnpkg.com/@vue/babel-helper-vue-transform-on/-/babel-helper-vue-transform-on-1.0.2.tgz#9b9c691cd06fc855221a2475c3cc831d774bc7dc"
@@ -19010,6 +19017,11 @@ validate-npm-package-name@^4.0.0:
1901019017
dependencies:
1901119018
builtins "^5.0.0"
1901219019

19020+
validator.tool@2.2.4:
19021+
version "2.2.4"
19022+
resolved "https://registry.npmjs.org/validator.tool/-/validator.tool-2.2.4.tgz#74877796be3eae23a038be06a74d00c0bbb47b82"
19023+
integrity sha512-JkjrGA16YeSbISFFijNJGiYjly0bybY7qBNNZGq0nHV7oL2wfKXD1EO8iyRpZlfS4cJjpdaXQ2BDrtcmas79jQ==
19024+
1901319025
vary@~1.1.2:
1901419026
version "1.1.2"
1901519027
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

0 commit comments

Comments
 (0)