Skip to content

Commit 2d09b3d

Browse files
committed
fix(Form): 暂时去除动态表单递归新增删除
1 parent a67879b commit 2d09b3d

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
2-
import {Form, Button, Toast, Slider, SearchBar} from '@uiw/react-native';
1+
import React, {useState} from 'react';
2+
import {Form, Button, Toast, Slider} from '@uiw/react-native';
3+
import {Text, View} from 'react-native';
34
import Layout, {Container} from '../../Layout';
45
const {Body, Footer} = Layout;
56

@@ -15,6 +16,7 @@ const FormDemo = () => {
1516
render: <Slider />,
1617
},
1718
});
19+
const [state, setStore] = useState({});
1820
const items = [
1921
{
2022
type: 'input',
@@ -109,16 +111,16 @@ const FormDemo = () => {
109111
type: 'cardList',
110112
field: 'cardList',
111113
name: '动态list',
114+
validate: (val: any) => {
115+
console.log('val', val);
116+
return '';
117+
},
112118
items: [
113119
{
114120
type: 'input',
115121
field: 'cardListItem1',
116122
name: '动态表单项1',
117-
},
118-
{
119-
type: 'input',
120-
field: 'cardListItem2',
121-
name: '动态表单项2',
123+
required: true,
122124
},
123125
],
124126
},
@@ -130,12 +132,15 @@ const FormDemo = () => {
130132
<Layout>
131133
<Body>
132134
<Form form={form} formDatas={items} initialValues={initialValues} />
135+
<View>
136+
<Text>{JSON.stringify(state)}</Text>
137+
</View>
133138
<Button
134139
type="primary"
135140
onPress={() => {
136141
form
137142
.validateFields()
138-
.then((values: any) => Toast.success(JSON.stringify(values)))
143+
.then((values: any) => setStore(values))
139144
.catch((errors: any) => Toast.warning(JSON.stringify(errors)));
140145
}}>
141146
确定

packages/core/src/Form/formList.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ import Button from '../Button';
1515
import Card from '../Card';
1616
import Label from './comps/label';
1717
import Tip from './comps/tip';
18-
import { View, SafeAreaView, StyleSheet, TextInput } from 'react-native';
18+
import { View, SafeAreaView, TextInput } from 'react-native';
1919
import styles from './styles';
20+
import { values } from 'lodash';
2021

21-
const FormList: FC<any> = ({ formListValue = {} }) => {
22+
const FormList = ({
23+
formListValue = {
24+
field: '',
25+
type: '',
26+
name: '',
27+
},
28+
}: {
29+
formListValue: FormItemsProps;
30+
}) => {
2231
const {
23-
innerMethods: { store = {}, updateStore, innerValidate, watch, customComponentList },
32+
innerMethods: { store = {}, updateStore, customComponentList },
2433
} = useContext(Context);
2534

2635
const change = (field: KeyType, value: unknown, index: number) => {
@@ -30,8 +39,7 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
3039
[field]: value,
3140
};
3241
list.splice(index, 1, obj);
33-
updateStore?.({ store: { ...store, [field]: list } });
34-
watch[field]?.(value);
42+
updateStore?.({ store: { ...store, [formListValue.field]: list } });
3543
};
3644

3745
const _renderComponent = (v: FormItemsProps, index: number) => {
@@ -43,7 +51,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
4351
value={values[v.field]}
4452
onChangeText={(value) => {
4553
change(v.field, value, index);
46-
innerValidate();
4754
}}
4855
{...v.attr}
4956
/>
@@ -54,7 +61,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
5461
<TextArea
5562
onChange={(value: string) => {
5663
change(v.field, value, index);
57-
innerValidate();
5864
}}
5965
value={store[v.field]}
6066
{...v.attr}
@@ -68,7 +74,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
6874
checked={item.value === values[v.field]}
6975
onPress={() => {
7076
change(v.field, item.value, index);
71-
innerValidate();
7277
}}
7378
{...v.attr}
7479
>
@@ -92,7 +97,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
9297
data.splice(idx, 1);
9398
}
9499
change(v.field, data, index);
95-
innerValidate();
96100
}}
97101
{...v.attr}
98102
>
@@ -106,7 +110,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
106110
<Rating
107111
onPress={(number) => {
108112
change(v.field, number, index);
109-
innerValidate();
110113
}}
111114
{...v.attr}
112115
/>
@@ -118,7 +121,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
118121
checked={values[v.field]}
119122
onValueChange={() => {
120123
change(v.field, !values[v.field], index);
121-
innerValidate();
122124
}}
123125
{...v.attr}
124126
/>
@@ -130,7 +132,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
130132
options={options}
131133
onChange={(value) => {
132134
change(v.field, value, index);
133-
innerValidate();
134135
}}
135136
contentStyle={{ paddingHorizontal: 0 }}
136137
{...v.attr}
@@ -146,7 +147,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
146147
value={values[v.field]}
147148
onChange={(value) => {
148149
change(v.field, value, index);
149-
innerValidate();
150150
}}
151151
{...v.attr}
152152
/>
@@ -158,7 +158,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
158158
value={values[v.field]}
159159
onChange={(value) => {
160160
change(v.field, value, index);
161-
innerValidate();
162161
}}
163162
{...v.attr}
164163
/>
@@ -172,7 +171,6 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
172171
value: values[v.field],
173172
onChange: (value: unknown) => {
174173
change(v.field, value, index);
175-
innerValidate();
176174
},
177175
})
178176
: null;
@@ -186,7 +184,7 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
186184
return null;
187185
}
188186
if (v.type === 'cardList') {
189-
return;
187+
return <FormList formListValue={v}></FormList>;
190188
}
191189
return (
192190
<View key={i} style={styles.form_items_container}>
@@ -210,7 +208,7 @@ const FormList: FC<any> = ({ formListValue = {} }) => {
210208
return (
211209
<SafeAreaView style={styles.warpper}>
212210
{(store[formListValue.field] || []).map((item: Record<string, unknown>, index: number) => (
213-
<Card>
211+
<Card key={index}>
214212
{_render(index)}
215213
<Card.Actions
216214
driver={false}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export default function useForm<
4040
};
4141

4242
const innerValidate = () => {
43-
const { showMessages } = validator;
43+
const { showMessages, errorMessages } = validator;
44+
console.log('errorMessages', errorMessages);
4445
if (changeValidate) {
4546
showMessages?.();
4647
forceUpdate?.();

0 commit comments

Comments
 (0)