UNPKG

@procore/core-react

Version:
476 lines (472 loc) • 22 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 = ["children", "inline", "variant"], _excluded2 = ["expanded", "aria-label"], _excluded3 = ["children", "shelf"], _excluded4 = ["children"], _excluded5 = ["children"], _excluded6 = ["sortOrder", "onClick", "onKeyUp", "onSortOrderChange", "sortable", "children", "snugfit", "variant"], _excluded7 = ["children"], _excluded8 = ["children"], _excluded9 = ["children", "colSpan", "depth"], _excluded0 = ["children", "overdue"], _excluded1 = ["children", "snugfit", "variant"], _excluded10 = ["children", "value", "currencyOptions"], _excluded11 = ["children", "className", "disabled", "errorMessage", "processing"], _excluded12 = ["children"], _excluded13 = ["disabled", "errorMessage", "processing", "size"], _excluded14 = ["children", "href"], _excluded15 = ["children"], _excluded16 = ["block", "children", "disabled", "errorMessage", "className", "processing"], _excluded17 = ["disabled", "errorMessage", "processing", "resizable"], _excluded18 = ["children"]; 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 _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 _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], -1 === t.indexOf(o) && {}.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 (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import { CurrencyFormatter } from '@procore/globalization-toolkit'; import { useId } from '@react-aria/utils'; import { VisuallyHidden } from '@react-aria/visually-hidden'; import cx from 'classnames'; import React from 'react'; import { Link as LinkElement } from '../Link'; import { tableSelectClassName } from '../Select/Select.styles'; import { Spinner } from '../Spinner'; import { useI18nContext } from '../_hooks/I18n'; import { mergeAriaIds } from '../_utils/a11y'; import { addSubcomponents } from '../_utils/addSubcomponents'; import { StyledCarat, StyledCaratWrapper, StyledCheckbox, StyledHead, StyledIconCell, StyledLoadingContainer, StyledSelectCellWrapper, StyledTable, StyledTableBodyCell, StyledTableBodyRow, StyledTableCellDateInput, StyledTableCellInput, StyledTableCellSelect, StyledTableCellText, StyledTableCellTextArea, StyledTableGroupRow, StyledTableGroupRowContent, StyledTableGroupRowHeader, StyledTableGroupRowTitle, StyledTableHeaderCell, StyledTableHeaderCellIconWrapper, StyledTableHeaderCellLabel, StyledTableHeaderCellSortableContainer, StyledTableHeaderCellSortIcon, StyledTableInlineEditContainer, StyledTableInlineEditErrorBox, StyledTableWrapper } from './Table.styles'; export var Table_ = /*#__PURE__*/React.forwardRef(function Table(_ref, ref) { var children = _ref.children, inline = _ref.inline, variant = _ref.variant, props = _objectWithoutProperties(_ref, _excluded); return /*#__PURE__*/React.createElement(StyledTable, _extends({ ref: ref }, props, { $inline: inline, $fixed: variant === 'fixed' }), children); }); export var Carat = /*#__PURE__*/React.forwardRef(function Carat(_ref2, ref) { var _ref2$expanded = _ref2.expanded, expanded = _ref2$expanded === void 0 ? false : _ref2$expanded, ariaLabel = _ref2['aria-label'], props = _objectWithoutProperties(_ref2, _excluded2); var I18n = useI18nContext(); var defaultLabel = I18n.t(expanded ? 'collapse' : 'expand', { scope: 'core.table.a11y' }); return /*#__PURE__*/React.createElement(StyledCaratWrapper, _extends({ ref: ref, "aria-label": ariaLabel !== null && ariaLabel !== void 0 ? ariaLabel : defaultLabel, "aria-expanded": expanded, variant: "tertiary", size: "sm", icon: /*#__PURE__*/React.createElement(StyledCarat, { $expanded: expanded }) }, props)); }); export var Container = /*#__PURE__*/React.forwardRef(function Container(_ref3, ref) { var children = _ref3.children, _ref3$shelf = _ref3.shelf, shelf = _ref3$shelf === void 0 ? false : _ref3$shelf, props = _objectWithoutProperties(_ref3, _excluded3); return /*#__PURE__*/React.createElement(StyledTableWrapper, _extends({}, props, { ref: ref, shadowStrength: 0, $withShelf: shelf }), children); }); export var Header = /*#__PURE__*/React.forwardRef(function Header(_ref4, ref) { var children = _ref4.children, props = _objectWithoutProperties(_ref4, _excluded4); return /*#__PURE__*/React.createElement(StyledHead, _extends({ ref: ref }, props), children); }); export var HeaderRow = /*#__PURE__*/React.forwardRef(function HeaderRow(_ref5, ref) { var children = _ref5.children, props = _objectWithoutProperties(_ref5, _excluded5); return /*#__PURE__*/React.createElement("tr", _extends({ ref: ref }, props), children); }); export var HeaderCell = /*#__PURE__*/React.forwardRef(function HeaderCell(_ref6, ref) { var sortOrder_ = _ref6.sortOrder, _onClick = _ref6.onClick, _onKeyUp = _ref6.onKeyUp, onSortOrderChange_ = _ref6.onSortOrderChange, sortable = _ref6.sortable, children = _ref6.children, _ref6$snugfit = _ref6.snugfit, snugfit = _ref6$snugfit === void 0 ? false : _ref6$snugfit, variant = _ref6.variant, props = _objectWithoutProperties(_ref6, _excluded6); var I18n = useI18nContext(); var descriptionId = useId(); var sortOrder = sortOrder_ !== null && sortOrder_ !== void 0 ? sortOrder_ : variant !== 'snugfit' ? variant : undefined; var isSortable = sortOrder !== undefined && sortable !== false; var ariaSortValues = { asc: 'ascending', desc: 'descending', '': 'none' }; var onSortOrderChange = function onSortOrderChange() { if (!onSortOrderChange_) { return; } var nextValue = sortOrder === '' ? 'asc' : sortOrder === 'asc' ? 'desc' : ''; onSortOrderChange_(nextValue); }; return /*#__PURE__*/React.createElement(StyledTableHeaderCell, _extends({ ref: ref }, props, { role: "columnheader", "aria-sort": isSortable ? ariaSortValues[sortOrder !== null && sortOrder !== void 0 ? sortOrder : ''] : undefined, tabIndex: isSortable ? 0 : undefined, "aria-describedby": isSortable ? descriptionId : undefined, $sortable: isSortable, $sortOrder: sortOrder, $snugfit: snugfit || variant === 'snugfit', onKeyUp: function onKeyUp(e) { if (['Space', 'Enter'].includes(e.code) && isSortable) { onSortOrderChange(); } return _onKeyUp === null || _onKeyUp === void 0 ? void 0 : _onKeyUp(e); }, onClick: function onClick(e) { if (isSortable) { onSortOrderChange(); } return _onClick === null || _onClick === void 0 ? void 0 : _onClick(e); } }), /*#__PURE__*/React.createElement(StyledTableHeaderCellSortableContainer, null, /*#__PURE__*/React.createElement(StyledTableHeaderCellLabel, null, children), isSortable && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledTableHeaderCellIconWrapper, { "aria-hidden": "true" }, /*#__PURE__*/React.createElement(StyledTableHeaderCellSortIcon, null)), /*#__PURE__*/React.createElement(VisuallyHidden, { "aria-hidden": "true", id: descriptionId }, I18n.t('core.table.sortableColumn'))))); }); export var Body = /*#__PURE__*/React.forwardRef(function Body(_ref7, ref) { var children = _ref7.children, props = _objectWithoutProperties(_ref7, _excluded7); return /*#__PURE__*/React.createElement("tbody", _extends({ ref: ref }, props), children); }); export var Checkbox = /*#__PURE__*/React.forwardRef(function Checkbox(props, ref) { var I18n = useI18nContext(); return /*#__PURE__*/React.createElement(StyledCheckbox, _extends({ "aria-label": I18n.t('core.table.checkboxLabel'), ref: ref }, props)); }); export var GroupTitle = /*#__PURE__*/React.forwardRef(function GroupTitle(_ref8, ref) { var children = _ref8.children, props = _objectWithoutProperties(_ref8, _excluded8); return /*#__PURE__*/React.createElement(StyledTableGroupRowTitle, _extends({ ref: ref }, props), children); }); export var Group = /*#__PURE__*/React.forwardRef(function Group(_ref9, ref) { var children = _ref9.children, _ref9$colSpan = _ref9.colSpan, colSpan = _ref9$colSpan === void 0 ? 1000 : _ref9$colSpan, _ref9$depth = _ref9.depth, depth = _ref9$depth === void 0 ? false : _ref9$depth, props = _objectWithoutProperties(_ref9, _excluded9); return /*#__PURE__*/React.createElement(StyledTableGroupRow, _extends({ ref: ref, $depth: depth }, props), /*#__PURE__*/React.createElement("td", { colSpan: colSpan }, /*#__PURE__*/React.createElement(StyledTableGroupRowContent, null, /*#__PURE__*/React.createElement(StyledTableGroupRowHeader, null, children)))); }); export var BodyRow = /*#__PURE__*/React.forwardRef(function BodyRow(_ref0, ref) { var children = _ref0.children, _ref0$overdue = _ref0.overdue, overdue = _ref0$overdue === void 0 ? false : _ref0$overdue, props = _objectWithoutProperties(_ref0, _excluded0); return /*#__PURE__*/React.createElement(StyledTableBodyRow, _extends({ ref: ref, $overdue: overdue }, props), children); }); export var BodyCell = /*#__PURE__*/React.forwardRef(function BodyCell(_ref1, ref) { var children = _ref1.children, _ref1$snugfit = _ref1.snugfit, snugfit = _ref1$snugfit === void 0 ? false : _ref1$snugfit, _ref1$variant = _ref1.variant, variant = _ref1$variant === void 0 ? '' : _ref1$variant, props = _objectWithoutProperties(_ref1, _excluded1); return /*#__PURE__*/React.createElement(StyledTableBodyCell, _extends({ ref: ref, $snugfit: snugfit || variant === 'snugfit' }, props), children); }); export var CurrencyCell = /*#__PURE__*/React.forwardRef(function CurrencyCell(_ref10, ref) { var children = _ref10.children, value = _ref10.value, currencyOptions = _ref10.currencyOptions, props = _objectWithoutProperties(_ref10, _excluded10); var I18n = useI18nContext(); if (children) { return /*#__PURE__*/React.createElement(StyledTableCellText, _extends({ $currency: true, ref: ref }, props), children); } var internalCurrencyOptions = _objectSpread({ locale: I18n.locale, currencyIsoCode: 'USD' }, currencyOptions); var formatter = new CurrencyFormatter(internalCurrencyOptions); var internalValue = value ? formatter.formatCurrency(value) : null; return /*#__PURE__*/React.createElement(StyledTableCellText, _extends({ $currency: true, ref: ref }, props), internalValue); }); export var DateSelectCell = /*#__PURE__*/React.forwardRef(function DateSelectCell(_ref11, ref) { var children = _ref11.children, className = _ref11.className, _ref11$disabled = _ref11.disabled, disabled = _ref11$disabled === void 0 ? false : _ref11$disabled, errorMessage = _ref11.errorMessage, _ref11$processing = _ref11.processing, processing = _ref11$processing === void 0 ? false : _ref11$processing, props = _objectWithoutProperties(_ref11, _excluded11); var error = Boolean(errorMessage); var errorId = useId(); var showErrorMessage = !processing && error; return /*#__PURE__*/React.createElement(StyledTableInlineEditContainer, { $disabled: disabled, $error: error, $size: "block", className: cx({ disabled: disabled, error: error }) }, processing && /*#__PURE__*/React.createElement(StyledLoadingContainer, null, /*#__PURE__*/React.createElement(Spinner, { loading: processing, size: "md" })), /*#__PURE__*/React.createElement(StyledTableCellDateInput, _extends({ "aria-invalid": props.error || error, "aria-describedby": showErrorMessage ? errorId : undefined }, props, { disabled: processing || disabled, ref: ref, variant: error ? 'error' : '', error: props.error || error })), showErrorMessage && /*#__PURE__*/React.createElement(StyledTableInlineEditErrorBox, { id: errorId }, errorMessage)); }); export var IconCell = /*#__PURE__*/React.forwardRef(function IconCell(_ref12, ref) { var children = _ref12.children, props = _objectWithoutProperties(_ref12, _excluded12); return /*#__PURE__*/React.createElement(StyledIconCell, _extends({ ref: ref }, props), children); }); export var InputCell = /*#__PURE__*/React.forwardRef(function InputCell(_ref13, ref) { var _ref13$disabled = _ref13.disabled, disabled = _ref13$disabled === void 0 ? false : _ref13$disabled, _ref13$errorMessage = _ref13.errorMessage, errorMessage = _ref13$errorMessage === void 0 ? '' : _ref13$errorMessage, _ref13$processing = _ref13.processing, processing = _ref13$processing === void 0 ? false : _ref13$processing, _ref13$size = _ref13.size, size = _ref13$size === void 0 ? 'block' : _ref13$size, props = _objectWithoutProperties(_ref13, _excluded13); var error = Boolean(errorMessage); var errorId = useId(); var showErrorMessage = !processing && error; return /*#__PURE__*/React.createElement(StyledTableInlineEditContainer, { $disabled: disabled, $error: error, $size: size, className: cx({ disabled: disabled, error: error }) }, processing && /*#__PURE__*/React.createElement(StyledLoadingContainer, null, /*#__PURE__*/React.createElement(Spinner, { loading: processing, size: "md" })), /*#__PURE__*/React.createElement(StyledTableCellInput, _extends({ "aria-describedby": mergeAriaIds(props['aria-describedby'], showErrorMessage ? errorId : undefined) }, props, { ref: ref, error: props.error || error, disabled: processing || disabled })), showErrorMessage && /*#__PURE__*/React.createElement(StyledTableInlineEditErrorBox, { $size: size, id: errorId }, errorMessage)); }); export var LinkCell = /*#__PURE__*/React.forwardRef(function LinkCell(_ref14, ref) { var children = _ref14.children, _ref14$href = _ref14.href, href = _ref14$href === void 0 ? '' : _ref14$href, props = _objectWithoutProperties(_ref14, _excluded14); return /*#__PURE__*/React.createElement(TextCell, { ref: ref }, /*#__PURE__*/React.createElement(LinkElement, _extends({ href: href }, props), children)); }); export var PercentCell = /*#__PURE__*/React.forwardRef(function PercentCell(_ref15, ref) { var children = _ref15.children, props = _objectWithoutProperties(_ref15, _excluded15); return /*#__PURE__*/React.createElement(StyledTableCellText, _extends({ $percent: true, ref: ref }, props), children); }); export var SelectCell = /*#__PURE__*/React.forwardRef(function SelectCell(_ref16, ref) { var _ref16$block = _ref16.block, block = _ref16$block === void 0 ? true : _ref16$block, children = _ref16.children, disabled = _ref16.disabled, errorMessage = _ref16.errorMessage, className = _ref16.className, _ref16$processing = _ref16.processing, processing = _ref16$processing === void 0 ? false : _ref16$processing, props = _objectWithoutProperties(_ref16, _excluded16); var errorId = useId(); var error = Boolean(errorMessage); var hasClearIcon = !disabled && !props.loading && Boolean(props.label); var showErrorMessage = !processing && error; return /*#__PURE__*/React.createElement(StyledTableInlineEditContainer, { $disabled: disabled, $error: error, $size: "block", className: cx({ disabled: disabled, error: error }) }, processing && /*#__PURE__*/React.createElement(StyledLoadingContainer, null, /*#__PURE__*/React.createElement(Spinner, { loading: processing, size: "md" })), /*#__PURE__*/React.createElement(StyledSelectCellWrapper, null, /*#__PURE__*/React.createElement(StyledTableCellSelect, _extends({ "aria-invalid": props.error || error, "aria-describedby": mergeAriaIds(props['aria-describedby'], showErrorMessage ? errorId : undefined) }, props, { $placeholder: !props.label, $hasClearIcon: hasClearIcon, block: block, className: cx(tableSelectClassName, className), disabled: processing || disabled, error: props.error || error, ref: ref }), children)), showErrorMessage && /*#__PURE__*/React.createElement(StyledTableInlineEditErrorBox, { id: errorId }, errorMessage)); }); export var TextAreaCell = /*#__PURE__*/React.forwardRef(function TextAreaCell(_ref17, ref) { var disabled = _ref17.disabled, _ref17$errorMessage = _ref17.errorMessage, errorMessage = _ref17$errorMessage === void 0 ? '' : _ref17$errorMessage, _ref17$processing = _ref17.processing, processing = _ref17$processing === void 0 ? false : _ref17$processing, _ref17$resizable = _ref17.resizable, resizable = _ref17$resizable === void 0 ? false : _ref17$resizable, props = _objectWithoutProperties(_ref17, _excluded17); var errorId = useId(); var textareaRef = ref || /*#__PURE__*/React.createRef(); var setHeight = function setHeight() { if (textareaRef && textareaRef.current) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = "".concat(textareaRef.current.scrollHeight, "px"); } }; var error = Boolean(errorMessage); var showErrorMessage = !processing && error; return /*#__PURE__*/React.createElement(StyledTableInlineEditContainer, { $disabled: disabled, $error: error, $size: "block", className: cx({ disabled: disabled, error: error }) }, processing && /*#__PURE__*/React.createElement(StyledLoadingContainer, null, /*#__PURE__*/React.createElement(Spinner, { loading: processing, size: "md" })), /*#__PURE__*/React.createElement(StyledTableCellTextArea, _extends({ "aria-invalid": error, "aria-describedby": mergeAriaIds(props['aria-describedby'], showErrorMessage ? errorId : undefined) }, props, { $resizable: resizable, disabled: processing || disabled, onBlur: function onBlur(event) { setHeight(); props.onBlur && props.onBlur(event); }, ref: textareaRef, resize: "none", rows: 1, error: error })), showErrorMessage && /*#__PURE__*/React.createElement(StyledTableInlineEditErrorBox, { id: errorId }, errorMessage)); }); export var TextCell = /*#__PURE__*/React.forwardRef(function TextCell(_ref18, ref) { var children = _ref18.children, props = _objectWithoutProperties(_ref18, _excluded18); return /*#__PURE__*/React.createElement(StyledTableCellText, _extends({ ref: ref }, props), children); }); Table_.displayName = 'Table'; Body.displayName = 'Table.Body'; BodyCell.displayName = 'Table.BodyCell'; BodyRow.displayName = 'Table.BodyRow'; Carat.displayName = 'Table.Carat'; Checkbox.displayName = 'Table.Checkbox'; Container.displayName = 'Table.Container'; CurrencyCell.displayName = 'Table.CurrencyCell'; DateSelectCell.displayName = 'Table.DateSelect'; Group.displayName = 'Table.Group'; GroupTitle.displayName = 'Table.GroupTitle'; Header.displayName = 'Table.Header'; HeaderCell.displayName = 'Table.HeaderCell'; HeaderRow.displayName = 'Table.HeaderRow'; IconCell.displayName = 'Table.IconCell'; InputCell.displayName = 'Table.InputCell'; LinkCell.displayName = 'Table.LinkCell'; PercentCell.displayName = 'Table.ParentCell'; TextAreaCell.displayName = 'Table.TextAreaCell'; TextCell.displayName = 'Table.TextCell'; /** @since 10.19.0 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-table--demo) */ export var Table = addSubcomponents({ Body: Body, BodyCell: BodyCell, BodyRow: BodyRow, Carat: Carat, Checkbox: Checkbox, Container: Container, CurrencyCell: CurrencyCell, DateSelectCell: DateSelectCell, Group: Group, GroupTitle: GroupTitle, Header: Header, HeaderCell: HeaderCell, HeaderRow: HeaderRow, IconCell: IconCell, InputCell: InputCell, LinkCell: LinkCell, PercentCell: PercentCell, SelectCell: SelectCell, TextAreaCell: TextAreaCell, TextCell: TextCell }, Table_); //# sourceMappingURL=Table.js.map