Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 18x 12x 6x 7x 12x 12x 7x 4x 4x 28x 64x 12x 15x 7x 8x 8x 12x 12x 9x 9x 6x 9x 9x 6x 12x 12x 12x 7x 7x 7x 7x 7x 4x 7x 4x 7x 7x 7x 4x 7x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 7x 7x 3x 7x 7x 7x 4x 7x | // @flow strict
import React, { useEffect, useRef, useState } from 'react';
import cx from 'classnames';
import { closest } from '@material/dom/ponyfill';
import { MDCDataTableFoundation } from '@material/data-table/foundation';
import { strings, cssClasses } from '@material/data-table/constants';
import type { Node } from 'react';
import type {
HTMLMTCheckboxElement,
HTMLDataTableRowElement,
} from '../mt-types';
type Props = {
children?: Node,
label?: string,
className?: string,
defaultSelected?: Array<string>,
selected?: Array<string>,
onSelectedAll?: () => void,
onUnselectedAll?: () => void,
onRowSelectionChanged?: ({
row: HTMLTableRowElement,
rowId: string,
rowIndex: number,
selected: boolean,
}) => void,
};
const noOp = () => {};
const DataTable = ({
children,
label,
className,
defaultSelected: propDefaultSelected,
selected: propSelected,
onSelectedAll = noOp,
onUnselectedAll = noOp,
onRowSelectionChanged = noOp,
...rest
}: Props) => {
const rootRef = useRef<?HTMLDivElement>();
const foundationRef = useRef<?MDCDataTableFoundation>();
const [selected, setSelected] = useState<?Array<string>>(propSelected);
const [defaultSelected] = useState<?Array<string>>(
propSelected || propDefaultSelected,
);
useEffect(() => {
const root = rootRef.current;
let headerRow;
let content;
Eif (root) {
headerRow = root.querySelector(`.${cssClasses.HEADER_ROW}`);
content = root.querySelector(`.${cssClasses.CONTENT}`);
}
const getHeaderCheckbox = (): ?HTMLMTCheckboxElement => {
if (headerRow) {
// flowlint-next-line unclear-type:off
return (headerRow.querySelector('input[type=checkbox]'): any);
}
return undefined;
};
const getCheckbox = (index: number): ?HTMLMTCheckboxElement => {
Eif (content) {
// flowlint-next-line unclear-type:off
return (content.querySelectorAll('input[type=checkbox]')[index]: any);
}
return undefined;
};
const foundation = new MDCDataTableFoundation({
addClassAtRowIndex: (rowIndex: number, mClassName: string) => {
const row: HTMLDataTableRowElement = foundation.getRows()[rowIndex];
row.mtAddClassName(mClassName);
},
getRowCount: () => foundation.getRows().length,
getRowElements: () =>
root && [].slice.call(root.querySelectorAll(strings.ROW_SELECTOR)),
getRowIdAtIndex: (rowIndex: number) =>
foundation.getRows()[rowIndex].getAttribute(strings.DATA_ROW_ID_ATTR),
getRowIndexByChildElement: (el: Element) => {
return foundation.getRows().indexOf(closest(el, strings.ROW_SELECTOR));
},
getSelectedRowCount: () =>
root && root.querySelectorAll(strings.ROW_SELECTED_SELECTOR).length,
isCheckboxAtRowIndexChecked: (rowIndex: number) => {
const checkbox = getCheckbox(rowIndex);
return checkbox && checkbox.checked;
},
isHeaderRowCheckboxChecked: () => {
const checkbox = getHeaderCheckbox();
return checkbox && checkbox.checked;
},
isRowsSelectable: () =>
!!(root && root.querySelector(strings.ROW_CHECKBOX_SELECTOR)),
// notifyUnselectedAll: () => this.emit(events.UNSELECTED_ALL, {}, /** shouldBubble */ true),
// Note. This only instanciate mdc-checkbox component, We don't need this.
registerHeaderRowCheckbox: () => {},
// Note. This only instanciate mdc-checkbox components, We don't need this.
registerRowCheckboxes: () => {},
removeClassAtRowIndex: (rowIndex: number, mClassName: string) => {
const row: HTMLDataTableRowElement = foundation.getRows()[rowIndex];
row.mtRemoveClassName(mClassName);
},
setAttributeAtRowIndex: (
rowIndex: number,
attr: string,
value: string,
) => {
const row: HTMLDataTableRowElement = foundation.getRows()[rowIndex];
row.mtSetAttr(attr, value);
},
setHeaderRowCheckboxChecked: (checked: boolean) => {
const checkbox = getHeaderCheckbox();
if (checkbox) {
checkbox.mtSetChecked(checked);
}
},
setHeaderRowCheckboxIndeterminate: (indeterminate: boolean) => {
const checkbox = getHeaderCheckbox();
if (checkbox) {
checkbox.mtSetIndterminate(indeterminate);
}
},
setRowCheckboxCheckedAtIndex: (rowIndex: number, checked: boolean) => {
const checkbox = getCheckbox(rowIndex);
Eif (checkbox) {
checkbox.mtSetChecked(checked);
}
},
});
const handleHeaderRowCheckboxChange = (...args) =>
foundation.handleHeaderRowCheckboxChange(...args);
const handleRowCheckboxChange = (...args) =>
foundation.handleRowCheckboxChange(...args);
foundation.init();
foundation.layout();
if (content) {
content.addEventListener('change', handleRowCheckboxChange);
}
if (headerRow) {
headerRow.addEventListener('change', handleHeaderRowCheckboxChange);
}
foundationRef.current = foundation;
return () => {
if (content) {
content.removeEventListener('change', handleRowCheckboxChange);
}
if (headerRow) {
headerRow.removeEventListener('change', handleHeaderRowCheckboxChange);
}
foundation.destroy();
foundationRef.current = null;
};
}, []);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
foundation.adapter_.notifySelectedAll = onSelectedAll;
}
}, [onSelectedAll]);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
foundation.adapter_.notifyUnselectedAll = onUnselectedAll;
}
}, [onUnselectedAll]);
useEffect(() => {
const foundation = foundationRef.current;
Eif (foundation) {
foundation.adapter_.notifyRowSelectionChanged = data => {
onRowSelectionChanged({
row: foundation.getRows()[data.rowIndex],
rowId: foundation
.getRows()
[data.rowIndex].getAttribute(strings.DATA_ROW_ID_ATTR),
rowIndex: data.rowIndex,
selected: data.selected,
});
};
}
}, [onRowSelectionChanged]);
useEffect(() => {
const foundation = foundationRef.current;
if (foundation && selected != null) {
foundation.setSelectedRowIds(selected);
}
}, [selected]);
useEffect(() => {
if (propSelected != null) {
setSelected(propSelected);
}
}, [propSelected]);
useEffect(() => {
const foundation = foundationRef.current;
if (foundation && defaultSelected != null) {
foundation.setSelectedRowIds(defaultSelected);
}
}, [defaultSelected]);
return (
<div {...rest} ref={rootRef} className={cx('mdc-data-table', className)}>
<table className="mdc-data-table__table" aria-label={label}>
{children}
</table>
</div>
);
};
export default DataTable;
|