PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Views / Components / ListTable / BulkActions / BulkActionCheckbox.tsx

BulkActionCheckbox.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Views/Components/ListTable/BulkActions/BulkActionCheckbox.tsx

89 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {__, sprintf} from '@wordpress/i18n';
2 import {useContext, useEffect, useState} from 'react';
3 import {CheckboxContext} from '@givewp/components/ListTable/ListTablePage';
4 import styles from './BulkActionCheckbox.module.scss';
5
6 export const BulkActionCheckbox = ({id, name, singleName}) => {
7 const checkboxRefs = useContext<CheckboxContextProps>(CheckboxContext);
8 // add this element's ref to the list of checkboxes, so we can access them imperatively
9 const updateCheckboxRefs = (node: HTMLInputElement) => {
10 if (node !== null && !checkboxRefs?.current.includes(node)) {
11 checkboxRefs?.current.push(node);
12 }
13 };
14
15 useEffect(() => {
16 // cleanup function to remove the ref checked value when the component unmounts
17 return () => {
18 checkboxRefs.current.forEach((checkbox) => {
19 checkbox.checked = false;
20 });
21 checkboxRefs.current = checkboxRefs.current.filter((checkbox) => checkbox.dataset.id === id);
22 };
23 }, []);
24 return (
25 <>
26 <label
27 htmlFor={`giveListTableSelect${id}`}
28 id={`giveListTableSelect${id}-Label`}
29 className="give-visually-hidden"
30 >
31 {sprintf(__('Select %1s %2s', 'give'), singleName, id)}
32 </label>
33 <input
34 ref={updateCheckboxRefs}
35 className="giveListTableSelect"
36 data-id={id}
37 data-name={name ? name : null}
38 id={`giveListTableSelect${id}`}
39 aria-labelledby={`giveListTableSelect${id}-Label`}
40 type="checkbox"
41 />
42 </>
43 );
44 };
45
46 export const BulkActionCheckboxAll = ({pluralName, data}) => {
47 const checkboxRefs = useContext<CheckboxContextProps>(CheckboxContext);
48 const [checked, setChecked] = useState(false);
49 // reset the 'Select all' checkbox when table contents change
50 useEffect(() => {
51 setChecked(false);
52 }, [data]);
53 return (
54 <>
55 <label htmlFor="giveListTableSelectAll" id="giveListTableSelectAll-Label" className="give-visually-hidden">
56 {sprintf(__('Select all %s', 'give'), pluralName)}
57 </label>
58 <input
59 id="giveListTableSelectAll"
60 type="checkbox"
61 className={styles.checkbox}
62 aria-labelledby="giveListTableSelectAll-Label"
63 onChange={(event) => toggleAllRowCheckboxes(event, checkboxRefs, setChecked, checked)}
64 checked={checked}
65 />
66 </>
67 );
68 };
69
70 const toggleAllRowCheckboxes: ToggleAllRowCheckboxesProps = (_event, checkboxRefs, setChecked, checked) => {
71 checkboxRefs.current.forEach((checkbox) => {
72 checkbox.checked = !checked;
73 });
74 setChecked(!checked);
75 };
76
77 type CheckboxContextProps = {
78 current: HTMLInputElement[];
79 };
80
81 interface ToggleAllRowCheckboxesProps {
82 (
83 event: React.ChangeEvent<HTMLInputElement>,
84 checkboxRefs: CheckboxContextProps,
85 setChecked: React.Dispatch<React.SetStateAction<boolean>>,
86 checked: boolean
87 ): void;
88 }
89