UNPKG

@trail-ui/react

Version:
298 lines (290 loc) 11.8 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, ariaLabelledby, isToggled, activeText, inactiveText, isDisabled = false, onChange, tabIndex = void 0, onFocus, classNames }, ref) => { var _a; 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": (_a = ariaLabelledby != null ? ariaLabelledby : labelId) != null ? _a : "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_react5 = 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: {}, tableName: "", groupData: [], allowBulkGroupSelection: false, doesRowHeaderExist: false }); function useTableContext(cell) { const { handleSaveData, ...rest } = (0, import_react2.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 }; } // src/editable-table/focus-handler/focus-handler-provider.tsx var import_react3 = require("react"); var import_jsx_runtime3 = require("react/jsx-runtime"); var FocusDataContext = (0, import_react3.createContext)({ columnIndex: 0, rowId: null, cellType: "headerCell" /* headerCell */, groupId: null, rowIndex: 0 }); var FocusDispatchContext = (0, import_react3.createContext)(() => { }); function useFocusHandler({ cellType, columnIndex, groupId, rowId, rowIndex }) { var _a; const focusedCell = (0, import_react3.useContext)(FocusDataContext); const setFocusedCell = (0, import_react3.useContext)(FocusDispatchContext); const { allowBulkGroupSelection, tableRef, table, serverPagination } = useTableContext(); const filteredRows = (_a = table == null ? void 0 : table.getRowModel().rows) != null ? _a : []; const localRowIndex = filteredRows.findIndex((row) => row.id === rowId); const isCurrentCellFocused = (0, import_react3.useMemo)(() => { var _a2; const isFocused = ((_a2 = tableRef.current) == null ? void 0 : _a2.contains(document.activeElement)) && focusedCell.cellType === cellType; switch (cellType) { case "headerCell" /* headerCell */: case "footerCell" /* footerCell */: return isFocused && focusedCell.columnIndex === columnIndex; case "groupHeaderCell" /* groupHeaderCell */: { let isGroup = focusedCell.groupId === groupId; if (allowBulkGroupSelection) { isGroup && (isGroup = columnIndex === 0 ? focusedCell.columnIndex === 0 : focusedCell.columnIndex > 0); } return isFocused && isGroup; } case "dataCell" /* dataCell */: return isFocused && focusedCell.rowIndex === localRowIndex && focusedCell.columnIndex === columnIndex; default: return false; } }, [focusedCell, columnIndex, cellType, groupId, localRowIndex, allowBulkGroupSelection]); const focusCurrentCell = (0, import_react3.useCallback)(() => { setFocusedCell == null ? void 0 : setFocusedCell({ columnIndex, rowId, cellType, groupId, rowIndex }); }, [columnIndex, rowId, cellType, groupId, rowIndex]); const resetFocus = (0, import_react3.useCallback)(() => { setFocusedCell == null ? void 0 : setFocusedCell({ cellType: "headerCell" /* headerCell */, rowId: null, groupId: null, rowIndex: -1, columnIndex: 0 }); }, []); return { isCurrentCellFocused, focusCurrentCell, resetFocus }; } // src/_utils/utils.tsx var import_utils = require("@react-aria/utils"); var import_react4 = __toESM(require("react")); var import_react_aria = require("react-aria"); var import_react_dom = __toESM(require("react-dom")); var import_jsx_runtime4 = require("react/jsx-runtime"); if (typeof HTMLTemplateElement !== "undefined") { const getFirstChild = Object.getOwnPropertyDescriptor(Node.prototype, "firstChild").get; Object.defineProperty(HTMLTemplateElement.prototype, "firstChild", { configurable: true, enumerable: true, get: function() { if (this.dataset.reactAriaHidden) { return this.content.firstChild; } else { return getFirstChild.call(this); } } }); } var HiddenContext = (0, import_react4.createContext)(false); var hiddenFragment = typeof DocumentFragment !== "undefined" ? new DocumentFragment() : null; function replaceSpacesWithHyphens(str) { return str.toLowerCase().trim().replace(/\s+/g, "-"); } // src/editable-table/editable-components/switch.tsx var import_jsx_runtime5 = require("react/jsx-runtime"); function EditableSwitch(props) { var _a, _b, _c; const [value, setValue] = (0, import_react5.useState)(false); const cellRef = (0, import_react5.useRef)(null); const switchRef = (0, import_react5.useRef)(null); const { updateData, tableRef, tableName, doesRowHeaderExist } = useTableContext( props.cell ); const { isCurrentCellFocused, focusCurrentCell } = useFocusHandler({ cellType: "dataCell" /* dataCell */, columnIndex: props.cell.column.getIndex(), groupId: null, rowId: props.cell.row.id, rowIndex: props.cell.row.index }); const metaData = props.cell.column.columnDef.meta; (0, import_react5.useEffect)(() => { setValue(props.cell.getValue()); }, []); (0, import_react5.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_runtime5.jsx)("div", { ref: cellRef, className: "td-content relative rounded px-2 py-0.5", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)( switch_default, { ref: switchRef, isToggled: value, onChange: (newValue) => { setValue(newValue); updateData(newValue); focusCurrentCell(); }, 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: `${replaceSpacesWithHyphens(tableName)}-${props.cell.column.getIndex()}-${props.cell.row.id}` } : { ariaLabelledby: `${doesRowHeaderExist ? props.cell.row.id : ""} ${replaceSpacesWithHyphens(tableName)}-${props.cell.column.getIndex()}` }, activeText: (_b = metaData.activeText) != null ? _b : "Enabled", inactiveText: (_c = metaData.inactiveText) != null ? _c : "Disabled", classNames: { label: "sr-only" } } ) }); }