UNPKG

@trail-ui/react

Version:
217 lines (210 loc) 8.99 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/switch.tsx var switch_exports = {}; __export(switch_exports, { default: () => EditableSwitch }); module.exports = __toCommonJS(switch_exports); // src/switch/switch.tsx var import_icons = require("@trail-ui/icons"); var import_shared_utils = require("@trail-ui/shared-utils"); var import_react = require("react"); var import_jsx_runtime = require("react/jsx-runtime"); var Switch = (0, import_react.forwardRef)( ({ label, labelId, isToggled, activeText, inactiveText, isDisabled = false, onChange, tabIndex = void 0, onFocus, classNames }, ref) => { const handleToggle = () => { if (isDisabled) { return; } onChange(!isToggled); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( "div", { className: (0, import_shared_utils.clsx)( isDisabled ? "pointer-events-none select-none opacity-40" : "", "flex items-center gap-1.5 text-sm text-neutral-900", classNames == null ? void 0 : classNames.base ), children: [ label && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: labelId != null ? labelId : "label", className: (0, import_shared_utils.clsx)("mr-0.5", classNames == null ? void 0 : classNames.label), children: label }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( "div", { role: "switch", ref, "aria-labelledby": labelId != null ? labelId : "label", "aria-checked": isToggled, tabIndex: tabIndex != null ? tabIndex : isDisabled ? -1 : 0, className: (0, import_shared_utils.clsx)( `relative inline-flex h-5 w-10 cursor-pointer items-center rounded-full transition-all duration-300 ease-in-out focus-visible:outline-2 focus-visible:outline-offset-[3px] focus-visible:outline-purple-600`, isToggled ? "bg-purple-600" : "bg-neutral-600", classNames == null ? void 0 : classNames.switch ), onClick: handleToggle, onKeyDown: (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); handleToggle(); } }, onFocus, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "absolute inset-0 flex items-center justify-between px-1", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.CheckIcon, { className: "h-5 w-5 text-neutral-50" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.CloseIcon, { className: "h-5 w-5 text-neutral-50" }) ] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { className: `absolute left-0.5 top-0.5 h-4 w-4 rounded-full bg-neutral-50 transition-transform duration-300 ${isToggled ? "translate-x-5" : "translate-x-0"}` } ) ] } ), activeText && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { "aria-hidden": "true", className: classNames == null ? void 0 : classNames.text, children: isToggled ? activeText : inactiveText }) ] } ); } ); Switch.displayName = "Switch"; var switch_default = Switch; // src/editable-table/editable-components/switch.tsx var import_react4 = require("react"); // src/editable-table/table-context/table-context-provider.tsx var import_react2 = require("react"); var import_jsx_runtime2 = require("react/jsx-runtime"); var EditableTableContext = (0, import_react2.createContext)({ handleSaveData: () => { }, idSelector: () => { }, table: {} }); function useTableContext(cell) { const { handleSaveData, idSelector, table } = (0, import_react2.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_react3 = __toESM(require("react")); var import_jsx_runtime3 = require("react/jsx-runtime"); var FocusHandlerContext = (0, import_react3.createContext)({ tableRef: import_react3.default.createRef(), focusedCell: { columnIndex: 0, rowIndex: 0, isHeader: true }, setFocusedCell: () => { } }); function useFocusHandler(cell, isHeader) { var _a, _b; const { focusedCell, setFocusedCell, tableRef } = (0, import_react3.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/switch.tsx var import_jsx_runtime4 = require("react/jsx-runtime"); function EditableSwitch(props) { var _a, _b, _c; const [value, setValue] = (0, import_react4.useState)(false); const cellRef = (0, import_react4.useRef)(null); const switchRef = (0, import_react4.useRef)(null); const { updateData } = useTableContext(props.cell); const { isCurrentCellFocused, focusCurrentCell, tableRef } = useFocusHandler( props.cell, false ); const metaData = props.cell.column.columnDef.meta; (0, import_react4.useEffect)(() => { setValue(props.cell.getValue()); }, []); (0, import_react4.useEffect)(() => { var _a2, _b2; if (isCurrentCellFocused && ((_a2 = tableRef.current) == null ? void 0 : _a2.contains(document.activeElement))) { (_b2 = switchRef.current) == null ? void 0 : _b2.focus(); } }, [isCurrentCellFocused]); return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { ref: cellRef, className: "td-content relative rounded px-2 py-0.5", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)( switch_default, { ref: switchRef, isToggled: value, onChange: (newValue) => { setValue(newValue); updateData(newValue); }, tabIndex: isCurrentCellFocused ? 0 : -1, isDisabled: ((_a = props.isCellDisabled) == null ? void 0 : _a.call(props, props.cell.row.original, props.cell.column.columnDef.id)) || false, ...metaData.label ? { label: metaData.label(props.cell.row.original) } : { labelId: props.headerId }, activeText: (_b = metaData.activeText) != null ? _b : "Enabled", inactiveText: (_c = metaData.inactiveText) != null ? _c : "Disabled", onFocus: focusCurrentCell, classNames: { label: "sr-only" } } ) }); }