-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsingle-selection-checkbox.js
61 lines (60 loc) · 1.39 KB
/
single-selection-checkbox.js
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
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { CheckboxControl } from '@wordpress/components';
export default function SingleSelectionCheckbox( {
selection,
onSelectionChange,
item,
data,
getItemId,
primaryField,
disabled,
} ) {
const id = getItemId( item );
const isSelected = selection.includes( id );
let selectionLabel;
if ( primaryField?.getValue && item ) {
// eslint-disable-next-line @wordpress/valid-sprintf
selectionLabel = sprintf(
/* translators: %s: item title. */
isSelected ? __( 'Deselect item: %s' ) : __( 'Select item: %s' ),
primaryField.getValue( { item } )
);
} else {
selectionLabel = isSelected
? __( 'Select a new item' )
: __( 'Deselect item' );
}
return (
<CheckboxControl
className="dataviews-view-table-selection-checkbox"
__nextHasNoMarginBottom
checked={ isSelected }
label={ selectionLabel }
disabled={ disabled }
onChange={ () => {
if ( ! isSelected ) {
onSelectionChange(
data.filter( ( _item ) => {
const itemId = getItemId?.( _item );
return (
itemId === id || selection.includes( itemId )
);
} )
);
} else {
onSelectionChange(
data.filter( ( _item ) => {
const itemId = getItemId?.( _item );
return (
itemId !== id && selection.includes( itemId )
);
} )
);
}
} }
/>
);
}