hooks
4 years ago
images
4 years ago
BulkActionCheckbox.tsx
4 years ago
BulkActionSelect.module.scss
4 years ago
BulkActionSelect.tsx
4 years ago
Filters.tsx
4 years ago
FormSelect.module.scss
4 years ago
FormSelect.tsx
4 years ago
Input.module.scss
4 years ago
Input.tsx
4 years ago
ListTable.module.scss
4 years ago
ListTable.tsx
4 years ago
ListTablePage.module.scss
3 years ago
ListTableRows.module.scss
3 years ago
ListTableRows.tsx
4 years ago
Pagination.module.scss
4 years ago
Pagination.tsx
4 years ago
README.MD
4 years ago
RowAction.module.scss
4 years ago
RowAction.tsx
4 years ago
Select.module.scss
4 years ago
Select.tsx
4 years ago
TableCell.module.scss
4 years ago
TableCell.tsx
4 years ago
TestLabel.module.scss
4 years ago
TestLabel.tsx
4 years ago
TypeBadge.module.scss
4 years ago
TypeBadge.tsx
4 years ago
api.ts
4 years ago
index.tsx
4 years ago
BulkActionCheckbox.tsx
72 lines
| 1 | import {__, sprintf} from "@wordpress/i18n"; |
| 2 | import {useCallback, useContext, useEffect, useState} from "react"; |
| 3 | import {CheckboxContext} from "@givewp/components/ListTable/index"; |
| 4 | |
| 5 | export const BulkActionCheckbox = ({id, name, singleName}) => { |
| 6 | const checkboxRefs = useContext(CheckboxContext); |
| 7 | // add this element's ref to the list of checkboxes so we can access them imperatively |
| 8 | const updateCheckboxRefs = useCallback(node => { |
| 9 | if (node !== null) { |
| 10 | checkboxRefs?.current.push(node); |
| 11 | } |
| 12 | }, []); |
| 13 | |
| 14 | useEffect(() => { |
| 15 | // cleanup function to remove the ref when the component unmounts |
| 16 | return () => { |
| 17 | checkboxRefs.current = checkboxRefs.current.filter(checkbox => ( |
| 18 | checkbox.dataset.id === id |
| 19 | )); |
| 20 | }; |
| 21 | }, []); |
| 22 | |
| 23 | return ( |
| 24 | <> |
| 25 | <label htmlFor={`giveListTableSelect${id}`} id={`giveListTableSelect${id}-Label`} className='give-visually-hidden'> |
| 26 | {sprintf(__('Select %1s %2s', 'give'), singleName, id)} |
| 27 | </label> |
| 28 | <input |
| 29 | ref={updateCheckboxRefs} |
| 30 | className='giveListTableSelect' |
| 31 | data-id={id} |
| 32 | data-name={name ? name : null} |
| 33 | id={`giveListTableSelect${id}`} |
| 34 | aria-labelledby={`giveListTableSelect${id}-Label`} |
| 35 | type='checkbox' |
| 36 | /> |
| 37 | </> |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | export const BulkActionCheckboxAll = ({pluralName, data}) => { |
| 42 | const checkboxRefs = useContext(CheckboxContext); |
| 43 | const [checked, setChecked] = useState(false); |
| 44 | // reset the 'Select all' checkbox when table contents change |
| 45 | useEffect(() => { |
| 46 | setChecked(false); |
| 47 | }, [data]); |
| 48 | return ( |
| 49 | <> |
| 50 | <label htmlFor='giveListTableSelectAll' id='giveListTableSelectAll-Label' |
| 51 | className='give-visually-hidden' |
| 52 | > |
| 53 | {sprintf(__('Select all %s', 'give'), pluralName)} |
| 54 | </label> |
| 55 | <input id='giveListTableSelectAll' |
| 56 | type='checkbox' |
| 57 | className='giveListTableSelect' |
| 58 | aria-labelledby='giveListTableSelectAll-Label' |
| 59 | onChange={(event) => toggleAllRowCheckboxes(event, checkboxRefs, setChecked, checked)} |
| 60 | checked={checked} |
| 61 | /> |
| 62 | </> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | const toggleAllRowCheckboxes = (event, checkboxRefs, setChecked, checked) => { |
| 67 | checkboxRefs.current.forEach((checkbox) => { |
| 68 | checkbox.checked = !checked; |
| 69 | }); |
| 70 | setChecked(!checked); |
| 71 | } |
| 72 |