-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathMuiColumnPickerDialog.tsx
More file actions
152 lines (138 loc) · 4.12 KB
/
MuiColumnPickerDialog.tsx
File metadata and controls
152 lines (138 loc) · 4.12 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { useEffect, useState } from 'react';
import { defineMessages } from 'react-intl';
import {
Alert,
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
} from '@mui/material';
import useTranslation from 'lib/hooks/useTranslation';
import { ColumnPickerTemplate } from '../builder';
const translations = defineMessages({
defaultTitle: {
id: 'lib.components.table.MuiColumnPickerDialog.defaultTitle',
defaultMessage: 'Select columns',
},
apply: {
id: 'lib.components.table.MuiColumnPickerDialog.apply',
defaultMessage: 'Apply to view',
},
cancel: {
id: 'lib.components.table.MuiColumnPickerDialog.cancel',
defaultMessage: 'Cancel',
},
defaultExport: {
id: 'lib.components.table.MuiColumnPickerDialog.export',
defaultMessage: 'Apply and Export',
},
});
interface MuiColumnPickerDialogProps {
open: boolean;
onClose: () => void;
initialVisibility: Record<string, boolean>;
locked?: string[];
columnPicker: ColumnPickerTemplate;
commitColumnVisibility: (next: Record<string, boolean>) => void;
onExportFromPicker?: (visibility: Record<string, boolean>) => void;
}
const enforceLockedLocal = (
next: Record<string, boolean>,
locked: string[] | undefined,
): Record<string, boolean> => {
if (!locked || locked.length === 0) return next;
const enforced = { ...next };
locked.forEach((id) => {
enforced[id] = true;
});
return enforced;
};
const MuiColumnPickerDialog = ({
open,
onClose,
initialVisibility,
locked,
columnPicker,
commitColumnVisibility,
onExportFromPicker,
}: MuiColumnPickerDialogProps): JSX.Element => {
const { t } = useTranslation();
const [staged, setStaged] = useState<Record<string, boolean>>(() =>
enforceLockedLocal({ ...initialVisibility }, locked),
);
const dataColumnIds = columnPicker.dataColumnIds;
const hasDataColumns =
!dataColumnIds ||
dataColumnIds.length === 0 ||
dataColumnIds.some((id) => staged[id]);
useEffect(() => {
if (open) {
setStaged(enforceLockedLocal({ ...initialVisibility }, locked));
}
}, [open, initialVisibility, locked]);
const ctx = {
isVisible: (id: string): boolean => staged[id] ?? false,
setVisible: (id: string, v: boolean): void => {
if (locked?.includes(id)) return;
setStaged((prev) =>
Object.hasOwn(prev, id) ? { ...prev, [id]: v } : prev,
);
},
setManyVisible: (ids: string[], v: boolean): void => {
setStaged((prev) => {
const next = { ...prev };
let changed = false;
ids.forEach((id) => {
if (!Object.hasOwn(next, id)) return;
if (locked?.includes(id)) return;
if (next[id] !== v) {
next[id] = v;
changed = true;
}
});
return changed ? next : prev;
});
},
};
const commitAndClose = (): void => {
commitColumnVisibility(enforceLockedLocal(staged, locked));
onClose();
};
const cancelAndClose = (): void => {
onClose();
};
const exportAndClose = (): void => {
const enforced = enforceLockedLocal(staged, locked);
commitColumnVisibility(enforced);
onExportFromPicker?.(enforced);
onClose();
};
return (
<Dialog
aria-labelledby="column-picker-dialog-title"
fullWidth
maxWidth="sm"
onClose={cancelAndClose}
open={open}
>
<DialogTitle id="column-picker-dialog-title">
{columnPicker.dialogTitle ?? t(translations.defaultTitle)}
</DialogTitle>
<DialogContent dividers>{columnPicker.renderTree(ctx)}</DialogContent>
{!hasDataColumns && columnPicker.noDataColumnsHint && (
<Alert severity="info" sx={{ borderRadius: 0 }}>
{columnPicker.noDataColumnsHint}
</Alert>
)}
<DialogActions>
<Button onClick={cancelAndClose}>{t(translations.cancel)}</Button>
<Button onClick={commitAndClose}>{t(translations.apply)}</Button>
<Button color="primary" onClick={exportAndClose} variant="contained">
{columnPicker.exportLabel ?? t(translations.defaultExport)}
</Button>
</DialogActions>
</Dialog>
);
};
export default MuiColumnPickerDialog;