UNPKG

orcs-design-system

Version:
107 lines 4.05 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 = /*#__PURE__*/styled.span.withConfig({ displayName: "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("(".concat(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 === null || table === void 0 ? void 0 : 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, _objectSpread(_objectSpread({}, 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": _objectSpread({ Header: props => /*#__PURE__*/_jsx(HeaderCell, _objectSpread(_objectSpread({}, props), {}, { firstCol: firstCol, tableConfig: tableConfig })), Cell: props => /*#__PURE__*/_jsxs(Flex, { alignItems: "center", children: [/*#__PURE__*/_jsx(MRT_ExpandButton, _objectSpread({}, props)), /*#__PURE__*/_jsx(GroupedCell, _objectSpread(_objectSpread({}, props), {}, { firstCol: firstCol }))] }), enableResizing: true, size: firstCol.size || DEFAULT_COLUMN_SIZE }, firstCol.grow ? { grow: firstCol.grow } : {}) });