@@ -22,7 +22,7 @@ const FormDemo = () => {
2222 ];
2323 return (
2424 < SafeAreaView>
25- < Form form= {form} formDatas = {items} initialValues= {initialValues} / >
25+ < Form form= {form} schema = {items} initialValues= {initialValues} / >
2626 < / SafeAreaView>
2727 );
2828}
@@ -53,7 +53,7 @@ const FormDemo = () => {
5353 ];
5454 return (
5555 < SafeAreaView>
56- < Form form= {form} formDatas = {items} initialValues= {initialValues} / >
56+ < Form form= {form} schema = {items} initialValues= {initialValues} / >
5757 < Button
5858 type= " primary"
5959 onPress= {() => {
@@ -100,7 +100,7 @@ const FormDemo = () => {
100100 ];
101101 return (
102102 < SafeAreaView>
103- < Form form= {form} formDatas = {items} initialValues= {initialValues} / >
103+ < Form form= {form} schema = {items} initialValues= {initialValues} / >
104104 < / SafeAreaView>
105105 );
106106};
@@ -134,47 +134,60 @@ const FormDemo = () => {
134134 ];
135135 return (
136136 < SafeAreaView>
137- < Form form= {form} formDatas = {items} initialValues= {initialValues} / >
137+ < Form form= {form} schema = {items} initialValues= {initialValues} / >
138138 < / SafeAreaView>
139139 );
140140};
141141```
142142<!-- End-->
143143
144- ### 动态表单list
144+ ### 动态添加表单list(当type为cardList时)
145145
146- > ⚠️ 警告:目前仅能嵌套一层cardList<!-- rehype:style=background: #F08800; color: #fff;--> 。
146+ > ⚠️ 警告:
147+ 1.目前仅能嵌套一层cardList
148+ 2.我们暂时无法验证到添加的表单项里的每一个字段<!-- rehype:style=background: #F08800; color: #fff;--> 。
147149<!-- rehype:style=border-left: 8px solid #ffe564;background-color: #ffe56440;padding: 12px 16px;-->
148150
149151<!-- DemoStart-->
150152``` jsx
151- import { SafeAreaView ,Toast } from ' react-native' ;
152- import { Form } from ' @uiw/react-native' ;
153+ import { SafeAreaView ,View , Text } from ' react-native' ;
154+ import { Form , Button } from ' @uiw/react-native' ;
153155
154156const FormDemo = () => {
155157 const form = Form .useForm ({
156158 changeValidate: true ,
157159 });
158160 const initialValues = {name: ' ' };
159161 const items = [
160- {
162+ {
161163 type: ' cardList' ,
162164 field: ' cardList' ,
163- name: ' 动态list' ,
164- required: true ,
165- items: [
165+ name: ' 联系人集合' ,
166+ renderHeader: (i: number, { remove }: { remove : () => void }) => (
167+ < View style= {{ marginTop: 12 , display: ' flex' , justifyContent: ' space-between' , flexDirection: ' row' }}>
168+ < View>< Text > {` 联系人${ i + 1 } ` }< / Text >< / View>
169+ < View>< Text onPress= {() => remove ()}> 删除< / Text >< / View>
170+ < / View>
171+ ),
172+ renderAdd: ({ add }: { add : () => void }) => (
173+ < View style= {{ display: ' flex' , justifyContent: ' center' , alignItems: ' center' , marginTop: 12 }}>
174+ < Button onPress= {() => add ()} type= " primary" size= " default" >
175+ 新增数据
176+ < / Button>
177+ < / View>
178+ ),
179+ items: [
166180 {
167181 type: ' input' ,
168- field: ' cardList' ,
169- name: ' 动态list' ,
170- required: true ,
171- }
172- ]
173- }
182+ field: ' cardListItem1' ,
183+ name: ' 联系人姓名' ,
184+ },
185+ ],
186+ },
174187 ];
175188 return (
176189 < SafeAreaView>
177- < Form form= {form} formDatas = {items} initialValues= {initialValues} / >
190+ < Form form= {form} schema = {items} initialValues= {initialValues} / >
178191 < / SafeAreaView>
179192 );
180193};
@@ -187,39 +200,71 @@ interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], Field
187200 /**
188201 * 表单集合
189202 */
190- formDatas ? : FormItemsProps [];
203+ schema ? : FormItemsProps [];
191204 /**
192- * useForm表单实例
205+ * 经 Form.useForm() 创建的 form 控制实例,不提供时会自动创建
193206 */
194207 form: FormInstance <FormData , FieldValue , FieldKey >;
195208 /**
196- * 表单默认值
209+ * 表单默认值,只有初始化以及重置时生效
197210 */
198211 initialValues? : Partial <FormData >;
212+ /**
213+ * 支持默认和卡片两种模式
214+ */
215+ mode? : ' default' | ' card'
199216}
200217```
201218
202219### FormItemsProps
203220``` ts
204221interface FormItemsProps {
222+ // 字段名
205223 field: string ;
224+ // 字段类型(默认继承了react-native-uiw中的 input | textArea | slider | rate | radio | search | switch | checkBox | stepper | cardList )
206225 type: string ;
226+ // 标签名
207227 name: string ;
228+ // 验证规则
208229 validate? : RulesOption [' validate' ];
209230 options? : Array <{ label: string ; value: KeyType | any }>;
231+ // 表单控件props
210232 attr? : any ;
233+ // 展示是否必填
211234 required? : boolean ;
235+ // 是否隐藏
212236 hide? : boolean
237+ // 当type为cardList生效,渲染每一项的头部内容
238+ renderHeader? : (index : number ,{ remove }: { remove: ()=> void })=> React .ReactNode ;
239+ // 当type为cardList生效,渲染添加按钮的文案
240+ renderAdd? : ( { add }: { add: ()=> void } )=> React .ReactNode ;
241+ // 当type为cardList生效,配置表单项
242+ items? : Omit <FormItemsProps , ' validate' | ' required' >[];
213243}
214244```
215245
216246### FormInstance
217247``` ts
218248type FormInstance <FormData = any , FieldValue = FormData [keyof FormData ], FieldKey extends KeyType = keyof FormData > = {
249+ /**
250+ * 获取对应字段名的值
251+ */
219252 getFieldValue: (field : FieldKey ) => FieldValue ;
253+ /**
254+ * 设置对应字段名的值
255+ */
220256 setFieldValue: (field : FieldKey , value : FieldValue ) => void ;
257+ /**
258+ * 重制表单
259+ */
221260 resetFieldValue: () => void ;
261+ /**
262+ * 触发验证
263+ */
222264 validate: () => Partial <Record <string , string >>;
265+ /**
266+ * 触发表单验证获取表单数据
267+ */
223268 validateFields: () => Promise <FormData > | any ;
224269 getInnerMethods: (inner ? : boolean ) => InnerMethodsReturnType <FormData >;
225270};
0 commit comments