UNPKG

orcs-design-system

Version:
110 lines 3.16 kB
import { Stack } from "@mui/material"; import { MRT_ExpandAllButton, MRT_ExpandButton } from "material-react-table"; import React from "react"; import Box from "../Box"; import Flex from "../Flex"; import { get } from "lodash"; import styled from "styled-components"; import { PropTypes } from "prop-types"; import { DEFAULT_COLUMN_SIZE } from "./const"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const StyledHighlight = styled.span.withConfig({ displayName: "getExpandColumnConfig__StyledHighlight", componentId: "sc-kekww4-0" })(["background-color:", ";border-radius:2px;padding:2px 1px;"], props => props.matchHighlightColor); const getHiglightedSearchTerm = _ref => { let { value, searchTerm, matchHighlightColor } = _ref; const regex = new RegExp(`(${searchTerm})`, "i"); return value.split(regex).map((part, i) => part.match(regex) ? /*#__PURE__*/_jsx(StyledHighlight, { matchHighlightColor: matchHighlightColor, children: part }, part + i) : part); }; const GroupedCell = props => { const { row, table, firstCol } = props; const { mrtTheme: { matchHighlightColor }, enableFilterMatchHighlighting } = table?.options || {}; const searchTerm = table.getState().globalFilter; const value = get(row.original, firstCol.accessorKey); let renderedCellValue = value; if (enableFilterMatchHighlighting && searchTerm) { renderedCellValue = getHiglightedSearchTerm({ value, searchTerm, matchHighlightColor }); } if (firstCol.Cell) { return /*#__PURE__*/_jsx(Flex, { alignItems: "center", children: /*#__PURE__*/_jsx(firstCol.Cell, { ...props, renderedCellValue: renderedCellValue }) }); } return /*#__PURE__*/_jsxs(Flex, { alignItems: "center", children: [renderedCellValue, " "] }); }; GroupedCell.propTypes = PropTypes.Obj; const HeaderCell = props => { const { table, firstCol, tableConfig } = props; let headerMarkup = firstCol.header; if (/*#__PURE__*/React.isValidElement(firstCol.Header)) { headerMarkup = firstCol.Header; } if (typeof firstCol.Header === "function") { headerMarkup = /*#__PURE__*/_jsx(firstCol.Header, {}); } return /*#__PURE__*/_jsxs(Stack, { direction: "row", alignItems: "center", children: [tableConfig.enableExpandAll && /*#__PURE__*/_jsx(MRT_ExpandAllButton, { table: table }), /*#__PURE__*/_jsx(Box, { children: headerMarkup })] }); }; HeaderCell.propTypes = PropTypes.Obj; export default (firstCol, tableConfig) => ({ "mrt-row-expand": { Header: props => /*#__PURE__*/_jsx(HeaderCell, { ...props, firstCol: firstCol, tableConfig: tableConfig }), Cell: props => /*#__PURE__*/_jsxs(Flex, { alignItems: "center", children: [/*#__PURE__*/_jsx(MRT_ExpandButton, { ...props }), /*#__PURE__*/_jsx(GroupedCell, { ...props, firstCol: firstCol })] }), enableResizing: true, size: firstCol.size || DEFAULT_COLUMN_SIZE, ...(firstCol.grow ? { grow: firstCol.grow } : {}) } });