Skip to content

Commit a21b416

Browse files
author
echoyl
committed
feat(ProFormList):增加箭头上下排序功能
1 parent ff3cfc5 commit a21b416

File tree

4 files changed

+165
-6
lines changed

4 files changed

+165
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
ProForm,
3+
ProFormGroup,
4+
ProFormList,
5+
ProFormText,
6+
} from '@ant-design/pro-components';
7+
8+
const Demo = () => {
9+
return (
10+
<div
11+
style={{
12+
display: 'flex',
13+
flexDirection: 'column',
14+
gap: 16,
15+
}}
16+
>
17+
<ProForm onFinish={async (e) => console.log(e)}>
18+
<ProFormText name="name" label="姓名" />
19+
20+
<ProFormList
21+
name="labels"
22+
label="用户信息"
23+
initialValue={[
24+
{
25+
value: '111',
26+
label: '111',
27+
},
28+
{
29+
value: '222',
30+
label: '222',
31+
},
32+
{
33+
value: '333',
34+
label: '333',
35+
},
36+
]}
37+
arrowSort={true}
38+
upIconProps={{ tooltipText: null }}
39+
>
40+
<ProFormGroup key="group">
41+
<ProFormText name="value" label="值" />
42+
<ProFormText name="label" label="显示名称" />
43+
</ProFormGroup>
44+
</ProFormList>
45+
</ProForm>
46+
</div>
47+
);
48+
};
49+
50+
export default Demo;

