@gravity-ui/uikit
Version:
Gravity UI base styling and components
112 lines (111 loc) • 5.64 kB
JavaScript
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { Ellipsis } from '@gravity-ui/icons';
import _memoize from "lodash/memoize.js";
import { useUniqId } from "../../../../hooks/index.js";
import { useBoolean } from "../../../../hooks/private/index.js";
import { Button } from "../../../Button/index.js";
import { Icon } from "../../../Icon/index.js";
import { Menu } from "../../../Menu/index.js";
import { Popup } from "../../../Popup/index.js";
import { block } from "../../../utils/cn.js";
import { getComponentName } from "../../../utils/getComponentName.js";
import i18n from "../../i18n/index.js";
import "./withTableActions.css";
export const actionsColumnId = '_actions';
export function enhanceSystemColumn(columns, enhancer) {
const existedColumn = columns.find(({ id }) => id === actionsColumnId);
const systemColumn = existedColumn || {
id: actionsColumnId,
name: '',
sticky: 'end',
width: 28, // button width
placeholder: '',
};
enhancer(systemColumn);
return existedColumn ? columns : [...columns, systemColumn];
}
const isActionGroup = (config) => {
return Array.isArray(config.items);
};
const b = block('table');
const actionsCn = b('actions');
const actionsButtonCn = b('actions-button');
const bPopup = block('table-action-popup');
const menuCn = bPopup('menu');
const menuItemCn = bPopup('menu-item');
const DEFAULT_PLACEMENT = ['bottom-end', 'top-end'];
const DefaultRowActions = ({ index, item, getRowActions, getRowDescriptor, rowActionsSize, isRowDisabled, tableQa, }) => {
const [isPopupOpen, , closePopup, togglePopup] = useBoolean(false);
const anchorRef = React.useRef(null);
const rowId = useUniqId();
if (getRowActions === undefined) {
return null;
}
const renderPopupMenuItem = (action, index) => {
if (isActionGroup(action)) {
return (_jsx(Menu.Group, { label: action.title, children: action.items.map(renderPopupMenuItem) }, index));
}
const { text, icon, handler, href, ...restProps } = action;
return (_jsx(Menu.Item, { onClick: (event) => {
event.stopPropagation();
handler(item, index, event);
closePopup();
}, href: typeof href === 'function' ? href(item, index) : href, iconStart: icon, className: menuItemCn, ...restProps, children: text }, index));
};
const disabled = getRowDescriptor?.(item, index)?.disabled || isRowDisabled?.(item, index);
const actions = getRowActions(item, index);
if (actions.length === 0) {
return null;
}
return (_jsxs("div", { className: actionsCn, children: [_jsx(Popup, { open: isPopupOpen, anchorRef: anchorRef, placement: DEFAULT_PLACEMENT, onOutsideClick: closePopup, id: rowId, qa: tableQa && `${tableQa}-actions-popup`, children: _jsx(Menu, { className: menuCn, size: rowActionsSize, children: actions.map(renderPopupMenuItem) }) }), _jsx(Button, { view: "flat-secondary", className: actionsButtonCn, onClick: togglePopup, size: rowActionsSize, ref: anchorRef, disabled: disabled, qa: tableQa && `${tableQa}-actions-trigger-${index}`, "aria-label": i18n('label-actions'), "aria-expanded": isPopupOpen, "aria-controls": rowId, children: _jsx(Icon, { data: Ellipsis }) })] }));
};
export function withTableActions(TableComponent) {
const componentName = getComponentName(TableComponent);
const displayName = `withTableActions(${componentName})`;
return class extends React.Component {
static displayName = displayName;
state = {
popupOpen: false,
popupData: null,
};
render() {
const { renderRowActions, // eslint-disable-line @typescript-eslint/no-unused-vars
getRowActions, // eslint-disable-line @typescript-eslint/no-unused-vars
columns, onRowClick, ...restTableProps } = this.props;
return (_jsx(TableComponent, { ...restTableProps, columns: this.enhanceColumns(columns), onRowClick: this.enhanceOnRowClick(onRowClick) }));
}
renderBodyCell = (item, index) => {
const { getRowActions, rowActionsSize, renderRowActions, isRowDisabled, getRowDescriptor, qa, } = this.props;
if (renderRowActions) {
return renderRowActions({ item, index });
}
return (_jsx(DefaultRowActions, { index: index, item: item, getRowActions: getRowActions, rowActionsSize: rowActionsSize, getRowDescriptor: getRowDescriptor, isRowDisabled: isRowDisabled, tableQa: qa }));
};
// eslint-disable-next-line @typescript-eslint/member-ordering
enhanceColumns = _memoize((columns) => enhanceSystemColumn(columns, (systemColumn) => {
systemColumn.template = this.renderBodyCell;
}));
// eslint-disable-next-line @typescript-eslint/member-ordering
enhanceOnRowClick = _memoize((onRowClick) => {
if (!onRowClick) {
return onRowClick;
}
return (item, index, event) => {
if (
// @ts-expect-error
event.nativeEvent.target.closest(`.${menuCn}`)) {
return undefined;
}
if (
// @ts-expect-error
event.nativeEvent.target.matches(`.${actionsButtonCn}, .${actionsButtonCn} *`)) {
return undefined;
}
return onRowClick(item, index, event);
};
});
};
}
//# sourceMappingURL=withTableActions.js.map