-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathToolbar.js
More file actions
152 lines (142 loc) · 5.02 KB
/
Toolbar.js
File metadata and controls
152 lines (142 loc) · 5.02 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 * as React from 'react';
import { styled } from '@mui/material/styles';
import {
GridClearIcon,
GridDeleteIcon,
gridRowSelectionCountSelector,
gridRowSelectionIdsSelector,
useGridApiContext,
useGridSelector,
Toolbar as ToolbarRoot,
QuickFilter,
QuickFilterControl,
QuickFilterClear,
ToolbarButton,
QuickFilterTrigger,
} from '@mui/x-data-grid-premium';
import Typography from '@mui/material/Typography';
import TextField from '@mui/material/TextField';
import InputAdornment from '@mui/material/InputAdornment';
import SearchIcon from '@mui/icons-material/Search';
import CancelIcon from '@mui/icons-material/Cancel';
import { ToolbarAddItem } from './ToolbarAddItem';
import { ToolbarColumnsItem } from './ToolbarColumnsItem';
import { ToolbarSortItem } from './ToolbarSortItem';
import { ToolbarDensityItem } from './ToolbarDensityItem';
import { ToolbarFilterItem } from './ToolbarFilterItem';
const StyledQuickFilter = styled(QuickFilter)({
display: 'grid',
alignItems: 'center',
});
const StyledToolbarButton = styled(ToolbarButton)(({ theme, ownerState }) => ({
gridArea: '1 / 1',
width: 'min-content',
height: 'min-content',
zIndex: 1,
opacity: ownerState.expanded ? 0 : 1,
pointerEvents: ownerState.expanded ? 'none' : 'auto',
transition: theme.transitions.create(['opacity']),
}));
const StyledTextField = styled(TextField)(({ theme, ownerState }) => ({
gridArea: '1 / 1',
overflowX: 'clip',
width: ownerState.expanded ? 180 : 'var(--trigger-width)',
opacity: ownerState.expanded ? 1 : 0,
transition: theme.transitions.create(['width', 'opacity']),
}));
export function Toolbar(props) {
const { listView = false, container, handleUpload, handleDelete } = props;
const apiRef = useGridApiContext();
const selectionCount = useGridSelector(apiRef, gridRowSelectionCountSelector);
const showSelectionOptions = selectionCount > 0;
const handleClearSelection = () => {
apiRef.current.setRowSelectionModel({ type: 'include', ids: new Set() });
};
const handleDeleteSelectedRows = () => {
handleClearSelection();
const selectedRows = gridRowSelectionIdsSelector(apiRef);
handleDelete?.(Array.from(selectedRows.keys()));
};
const itemProps = {
listView,
container,
};
return (
<ToolbarRoot>
{showSelectionOptions ? (
<React.Fragment>
<ToolbarButton
material={{ sx: { mr: 0.5 } }}
onClick={handleClearSelection}
>
<GridClearIcon fontSize="small" />
</ToolbarButton>
<Typography variant="body2">{selectionCount} selected</Typography>
<ToolbarButton
material={{ sx: { mr: 'auto' } }}
onClick={handleDeleteSelectedRows}
>
<GridDeleteIcon fontSize="small" />
</ToolbarButton>
</React.Fragment>
) : (
<React.Fragment>
<ToolbarColumnsItem {...itemProps} />
<ToolbarFilterItem {...itemProps} />
<ToolbarSortItem {...itemProps} />
<ToolbarDensityItem {...itemProps} />
<ToolbarAddItem {...itemProps} handleUpload={handleUpload} />
<StyledQuickFilter>
<QuickFilterTrigger
render={(triggerProps, state) => (
<StyledToolbarButton
{...triggerProps}
ownerState={{ expanded: state.expanded }}
color="default"
aria-disabled={state.expanded}
>
<SearchIcon fontSize="small" />
</StyledToolbarButton>
)}
/>
<QuickFilterControl
render={({ ref, ...controlProps }, state) => (
<StyledTextField
{...controlProps}
ownerState={{ expanded: state.expanded }}
inputRef={ref}
aria-label="Search"
placeholder="Search…"
size="small"
slotProps={{
input: {
startAdornment: (
<InputAdornment position="start">
<SearchIcon fontSize="small" />
</InputAdornment>
),
endAdornment: state.value ? (
<InputAdornment position="end">
<QuickFilterClear
edge="end"
size="small"
aria-label="Clear search"
material={{ sx: { marginRight: -0.75 } }}
>
<CancelIcon fontSize="small" />
</QuickFilterClear>
</InputAdornment>
) : null,
...controlProps.slotProps?.input,
},
...controlProps.slotProps,
}}
/>
)}
/>
</StyledQuickFilter>
</React.Fragment>
)}
</ToolbarRoot>
);
}