UNPKG

@trail-ui/react

Version:
142 lines (136 loc) 5.41 kB
"use strict"; 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/links.tsx var links_exports = {}; __export(links_exports, { default: () => EditableLink }); module.exports = __toCommonJS(links_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/links.tsx var import_jsx_runtime3 = require("react/jsx-runtime"); function EditableLink({ cell, rowHeaderId, checkBoxSelectedName }) { const cellRef = (0, import_react3.useRef)(null); const { isCurrentCellFocused, focusCurrentCell, tableRef } = useFocusHandler(cell, false); (0, import_react3.useEffect)(() => { var _a, _b; if (isCurrentCellFocused && ((_a = tableRef.current) == null ? void 0 : _a.contains(document.activeElement))) (_b = cellRef.current) == null ? void 0 : _b.focus(); }, [isCurrentCellFocused]); return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( "div", { ref: cellRef, id: checkBoxSelectedName === cell.column.id ? rowHeaderId : "", className: "td-content px-2 py-0.5 outline-purple-600", onClick: focusCurrentCell, onKeyDown: (e) => { var _a; if (e.key === "Enter") { const link = (_a = cellRef.current) == null ? void 0 : _a.querySelector("a"); if (!link) return; if (e.ctrlKey || e.metaKey) { window.open(link.href, "_blank"); } else { link.click(); } } }, onFocus: focusCurrentCell, tabIndex: isCurrentCellFocused ? 0 : -1, children: (0, import_react_table.flexRender)(cell.column.columnDef.cell, { ...cell.getContext(), tabIndex: -1 }) } ); }