-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.tsx
356 lines (340 loc) · 8.04 KB
/
index.tsx
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* WordPress dependencies
*/
import {
Button,
CheckboxControl,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { __, sprintf, _n } from '@wordpress/i18n';
import { useMemo, useState, useRef, useContext } from '@wordpress/element';
import { useRegistry } from '@wordpress/data';
import { closeSmall } from '@wordpress/icons';
/**
* Internal dependencies
*/
import DataViewsContext from '../dataviews-context';
import { ActionWithModal } from '../dataviews-item-actions';
import type { Action } from '../../types';
import type { SetSelection } from '../../private-types';
import type { ActionTriggerProps } from '../dataviews-item-actions';
export function useHasAPossibleBulkAction< Item >(
actions: Action< Item >[],
item: Item
) {
return useMemo( () => {
return actions.some( ( action ) => {
return (
action.supportsBulk &&
( ! action.isEligible || action.isEligible( item ) )
);
} );
}, [ actions, item ] );
}
export function useSomeItemHasAPossibleBulkAction< Item >(
actions: Action< Item >[],
data: Item[]
) {
return useMemo( () => {
return data.some( ( item ) => {
return actions.some( ( action ) => {
return (
action.supportsBulk &&
( ! action.isEligible || action.isEligible( item ) )
);
} );
} );
}, [ actions, data ] );
}
interface BulkSelectionCheckboxProps< Item > {
selection: string[];
onChangeSelection: SetSelection;
data: Item[];
actions: Action< Item >[];
getItemId: ( item: Item ) => string;
}
export function BulkSelectionCheckbox< Item >( {
selection,
onChangeSelection,
data,
actions,
getItemId,
}: BulkSelectionCheckboxProps< Item > ) {
const selectableItems = useMemo( () => {
return data.filter( ( item ) => {
return actions.some(
( action ) =>
action.supportsBulk &&
( ! action.isEligible || action.isEligible( item ) )
);
} );
}, [ data, actions ] );
const selectedItems = data.filter(
( item ) =>
selection.includes( getItemId( item ) ) &&
selectableItems.includes( item )
);
const areAllSelected = selectedItems.length === selectableItems.length;
return (
<CheckboxControl
className="dataviews-view-table-selection-checkbox"
__nextHasNoMarginBottom
checked={ areAllSelected }
indeterminate={ ! areAllSelected && !! selectedItems.length }
onChange={ () => {
if ( areAllSelected ) {
onChangeSelection( [] );
} else {
onChangeSelection(
selectableItems.map( ( item ) => getItemId( item ) )
);
}
} }
aria-label={
areAllSelected ? __( 'Deselect all' ) : __( 'Select all' )
}
/>
);
}
interface ActionButtonProps< Item > {
action: Action< Item >;
selectedItems: Item[];
actionInProgress: string | null;
setActionInProgress: ( actionId: string | null ) => void;
}
interface ToolbarContentProps< Item > {
selection: string[];
onChangeSelection: SetSelection;
data: Item[];
actions: Action< Item >[];
getItemId: ( item: Item ) => string;
}
function ActionTrigger< Item >( {
action,
onClick,
isBusy,
items,
}: ActionTriggerProps< Item > ) {
const label =
typeof action.label === 'string' ? action.label : action.label( items );
return (
<Button
disabled={ isBusy }
accessibleWhenDisabled
label={ label }
icon={ action.icon }
isDestructive={ action.isDestructive }
size="compact"
onClick={ onClick }
isBusy={ isBusy }
tooltipPosition="top"
/>
);
}
const EMPTY_ARRAY: [] = [];
function ActionButton< Item >( {
action,
selectedItems,
actionInProgress,
setActionInProgress,
}: ActionButtonProps< Item > ) {
const registry = useRegistry();
const selectedEligibleItems = useMemo( () => {
return selectedItems.filter( ( item ) => {
return ! action.isEligible || action.isEligible( item );
} );
}, [ action, selectedItems ] );
if ( 'RenderModal' in action ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ selectedEligibleItems }
ActionTrigger={ ActionTrigger }
/>
);
}
return (
<ActionTrigger
key={ action.id }
action={ action }
onClick={ async () => {
setActionInProgress( action.id );
await action.callback( selectedItems, {
registry,
} );
setActionInProgress( null );
} }
items={ selectedEligibleItems }
isBusy={ actionInProgress === action.id }
/>
);
}
function renderFooterContent< Item >(
data: Item[],
actions: Action< Item >[],
getItemId: ( item: Item ) => string,
selection: string[],
actionsToShow: Action< Item >[],
selectedItems: Item[],
actionInProgress: string | null,
setActionInProgress: ( actionId: string | null ) => void,
onChangeSelection: SetSelection
) {
const message =
selectedItems.length > 0
? sprintf(
/* translators: %d: number of items. */
_n(
'%d Item selected',
'%d Items selected',
selectedItems.length
),
selectedItems.length
)
: sprintf(
/* translators: %d: number of items. */
_n( '%d Item', '%d Items', data.length ),
data.length
);
return (
<HStack
expanded={ false }
className="dataviews-bulk-actions-footer__container"
spacing={ 3 }
>
<BulkSelectionCheckbox
selection={ selection }
onChangeSelection={ onChangeSelection }
data={ data }
actions={ actions }
getItemId={ getItemId }
/>
<span className="dataviews-bulk-actions-footer__item-count">
{ message }
</span>
<HStack
className="dataviews-bulk-actions-footer__action-buttons"
expanded={ false }
spacing={ 1 }
>
{ actionsToShow.map( ( action ) => {
return (
<ActionButton
key={ action.id }
action={ action }
selectedItems={ selectedItems }
actionInProgress={ actionInProgress }
setActionInProgress={ setActionInProgress }
/>
);
} ) }
{ selectedItems.length > 0 && (
<Button
icon={ closeSmall }
showTooltip
tooltipPosition="top"
size="compact"
label={ __( 'Cancel' ) }
disabled={ !! actionInProgress }
accessibleWhenDisabled={ false }
onClick={ () => {
onChangeSelection( EMPTY_ARRAY );
} }
/>
) }
</HStack>
</HStack>
);
}
function FooterContent< Item >( {
selection,
actions,
onChangeSelection,
data,
getItemId,
}: ToolbarContentProps< Item > ) {
const [ actionInProgress, setActionInProgress ] = useState< string | null >(
null
);
const footerContentRef = useRef< JSX.Element | null >( null );
const bulkActions = useMemo(
() => actions.filter( ( action ) => action.supportsBulk ),
[ actions ]
);
const selectableItems = useMemo( () => {
return data.filter( ( item ) => {
return bulkActions.some(
( action ) => ! action.isEligible || action.isEligible( item )
);
} );
}, [ data, bulkActions ] );
const selectedItems = useMemo( () => {
return data.filter(
( item ) =>
selection.includes( getItemId( item ) ) &&
selectableItems.includes( item )
);
}, [ selection, data, getItemId, selectableItems ] );
const actionsToShow = useMemo(
() =>
actions.filter( ( action ) => {
return (
action.supportsBulk &&
action.icon &&
selectedItems.some(
( item ) =>
! action.isEligible || action.isEligible( item )
)
);
} ),
[ actions, selectedItems ]
);
if ( ! actionInProgress ) {
if ( footerContentRef.current ) {
footerContentRef.current = null;
}
return renderFooterContent(
data,
actions,
getItemId,
selection,
actionsToShow,
selectedItems,
actionInProgress,
setActionInProgress,
onChangeSelection
);
} else if ( ! footerContentRef.current ) {
footerContentRef.current = renderFooterContent(
data,
actions,
getItemId,
selection,
actionsToShow,
selectedItems,
actionInProgress,
setActionInProgress,
onChangeSelection
);
}
return footerContentRef.current;
}
export function BulkActionsFooter() {
const {
data,
selection,
actions = EMPTY_ARRAY,
onChangeSelection,
getItemId,
} = useContext( DataViewsContext );
return (
<FooterContent
selection={ selection }
onChangeSelection={ onChangeSelection }
data={ data }
actions={ actions }
getItemId={ getItemId }
/>
);
}