ColumnConfigModal.tsx
3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { useEffect, useState } from 'react';
import { ArrowDownOutlined, ArrowUpOutlined } from '@ant-design/icons';
import { UAlert, UButton, UCheckbox, UModal, UText } from '@uj/ui';
import { COLUMN_LABELS, DEFAULT_COLUMNS, errorText, type ListingColumn } from '../tools/listing';
interface Props {
open: boolean;
columns: ListingColumn[];
loading: boolean;
error: unknown;
saving: boolean;
saveError: unknown;
onClose: () => void;
onRetry: () => void;
onSave: (columns: ListingColumn[]) => void;
}
export function ColumnConfigModal(props: Props) {
const [order, setOrder] = useState<ListingColumn[]>(DEFAULT_COLUMNS);
const [selected, setSelected] = useState<ListingColumn[]>(props.columns);
useEffect(() => {
if (props.open) {
setOrder([
...props.columns,
...DEFAULT_COLUMNS.filter((column) => !props.columns.includes(column)),
]);
setSelected(props.columns);
}
}, [props.open, props.columns]);
function move(index: number, offset: number) {
setOrder((current) => {
const next = [...current];
[next[index], next[index + offset]] = [next[index + offset], next[index]];
return next;
});
}
return (
<UModal
title="列配置"
open={props.open}
onCancel={props.saving ? undefined : props.onClose}
okText="保存"
cancelText="取消"
confirmLoading={props.saving}
okButtonProps={{ disabled: props.loading || Boolean(props.error) || props.saving }}
cancelButtonProps={{ disabled: props.saving }}
onOk={() => props.onSave(order.filter((column) => selected.includes(column)))}
>
<UText type="secondary">门店名称和操作列始终显示。勾选显示列,使用箭头调整顺序。</UText>
{props.error ? (
<UAlert
type="error"
showIcon
title={errorText(props.error, '列配置加载失败')}
action={
<UButton type="link" onClick={props.onRetry} loading={props.loading}>
重新加载列配置
</UButton>
}
/>
) : null}
{props.loading && <p role="status">正在加载列配置…</p>}
<div className="store-column-options">
{order.map((column, index) => (
<div className="store-column-option" key={column}>
<UCheckbox
checked={selected.includes(column)}
disabled={
column === 'storeName' || props.loading || Boolean(props.error) || props.saving
}
onChange={(event) =>
setSelected((current) =>
event.target.checked
? [...current, column]
: current.filter((key) => key !== column),
)
}
>
{COLUMN_LABELS[column]}
</UCheckbox>
<div>
<UButton
type="text"
icon={<ArrowUpOutlined />}
aria-label={`上移${COLUMN_LABELS[column]}`}
disabled={index < 2 || props.loading || Boolean(props.error) || props.saving}
onClick={() => move(index, -1)}
/>
<UButton
type="text"
icon={<ArrowDownOutlined />}
aria-label={`下移${COLUMN_LABELS[column]}`}
disabled={
index === 0 ||
index === order.length - 1 ||
props.loading ||
Boolean(props.error) ||
props.saving
}
onClick={() => move(index, 1)}
/>
</div>
</div>
))}
</div>
{props.saveError ? (
<UAlert
type="error"
showIcon
title={errorText(props.saveError, '列配置保存失败,请重试')}
/>
) : null}
</UModal>
);
}