UNPKG

@salesforce/design-system-react

Version:

Salesforce Lightning Design System for React

201 lines (187 loc) 8.63 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */ /* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */ // ### React import React, { useCallback, useContext, useMemo } from 'react'; import PropTypes from 'prop-types'; // ### classNames import classNames from 'classnames'; // ### find import find from 'lodash.find'; // ## Children import Checkbox from '../../checkbox'; import Radio from '../../radio'; // ## Constants import { DATA_TABLE_ROW, DATA_TABLE_ROW_ACTIONS, DATA_TABLE_CELL } from '../../../utilities/constants'; import InteractiveElement from '../interactive-element'; import CellContext from '../private/cell-context'; import TableContext from '../private/table-context'; import useContextHelper from './context-helper'; var InteractiveCheckbox = InteractiveElement(Checkbox); var InteractiveRadio = InteractiveElement(Radio); var propTypes = { assistiveText: PropTypes.shape({ actionsHeader: PropTypes.string, columnSort: PropTypes.string, columnSortedAscending: PropTypes.string, columnSortedDescending: PropTypes.string, selectAllRows: PropTypes.string, selectRow: PropTypes.string }), canSelectRows: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['checkbox', 'radio'])]), className: PropTypes.string, columns: PropTypes.arrayOf(PropTypes.shape({ Cell: PropTypes.func, props: PropTypes.object })), /** * Use this if you are creating an advanced table (selectable, sortable, or resizable rows) */ fixedLayout: PropTypes.bool, id: PropTypes.string.isRequired, item: PropTypes.object.isRequired, onToggle: PropTypes.func, rowActions: PropTypes.element, selection: PropTypes.array, tableId: PropTypes.string, disabledSelection: PropTypes.array }; /** * Used internally, provides row rendering to the DataTable. */ var DataTableRow = function DataTableRow(props) { var tableContext = useContext(TableContext); var selectRowCellContext = useMemo(function () { return { rowIndex: props.rowIndex, columnIndex: 0 }; }, [props.rowIndex]); var _useContextHelper = useContextHelper(tableContext, selectRowCellContext, props.fixedLayout), tabIndex = _useContextHelper.tabIndex, hasFocus = _useContextHelper.hasFocus, handleFocus = _useContextHelper.handleFocus, handleKeyDown = _useContextHelper.handleKeyDown; var item = props.item, onToggle = props.onToggle; var handleToggle = useCallback(function (e, _ref) { var checked = _ref.checked; return onToggle(item, checked, e); }, [item, onToggle]); var isSelected = !!find(props.selection, item); var isDisabled = props.disabledSelection && !!find(props.disabledSelection, item); var ariaProps = useMemo(function () { var result = {}; if (props.canSelectRows) { result['aria-selected'] = isSelected ? 'true' : 'false'; } return result; }, [isSelected, props.canSelectRows]); var radionSelection = useMemo(function () { return isDisabled ? /*#__PURE__*/React.createElement(Radio, { assistiveText: { label: "".concat(props.assistiveText.selectRow, " ").concat(Number(props.index) + 1) }, "aria-labelledby": "".concat(props.id, "-SelectRow-label ").concat(props.tableId, "-SLDSDataTableHead-column-group-header-row-select"), checked: isSelected, className: "slds-m-right_x-small", id: "".concat(props.id, "-SelectRow"), labelId: "".concat(props.id, "-SelectRow-label"), name: "".concat(props.tableId, "-SelectRow"), disabled: isDisabled }) : /*#__PURE__*/React.createElement(InteractiveRadio, { assistiveText: { label: "".concat(props.assistiveText.selectRow, " ").concat(Number(props.index) + 1) }, "aria-labelledby": "".concat(props.id, "-SelectRow-label ").concat(props.tableId, "-SLDSDataTableHead-column-group-header-row-select"), checked: isSelected, className: "slds-m-right_x-small", id: "".concat(props.id, "-SelectRow"), labelId: "".concat(props.id, "-SelectRow-label"), name: "".concat(props.tableId, "-SelectRow"), onChange: handleToggle, disabled: isDisabled }); }, [handleToggle, isSelected, isDisabled, props.assistiveText.selectRow, props.id, props.index, props.tableId]); var checkboxSelection = useMemo(function () { return isDisabled ? /*#__PURE__*/React.createElement(Checkbox, { assistiveText: { label: "".concat(props.assistiveText.selectRow, " ").concat(Number(props.index) + 1) }, "aria-labelledby": "".concat(props.id, "-SelectRow-label ").concat(props.tableId, "-SLDSDataTableHead-column-group-header-row-select"), checked: isSelected, id: "".concat(props.id, "-SelectRow"), labelId: "".concat(props.id, "-SelectRow-label"), name: "SelectRow".concat(props.index + 1), disabled: isDisabled }) : /*#__PURE__*/React.createElement(InteractiveCheckbox, { assistiveText: { label: "".concat(props.assistiveText.selectRow, " ").concat(Number(props.index) + 1) }, "aria-labelledby": "".concat(props.id, "-SelectRow-label ").concat(props.tableId, "-SLDSDataTableHead-column-group-header-row-select"), checked: isSelected, id: "".concat(props.id, "-SelectRow"), labelId: "".concat(props.id, "-SelectRow-label"), name: "SelectRow".concat(props.index + 1), onChange: handleToggle, disabled: isDisabled }); }, [handleToggle, isSelected, isDisabled, props.assistiveText.selectRow, props.id, props.index, props.tableId]); // i18n return /*#__PURE__*/React.createElement("tr", _extends({}, ariaProps, { className: classNames(props.className, { 'slds-hint-parent': props.rowActions, 'slds-is-selected': props.canSelectRows && isSelected, 'slds-has-focus': hasFocus }) }), useMemo(function () { return /*#__PURE__*/React.createElement(React.Fragment, null, props.canSelectRows ? /*#__PURE__*/ // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions React.createElement("td", { role: props.fixedLayout ? 'gridcell' : null, className: "slds-text-align_right", "data-label": props.stacked ? 'Select Row' : undefined, style: { width: '3.25rem' }, onFocus: handleFocus, onKeyDown: handleKeyDown, ref: function ref(_ref2) { if (_ref2 && hasFocus) { _ref2.focus(); } }, tabIndex: tabIndex }, /*#__PURE__*/React.createElement(CellContext.Provider, { value: selectRowCellContext }, props.canSelectRows === 'radio' ? radionSelection : checkboxSelection)) : null, props.columns.map(function (column, index) { var Cell = column.Cell; var cellId = "".concat(props.id, "-").concat(DATA_TABLE_CELL, "-").concat(column.props.property); return /*#__PURE__*/React.createElement(CellContext.Provider, { key: cellId, value: { columnIndex: props.canSelectRows ? index + 1 : index, rowIndex: props.rowIndex } }, /*#__PURE__*/React.createElement(Cell, _extends({}, column.props, { className: column.props.truncate ? 'slds-truncate' : null, fixedLayout: props.fixedLayout, rowHeader: column.props.primaryColumn, id: cellId, item: item, width: column.props.width, headerId: item.headerId, columns: props.columns }), item[column.props.property])); }), /*#__PURE__*/React.createElement(CellContext.Provider, { value: { columnIndex: props.canSelectRows ? props.columns.length + 1 : props.columns.length, rowIndex: props.rowIndex } }, props.rowActions ? /*#__PURE__*/React.cloneElement(props.rowActions, { id: "".concat(props.id, "-").concat(DATA_TABLE_ROW_ACTIONS), item: item, fixedLayout: props.fixedLayout }) : null)); }, [handleFocus, handleKeyDown, hasFocus, item, tabIndex, props.canSelectRows, props.columns, props.fixedLayout, props.id, props.rowActions, props.rowIndex, props.stacked, selectRowCellContext, checkboxSelection, radionSelection])); }; DataTableRow.displayName = DATA_TABLE_ROW; DataTableRow.propTypes = propTypes; export default DataTableRow; //# sourceMappingURL=row.js.map