terra-clinical-data-grid
Version:
An organizational component that renders a collection of data in a grid-like format.
203 lines (200 loc) • 12 kB
JavaScript
;
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _bind = _interopRequireDefault(require("classnames/bind"));
var _terraThemeContext = _interopRequireDefault(require("terra-theme-context"));
var _memoizeOne = _interopRequireDefault(require("memoize-one"));
var _keycodeJs = require("keycode-js");
var _CellModule = _interopRequireDefault(require("./Cell.module.scss"));
var _excluded = ["sectionId", "rowId", "columnId", "isSelectable", "isSelected", "width", "onSelect", "children", "selectableRefCallback", "onHoverStart", "onHoverEnd", "ariaLabel", "isColumnHighlighted", "isFirstRow", "isLastRow"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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 _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var cx = _bind.default.bind(_CellModule.default);
var propTypes = {
/**
* Accessible text provided for the Cell.
*/
ariaLabel: _propTypes.default.string,
/**
* String identifier of the section in which the Cell will be rendered.
*/
sectionId: _propTypes.default.string.isRequired,
/**
* String identifier of the row in which the Cell will be rendered.
*/
rowId: _propTypes.default.string.isRequired,
/**
* String identifier of the column in which the Cell will be rendered.
*/
columnId: _propTypes.default.string.isRequired,
/**
* String-formatted width that the Cell should be rendered as. Values are suggested to be in `rem`s (ex `'5rem'`), but any valid CSS height value is accepted.
*/
width: _propTypes.default.string.isRequired,
/**
* Boolean indicating whether the Cell is selectable.
*/
isSelectable: _propTypes.default.bool,
/**
* Boolean indicating whether the Cell is actively selected.
*/
isSelected: _propTypes.default.bool,
/**
* Function that will be called upon Cell selection. The `isSelectable` prop must be true for this function to be called.
* Parameters: `onSelect(sectionId, rowId, columnId)`
*/
onSelect: _propTypes.default.func,
/**
* Function that will be called upon the mouse entering the selectable region of the Cell. The `isSelectable` prop must be true for this function to be called.
* Parameters: `onHoverStart(event)`
*/
onHoverStart: _propTypes.default.func,
/**
* Function that will be called upon the mouse leaving the selectable region of the Cell. The `isSelectable` prop must be true for this function to be called.
* Parameters: `onHoverEnd(event)`
*/
onHoverEnd: _propTypes.default.func,
/**
* Content that will rendered within the Cell.
*/
children: _propTypes.default.node,
/**
* Function that will be called with a ref to the Cell's selectable element. Parameters: `selectableRefCallback(selectableRef)`
*/
selectableRefCallback: _propTypes.default.func,
/**
* Boolean indicating whether the cell should be highlighted with background styling.
*/
isColumnHighlighted: _propTypes.default.bool,
/**
* Boolean indicating whether the cell is in the first row, also used with column highlight background styling.
*/
isFirstRow: _propTypes.default.bool,
/**
* Boolean indicating whether the cell is in the last row, also used with column highlight background styling.
*/
isLastRow: _propTypes.default.bool
};
var Cell = /*#__PURE__*/function (_React$Component) {
_inherits(Cell, _React$Component);
function Cell(props) {
var _this;
_classCallCheck(this, Cell);
_this = _callSuper(this, Cell, [props]);
_this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_this));
_this.handleTargetClick = _this.handleTargetClick.bind(_assertThisInitialized(_this));
_this.getCellStyles = (0, _memoizeOne.default)(_this.getCellStyles);
return _this;
}
_createClass(Cell, [{
key: "handleKeyDown",
value: function handleKeyDown(event) {
if (event.nativeEvent.keyCode === _keycodeJs.KEY_RETURN || event.nativeEvent.keyCode === _keycodeJs.KEY_SPACE) {
var onSelect = this.props.onSelect;
if (onSelect) {
event.preventDefault();
onSelect(this.props.sectionId, this.props.rowId, this.props.columnId);
}
}
}
}, {
key: "handleTargetClick",
value: function handleTargetClick() {
var onSelect = this.props.onSelect;
if (onSelect) {
onSelect(this.props.sectionId, this.props.rowId, this.props.columnId);
}
}
/* eslint-disable class-methods-use-this */
/**
* This function is memoized in the constructor so that for a given width value, the same object reference will be returned.
* This allows for repeat renders of the Cell to occur more efficiently if the width value has not changed between renders.
*/
}, {
key: "getCellStyles",
value: function getCellStyles(width) {
return {
width: width
};
}
/* eslint-enable class-methods-use-this */
}, {
key: "render",
value: function render() {
var _this$props = this.props,
sectionId = _this$props.sectionId,
rowId = _this$props.rowId,
columnId = _this$props.columnId,
isSelectable = _this$props.isSelectable,
isSelected = _this$props.isSelected,
width = _this$props.width,
onSelect = _this$props.onSelect,
children = _this$props.children,
selectableRefCallback = _this$props.selectableRefCallback,
onHoverStart = _this$props.onHoverStart,
onHoverEnd = _this$props.onHoverEnd,
ariaLabel = _this$props.ariaLabel,
isColumnHighlighted = _this$props.isColumnHighlighted,
isFirstRow = _this$props.isFirstRow,
isLastRow = _this$props.isLastRow,
customProps = _objectWithoutProperties(_this$props, _excluded);
/* eslint-disable react/forbid-dom-props, jsx-a11y/no-static-element-interactions */
var theme = this.context;
var role = isSelectable ? 'button' : undefined;
var tabIndex = isSelectable ? '0' : undefined;
var highlightCellClassNames = !isSelected && isColumnHighlighted && cx([{
highlighted: !isSelectable
}, {
'highlighted-selectable': isSelectable
}, {
first: isFirstRow
}, {
last: isLastRow
}]);
return /*#__PURE__*/_react.default.createElement("div", _extends({}, customProps, {
className: (0, _classnames.default)(cx('container', theme.className), customProps.className),
style: this.getCellStyles(width),
"aria-selected": isSelected ? true : undefined
}), /*#__PURE__*/_react.default.createElement("div", {
role: role,
className: cx(['content', highlightCellClassNames, {
selectable: isSelectable
}, {
selected: isSelected
}]),
onClick: isSelectable ? this.handleTargetClick : undefined,
onKeyDown: isSelectable ? this.handleKeyDown : undefined,
onMouseEnter: onHoverStart,
onMouseLeave: onHoverEnd,
tabIndex: tabIndex,
ref: selectableRefCallback,
"aria-label": ariaLabel
}, children));
/* eslint-enable react/forbid-dom-props, jsx-a11y/no-static-element-interactions */
}
}]);
return Cell;
}(_react.default.Component);
Cell.propTypes = propTypes;
Cell.contextType = _terraThemeContext.default;
var _default = exports.default = Cell;