1- import React , { FC , useContext , useEffect } from 'react' ;
1+ import React , { FC , useContext } from 'react' ;
22import { KeyType , FormItemsProps } from './types' ;
33import { Context } from './hooks/context' ;
4- import Input from '../Input' ;
5- import { View , Text , TextInput } from 'react-native' ;
4+ import Radio from '../Radio' ;
5+ import CheckBox from '../CheckBox' ;
6+ import Rating from '../Rating' ;
7+ import Switch from '../Switch' ;
8+ import SearchBar from '../SearchBar' ;
9+ import { View , Text , SafeAreaView , StyleSheet , TextInput } from 'react-native' ;
610
711const FormItems : FC < any > = ( { formDatas = [ ] } ) => {
812 const {
@@ -16,28 +20,128 @@ const FormItems: FC<any> = ({ formDatas = [] }) => {
1620 forceUpdate ( ) ;
1721 } ;
1822
23+ const _renderComponent = ( v : FormItemsProps ) => {
24+ const options = v . options || [ ] ;
25+ if ( v . type === 'render' ) {
26+ return v . render ;
27+ }
28+ if ( v . type === 'input' ) {
29+ return (
30+ < TextInput
31+ value = { store [ v . field ] }
32+ onChangeText = { ( value ) => change ( v . field , value ) }
33+ onBlur = { ( ) => validate ( ) }
34+ { ...v . attr }
35+ />
36+ ) ;
37+ }
38+ if ( v . type === 'radio' ) {
39+ return options . map ( ( item , idx ) => (
40+ < Radio
41+ key = { idx }
42+ checked = { item . value === store [ v . field ] }
43+ onPress = { ( ) => change ( v . field , item . value ) }
44+ { ...v . attr }
45+ >
46+ { item . label }
47+ </ Radio >
48+ ) ) ;
49+ }
50+ if ( v . type === 'checkBox' ) {
51+ return options . map ( ( item , idx ) => {
52+ const values = store [ v . field ] || [ ] ;
53+ return (
54+ < CheckBox
55+ key = { idx }
56+ checked = { values . includes ( item . value ) }
57+ onChange = { ( ) => {
58+ let data = store [ v . field ] || [ ] ;
59+ if ( ! data . includes ( item . value ) ) {
60+ data . push ( item . value ) ;
61+ } else {
62+ const idx = data . findIndex ( ( v : KeyType ) => v === item . value ) ;
63+ data . splice ( idx , 1 ) ;
64+ }
65+ change ( v . field , data ) ;
66+ } }
67+ { ...v . attr }
68+ >
69+ { item . label }
70+ </ CheckBox >
71+ ) ;
72+ } ) ;
73+ }
74+ if ( v . type === 'rate' ) {
75+ return < Rating onPress = { ( number ) => change ( v . field , number ) } { ...v . attr } /> ;
76+ }
77+ if ( v . type === 'switch' ) {
78+ return < Switch checked = { store [ v . field ] } onValueChange = { ( ) => change ( v . field , ! store [ v . field ] ) } { ...v . attr } /> ;
79+ }
80+ if ( v . type === 'search' ) {
81+ return (
82+ < SearchBar
83+ options = { options }
84+ onChange = { ( val ) => change ( v . field , val ) }
85+ contentStyle = { { paddingHorizontal : 0 } }
86+ { ...v . attr }
87+ />
88+ ) ;
89+ }
90+ return null ;
91+ } ;
92+
93+ const Label = ( v : FormItemsProps ) => {
94+ return (
95+ < Text style = { styles . label } { ...v . attr } >
96+ { v . name }
97+ </ Text >
98+ ) ;
99+ } ;
100+
101+ const Tip = ( v : FormItemsProps ) => {
102+ const content = validator . message ( v . field , store [ v . field ] , {
103+ validate : v ?. validate ,
104+ } ) ;
105+ return < Text style = { { color : 'red' , marginBottom : content && 10 , marginTop : content && 10 } } > { content } </ Text > ;
106+ } ;
107+
19108 const _render = ( ) => {
20109 return formDatas . map ( ( v : FormItemsProps , i : number ) => {
21- let _render ;
22- if ( v . type === 'input' ) {
23- _render = (
24- < Input value = { store [ v . field ] } onChangeText = { ( value ) => change ( v . field , value ) } onBlur = { ( ) => validate ( ) } />
25- ) ;
26- }
27110 return (
28- < View key = { i } >
29- { _render }
30- < Text style = { { color : 'red' } } >
31- { validator . message ( v . field , store [ v . field ] , {
32- validate : v ?. validate ,
33- } ) }
34- </ Text >
111+ < View key = { i } style = { styles . form_items_container } >
112+ < View style = { styles . form_items } >
113+ { Label ( v ) }
114+ { _renderComponent ( v ) }
115+ { Tip ( v ) }
116+ </ View >
35117 </ View >
36118 ) ;
37119 } ) ;
38120 } ;
39121
40- return < React . Fragment > { _render ( ) } </ React . Fragment > ;
122+ return < SafeAreaView style = { styles . warpper } > { _render ( ) } </ SafeAreaView > ;
41123} ;
42124
125+ const styles = StyleSheet . create ( {
126+ warpper : {
127+ backgroundColor : '#fff' ,
128+ } ,
129+ form_items_container : {
130+ flex : 1 ,
131+ } ,
132+ form_items : {
133+ textAlign : 'center' ,
134+ margin : 10 ,
135+ borderBottomWidth : 0.5 ,
136+ borderBottomColor : '#eee' ,
137+ } ,
138+ label : {
139+ width : 110 ,
140+ fontSize : 16 ,
141+ color : '#434343' ,
142+ fontWeight : '500' ,
143+ marginBottom : 10 ,
144+ } ,
145+ } ) ;
146+
43147export default FormItems ;
0 commit comments