|
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { Context } from './hooks/context'; |
| 4 | +import { KeyType, FormItemsProps } from './types'; |
| 5 | +import Label from './comps/label'; |
| 6 | +import Tip from './comps/tip'; |
| 7 | +import Container from './comps/container'; |
| 8 | +import styles from './styles'; |
| 9 | + |
| 10 | +const formchildItem = (props: Partial<FormItemsProps> & { field: string; children: React.ReactElement }) => { |
| 11 | + const { field, name = '', required = false, validate, children } = props; |
| 12 | + const { |
| 13 | + mode, |
| 14 | + innerMethods: { store = {}, updateStore, innerValidate }, |
| 15 | + watch, |
| 16 | + changeValidate, |
| 17 | + } = useContext(Context); |
| 18 | + |
| 19 | + const change = (field: KeyType, value: unknown) => { |
| 20 | + updateStore?.({ store: { ...store, [field]: value } }); |
| 21 | + watch && watch[field]?.(value); |
| 22 | + }; |
| 23 | + |
| 24 | + const _renderComponent = (children: React.ReactElement) => { |
| 25 | + return React.isValidElement(children) |
| 26 | + ? React.cloneElement(children, { |
| 27 | + value: store[field], |
| 28 | + onChange: (value: unknown) => { |
| 29 | + change(field, value); |
| 30 | + if (changeValidate) innerValidate(); |
| 31 | + }, |
| 32 | + } as any) |
| 33 | + : null; |
| 34 | + }; |
| 35 | + return ( |
| 36 | + <Container mode={mode}> |
| 37 | + <View style={styles.form_items_container}> |
| 38 | + <View style={[styles.form_items, styles.border_none]}> |
| 39 | + <Label v={{ name: name, required: required }} /> |
| 40 | + {_renderComponent(children)} |
| 41 | + <Tip v={{ validate: validate, field: field }} /> |
| 42 | + </View> |
| 43 | + </View> |
| 44 | + </Container> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +export default formchildItem; |
0 commit comments