UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

332 lines (281 loc) 10.8 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import { __decorate } from "tslib"; import React, { Component } from 'react'; import classNames from 'classnames'; import { action } from 'mobx'; import noop from 'lodash/noop'; import KeyCode from '../../../es/_util/KeyCode'; import { getConfig } from '../../../es/configure'; import Table from '../table/Table'; import TableProfessionalBar from '../table/query-bar/TableProfessionalBar'; import { SelectionMode, TableMode, TableQueryBarType } from '../table/enum'; import { DataSetEvents, DataSetSelection } from '../data-set/enum'; import autobind from '../_util/autobind'; import { getColumnKey } from '../table/utils'; var LovView = /*#__PURE__*/ function (_Component) { _inherits(LovView, _Component); var _super = _createSuper(LovView); function LovView() { var _this; _classCallCheck(this, LovView); _this = _super.apply(this, arguments); _this.resizedColumns = new Map(); return _this; } _createClass(LovView, [{ key: "componentWillMount", value: function componentWillMount() { var _this$props = this.props, dataSet = _this$props.dataSet, selection = _this$props.dataSet.selection, multiple = _this$props.multiple, popup = _this$props.popup; this.selection = selection; dataSet.selection = multiple ? DataSetSelection.multiple : DataSetSelection.single; if (popup && multiple) { dataSet.addEventListener(DataSetEvents.batchSelect, this.handleSelect); dataSet.addEventListener(DataSetEvents.batchUnSelect, this.handleSelect); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this$props2 = this.props, dataSet = _this$props2.dataSet, multiple = _this$props2.multiple, popup = _this$props2.popup; dataSet.selection = this.selection; if (popup && multiple) { dataSet.removeEventListener(DataSetEvents.batchSelect, this.handleSelect); dataSet.removeEventListener(DataSetEvents.batchUnSelect, this.handleSelect); } } }, { key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { var popup = this.props.popup; if (popup && nextProps.popupHidden) { return false; } return true; } /* istanbul ignore next */ }, { key: "getColumns", value: function getColumns() { var _this2 = this; var _this$props3 = this.props, lovItems = _this$props3.config.lovItems, tableProps = _this$props3.tableProps, popup = _this$props3.popup; return lovItems ? lovItems.filter(function (_ref) { var gridField = _ref.gridField; return gridField === 'Y'; }).sort(function (_ref2, _ref3) { var seq1 = _ref2.gridFieldSequence; var seq2 = _ref3.gridFieldSequence; return seq1 - seq2; }).map(function (_ref4) { var display = _ref4.display, gridFieldName = _ref4.gridFieldName, gridFieldWidth = _ref4.gridFieldWidth, gridFieldAlign = _ref4.gridFieldAlign; var column = {}; if (tableProps && tableProps.columns) { column = tableProps.columns.find(function (c) { return c.name === gridFieldName; }); } return _objectSpread({}, column, { key: gridFieldName, header: display, name: gridFieldName, width: popup ? gridFieldName ? _this2.resizedColumns.get(gridFieldName) : undefined : gridFieldWidth, align: gridFieldAlign, editor: false }); }) : undefined; } }, { key: "handleSelect", value: function handleSelect(event) { var selectionMode = this.selectionMode; var _this$props4 = this.props, onSelect = _this$props4.onSelect, _this$props4$onBefore = _this$props4.onBeforeSelect, onBeforeSelect = _this$props4$onBefore === void 0 ? noop : _this$props4$onBefore, modal = _this$props4.modal, multiple = _this$props4.multiple, dataSet = _this$props4.dataSet, tableProps = _this$props4.tableProps; var records = selectionMode === SelectionMode.treebox ? dataSet.treeSelected : selectionMode === SelectionMode.rowbox || multiple ? dataSet.selected : dataSet.current ? [dataSet.current] : []; // 满足单选模式下,双击和勾选框选中均支持 if (tableProps && tableProps.alwaysShowRowBox && !event) { records = dataSet.selected; } var record = multiple ? records : records[0]; if (record && onBeforeSelect(record) !== false) { if (modal) { modal.close(); } onSelect(record); } return false; } /* istanbul ignore next */ }, { key: "handleKeyDown", value: function handleKeyDown(e) { if (e.keyCode === KeyCode.ENTER) { this.handleSelect(); } } /** * 单选 onRow 处理 * @param props */ }, { key: "handleRow", value: function handleRow(props) { var tableProps = this.props.tableProps; if (tableProps) { var onRow = tableProps.onRow; if (onRow) { return _objectSpread({ onDoubleClick: this.handleSelect }, onRow(props)); } } return { onDoubleClick: this.handleSelect }; } }, { key: "handleSingleRow", value: function handleSingleRow() { return { onClick: this.handleSelect }; } }, { key: "handleColumnResize", value: function handleColumnResize(props) { var width = props.width, column = props.column; this.resizedColumns.set(getColumnKey(column), width); } }, { key: "render", value: function render() { var _this$props5 = this.props, dataSet = _this$props5.dataSet, _this$props5$config = _this$props5.config, queryBar = _this$props5$config.queryBar, height = _this$props5$config.height, treeFlag = _this$props5$config.treeFlag, queryColumns = _this$props5$config.queryColumns, configTableProps = _this$props5$config.tableProps, multiple = _this$props5.multiple, popup = _this$props5.popup, tableProps = _this$props5.tableProps, modal = _this$props5.modal; if (modal) { modal.handleOk(this.handleSelect); } var columns = this.getColumns(); var lovTableProps = _objectSpread({ autoFocus: true, mode: treeFlag === 'Y' ? TableMode.tree : TableMode.list, onKeyDown: this.handleKeyDown, dataSet: dataSet, columns: columns, queryFieldsLimit: queryColumns, queryBar: queryBar || getConfig('lovQueryBar') || getConfig('queryBar'), selectionMode: SelectionMode.none }, configTableProps, {}, tableProps, { className: classNames(configTableProps && configTableProps.className, tableProps && tableProps.className), style: _objectSpread({}, configTableProps && configTableProps.style, { height: height }, tableProps && tableProps.style), queryBarProps: _objectSpread({}, tableProps && tableProps.queryBarProps, {}, getConfig('lovQueryBarProps')) }); if (multiple) { if (popup || !tableProps || !tableProps.selectionMode) { if (lovTableProps.mode === TableMode.tree) { lovTableProps.selectionMode = SelectionMode.treebox; } else { lovTableProps.selectionMode = SelectionMode.rowbox; } } if (lovTableProps.selectionMode !== SelectionMode.rowbox && lovTableProps.selectionMode !== SelectionMode.treebox) { lovTableProps.highLightRow = false; lovTableProps.selectedHighLightRow = true; } } else if (popup) { lovTableProps.onRow = this.handleSingleRow; } else if (lovTableProps.selectionMode !== SelectionMode.rowbox) { lovTableProps.onRow = this.handleRow; } else { lovTableProps.highLightRow = false; lovTableProps.selectedHighLightRow = true; } if (popup) { if (lovTableProps.showSelectionCachedButton === undefined) { lovTableProps.showSelectionCachedButton = false; lovTableProps.showCachedSelection = true; } lovTableProps.autoWidth = true; lovTableProps.onColumnResize = this.handleColumnResize; } var isProfessionalBar = lovTableProps.queryBar === TableQueryBarType.professionalBar; if (!popup && !lovTableProps.queryBar && isProfessionalBar) { lovTableProps.queryBar = function (props) { return React.createElement(TableProfessionalBar, _extends({}, props)); }; } this.selectionMode = lovTableProps.selectionMode; return React.createElement(Table, _extends({}, lovTableProps)); } }]); return LovView; }(Component); export { LovView as default }; __decorate([action], LovView.prototype, "componentWillMount", null); __decorate([action], LovView.prototype, "componentWillUnmount", null); __decorate([autobind], LovView.prototype, "handleSelect", null); __decorate([autobind], LovView.prototype, "handleKeyDown", null); __decorate([autobind], LovView.prototype, "handleRow", null); __decorate([autobind], LovView.prototype, "handleSingleRow", null); __decorate([autobind], LovView.prototype, "handleColumnResize", null); //# sourceMappingURL=LovView.js.map