UNPKG

@elastic/eui

Version:

Elastic UI Component Library

257 lines (256 loc) 14.5 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _excluded = ["align", "children", "className", "truncateText", "setScopeRow", "textOnly", "hasActions", "isExpander", "style", "width", "minWidth", "maxWidth", "valign", "mobileOptions", "append", "sticky"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } 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; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React from 'react'; import PropTypes from "prop-types"; import classNames from 'classnames'; import { useEuiMemoizedStyles, LEFT_ALIGNMENT } from '../../services'; import { useEuiTableIsResponsive } from './mobile/responsive_context'; import { resolveWidthPropsAsStyle } from './utils'; import { EuiTableCellContent } from './_table_cell_content'; import { euiTableRowCellStyles } from './table_row_cell.styles'; import { _useEuiTableStickyCellStyles } from './table_cells_shared.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiTableRowCell = function EuiTableRowCell(_ref) { var _ref$align = _ref.align, align = _ref$align === void 0 ? LEFT_ALIGNMENT : _ref$align, children = _ref.children, className = _ref.className, truncateText = _ref.truncateText, setScopeRow = _ref.setScopeRow, _ref$textOnly = _ref.textOnly, textOnly = _ref$textOnly === void 0 ? true : _ref$textOnly, hasActions = _ref.hasActions, isExpander = _ref.isExpander, _style = _ref.style, width = _ref.width, minWidth = _ref.minWidth, maxWidth = _ref.maxWidth, _ref$valign = _ref.valign, valign = _ref$valign === void 0 ? 'middle' : _ref$valign, mobileOptions = _ref.mobileOptions, append = _ref.append, sticky = _ref.sticky, rest = _objectWithoutProperties(_ref, _excluded); var isResponsive = useEuiTableIsResponsive(); var styles = useEuiMemoizedStyles(euiTableRowCellStyles); var stickyStyles = _useEuiTableStickyCellStyles(sticky); var cssStyles = [styles.euiTableRowCell, setScopeRow && styles.rowHeader, isExpander && styles.isExpander, hasActions && styles.hasActions, styles[valign]].concat(_toConsumableArray(isResponsive ? [styles.mobile.mobile, (mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.enlarge) && styles.mobile.enlarge, hasActions === 'custom' && styles.mobile.customActions, hasActions === true && styles.mobile.actions, isExpander && styles.mobile.expander] : [styles.desktop.desktop, hasActions && styles.desktop.actions, stickyStyles])); var cellClasses = classNames('euiTableRowCell', className, { 'euiTableRowCell--hasActions': hasActions, 'euiTableRowCell--isExpander': isExpander }); var getResponsiveWidth = function getResponsiveWidth(defaultWidth, mobileWidth) { if (isResponsive) { if (hasActions || isExpander) { // On mobile, actions are shifted to a right column via CSS return undefined; } return mobileWidth; } return defaultWidth; }; var inlineWidthStyles = resolveWidthPropsAsStyle(_style, { width: getResponsiveWidth(width, mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.width), minWidth: getResponsiveWidth(minWidth, mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.minWidth), maxWidth: getResponsiveWidth(maxWidth, mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.maxWidth) }); var Element = setScopeRow ? 'th' : 'td'; var sharedProps = _objectSpread({ scope: setScopeRow ? 'row' : undefined, style: _objectSpread(_objectSpread({}, _style), inlineWidthStyles), css: cssStyles }, rest); var sharedContentProps = { align: align, textOnly: textOnly, truncateText: truncateText, hasActions: hasActions || isExpander }; if (isResponsive) { // Mobile view if ((mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.show) === false) { return null; } else { var _mobileOptions$align, _mobileOptions$trunca, _mobileOptions$textOn; return ___EmotionJSX(Element, _extends({ className: cellClasses }, sharedProps), (mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.header) && ___EmotionJSX("div", { className: "euiTableRowCell__mobileHeader", css: styles.euiTableRowCell__mobileHeader }, mobileOptions.header), ___EmotionJSX(EuiTableCellContent, _extends({}, sharedContentProps, { align: (_mobileOptions$align = mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.align) !== null && _mobileOptions$align !== void 0 ? _mobileOptions$align : 'left' // Default to left aligned mobile cells, unless consumers specifically set an alignment for mobile , truncateText: (_mobileOptions$trunca = mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.truncateText) !== null && _mobileOptions$trunca !== void 0 ? _mobileOptions$trunca : truncateText, textOnly: (_mobileOptions$textOn = mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.textOnly) !== null && _mobileOptions$textOn !== void 0 ? _mobileOptions$textOn : textOnly }), (mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.render) || children), append); } } else { // Desktop view if (mobileOptions !== null && mobileOptions !== void 0 && mobileOptions.only) { return null; } else { return ___EmotionJSX(Element, _extends({ className: cellClasses }, sharedProps, { "data-sticky": sticky === null || sticky === void 0 ? void 0 : sticky.side }), ___EmotionJSX(EuiTableCellContent, sharedContentProps, children), append); } } }; EuiTableRowCell.propTypes = { className: PropTypes.string, "aria-label": PropTypes.string, "data-test-subj": PropTypes.string, css: PropTypes.any, /** * Vertical alignment of the content in the cell */ valign: PropTypes.any, /** * Indicates whether the cell should be marked as the heading for its row */ setScopeRow: PropTypes.bool, /** * Indicates if the cell is dedicated to row actions * (used for mobile styling and desktop action hover behavior) */ hasActions: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.oneOf(["custom"])]), /** * Indicates if the column is dedicated as the expandable row toggle */ isExpander: PropTypes.bool, /** * Mobile options for displaying differently at small screens; * See {@link EuiTableRowCellMobileOptionsShape} */ mobileOptions: PropTypes.shape({ /** * If false, will not render the cell at all for mobile * @default true */ show: PropTypes.bool, /** * Only show for mobile? If true, will not render the column at all for desktop * @default false */ only: PropTypes.bool, /** * Custom render/children if different from desktop */ render: PropTypes.node, /** * The column's header for use in mobile view (automatically passed down * when using `EuiBasicTable`). * Or pass `false` to not show a header at all. */ header: PropTypes.oneOfType([PropTypes.node.isRequired, PropTypes.bool.isRequired]), /** * Increase text size compared to rest of cells * @default false */ enlarge: PropTypes.bool, /** * Applies the value to the width of the cell in mobile view (typically 50%) * @default 50% */ width: PropTypes.oneOfType([PropTypes.any, PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired])]), /** * Horizontal alignment of the text in the cell */ align: PropTypes.any, /** * Creates a text wrapper around cell content that helps word break or truncate * long text correctly. * @default true */ textOnly: PropTypes.bool, /** * Indicates whether this column should truncate overflowing text content. * - Set to `true` to enable single-line truncation. * - To enable multi-line truncation, use a configuration object with `lines` * set to a number of lines to truncate to. * @default false */ truncateText: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.shape({ lines: PropTypes.number.isRequired }).isRequired]), minWidth: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]), maxWidth: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]) }), /** * Content rendered outside the visible cell content wrapper. Useful for, e.g. screen reader text. * * Used by EuiBasicTable to render hidden copy markers */ append: PropTypes.node, /** * Whether the cell should stick to a side of the table. * * This option is not applied in the responsive cards layout - see * {@link EuiTableProps#responsiveBreakpoint|`responsiveBreakpoint`}. * * Currently, it can only be used when the cell is in the first or the last * column of a table. * * When set to `true` and `hasBackground: false` is set on the table, * `--euiTableCellStickyBackgroundColor` CSS variable should be set to match * the background color of the element containing the table. * Otherwise, the sticky cell will use the default `backgroundBasePlain` * background color. * @internal * @beta * @default false */ sticky: PropTypes.shape({ /** * The side the cell should stick to. * In horizontal writing-mode, `start` equals the left side and `end` * the right side. */ side: PropTypes.oneOf(["start", "end"]).isRequired }), /** * Horizontal alignment of the text in the cell */ align: PropTypes.any, /** * Creates a text wrapper around cell content that helps word break or truncate * long text correctly. * @default true */ textOnly: PropTypes.bool, /** * Indicates whether this column should truncate overflowing text content. * - Set to `true` to enable single-line truncation. * - To enable multi-line truncation, use a configuration object with `lines` * set to a number of lines to truncate to. * @default false */ truncateText: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.shape({ lines: PropTypes.number.isRequired }).isRequired]), width: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]), minWidth: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]), maxWidth: PropTypes.oneOfType([PropTypes.string.isRequired, PropTypes.number.isRequired]) };