@elastic/eui
Version:
Elastic UI Component Library
121 lines (119 loc) • 5.05 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "align", "onSort", "isSorted", "isSortAscending", "className", "scope", "mobileOptions", "width", "style", "readOnly", "description", "append"];
/*
* 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 classNames from 'classnames';
import { useEuiMemoizedStyles, LEFT_ALIGNMENT } from '../../services';
import { EuiI18n } from '../i18n';
import { EuiScreenReaderOnly } from '../accessibility';
import { EuiIcon } from '../icon';
import { EuiInnerText } from '../inner_text';
import { resolveWidthAsStyle } from './utils';
import { useEuiTableIsResponsive } from './mobile/responsive_context';
import { EuiTableCellContent } from './_table_cell_content';
import { euiTableHeaderFooterCellStyles } from './table_cells_shared.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
var CellContents = function CellContents(_ref) {
var className = _ref.className,
align = _ref.align,
description = _ref.description,
children = _ref.children,
canSort = _ref.canSort,
isSorted = _ref.isSorted,
isSortAscending = _ref.isSortAscending;
return ___EmotionJSX(EuiTableCellContent, {
className: className,
align: align,
textOnly: false,
truncateText: null
}, ___EmotionJSX(EuiInnerText, null, function (ref, innerText) {
return ___EmotionJSX(EuiI18n, {
token: "euiTableHeaderCell.titleTextWithDesc",
default: "{innerText}; {description}",
values: {
innerText: innerText,
description: description
}
}, function (titleTextWithDesc) {
return ___EmotionJSX("span", {
title: description ? titleTextWithDesc : innerText,
ref: ref,
className: "eui-textTruncate"
}, children);
});
}), description && ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("span", null, description)), isSorted ? ___EmotionJSX(EuiIcon, {
className: "euiTableSortIcon",
type: isSortAscending ? 'sortUp' : 'sortDown',
size: "m"
}) : canSort ? ___EmotionJSX(EuiIcon, {
className: "euiTableSortIcon euiTableSortIcon--sortable",
type: "sortable",
size: "m",
color: "subdued" // Tinted a bit further via CSS
}) : null);
};
export var EuiTableHeaderCell = function EuiTableHeaderCell(_ref2) {
var children = _ref2.children,
_ref2$align = _ref2.align,
align = _ref2$align === void 0 ? LEFT_ALIGNMENT : _ref2$align,
onSort = _ref2.onSort,
isSorted = _ref2.isSorted,
isSortAscending = _ref2.isSortAscending,
className = _ref2.className,
scope = _ref2.scope,
mobileOptions = _ref2.mobileOptions,
width = _ref2.width,
style = _ref2.style,
readOnly = _ref2.readOnly,
description = _ref2.description,
append = _ref2.append,
rest = _objectWithoutProperties(_ref2, _excluded);
var styles = useEuiMemoizedStyles(euiTableHeaderFooterCellStyles);
var isResponsive = useEuiTableIsResponsive();
var hideForDesktop = !isResponsive && (mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.only);
var hideForMobile = isResponsive && (mobileOptions === null || mobileOptions === void 0 ? void 0 : mobileOptions.show) === false;
if (hideForDesktop || hideForMobile) return null;
var classes = classNames('euiTableHeaderCell', className);
var inlineStyles = resolveWidthAsStyle(style, width);
var CellComponent = children ? 'th' : 'td';
var cellScope = CellComponent === 'th' ? scope !== null && scope !== void 0 ? scope : 'col' : undefined; // `scope` is only valid on `th` elements
var canSort = !!(onSort && !readOnly);
var ariaSortValue;
if (isSorted) {
ariaSortValue = isSortAscending ? 'ascending' : 'descending';
} else if (canSort) {
ariaSortValue = 'none';
}
var cellContentsProps = {
css: styles.euiTableHeaderCell__content,
align: align,
description: description,
canSort: canSort,
isSorted: isSorted,
isSortAscending: isSortAscending,
children: children
};
return ___EmotionJSX(CellComponent, _extends({
css: styles.euiTableHeaderCell,
className: classes,
scope: cellScope,
role: "columnheader",
"aria-sort": ariaSortValue,
style: inlineStyles
}, rest), canSort ? ___EmotionJSX("button", {
type: "button",
css: styles.euiTableHeaderCell__button,
className: classNames('euiTableHeaderButton', {
'euiTableHeaderButton-isSorted': isSorted
}),
onClick: onSort,
"data-test-subj": "tableHeaderSortButton"
}, ___EmotionJSX(CellContents, cellContentsProps)) : ___EmotionJSX(CellContents, cellContentsProps), append);
};