@trail-ui/react
Version:
160 lines (154 loc) • 6.34 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/editable-table/editable-components/checkbox.tsx
var checkbox_exports = {};
__export(checkbox_exports, {
default: () => EditableCheckBox
});
module.exports = __toCommonJS(checkbox_exports);
var import_react_table = require("@tanstack/react-table");
var import_react3 = require("react");
// src/editable-table/focus-handler/focus-handler-provider.tsx
var import_react2 = __toESM(require("react"));
// src/editable-table/table-context/table-context-provider.tsx
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var EditableTableContext = (0, import_react.createContext)({
handleSaveData: () => {
},
idSelector: () => {
},
table: {}
});
function useTableContext(cell) {
const { handleSaveData, idSelector, table } = (0, import_react.useContext)(
EditableTableContext
);
function updateData(value) {
var _a;
const accessorKey = cell.column.columnDef.accessorKey;
handleSaveData({
accessorKey,
columnId: cell.column.id,
rowId: idSelector(cell.row.original),
value
});
(_a = table.options.meta) == null ? void 0 : _a.updateData(cell.row.id, cell.column.id, value);
}
return { updateData, idSelector, table };
}
// src/editable-table/focus-handler/focus-handler-provider.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var FocusHandlerContext = (0, import_react2.createContext)({
tableRef: import_react2.default.createRef(),
focusedCell: { columnIndex: 0, rowIndex: 0, isHeader: true },
setFocusedCell: () => {
}
});
function useFocusHandler(cell, isHeader) {
var _a, _b;
const { focusedCell, setFocusedCell, tableRef } = (0, import_react2.useContext)(FocusHandlerContext);
const { table } = useTableContext(cell);
const pageIndex = (_a = table == null ? void 0 : table.getState().pagination.pageIndex) != null ? _a : 0;
const pageSize = (_b = table == null ? void 0 : table.getState().pagination.pageSize) != null ? _b : 10;
const localRowIndex = cell ? cell.row.index - pageIndex * pageSize : 0;
const isCurrentCellFocused = focusedCell.isHeader === isHeader && focusedCell.rowIndex === localRowIndex && focusedCell.columnIndex === (cell == null ? void 0 : cell.column.getIndex());
function focusCurrentCell() {
if (!cell)
return;
setFocusedCell({
columnIndex: cell.column.getIndex(),
rowIndex: localRowIndex,
// 👈 Should be page-local
isHeader: isHeader != null ? isHeader : false
});
}
return { focusedCell, setFocusedCell, tableRef, isCurrentCellFocused, focusCurrentCell };
}
// src/editable-table/editable-components/checkbox.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
function EditableCheckBox(props) {
var _a;
const cellRef = (0, import_react3.useRef)(null);
const { isCurrentCellFocused, focusCurrentCell, tableRef } = useFocusHandler(
props.cell,
false
);
const metaData = props.cell.column.columnDef.meta;
const onKeyDown = (e) => {
const tableWrapper = document.querySelector(".editable-table");
const activeElement = document.activeElement;
if (activeElement && activeElement.tagName === "INPUT" && !(tableWrapper == null ? void 0 : tableWrapper.contains(activeElement)) || e.key === "Tab") {
return;
}
};
(0, import_react3.useEffect)(() => {
var _a2, _b;
if (isCurrentCellFocused && ((_a2 = tableRef.current) == null ? void 0 : _a2.contains(document.activeElement))) {
(_b = cellRef.current) == null ? void 0 : _b.focus();
}
}, [isCurrentCellFocused]);
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
"div",
{
...metaData.label ? { "aria-label": metaData.label(props.cell.row.original) } : { "aria-labelledby": (_a = props.rowHeaderId) != null ? _a : "" },
role: "checkbox",
"aria-checked": props.cell.row.getIsSelected(),
ref: cellRef,
className: "td-content flex h-[18px] w-[18px] items-center justify-center p-[10px] 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();
}
}
onKeyDown(e);
},
onFocus: focusCurrentCell,
tabIndex: isCurrentCellFocused ? 0 : -1,
children: (0, import_react_table.flexRender)(props.cell.column.columnDef.cell, {
...props.cell.getContext(),
tabIndex: -1
})
}
);
}