BulkActionCheckbox.module.scss
8 months ago
BulkActionCheckbox.tsx
9 months ago
BulkActionSelect.module.scss
1 year ago
BulkActionSelect.tsx
9 months ago
BulkActionCheckbox.tsx
89 lines
| 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 |