ColumnConfigModal.tsx
2.58 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
import { useEffect, useState } from 'react';
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 [selected, setSelected] = useState<ListingColumn[]>(props.columns);
useEffect(() => {
if (props.open) {
setSelected(props.columns);
}
}, [props.open, props.columns]);
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(DEFAULT_COLUMNS.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">
{DEFAULT_COLUMNS.map((column) => (
<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>
))}
</div>
{props.saveError ? (
<UAlert
type="error"
showIcon
title={errorText(props.saveError, '列配置保存失败,请重试')}
/>
) : null}
</UModal>
);
}