Skip to content

Commit b0b99fb

Browse files
committed
feat(Form): 动态列表增加上移/下移/置顶/置底功能
1 parent 1081137 commit b0b99fb

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import React, {useState} from 'react';
2-
import {Form, Button, Toast, Slider} from '@uiw/react-native';
2+
import {Form, Button, Toast, Slider, Flex} from '@uiw/react-native';
33
import {Text, View} from 'react-native';
44
import Layout, {Container} from '../../Layout';
55
const {Body, Footer} = Layout;
66

7+
interface actionProps {
8+
remove: () => void;
9+
moveUp: () => void;
10+
moveDown: () => void;
11+
moveToTop: () => void;
12+
moveToBottom: () => void;
13+
}
14+
715
const FormDemo = () => {
816
const form = Form.useForm({
917
changeValidate: true,
@@ -119,14 +127,26 @@ const FormDemo = () => {
119127
type: 'cardList',
120128
field: 'cardList',
121129
name: '联系人集合',
122-
renderHeader: (i: number, {remove}: {remove: () => void}) => (
130+
renderHeader: (i: number, {remove, moveUp, moveDown, moveToTop, moveToBottom}: actionProps) => (
123131
<View style={{marginTop: 12, display: 'flex', justifyContent: 'space-between', flexDirection: 'row'}}>
124132
<View>
125133
<Text>{`联系人${i + 1}`}</Text>
126134
</View>
127-
<View>
135+
<Flex>
136+
<Text style={{marginRight: 5}} onPress={() => moveUp()}>
137+
上移
138+
</Text>
139+
<Text style={{marginRight: 5}} onPress={() => moveDown()}>
140+
下移
141+
</Text>
142+
<Text style={{marginRight: 5}} onPress={() => moveToTop()}>
143+
置顶
144+
</Text>
145+
<Text style={{marginRight: 5}} onPress={() => moveToBottom()}>
146+
置底
147+
</Text>
128148
<Text onPress={() => remove()}>删除</Text>
129-
</View>
149+
</Flex>
130150
</View>
131151
),
132152
renderAdd: ({add}: {add: () => void}) => (

packages/core/src/Form/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,37 +151,50 @@ const FormDemo = () => {
151151
<!--DemoStart-->
152152
```jsx
153153
import { SafeAreaView,View,Text } from 'react-native';
154-
import { Form,Button } from '@uiw/react-native';
154+
import { Form,Button,Flex } from '@uiw/react-native';
155155

156156
const FormDemo = () => {
157157
const form = Form.useForm({
158158
changeValidate: true,
159159
});
160160
const initialValues = {name: ''};
161161
const items = [
162-
{
162+
{
163163
type: 'cardList',
164164
field: 'cardList',
165165
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>
166+
renderHeader: (i, { remove ,moveUp,moveDown,moveToTop,moveToBottom }) => (
167+
<View style={{marginTop: 12, display: 'flex', justifyContent: 'space-between', flexDirection: 'row'}}>
168+
<View>
169+
<Text>{`联系人${i + 1}`}</Text>
170+
</View>
171+
<Flex>
172+
<Text style={{marginRight:5}} onPress={() => moveUp()}>上移</Text>
173+
<Text style={{marginRight:5}} onPress={() => moveDown()}>下移</Text>
174+
<Text style={{marginRight:5}} onPress={() => moveToTop()}>置顶</Text>
175+
<Text style={{marginRight:5}} onPress={() => moveToBottom()}>置底</Text>
176+
<Text onPress={() => remove()}>删除</Text>
177+
</Flex>
170178
</View>
171179
),
172-
renderAdd: ({ add }: { add: () => void }) => (
173-
<View style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', marginTop: 12 }}>
174-
<Button onPress={() => add()} type="primary" size="default">
180+
renderAdd: ({add}: {add: () => void}) => (
181+
<View style={{marginTop: 12}}>
182+
<Button onPress={() => add()} type="primary" size="default" bordered={false}>
175183
新增数据
176184
</Button>
177185
</View>
178186
),
179187
items: [
180188
{
181189
type: 'input',
182-
field: 'cardListItem1',
190+
field: 'name',
183191
name: '联系人姓名',
184192
},
193+
{
194+
type: 'input',
195+
field: 'phone',
196+
name: '联系人电话',
197+
},
185198
],
186199
},
187200
];

packages/core/src/Form/formList.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { useContext } from 'react';
22
import { KeyType, FormItemsProps } from './types';
33
import { isObjectEmpty } from './utils/is';
44
import { Context } from './hooks/context';
5-
import Button from '../Button';
65
import Card from '../Card';
76
import Label from './comps/label';
87
import Tip from './comps/tip';
@@ -26,10 +25,38 @@ const FormList = ({
2625

2726
const { field, items = [], renderAdd, renderHeader } = formListValue;
2827

29-
const handleOperate = (type = '', index?: number) => {
28+
const handleOperate = (type = '', index: number) => {
3029
let list = store[field] || [];
3130
if (type === 'add') list.push({});
3231
if (type === 'delete') list.splice(index, 1);
32+
// 下移
33+
if (type === 'moveDown') {
34+
if (index !== list.length - 1) {
35+
list[index] = list.splice(index + 1, 1, list[index])[0];
36+
} else {
37+
list.unshift(list.splice(index, 1)[0]);
38+
}
39+
}
40+
// 上移
41+
if (type === 'moveUp') {
42+
if (index !== 0) {
43+
list[index] = list.splice(index - 1, 1, list[index])[0];
44+
} else {
45+
list.push(list.shift());
46+
}
47+
}
48+
// 置顶
49+
if (type === 'moveToTop') {
50+
if (index !== 0) {
51+
list.unshift(list.splice(index, 1)[0]);
52+
}
53+
}
54+
// 置底
55+
if (type === 'moveToBottom') {
56+
if (index !== list.length - 1) {
57+
list.push(list.splice(index, 1)[0]);
58+
}
59+
}
3360
updateStore?.({ store: { ...store, [field]: list } });
3461
};
3562

@@ -84,11 +111,17 @@ const FormList = ({
84111
<SafeAreaView style={styles.warpper}>
85112
{(store[field] || []).map((item: Record<string, unknown>, index: number) => (
86113
<React.Fragment key={index}>
87-
{renderHeader?.(index, { remove: () => handleOperate('delete', index) })}
114+
{renderHeader?.(index, {
115+
remove: () => handleOperate('delete', index),
116+
moveUp: () => handleOperate('moveUp', index),
117+
moveDown: () => handleOperate('moveDown', index),
118+
moveToTop: () => handleOperate('moveToTop', index),
119+
moveToBottom: () => handleOperate('moveToBottom', index),
120+
})}
88121
<Card>{_render(index)}</Card>
89122
</React.Fragment>
90123
))}
91-
{renderAdd?.({ add: () => handleOperate('add') })}
124+
{renderAdd?.({ add: () => handleOperate('add', 0) })}
92125
</SafeAreaView>
93126
);
94127
};

packages/core/src/Form/types/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ interface FormProps<FormData = any, FieldValue = FormData[keyof FormData], Field
3333
mode?: 'default' | 'card';
3434
}
3535

36+
interface actionProps {
37+
remove: () => void;
38+
moveUp: () => void;
39+
moveDown: () => void;
40+
moveToTop: () => void;
41+
moveToBottom: () => void;
42+
}
43+
3644
interface FormItemsProps {
3745
field: string;
3846
type: string;
@@ -41,7 +49,7 @@ interface FormItemsProps {
4149
options?: Array<{ label: string; value: KeyType | any }>;
4250
attr?: any;
4351
required?: boolean;
44-
renderHeader?: (index: number, { remove }: { remove: () => void }) => React.ReactNode;
52+
renderHeader?: (index: number, { remove, moveUp, moveDown }: actionProps) => React.ReactNode;
4553
renderAdd?: ({ add }: { add: () => void }) => React.ReactNode;
4654
items?: Omit<FormItemsProps, 'validate' | 'required'>[];
4755
hide?: boolean;

0 commit comments

Comments
 (0)