UNPKG

@trail-ui/react

Version:
184 lines (181 loc) 6.25 kB
import { checkbox_default } from "./chunk-5E77NYRI.mjs"; import { useFocusHandler } from "./chunk-F5W73ZFA.mjs"; import { useTableContext } from "./chunk-A4IWBDSQ.mjs"; // src/editable-table/grouped-row-header/grouped-row-header.tsx import { useEffect, useId, useMemo, useRef, useState } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; function GroupedRowHeader(props) { const { groupData, table, allowBulkGroupSelection } = useTableContext(); const currentGroupData = groupData.find((d) => d.groupId === props.groupId); const columns = table.getHeaderGroups()[0].headers.length; return /* @__PURE__ */ jsxs("tr", { children: [ allowBulkGroupSelection && currentGroupData && /* @__PURE__ */ jsx( GroupHeaderRowCheckbox, { groupData: currentGroupData, columnIndex: allowBulkGroupSelection ? 1 : 0 } ), currentGroupData && /* @__PURE__ */ jsx( GroupRowHeaderCell, { groupData: currentGroupData, columnIndex: allowBulkGroupSelection ? 1 : 0, colSpan: allowBulkGroupSelection ? columns - 1 : columns } ) ] }); } function GroupHeaderRowCheckbox(props) { var _a; const [isChecked, setIsChecked] = useState(false); const cellRef = useRef(null); const selectAllId = useId(); const { table } = useTableContext(); const { focusCurrentCell, isCurrentCellFocused } = useFocusHandler({ cellType: "groupHeaderCell" /* groupHeaderCell */, columnIndex: 0, groupId: (_a = props.groupData) == null ? void 0 : _a.groupId, rowId: null, rowIndex: -1 }); const currentPageRowIds = useMemo( () => new Set( table.getRowModel().rows.filter( (r) => r.original.editableTableGroupId === props.groupData.groupId ).map((r) => r.id) ), [table.getState().pagination.pageSize, table.getState().pagination.pageIndex] ); function toggleRowsSelection() { let newValue = false; if (!isChecked || isChecked === "mixed") { newValue = true; } currentPageRowIds.forEach((id) => { table.getRowModel().rowsById[id].toggleSelected(newValue); }); } useEffect(() => { var _a2; if (isCurrentCellFocused) { (_a2 = cellRef.current) == null ? void 0 : _a2.focus(); } }, [isCurrentCellFocused]); useEffect(() => { const ids = props.groupData.ids.filter((id) => currentPageRowIds.has(id)); const selectedRowIds = Object.keys(table.getState().rowSelection); if (hasAll(ids, selectedRowIds)) { setIsChecked(true); } else if (hasSome(ids, selectedRowIds)) { setIsChecked("mixed"); } else { setIsChecked(false); } }, [ table.getState().rowSelection, table.getState().pagination.pageSize, table.getState().pagination.pageIndex ]); return /* @__PURE__ */ jsxs("td", { className: "border-b border-r border-t border-neutral-200 bg-neutral-50 px-2 py-2 text-left text-sm font-normal text-neutral-900 last:border-r-0", children: [ /* @__PURE__ */ jsx("span", { id: selectAllId, className: "hidden", children: "Select all instances under" }), /* @__PURE__ */ jsx( "div", { "aria-labelledby": `${selectAllId} ${props.groupData.groupId}`, role: "checkbox", "aria-checked": isChecked, ref: cellRef, className: "td-content flex h-6 w-6 items-center justify-center outline-purple-600", onClick: (e) => { var _a2; if (e.target.tagName.toLowerCase() === "input") { focusCurrentCell(); return; } const checkbox = (_a2 = cellRef.current) == null ? void 0 : _a2.querySelector('input[type="checkbox"]'); checkbox == null ? void 0 : checkbox.click(); focusCurrentCell(); }, onKeyDown: (e) => { var _a2; if (e.key === "Enter" || e.key === " ") { e.preventDefault(); const checkbox = (_a2 = cellRef.current) == null ? void 0 : _a2.querySelector('input[type="checkbox"]'); if (checkbox) { checkbox.click(); } } }, tabIndex: isCurrentCellFocused ? 0 : -1, children: /* @__PURE__ */ jsx( checkbox_default, { "aria-hidden": true, tabIndex: -1, type: "checkbox", "aria-label": "Select all status", checked: !!isChecked, isIndeterminate: isChecked === "mixed", isSelected: isChecked, onChange: toggleRowsSelection } ) } ) ] }); } function GroupRowHeaderCell(props) { var _a; const cellRef = useRef(null); const { focusCurrentCell, isCurrentCellFocused } = useFocusHandler({ cellType: "groupHeaderCell" /* groupHeaderCell */, columnIndex: props.columnIndex, groupId: (_a = props.groupData) == null ? void 0 : _a.groupId, rowId: null, rowIndex: -1 }); useEffect(() => { var _a2; if (isCurrentCellFocused) { (_a2 = cellRef.current) == null ? void 0 : _a2.focus(); } }, [isCurrentCellFocused]); return ( // biome-ignore lint/a11y/useKeyWithClickEvents: <explanation> /* @__PURE__ */ jsxs( "th", { id: props.groupData.groupId, ref: cellRef, className: `border-b border-r border-t border-neutral-200 px-2 py-2 text-left text-sm font-normal text-neutral-900 last:border-r-0 focus:outline-purple-700 focus:[outline-style:solid]`, onClick: focusCurrentCell, tabIndex: isCurrentCellFocused ? 0 : -1, colSpan: props.colSpan, scope: "colgroup", children: [ /* @__PURE__ */ jsx("span", { className: "overflow-hidden text-sm font-semibold", children: props.groupData.groupName }), " ", props.groupData.description && /* @__PURE__ */ jsx("span", { className: "overflow-hidden text-sm text-neutral-600", children: props.groupData.description }) ] } ) ); } function hasSome(array1, array2) { const set2 = new Set(array2); return array1.some((item) => set2.has(item)); } function hasAll(array1, array2) { const set2 = new Set(array2); return array1.every((item) => set2.has(item)); } export { GroupedRowHeader };