UNPKG

d2-ui

Version:
144 lines (124 loc) 7.15 kB
var _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component, isValidElement } from 'react'; import PropTypes from 'prop-types'; import classes from 'classnames'; import { isObject } from 'lodash/fp'; import { isString } from 'lodash/fp'; import IconButton from 'material-ui/IconButton'; import FontIcon from 'material-ui/FontIcon'; import MoreVert from 'material-ui/svg-icons/navigation/more-vert'; import addD2Context from '../component-helpers/addD2Context'; import { findValueRenderer } from './data-value/valueRenderers'; function getD2ModelValueType(dataSource, columnName) { return dataSource && dataSource.modelDefinition && dataSource.modelDefinition.modelValidations && dataSource.modelDefinition.modelValidations[columnName] && dataSource.modelDefinition.modelValidations[columnName].type; } var DataTableRow = addD2Context(function (_Component) { _inherits(_class2, _Component); function _class2() { var _ref; var _temp, _this, _ret; _classCallCheck(this, _class2); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = _class2.__proto__ || Object.getPrototypeOf(_class2)).call.apply(_ref, [this].concat(args))), _this), _this.iconMenuClick = function (event) { _this.props.itemClicked(event, _this.props.dataSource); }, _this.singleActionClick = function () { if (_this.hasSingleAction()) { _this.singleAction().action(_this.props.dataSource); } }, _this.handleContextClick = function (event) { event && event.preventDefault(); _this.props.itemClicked(event, _this.props.dataSource); }, _this.handleClick = function (event) { _this.props.primaryClick(_this.props.dataSource, event); }, _this.hasContextMenu = function () { return Object.keys(_this.props.contextMenuActions || {}).length > 1; }, _this.hasSingleAction = function () { return Object.keys(_this.props.contextMenuActions || {}).length === 1; }, _this.singleAction = function () { if (_this.hasSingleAction()) { var actionKeys = Object.keys(_this.props.contextMenuActions || {}); var label = actionKeys[0]; var action = _this.props.contextMenuActions[label]; var icon = _this.props.contextMenuIcons && _this.props.contextMenuIcons[label] ? _this.props.contextMenuIcons[label] : label; return { label: label, action: action, icon: icon }; } return null; }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(_class2, [{ key: 'render', value: function render() { var _this2 = this; var classList = classes('data-table__rows__row', { 'data-table__rows__row--even': !this.props.isOdd, 'data-table__rows__row--odd': this.props.isOdd }); var columns = this.props.columns.map(function (columnName, index) { var valueDetails = { valueType: getD2ModelValueType(_this2.props.dataSource, columnName), value: _this2.props.dataSource[columnName], columnName: columnName }; var Value = findValueRenderer(valueDetails); return React.createElement( 'div', { key: index, className: 'data-table__rows__row__column', onContextMenu: _this2.handleContextClick, onClick: _this2.handleClick }, React.createElement(Value, valueDetails) ); }); return React.createElement( 'div', { className: classList }, columns, this.hasContextMenu() && React.createElement( 'div', { className: 'data-table__rows__row__column', style: { width: '1%' } }, React.createElement( IconButton, { tooltip: this.context.d2.i18n.getTranslation('actions'), onClick: this.iconMenuClick }, React.createElement(MoreVert, null) ) ), this.hasSingleAction() && React.createElement( 'div', { className: 'data-table__rows__row__column', style: { width: '1%' } }, React.createElement( IconButton, { tooltip: this.context.d2.i18n.getTranslation(this.singleAction().label), onClick: this.singleActionClick }, React.createElement( FontIcon, { className: 'material-icons' }, this.singleAction().icon ) ) ) ); } }]); return _class2; }(Component)); DataTableRow.propTypes = { columns: PropTypes.arrayOf(PropTypes.string).isRequired, dataSource: PropTypes.object, isEven: PropTypes.bool, isOdd: PropTypes.bool, itemClicked: PropTypes.func.isRequired, primaryClick: PropTypes.func.isRequired, contextMenuActions: PropTypes.object, contextMenuIcons: PropTypes.object }; export default DataTableRow;