packages/form/src/components/Group/index.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ ProFormList 与 [Form.List](https://ant.design/components/form-cn/#Form.List) AP
7070
| actionRender | 自定义操作按钮 | `(field,action,defaultActionDom,count)=>React.ReactNode[]` | - |
7171
| onAfterAdd | 新增数据后的钩子 | `(defaultValue: StoreValue, insertIndex: number, count: number) => void` | - |
7272
| onAfterRemove | 删除数据后的钩子 | `(index: number, count: number) => void` | - |
73+
| arrowSort | 是否开启箭头按钮排序 | `boolean` | - |
74+
| upIconProps | 向上排序按钮的配置,false 可以取消 | `{ Icon?: React.FC<any>; tooltipText?: string; } \| false` | - |
75+
| downIconProps | 向下排序按钮的配置,false 可以取消 | `{ Icon?: React.FC<any>; tooltipText?: string; } \| false` | - |
7376

7477
### actionRef 操作项目实例
7578

@@ -362,12 +365,16 @@ name 参数必须要是一个数组,如果是嵌套的结构可以这样配置
362365

363366
### 行为守卫
364367

365-
<code src="./demos/pro-form-list.tsx" title="行为守卫"></code>
368+
<code src="./demos/pro-form-list.tsx" ></code>
366369

367370
### 增删条目限制
368371

369-
<code src="./demos/countLimit.tsx" title="增删条目限制"></code>
372+
<code src="./demos/countLimit.tsx" ></code>
370373

371374
### 横向布局
372375

373-
<code src="./demos/horizontal-layout.tsx" title="横向布局" ></code>
376+
<code src="./demos/horizontal-layout.tsx" ></code>
377+
378+
### 箭头排序
379+
380+
<code src="./demos/list-arrowsort.tsx" ></code>

packages/form/src/components/List/ListItem.tsx

+92-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
CopyOutlined,
33
DeleteOutlined,
44
LoadingOutlined,
5+
ArrowUpOutlined,
6+
ArrowDownOutlined
57
} from '@ant-design/icons';
68
import { ProProvider } from '@ant-design/pro-provider';
79
import { SearchTransformKeyFn } from '@ant-design/pro-utils';
@@ -158,6 +160,24 @@ export type ProFromListCommonProps = {
158160
* @type {IconConfig|false}
159161
*/
160162
deleteIconProps?: IconConfig | false;
163+
/**
164+
* @name 向上排序按钮的配置
165+
* @description 可以自定义向上排序按钮的文案,图标,tooltip,设置为 false 就会消失
166+
* @type {IconConfig|false}
167+
*/
168+
upIconProps?: IconConfig | false;
169+
/**
170+
* @name 向下排序按钮的配置
171+
* @description 可以自定义向下排序按钮的文案,图标,tooltip,设置为 false 就会消失
172+
* @type {IconConfig|false}
173+
*/
174+
downIconProps?: IconConfig | false;
175+
/**
176+
* @name 箭头排序是否开启开关
177+
* @description 是否开启箭头排序 默认关闭
178+
* @type {boolean}
179+
*/
180+
arrowSort?: boolean;
161181

162182
/**
163183
* @name 新建增加的默认数据
@@ -294,6 +314,9 @@ const ProFormListItem: React.FC<
294314
creatorButtonProps,
295315
deleteIconProps,
296316
copyIconProps,
317+
arrowSort,
318+
upIconProps,
319+
downIconProps,
297320
itemContainerRender,
298321
itemRender,
299322
alwaysShowItemLabel,
@@ -458,13 +481,80 @@ const ProFormListItem: React.FC<
458481
action,
459482
field.name,
460483
]);
484+
const upIcon = useMemo(() => {
485+
if(!arrowSort)
486+
{
487+
return null;
488+
}
489+
if (mode === 'read') return null;
490+
if (upIconProps === false) return null;
491+
const toIndex = index - 1;
492+
if(toIndex < 0)
493+
{
494+
return null;
495+
}
496+
const { Icon = ArrowUpOutlined, tooltipText } = upIconProps!;
497+
return (
498+
<Tooltip title={tooltipText} key="up">
499+
<Icon
500+
className={classNames(
501+
`${prefixCls}-action-icon action-up`,
502+
hashId,
503+
)}
504+
onClick={async () => {
505+
await action.move(index,toIndex);
506+
}}
507+
/>
508+
</Tooltip>
509+
);
510+
}, [
511+
upIconProps,
512+
prefixCls,
513+
hashId,
514+
action,
515+
arrowSort
516+
]);
517+
518+
const downIcon = useMemo(() => {
519+
if(!arrowSort)
520+
{
521+
return null;
522+
}
523+
if (mode === 'read') return null;
524+
if (downIconProps === false) return null;
525+
const toIndex = index + 1;
526+
if(toIndex >= count)
527+
{
528+
return null;
529+
}
530+
const { Icon = ArrowDownOutlined, tooltipText } = downIconProps!;
531+
return (
532+
<Tooltip title={tooltipText} key="down">
533+
<Icon
534+
className={classNames(
535+
`${prefixCls}-action-icon action-down`,
536+
hashId,
537+
)}
538+
onClick={async () => {
539+
await action.move(index,toIndex);
540+
}}
541+
/>
542+
</Tooltip>
543+
);
544+
}, [
545+
upIconProps,
546+
prefixCls,
547+
hashId,
548+
action,
549+
arrowSort
550+
]);
461551

462552
const defaultActionDom: React.ReactNode[] = useMemo(
463553
() =>
464-
[copyIcon, deleteIcon].filter(
554+
[copyIcon, deleteIcon, upIcon, downIcon].filter(
465555
(item) => item !== null && item !== undefined,
466556
),
467-
[copyIcon, deleteIcon],
557+
[copyIcon, deleteIcon, upIcon, downIcon],
468558
);
469559

470560
const actions =

packages/form/src/components/List/index.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CopyOutlined, DeleteOutlined } from '@ant-design/icons';
1+
import { CopyOutlined, DeleteOutlined, ArrowUpOutlined, ArrowDownOutlined } from '@ant-design/icons';
22
import { useIntl } from '@ant-design/pro-provider';
33
import { ProFormContext } from '@ant-design/pro-utils';
44
import type { ColProps } from 'antd';
@@ -150,6 +150,15 @@ function ProFormList<T>(props: ProFormListProps<T>) {
150150
Icon: DeleteOutlined,
151151
tooltipText: intl.getMessage('deleteThisLine', '删除此项'),
152152
},
153+
arrowSort,
154+
upIconProps = {
155+
Icon: ArrowUpOutlined,
156+
tooltipText: intl.getMessage('sortUpThisLine', '向上排序'),
157+
},
158+
downIconProps = {
159+
Icon: ArrowDownOutlined,
160+
tooltipText: intl.getMessage('sortDownThisLine', '向下排序'),
161+
},
153162
actionRef,
154163
style,
155164
prefixCls,
@@ -272,6 +281,9 @@ function ProFormList<T>(props: ProFormListProps<T>) {
272281
originName={rest.name}
273282
copyIconProps={copyIconProps}
274283
deleteIconProps={deleteIconProps}
284+
arrowSort={arrowSort}
285+
upIconProps={upIconProps}
286+
downIconProps={downIconProps}
275287
formInstance={proFormContext.formRef!.current!}
276288
prefixCls={baseClassName}
277289
meta={meta}

0 commit comments

Comments
 (0)