UNPKG

@razorpay/blade

Version:

The Design System that powers Razorpay

436 lines (433 loc) 17.9 kB
import _typeof from '@babel/runtime/helpers/typeof'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import React__default, { useEffect } from 'react'; import { Body, Cell, Row } from '@table-library/react-table-library/table'; import { Virtualized } from '@table-library/react-table-library/virtualized'; import styled from 'styled-components'; import { useTableContext } from './TableContext.js'; import { tableRow, checkboxCellWidth, classes } from './tokens.js'; import { ComponentIds } from './componentIds.js'; import { getTableRowBackgroundTransition, getTableActionsHoverStyles } from './utils.js'; import './commonStyles/index.js'; import getIn from '../../utils/lodashButBetter/get.js'; import '../Typography/index.js'; import '../Checkbox/index.js'; import '../../utils/index.js'; import '../Box/BaseBox/index.js'; import '../../utils/metaAttribute/index.js'; import '../../utils/assignWithoutSideEffects/index.js'; import '../../utils/getFocusRingStyles/index.js'; import '../../tokens/global/index.js'; import '../../utils/makeAccessible/index.js'; import { useIsomorphicLayoutEffect } from '../../utils/useIsomorphicLayoutEffect.js'; import '../../utils/makeAnalyticsAttribute/index.js'; import { jsx, jsxs } from 'react/jsx-runtime'; import { getTableBodyStyles } from './commonStyles/tableBodyStyles.js'; import { metaAttribute } from '../../utils/metaAttribute/metaAttribute.web.js'; import { MetaConstants } from '../../utils/metaAttribute/metaConstants.js'; import { makeAnalyticsAttribute } from '../../utils/makeAnalyticsAttribute/makeAnalyticsAttribute.js'; import { assignWithoutSideEffects } from '../../utils/assignWithoutSideEffects/assignWithoutSideEffects.js'; import { getFocusRingStyles } from '../../utils/getFocusRingStyles/getFocusRingStyles.web.js'; import { BaseBox } from '../Box/BaseBox/BaseBox.web.js'; import { makeMotionTime } from '../../utils/makeMotionTime/makeMotionTime.web.js'; import { makeSpace } from '../../utils/makeSpace/makeSpace.js'; import { makeSize } from '../../utils/makeSize/makeSize.js'; import { size } from '../../tokens/global/size.js'; import { Text } from '../Typography/Text/Text.js'; import { Checkbox } from '../Checkbox/Checkbox.js'; import { getMediaQuery } from '../../utils/getMediaQuery/getMediaQuery.js'; import { makeAccessible } from '../../utils/makeAccessible/makeAccessible.web.js'; var _excluded = ["children"], _excluded2 = ["children", "textAlign", "_hasPadding"], _excluded3 = ["children", "item", "isDisabled", "onHover", "onClick", "hoverActions", "testID"]; 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; } var StyledBody = /*#__PURE__*/styled(Body).withConfig({ displayName: "TableBodyweb__StyledBody", componentId: "sc-1fpz8hn-0" })(function (_ref) { var theme = _ref.theme, $showStripedRows = _ref.$showStripedRows, $isSelectable = _ref.$isSelectable; var rowBackgroundTransition = getTableRowBackgroundTransition(theme); return { '&&&': _objectSpread({ border: 'none', transition: rowBackgroundTransition, '& tr:last-child .cell-wrapper': { borderBottom: 'none' } }, _objectSpread({}, getTableBodyStyles({ addCommonStyles: true, theme: theme, isSelectable: $isSelectable, showStripedRows: $showStripedRows, isVirtualized: false }))) }; }); var _TableBody = function _TableBody(_ref2) { var children = _ref2.children, rest = _objectWithoutProperties(_ref2, _excluded); var _useTableContext = useTableContext(), showStripedRows = _useTableContext.showStripedRows, selectionType = _useTableContext.selectionType; var isSelectable = selectionType !== 'none'; return /*#__PURE__*/jsx(StyledBody, _objectSpread(_objectSpread(_objectSpread({ $isSelectable: isSelectable, $showStripedRows: showStripedRows, $showBorderedCells: true }, metaAttribute({ name: MetaConstants.TableBody })), makeAnalyticsAttribute(rest)), {}, { children: children })); }; var TableBody = /*#__PURE__*/assignWithoutSideEffects(_TableBody, { componentId: ComponentIds.TableBody }); var StyledCell = /*#__PURE__*/styled(Cell).withConfig({ displayName: "TableBodyweb__StyledCell", componentId: "sc-1fpz8hn-1" })(function (_ref3) { var theme = _ref3.theme, $backgroundColor = _ref3.$backgroundColor; return { '&&&': { height: '100%', backgroundColor: getIn(theme.colors, $backgroundColor), '& > div:first-child': { alignSelf: 'stretch' }, '&:focus-visible': _objectSpread({}, getFocusRingStyles({ theme: theme, negativeOffset: true })) } }; }); var CellWrapper = /*#__PURE__*/styled(BaseBox).withConfig({ displayName: "TableBodyweb__CellWrapper", componentId: "sc-1fpz8hn-2" })(function (_ref4) { var theme = _ref4.theme, $rowDensity = _ref4.$rowDensity, showStripedRows = _ref4.showStripedRows, _ref4$hasPadding = _ref4.hasPadding, hasPadding = _ref4$hasPadding === void 0 ? true : _ref4$hasPadding, textAlign = _ref4.textAlign; var rowBackgroundTransition = "background-color ".concat(makeMotionTime(getIn(theme.motion, tableRow.backgroundColorMotionDuration)), " ").concat(getIn(theme.motion, tableRow.backgroundColorMotionEasing)); return { '&&&': _objectSpread({ transition: rowBackgroundTransition, backgroundColor: tableRow.nonStripeWrapper.backgroundColor, paddingLeft: hasPadding ? makeSpace(getIn(theme, tableRow.paddingLeft[$rowDensity])) : '0px', paddingRight: hasPadding ? makeSpace(getIn(theme, tableRow.paddingRight[$rowDensity])) : '0px', minHeight: makeSize(getIn(size, tableRow.minHeight[$rowDensity])), height: '100%', width: '100%', justifyContent: textAlign }, !showStripedRows && { borderBottomWidth: makeSpace(getIn(theme.border.width, tableRow.borderBottomWidth)), borderBottomColor: getIn(theme.colors, tableRow.borderColor), borderBottomStyle: 'solid' }) }; }); var _TableCell = function _TableCell(_ref5) { var children = _ref5.children, textAlign = _ref5.textAlign, _hasPadding = _ref5._hasPadding, rest = _objectWithoutProperties(_ref5, _excluded2); var isChildrenString = typeof children === 'string'; var _useTableContext2 = useTableContext(), selectionType = _useTableContext2.selectionType, rowDensity = _useTableContext2.rowDensity, showStripedRows = _useTableContext2.showStripedRows, backgroundColor = _useTableContext2.backgroundColor; var isSelectable = selectionType !== 'none'; return /*#__PURE__*/jsx(StyledCell, _objectSpread(_objectSpread(_objectSpread({ tabIndex: 0, role: "cell", $backgroundColor: backgroundColor }, metaAttribute({ name: MetaConstants.TableCell })), makeAnalyticsAttribute(rest)), {}, { children: /*#__PURE__*/jsx(BaseBox, { className: "cell-wrapper-base", display: "flex", alignItems: "center", height: "100%", children: /*#__PURE__*/jsx(CellWrapper, _objectSpread(_objectSpread({ className: "cell-wrapper", $rowDensity: rowDensity, showStripedRows: showStripedRows, display: "flex", alignItems: "center", hasPadding: _hasPadding, flex: 1, textAlign: textAlign // when a direct string child is passed we want to disable pointer events // for custom cells components, consumers can handle pointer events themselves , pointerEvents: isChildrenString && isSelectable ? 'none' : 'auto' // allow text to wrap, so that if the <Text> overflows it can truncate , whiteSpace: "normal", position: "relative" }, metaAttribute({ name: MetaConstants.TableCellWrapper })), {}, { children: isChildrenString ? /*#__PURE__*/jsx(Text, { size: "medium", truncateAfterLines: 1, children: children }) : children })) }) })); }; var TableCell = /*#__PURE__*/assignWithoutSideEffects(_TableCell, { componentId: ComponentIds.TableCell }); var TableCheckboxCell = function TableCheckboxCell(_ref6) { var isChecked = _ref6.isChecked, onChange = _ref6.onChange, isDisabled = _ref6.isDisabled; return /*#__PURE__*/jsx(TableCell, { children: /*#__PURE__*/jsx(BaseBox, { display: "flex", alignItems: "center", justifyContent: "center", flex: 1, width: makeSize(checkboxCellWidth), onClick: function onClick(e) { return e.stopPropagation(); }, children: /*#__PURE__*/jsx(Checkbox, { isDisabled: isDisabled, isChecked: isChecked, onChange: onChange }) }) }); }; var StyledRow = /*#__PURE__*/styled(Row).withConfig({ displayName: "TableBodyweb__StyledRow", componentId: "sc-1fpz8hn-3" })(function (_ref7) { var theme = _ref7.theme, $isSelectable = _ref7.$isSelectable, $isHoverable = _ref7.$isHoverable, $showBorderedCells = _ref7.$showBorderedCells; var _useTableContext3 = useTableContext(), hasHoverActions = _useTableContext3.hasHoverActions; var rowBackgroundTransition = "background-color ".concat(makeMotionTime(getIn(theme.motion, tableRow.backgroundColorMotionDuration)), " ").concat(getIn(theme.motion, tableRow.backgroundColorMotionEasing)); return { '&&&': _objectSpread(_objectSpread(_objectSpread(_objectSpread({ backgroundColor: 'transparent', '& .cell-wrapper': $showBorderedCells ? { borderRightWidth: makeSpace(getIn(theme.border.width, tableRow.borderBottomWidth)), borderRightStyle: 'solid', borderRightColor: getIn(theme.colors, tableRow.borderColor) } : undefined, '& td:last-child .cell-wrapper': { borderRight: 'none' } }, hasHoverActions ? _defineProperty({}, "@media ".concat(getMediaQuery({ min: theme.breakpoints.m })), { '& td:last-child': { opacity: 0, position: 'sticky', zIndex: 2, right: 0, width: '0px', '& > div:first-child': { overflow: 'visible' } }, '& td:last-child:focus-within': _objectSpread({ opacity: 1 }, getTableActionsHoverStyles({ theme: theme, hoverColor: tableRow.nonStripe.backgroundColor })), '&:hover td:last-child': { opacity: 1 } }) : {}), ($isHoverable || $isSelectable) && { '&:hover:not(.disabled-row) .cell-wrapper-base': _objectSpread({ transition: rowBackgroundTransition, cursor: 'pointer', backgroundColor: getIn(theme.colors, tableRow.nonStripe.backgroundColorHover) }, getTableActionsHoverStyles({ hoverColor: tableRow.nonStripe.backgroundColorHover, theme: theme })) }), $isSelectable && { '&:focus:not(.disabled-row) .cell-wrapper-base': _objectSpread({ transition: rowBackgroundTransition, backgroundColor: getIn(theme.colors, tableRow.nonStripe.backgroundColorFocus), cursor: 'pointer' }, getTableActionsHoverStyles({ hoverColor: tableRow.nonStripe.backgroundColorFocus, backgroundGradientColor: tableRow.nonStripe.backgroundColorHover, theme: theme })), '&:active:not(.disabled-row) .cell-wrapper-base': { transition: rowBackgroundTransition, backgroundColor: getIn(theme.colors, tableRow.nonStripe.backgroundColorActive), cursor: 'pointer' } }), {}, { '&:focus': getFocusRingStyles({ theme: theme, negativeOffset: true }) }) }; }); var _TableRow = function _TableRow(_ref9) { var children = _ref9.children, item = _ref9.item, isDisabled = _ref9.isDisabled, onHover = _ref9.onHover, _onClick = _ref9.onClick, hoverActions = _ref9.hoverActions, testID = _ref9.testID, rest = _objectWithoutProperties(_ref9, _excluded3); var _useTableContext4 = useTableContext(), selectionType = _useTableContext4.selectionType, selectedRows = _useTableContext4.selectedRows, toggleRowSelectionById = _useTableContext4.toggleRowSelectionById, setDisabledRows = _useTableContext4.setDisabledRows, showBorderedCells = _useTableContext4.showBorderedCells, setHasHoverActions = _useTableContext4.setHasHoverActions, isVirtualized = _useTableContext4.isVirtualized; var isSelectable = selectionType !== 'none'; var isMultiSelect = selectionType === 'multiple'; var isSelected = selectedRows === null || selectedRows === void 0 ? void 0 : selectedRows.includes(item.id); var hasHoverActions = Boolean(hoverActions); useEffect(function () { if (isDisabled) { setDisabledRows(function (prev) { return [].concat(_toConsumableArray(prev), [item.id]); }); } }, [isDisabled, item.id, setDisabledRows]); useIsomorphicLayoutEffect(function () { if (hasHoverActions) { setHasHoverActions(true); } }, [hasHoverActions]); return /*#__PURE__*/jsxs(StyledRow, _objectSpread(_objectSpread(_objectSpread(_objectSpread({ disabled: isDisabled, $isSelectable: isDisabled ? false : isSelectable, $isHoverable: isDisabled ? false : Boolean(onHover) || Boolean(_onClick), $showBorderedCells: showBorderedCells, item: item, className: isDisabled ? 'disabled-row' : '', onMouseEnter: function onMouseEnter() { return onHover === null || onHover === void 0 ? void 0 : onHover({ item: item }); }, onClick: function onClick() { return _onClick === null || _onClick === void 0 ? void 0 : _onClick({ item: item }); } }, makeAccessible({ selected: isSelected })), metaAttribute({ name: MetaConstants.TableRow, testID: testID })), makeAnalyticsAttribute(rest)), {}, { $isVirtualized: isVirtualized, children: [isMultiSelect && /*#__PURE__*/jsx(TableCheckboxCell, { isChecked: isSelected, onChange: function onChange() { return !isDisabled && toggleRowSelectionById(item.id); }, isDisabled: isDisabled }), children, hoverActions ? /*#__PURE__*/jsx(TableCell, { _hasPadding: false, children: /*#__PURE__*/jsx(BaseBox, { className: classes.HOVER_ACTIONS, position: { base: 'relative', m: 'absolute' }, top: "spacing.0", right: "spacing.0", height: "100%", flexShrink: 0, flexGrow: 1, width: "max-content", children: /*#__PURE__*/jsx(BaseBox, { className: classes.HOVER_ACTIONS_LAYER2, height: "100%", width: "max-content", display: "flex", alignItems: "center", children: /*#__PURE__*/jsx(BaseBox, { height: "100%", width: "max-content", className: classes.HOVER_ACTIONS_LAYER3, display: "flex", alignItems: "center", paddingLeft: { base: 'spacing.4', m: 'spacing.6' }, paddingRight: "spacing.4", gap: "spacing.3", children: hoverActions }) }) }) }) : null] })); }; var _Virtulized = function _Virtulized(_ref10) { var headerHeight = _ref10.headerHeight, rowHeight = _ref10.rowHeight, children = _ref10.children; var _React$Children$toArr = React__default.Children.toArray(children), _React$Children$toArr2 = _slicedToArray(_React$Children$toArr, 2), _React$Children$toArr3 = _React$Children$toArr2[0], parsedHeader = _React$Children$toArr3 === void 0 ? null : _React$Children$toArr3, _React$Children$toArr4 = _React$Children$toArr2[1], parsedBody = _React$Children$toArr4 === void 0 ? null : _React$Children$toArr4; var _useTableContext5 = useTableContext(), rowDensity = _useTableContext5.rowDensity, tableData = _useTableContext5.tableData; var _tableRow = Number(tableRow.minHeight[rowDensity]); var _rowHeight = function _rowHeight(item, index) { if (index === 0) { return headerHeight !== null && headerHeight !== void 0 ? headerHeight : _tableRow; } return rowHeight ? rowHeight(item, index - 1) : _tableRow; }; var bodyFunction = /*#__PURE__*/React__default.isValidElement(parsedBody) && parsedBody.props && _typeof(parsedBody.props) === 'object' && 'children' in parsedBody.props ? parsedBody.props.children : null; return /*#__PURE__*/jsx(Virtualized, { tableList: tableData, rowHeight: _rowHeight, header: function header() { return parsedHeader; }, body: bodyFunction }); }; var TableRow = /*#__PURE__*/assignWithoutSideEffects(_TableRow, { componentId: ComponentIds.TableRow }); var TableVirtualizedWrapper = /*#__PURE__*/assignWithoutSideEffects(_Virtulized, { componentId: ComponentIds.VirtualizedTable }); export { CellWrapper, StyledCell, TableBody, TableCell, TableRow, TableVirtualizedWrapper }; //# sourceMappingURL=TableBody.web.js.map