UNPKG

@trail-ui/react

Version:
49 lines (47 loc) 1.29 kB
// src/editable-table/table-context/table-context-provider.tsx import { createContext, useContext } from "react"; import { jsx } from "react/jsx-runtime"; var EditableTableContext = createContext({ handleSaveData: () => { }, idSelector: () => { }, table: {}, tableName: "", groupData: [], allowBulkGroupSelection: false, doesRowHeaderExist: false }); function TableContextProvider({ children, ...props }) { return /* @__PURE__ */ jsx(EditableTableContext.Provider, { value: props, children }); } function useTableContext(cell) { const { handleSaveData, ...rest } = useContext(EditableTableContext); function updateData(value) { var _a, _b; if (cell) { const accessorKey = cell.column.columnDef.accessorKey; const groupId = (_a = cell.row.original) == null ? void 0 : _a.editableTableGroupId; handleSaveData({ accessorKey, columnId: cell.column.id, rowId: rest.idSelector(cell.row.original), value, ...groupId ? { groupId } : {} }); (_b = rest.table.options.meta) == null ? void 0 : _b.updateData(cell.row.id, accessorKey, value); } } return { ...cell ? { updateData } : {}, ...rest }; } export { EditableTableContext, TableContextProvider, useTableContext };