@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
494 lines • 37.1 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scrollable = exports.LoadingPlaceholder = exports.NoDataIcon = exports.NoDataPlaceholder = exports.PaginationNumericButton = exports.PaginationArrowButton = exports.PaginationNumericButtons = exports.Pagination = exports.RowsPerPage = exports.TableResults = exports.Navigation = exports.HeaderTitle = exports.HeaderCell = exports.ActionCell = exports.DropDownCell = exports.InlineEditCell = exports.BodyRow = exports.BodyCell = exports.Body = exports.Header = exports.ResizingBar = exports.NoDataPlaceholderContentWrapper = exports.ColumnControls = exports.ActionButton = exports.DEFAULT_SORT_ACTIONS = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const react_1 = require("@floating-ui/react");
const react_table_1 = require("@tanstack/react-table");
const classnames_1 = __importDefault(require("classnames"));
const react_2 = require("react");
const clean_icon_button_1 = require("../clean-icon-button");
const icons_1 = require("../icons");
const loading_spinner_1 = require("../loading-spinner");
const menu_1 = require("../menu");
const select_1 = require("../select");
const skeleton_1 = require("../skeleton");
const typography_1 = require("../typography");
const data_grid_context_1 = require("./data-grid-context");
const helpers_1 = require("./helpers");
/** Table results per page, with the first option being the default */
const TABLE_RESULTS = {
DEFAULT: 10,
values: [10, 25, 50, 100],
};
const getPaginationOptions = (pageSize) => {
return [...new Set([...TABLE_RESULTS.values, pageSize])]
.sort((a, b) => a - b)
.map((option) => ({
label: `${option}`,
value: option,
}));
};
const DEFAULT_SORT_ACTIONS = (cell) => ({
asc: {
icon: (0, jsx_runtime_1.jsx)(icons_1.BarsArrowUpIconOutline, {}),
onClick: () => cell.column.toggleSorting(false),
title: 'Sort ascending',
},
default: {
icon: (0, jsx_runtime_1.jsx)(icons_1.Bars3CenterLeftIconOutline, {}),
onClick: () => cell.column.clearSorting(),
title: 'Default sort',
},
desc: {
icon: (0, jsx_runtime_1.jsx)(icons_1.BarsArrowDownIconOutline, {}),
onClick: () => cell.column.toggleSorting(true),
title: 'Sort descending',
},
});
exports.DEFAULT_SORT_ACTIONS = DEFAULT_SORT_ACTIONS;
const ResizingBar = (_a) => {
var { header, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["header", "className", "style", "htmlAttributes", "ref"]);
const { isKeyboardNavigationEnabled } = (0, data_grid_context_1.useDataGridContext)();
const handleKeyDown = (event) => {
if (event.key === 'ArrowLeft') {
event.stopPropagation();
event.preventDefault();
(0, helpers_1.resizeColumn)(helpers_1.ResizeDirection.LEFT, header);
}
else if (event.key === 'ArrowRight') {
event.stopPropagation();
event.preventDefault();
(0, helpers_1.resizeColumn)(helpers_1.ResizeDirection.RIGHT, header);
}
};
return ((0, jsx_runtime_1.jsx)("button", Object.assign({ ref: ref, type: "button", className: (0, classnames_1.default)('ndl-data-grid-resizer', className, {
'ndl-data-grid-is-resizing': header.column.getIsResizing(),
}), style: style, onMouseDown: header.getResizeHandler(), onTouchStart: header.getResizeHandler(), onKeyDown: handleKeyDown, "aria-label": "Resizing bar", tabIndex: isKeyboardNavigationEnabled ? -1 : 0 }, restProps, htmlAttributes)));
};
exports.ResizingBar = ResizingBar;
const Scrollable = (_a) => {
var { children, ref, className, style, htmlAttributes } = _a, restProps = __rest(_a, ["children", "ref", "className", "style", "htmlAttributes"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-scrollable', className) }, restProps, htmlAttributes, { children: children })));
};
exports.Scrollable = Scrollable;
const Header = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
const { tableProps, components } = (0, data_grid_context_1.useDataGridContext)();
const { getHeaderGroups, getState } = tableProps;
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ role: "rowgroup", className: (0, classnames_1.default)('ndl-data-grid-thead', className, {
'ndl-data-grid-is-resizing': getState().columnSizingInfo.isResizingColumn,
}), style: style, ref: ref }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: getHeaderGroups().map((headerGroup) => ((0, jsx_runtime_1.jsx)("div", { className: "ndl-data-grid-tr", role: "row", children: headerGroup.headers.map((header) => components.HeaderCell && ((0, jsx_runtime_1.jsx)(components.HeaderCell, { cell: header }, header.id))) }, headerGroup.id))) })) })));
};
exports.Header = Header;
const ActionButton = (_a) => {
var { action, onClose, as, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["action", "onClose", "as", "className", "style", "htmlAttributes", "ref"]);
const { onClick } = action, restAction = __rest(action, ["onClick"]);
const interceptedOnClick = (0, react_2.useCallback)((e) => {
const providedOnClick = onClick !== null && onClick !== void 0 ? onClick : action.onClick;
if (typeof providedOnClick === 'function') {
providedOnClick(e);
}
onClose === null || onClose === void 0 ? void 0 : onClose();
}, [action.onClick, onClick, onClose]);
return ((0, jsx_runtime_1.jsx)(menu_1.Menu.Item, Object.assign({ onClick: interceptedOnClick, as: as, className: className, style: style, htmlAttributes: htmlAttributes, ref: ref }, restAction, restProps)));
};
exports.ActionButton = ActionButton;
const ActionCell = ({ cell, innerCleanIconButtonProps, innerMenuProps, }) => {
var _a, _b;
const { components, isSkeletonLoading, portalTarget } = (0, data_grid_context_1.useDataGridContext)();
const actionsButtonRef = (0, react_2.useRef)(null);
const [isActionsOpen, setIsActionsOpen] = (0, react_2.useState)(false);
if (!cell ||
!cell.column.columnDef.meta ||
typeof ((_b = (_a = cell.column.columnDef) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.isActionCell) !== 'object') {
return null;
}
const { actions, onOpenChange } = cell.column.columnDef.meta.isActionCell;
if (isSkeletonLoading) {
return null;
}
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, Object.assign({ as: "button", size: "medium", description: "Actions", ref: actionsButtonRef, isActive: isActionsOpen, onClick: () => {
onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(!isActionsOpen);
setIsActionsOpen(!isActionsOpen);
} }, innerCleanIconButtonProps, { htmlAttributes: Object.assign({ tabIndex: 0 }, innerCleanIconButtonProps === null || innerCleanIconButtonProps === void 0 ? void 0 : innerCleanIconButtonProps.htmlAttributes), children: (innerCleanIconButtonProps === null || innerCleanIconButtonProps === void 0 ? void 0 : innerCleanIconButtonProps.children) || ((0, jsx_runtime_1.jsx)(icons_1.EllipsisHorizontalIconOutline, {})) })), (0, jsx_runtime_1.jsx)(menu_1.Menu, Object.assign({ isOpen: isActionsOpen, anchorRef: actionsButtonRef, onClose: () => {
onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(false);
setIsActionsOpen(false);
} }, innerMenuProps, { portalTarget: portalTarget, children: (innerMenuProps === null || innerMenuProps === void 0 ? void 0 : innerMenuProps.children) || ((0, jsx_runtime_1.jsx)(menu_1.Menu.Items, { children: actions.map((action, i) => components.ActionButton && ((0, jsx_runtime_1.jsx)(components.ActionButton, { action: Object.assign(Object.assign({}, action), { onClick: (e) => {
var _a;
(_a = action.onClick) === null || _a === void 0 ? void 0 : _a.call(action, e, cell);
} }), onClose: () => setIsActionsOpen(false) }, `${cell.column.id}-action-${i}`))) })) }))] }));
};
exports.ActionCell = ActionCell;
const ColumnControls = (_a) => {
var _b;
var { cell, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["cell", "children", "className", "style", "htmlAttributes", "ref"]);
const { components, portalTarget } = (0, data_grid_context_1.useDataGridContext)();
const sort = cell.column.getIsSorted();
const canSort = cell.column.getCanSort();
const { hasDefaultSortingActions = true, actions } = ((_b = cell.column.columnDef.meta) === null || _b === void 0 ? void 0 : _b.columnActions) || {};
const sortingActions = (0, react_2.useMemo)(() => {
if (canSort) {
if (sort !== 'asc' && sort !== 'desc') {
return [
(0, exports.DEFAULT_SORT_ACTIONS)(cell).asc,
(0, exports.DEFAULT_SORT_ACTIONS)(cell).desc,
];
}
if (sort === 'asc') {
return [
(0, exports.DEFAULT_SORT_ACTIONS)(cell).default,
(0, exports.DEFAULT_SORT_ACTIONS)(cell).desc,
];
}
if (sort === 'desc') {
return [
(0, exports.DEFAULT_SORT_ACTIONS)(cell).default,
(0, exports.DEFAULT_SORT_ACTIONS)(cell).asc,
];
}
}
// Default case
return [];
}, [sort, cell, canSort]);
const newActions = [
...(hasDefaultSortingActions ? sortingActions : []),
...(actions !== null && actions !== void 0 ? actions : []),
];
const actionsButtonRef = (0, react_2.useRef)(null);
const [isActionsOpen, setIsActionsOpen] = (0, react_2.useState)(false);
if (!actions || actions.length === 0) {
return null;
}
return newActions.length > 0 ? ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, className: (0, classnames_1.default)('ndl-header-action-group', className), style: style }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(react_2.Fragment, { children: [(0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, { size: "small", description: "Actions", ref: actionsButtonRef, isActive: isActionsOpen, onClick: () => {
setIsActionsOpen(!isActionsOpen);
}, htmlAttributes: {
tabIndex: 0,
}, children: (0, jsx_runtime_1.jsx)(icons_1.ChevronDownIconSolid, {}) }), (0, jsx_runtime_1.jsx)(menu_1.Menu, { isOpen: isActionsOpen, anchorRef: actionsButtonRef, onClose: () => setIsActionsOpen(false), portalTarget: portalTarget, children: (0, jsx_runtime_1.jsx)(menu_1.Menu.Items, { children: newActions.map((action, i) => components.ActionButton && ((0, jsx_runtime_1.jsx)(components.ActionButton, { action: action, onClose: () => setIsActionsOpen(false) }, `${cell.column.id}-action-${i}`))) }) })] })) }))) : null;
};
exports.ColumnControls = ColumnControls;
const HeaderCell = (_a) => {
var { cell, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["cell", "children", "className", "style", "htmlAttributes", "ref"]);
const { components, isResizable, isKeyboardNavigationEnabled } = (0, data_grid_context_1.useDataGridContext)();
const sort = cell.column.getIsSorted();
const canSort = cell.column.getCanSort();
const ariaSortRole = (0, react_2.useMemo)(() => {
switch (sort) {
case 'asc':
return 'ascending';
case 'desc':
return 'descending';
default:
return 'none';
}
}, [sort]);
const Icon = (0, react_2.useMemo)(() => {
switch (sort) {
case 'asc':
return (0, jsx_runtime_1.jsx)(icons_1.BarsArrowUpIconOutline, { className: "ndl-header-icon" });
case 'desc':
return (0, jsx_runtime_1.jsx)(icons_1.BarsArrowDownIconOutline, { className: "ndl-header-icon" });
default:
return ((0, jsx_runtime_1.jsx)(icons_1.Bars3CenterLeftIconOutline, { className: "ndl-hoverable-indicator ndl-header-icon" }));
}
}, [sort]);
const ariaDescription = (0, react_2.useMemo)(() => {
return canSort
? `Press ENTER or SPACE to sort column ${cell.column.id}`
: `Column id: ${cell.column.id}`;
}, [canSort, cell.column.id]);
const isActionColumn = (0, react_2.useMemo)(() => { var _a; return typeof ((_a = cell.column.columnDef.meta) === null || _a === void 0 ? void 0 : _a.isActionCell) === 'object'; }, [cell.column.columnDef.meta]);
const Tag = canSort ? 'button' : 'div';
const InnerHeaderCell = () => ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-header-cell", children: [cell.isPlaceholder
? null
: (0, react_table_1.flexRender)(cell.column.columnDef.header, cell.getContext()), canSort && Icon] }));
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, tabIndex: isKeyboardNavigationEnabled && !isActionColumn ? 0 : undefined, role: "columnheader", className: (0, classnames_1.default)('ndl-data-grid-th', className, {
'ndl-focusable-cell': isKeyboardNavigationEnabled,
'ndl-data-grid-row-action': isActionColumn,
'ndl-data-grid-pinned-cell': cell.column.getIsPinned(),
'ndl-data-grid-pinned-cell-right': cell.column.getIsPinned() === 'right',
'ndl-data-grid-pinned-cell-left': cell.column.getIsPinned() === 'left',
'ndl-data-grid-is-resizing': cell.column.getIsResizing(),
}), "aria-sort": ariaSortRole, style: Object.assign(Object.assign({}, style), { width: isActionColumn ? 40 : cell.getSize(), maxWidth: cell.column.columnDef.maxSize
? `${cell.column.columnDef.maxSize}px`
: 'none' }) }, (isKeyboardNavigationEnabled
? {
onKeyDown: (e) => {
if (isResizable &&
e.altKey &&
(e.key === 'ArrowLeft' || e.key === 'ArrowRight')) {
const direction = e.key === 'ArrowLeft'
? helpers_1.ResizeDirection.LEFT
: helpers_1.ResizeDirection.RIGHT;
(0, helpers_1.resizeColumn)(direction, cell);
e.preventDefault();
e.stopPropagation();
return;
}
},
}
: {}), restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Tag, { className: "ndl-header-group", style: {
cursor: canSort ? 'pointer' : 'default',
}, "aria-description": ariaDescription, onClick: () => canSort && cell.column.toggleSorting(), tabIndex: canSort ? 0 : undefined, children: components.HeaderTitle ? ((0, jsx_runtime_1.jsx)(components.HeaderTitle, { cell: cell, children: (0, jsx_runtime_1.jsx)(InnerHeaderCell, {}) })) : ((0, jsx_runtime_1.jsx)(InnerHeaderCell, {})) }), components.ColumnControls && ((0, jsx_runtime_1.jsx)(components.ColumnControls, { cell: cell })), isResizable &&
cell.column.getCanResize() &&
components.ResizingBar && (0, jsx_runtime_1.jsx)(components.ResizingBar, { header: cell })] })) }), cell.id));
};
exports.HeaderCell = HeaderCell;
const HeaderTitle = (_a) => {
var { cell, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["cell", "children", "className", "style", "htmlAttributes", "ref"]);
const { column, id } = cell;
const title = typeof column.columnDef.header === 'string' ? column.columnDef.header : id;
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, className: className, title: title, style: style }, restProps, htmlAttributes, { children: children })));
};
exports.HeaderTitle = HeaderTitle;
const Body = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
const { tableProps, components, isLoading } = (0, data_grid_context_1.useDataGridContext)();
const { getRowModel } = tableProps;
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-tbody', className), role: "rowgroup", "aria-busy": isLoading ? 'true' : 'false' }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isLoading && components.LoadingPlaceholder && ((0, jsx_runtime_1.jsx)(components.LoadingPlaceholder, {})), !isLoading &&
getRowModel().rows.length === 0 &&
components.NoDataPlaceholder && (0, jsx_runtime_1.jsx)(components.NoDataPlaceholder, {}), !isLoading &&
getRowModel().rows.map((row, idx) => components.BodyRow && ((0, jsx_runtime_1.jsx)(components.BodyRow, { row: row }, `table-row-${idx}`)))] })) })));
};
exports.Body = Body;
const BodyCell = (_a) => {
var _b, _c, _d;
var { cell, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["cell", "children", "className", "style", "htmlAttributes", "ref"]);
const isResizing = cell.column.getIsResizing();
const isActionColumn = Boolean((_b = cell.column.columnDef.meta) === null || _b === void 0 ? void 0 : _b.isActionCell);
const isInlineEditCell = Boolean((_c = cell.column.columnDef.meta) === null || _c === void 0 ? void 0 : _c.isInlineEditCell);
const isDropDownCell = Boolean((_d = cell.column.columnDef.meta) === null || _d === void 0 ? void 0 : _d.isDropDownCell);
const isCustomCell = isActionColumn || isDropDownCell;
const { isKeyboardNavigationEnabled, isSkeletonLoading, skeletonProps } = (0, data_grid_context_1.useDataGridContext)();
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, className: (0, classnames_1.default)('ndl-data-grid-td', className, {
'ndl-focusable-cell': isKeyboardNavigationEnabled,
'ndl-data-grid-is-resizing': isResizing,
'ndl-data-grid-custom-cell': isCustomCell,
'ndl-data-grid-row-action': isActionColumn,
'ndl-data-grid-inline-edit': isInlineEditCell,
'ndl-data-grid-dropdown-cell': isDropDownCell,
'ndl-data-grid-pinned-cell': cell.column.getIsPinned(),
'ndl-data-grid-pinned-cell-right': cell.column.getIsPinned() === 'right',
'ndl-data-grid-pinned-cell-left': cell.column.getIsPinned() === 'left',
}), role: "cell", tabIndex: isKeyboardNavigationEnabled ? 0 : undefined, style: Object.assign(Object.assign({}, style), { width: isActionColumn ? 40 : cell.column.getSize() }) }, restProps, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", shape: "rectangular", width: "100%" }, skeletonProps, { isLoading: isSkeletonLoading && !isActionColumn, children: children || (0, react_table_1.flexRender)(cell.column.columnDef.cell, cell.getContext()) })) }), cell.id));
};
exports.BodyCell = BodyCell;
const BodyRow = (_a) => {
var { row, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["row", "children", "className", "style", "htmlAttributes", "ref"]);
const { components } = (0, data_grid_context_1.useDataGridContext)();
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-tr', className), role: "row" }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: row
.getVisibleCells()
.map((cell) => components.BodyCell && ((0, jsx_runtime_1.jsx)(components.BodyCell, { cell: cell }, cell.id))) })) }), row.id));
};
exports.BodyRow = BodyRow;
const TableResults = (_a) => {
var { manualPagination, children, className, style, htmlAttributes, ref, resultsLabel = 'results' } = _a, restProps = __rest(_a, ["manualPagination", "children", "className", "style", "htmlAttributes", "ref", "resultsLabel"]);
const { tableProps, isSkeletonLoading, skeletonProps, isSmallNavigation } = (0, data_grid_context_1.useDataGridContext)();
const { getState, getRowModel, getCoreRowModel } = tableProps;
const { pagination: { pageSize, pageIndex }, } = getState();
const { rows } = getRowModel();
const { from, to, totalRows } = (0, react_2.useMemo)(() => manualPagination || {
from: 1 + pageIndex * pageSize,
to: rows.length + pageIndex * pageSize,
totalRows: getCoreRowModel().rows.length,
}, [pageIndex, pageSize, manualPagination, rows, getCoreRowModel]);
if (isSmallNavigation) {
return null;
}
return ((0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ as: "span", onBackground: "weak", shape: "rectangular" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsx)("span", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-results', className) }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)("span", { children: ["Showing", ' ', (0, jsx_runtime_1.jsx)("b", { children: totalRows ? `${from}${to !== from ? `-${to}` : ''}` : 0 }), " of", ' ', (0, jsx_runtime_1.jsx)("b", { children: totalRows }), " ", resultsLabel] })) })) })));
};
exports.TableResults = TableResults;
const RowsPerPage = (_a) => {
var { portalTarget: portalTargetProp, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["portalTarget", "children", "className", "style", "htmlAttributes", "ref"]);
const { tableProps, portalTarget } = (0, data_grid_context_1.useDataGridContext)();
const { setPageSize, getState } = tableProps;
const { pagination: { pageSize }, } = getState();
const paginationOptions = getPaginationOptions(pageSize);
const defaultPaginationOption = paginationOptions.find((option) => option.value === pageSize);
const menuPortalTarget = portalTargetProp === undefined ? portalTarget : portalTargetProp;
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-rows-per-page', className) }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Show", (0, jsx_runtime_1.jsx)(select_1.Select, { type: "select", size: "medium", ariaLabel: "Select page size", selectProps: {
defaultValue: defaultPaginationOption,
isSearchable: false,
menuPortalTarget: menuPortalTarget,
menuPosition: 'fixed',
onChange: (option) => {
if (option) {
setPageSize(option.value);
}
},
options: paginationOptions,
styles: {
container: () => ({
width: '100%',
}),
},
} })] })) })));
};
exports.RowsPerPage = RowsPerPage;
const PaginationArrowButton = (_a) => {
var { action, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["action", "children", "className", "style", "htmlAttributes", "ref"]);
const PaginationIcon = action === 'previous' ? icons_1.ChevronLeftIconOutline : icons_1.ChevronRightIconOutline;
const ariaLabel = action === 'previous' ? 'Previous page' : 'Next page';
const classes = (0, classnames_1.default)('ndl-data-grid-pagination-icon-button', className);
return ((0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, Object.assign({ ref: ref, style: style, className: classes, description: ariaLabel, htmlAttributes: htmlAttributes }, restProps, { children: children || (0, jsx_runtime_1.jsx)(PaginationIcon, {}) })));
};
exports.PaginationArrowButton = PaginationArrowButton;
const PaginationNumericButton = (_a) => {
var { currentIndex, isSelected, children, className, style, htmlAttributes, onClick, ref } = _a, restProps = __rest(_a, ["currentIndex", "isSelected", "children", "className", "style", "htmlAttributes", "onClick", "ref"]);
return ((0, jsx_runtime_1.jsx)("button", Object.assign({ ref: ref, style: style, type: "button", "data-testid": `ndl-table-${currentIndex}`, className: (0, classnames_1.default)('ndl-data-grid-pagination-numeric-button', className, {
'ndl-is-selected': isSelected,
'ndl-not-selected': !isSelected,
'ndl-not-selected-numeric': typeof currentIndex === 'number' && !isSelected,
}), onClick: (e) => typeof currentIndex === 'number' && onClick && onClick(e), tabIndex: typeof currentIndex === 'number' ? 0 : -1 }, restProps, htmlAttributes, { children: children || currentIndex })));
};
exports.PaginationNumericButton = PaginationNumericButton;
const PaginationNumericButtons = () => {
const { tableProps, components } = (0, data_grid_context_1.useDataGridContext)();
const { getState, setPageIndex, getPageCount } = tableProps;
const { pagination: { pageIndex }, } = getState();
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, helpers_1.userFriendlyPagination)(pageIndex + 1, getPageCount()).map((option, idx) => components.PaginationNumericButton && ((0, jsx_runtime_1.jsx)(components.PaginationNumericButton
/** Use index to avoid situations where two "..." values will appear, causing a key conflict */
, { onClick: () => typeof option === 'number' && setPageIndex(option - 1), currentIndex: option, isSelected: option === pageIndex + 1 }, `${option}-${idx}`))) }));
};
exports.PaginationNumericButtons = PaginationNumericButtons;
const Pagination = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
const { tableProps, components, isMediumNavigation } = (0, data_grid_context_1.useDataGridContext)();
const { getCanPreviousPage, getCanNextPage, previousPage, nextPage } = tableProps;
return ((0, jsx_runtime_1.jsx)("nav", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-data-grid-nav', className), "aria-label": "Pagination" }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [getCanPreviousPage()
? components.PaginationArrowButton && ((0, jsx_runtime_1.jsx)(components.PaginationArrowButton, { action: "previous", onClick: previousPage }))
: null, !isMediumNavigation && components.PaginationNumericButtons && ((0, jsx_runtime_1.jsx)(components.PaginationNumericButtons, {})), getCanNextPage()
? components.PaginationArrowButton && ((0, jsx_runtime_1.jsx)(components.PaginationArrowButton, { action: "next", onClick: nextPage }))
: null] })) })));
};
exports.Pagination = Pagination;
const Navigation = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
const { tableProps, components, isSkeletonLoading, skeletonProps, isSmallNavigation, } = (0, data_grid_context_1.useDataGridContext)();
const { getPageCount } = tableProps;
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: Object.assign(Object.assign({}, style), (isSmallNavigation && { justifyContent: 'end' })), className: (0, classnames_1.default)('ndl-data-grid-navigation', className) }, restProps, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children || ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [components.TableResults && (0, jsx_runtime_1.jsx)(components.TableResults, {}), (0, jsx_runtime_1.jsx)(skeleton_1.Skeleton, Object.assign({ onBackground: "weak", shape: "rectangular", height: "36px" }, skeletonProps, { isLoading: isSkeletonLoading, children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-data-grid-navigation-right-items", children: [getPageCount() > 1 && components.Pagination && ((0, jsx_runtime_1.jsx)(components.Pagination, {})), components.RowsPerPage && (0, jsx_runtime_1.jsx)(components.RowsPerPage, {})] }) }))] })) }) })));
};
exports.Navigation = Navigation;
const LoadingPlaceholder = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('nld-table-placeholder-wrapper', className), role: "row" }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsx)("div", { role: "cell", className: "ndl-data-grid-placeholder", children: (0, jsx_runtime_1.jsxs)("div", { className: "ndl-data-grid-loading-placeholder", children: [(0, jsx_runtime_1.jsx)(loading_spinner_1.LoadingSpinner, {}), (0, jsx_runtime_1.jsx)("h6", { children: "Loading data" })] }) })) })));
};
exports.LoadingPlaceholder = LoadingPlaceholder;
const NoDataPlaceholder = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('nld-table-placeholder-wrapper', className), role: "row" }, restProps, htmlAttributes, { children: children || ((0, jsx_runtime_1.jsxs)(exports.NoDataPlaceholderContentWrapper, { children: [(0, jsx_runtime_1.jsx)(NoDataIcon, {}), (0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: "subheading-small", children: "No data available" })] })) })));
};
exports.NoDataPlaceholder = NoDataPlaceholder;
const NoDataPlaceholderContentWrapper = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ role: "cell", className: (0, classnames_1.default)('ndl-data-grid-placeholder', className), ref: ref, style: style }, restProps, htmlAttributes, { children: children })));
};
exports.NoDataPlaceholderContentWrapper = NoDataPlaceholderContentWrapper;
const InlineEditCell = (_a) => {
var _b;
var { cell, value, ariaLabel, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["cell", "value", "ariaLabel", "className", "style", "htmlAttributes", "ref"]);
const [inputValue, setInputValue] = (0, react_2.useState)(value);
(0, react_2.useEffect)(() => {
setInputValue(value);
}, [value]);
const { dataGridNav } = (0, data_grid_context_1.useDataGridContext)();
if (!((_b = cell.column.columnDef.meta) === null || _b === void 0 ? void 0 : _b.isInlineEditCell)) {
return null;
}
return ((0, jsx_runtime_1.jsx)("input", Object.assign({ ref: ref, style: style, className: className, onChange: (e) => setInputValue(e.target.value), value: inputValue, onFocus: () => {
dataGridNav.disable();
}, onKeyDown: (e) => {
var _a, _b, _c, _d;
if (e.key === 'Enter') {
(_b = (_a = cell === null || cell === void 0 ? void 0 : cell.column.columnDef.meta) === null || _a === void 0 ? void 0 : _a.isInlineEditCell) === null || _b === void 0 ? void 0 : _b.onEditChange(inputValue, cell);
(_c = e.currentTarget.parentElement) === null || _c === void 0 ? void 0 : _c.focus();
return;
}
else if (e.key === 'Escape') {
(_d = e.currentTarget.parentElement) === null || _d === void 0 ? void 0 : _d.focus();
return;
}
}, onBlur: () => {
setInputValue(value);
dataGridNav.enable();
}, "aria-label": ariaLabel }, restProps, htmlAttributes)));
};
exports.InlineEditCell = InlineEditCell;
const DropDownCell = (_a) => {
var _b;
var { cell, options, portalTarget: portalTargetProp, ariaLabel, className, style, htmlAttributes, ref: forwardRef, isDisabled } = _a, restProps = __rest(_a, ["cell", "options", "portalTarget", "ariaLabel", "className", "style", "htmlAttributes", "ref", "isDisabled"]);
const { dataGridNav, portalTarget } = (0, data_grid_context_1.useDataGridContext)();
const ref = (0, react_2.useRef)(null);
const mergedRef = (0, react_1.useMergeRefs)([ref, forwardRef]);
if (!((_b = cell.column.columnDef.meta) === null || _b === void 0 ? void 0 : _b.isDropDownCell)) {
return null;
}
const defaultOption = options.find((option) => { var _a; return option.value === ((_a = cell.getValue()) === null || _a === void 0 ? void 0 : _a.toString()); });
const menuPortalTarget = portalTargetProp === undefined ? portalTarget : portalTargetProp;
return ((0, jsx_runtime_1.jsx)(select_1.Select, Object.assign({ ref: mergedRef, className: className, isFluid: true, isClean: true, style: Object.assign(Object.assign({}, style), { height: '100%', width: '100%' }), type: "select", size: "medium", ariaLabel: ariaLabel, isDisabled: isDisabled, selectProps: {
defaultValue: defaultOption,
menuPortalTarget: menuPortalTarget,
menuPosition: 'fixed',
onChange: (e) => {
var _a, _b;
const value = e === null || e === void 0 ? void 0 : e.value;
(_b = (_a = cell === null || cell === void 0 ? void 0 : cell.column.columnDef.meta) === null || _a === void 0 ? void 0 : _a.isDropDownCell) === null || _b === void 0 ? void 0 : _b.onChange(value === null || value === void 0 ? void 0 : value.toString(), cell);
dataGridNav.enable();
(ref === null || ref === void 0 ? void 0 : ref.current) && dataGridNav.focusParentCell(ref.current);
},
onMenuOpen: () => {
dataGridNav.disable();
},
options: options,
styles: {
container: () => ({ height: '100%', width: '100%' }),
control: () => ({
backgroundColor: 'transparent',
borderRadius: 0,
height: '100%',
}),
},
}, htmlAttributes: htmlAttributes }, restProps)));
};
exports.DropDownCell = DropDownCell;
const NoDataIcon = (_a) => {
var { className } = _a, restProps = __rest(_a, ["className"]);
const classes = (0, classnames_1.default)('n-size-token-64', className);
return (0, jsx_runtime_1.jsx)(icons_1.DataGridCrossIcon, Object.assign({ className: classes }, restProps));
};
exports.NoDataIcon = NoDataIcon;
//# sourceMappingURL=Components.js.map