|
1 | 1 | import React, { FC, useContext } from 'react'; |
2 | | -import { FormProps } from './types'; |
| 2 | +import { FormProps, KeyType, FormItemsProps } from './types'; |
3 | 3 | import { Context } from './hooks/context'; |
4 | 4 | import Input from '../Input'; |
| 5 | +import { View, Text } from 'react-native'; |
| 6 | +import { useValidator } from '@validator.tool/hook'; |
5 | 7 |
|
6 | | -const FormItems: FC<FormProps & any> = ({ formDatas = [], initialValues = {} }) => { |
| 8 | +const FormItems: FC<FormProps> = ({ formDatas = [] }) => { |
7 | 9 | const { innerMethods } = useContext(Context); |
8 | 10 |
|
9 | 11 | const formValues = innerMethods.innerGetStore(); |
10 | 12 |
|
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); |
12 | 18 |
|
13 | 19 | const _render = () => { |
14 | | - return formDatas.map((v: any, i: number) => { |
| 20 | + return formDatas.map((v: FormItemsProps, i: number) => { |
| 21 | + let _render; |
15 | 22 | 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 | + ); |
17 | 34 | } |
| 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 | + ); |
18 | 45 | }); |
19 | 46 | }; |
20 | 47 |
|
|
0 commit comments