choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
429 lines (371 loc) • 15.1 kB
JavaScript
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, toJS } from 'mobx';
import noop from 'lodash/noop';
import KeyCode from '../../../es/_util/KeyCode';
import ConfigContext from '../../../es/config-provider/ConfigContext';
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';
import SelectionList, { TIMESTAMP, SelectionsPosition } from './SelectionList';
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,
viewMode = _this$props.viewMode;
this.selection = selection;
dataSet.selection = multiple ? DataSetSelection.multiple : DataSetSelection.single;
if ((viewMode === 'popup' || viewMode === 'drawer' || viewMode === 'modal') && 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,
viewMode = _this$props2.viewMode;
dataSet.selection = this.selection;
if ((viewMode === 'popup' || viewMode === 'drawer' || viewMode === 'modal') && multiple) {
dataSet.removeEventListener(DataSetEvents.batchSelect, this.handleSelect);
dataSet.removeEventListener(DataSetEvents.batchUnSelect, this.handleSelect);
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps) {
var viewMode = this.props.viewMode;
if (viewMode === '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,
viewMode = _this$props3.viewMode;
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: viewMode === '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,
viewMode = _this$props4.viewMode,
showSelectedInView = _this$props4.showSelectedInView; // 为了drawer模式下右侧勾选项的顺序
if (showSelectedInView && (viewMode === 'drawer' || viewMode === 'modal') && multiple) {
if (event && event.records) {
event.records.forEach(function (item) {
if (item.isSelected) {
item.setState(TIMESTAMP, new Date().getTime());
} else {
item.setState(TIMESTAMP, 0);
}
});
}
}
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 && (!event || !multiple)) {
modal.close();
}
if (!event || !multiple || viewMode === 'popup') {
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: "renderTable",
value: function renderTable() {
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,
_this$props5$config$t = _this$props5$config.tableProps,
configTableProps = _this$props5$config$t === void 0 ? {} : _this$props5$config$t,
multiple = _this$props5.multiple,
tableProps = _this$props5.tableProps,
viewMode = _this$props5.viewMode,
context = _this$props5.context,
showSelectedInView = _this$props5.showSelectedInView;
var getConfig = context.getConfig;
var columns = this.getColumns();
var popup = viewMode === 'popup';
var modal = viewMode === 'modal';
var drawer = viewMode === 'drawer';
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));
};
}
if ((modal || drawer) && showSelectedInView) {
lovTableProps.showSelectionTips = false;
}
this.selectionMode = lovTableProps.selectionMode;
return React.createElement(React.Fragment, null, React.createElement(Table, _extends({}, lovTableProps)), modal && this.renderSelectionList());
}
}, {
key: "renderSelectionList",
value: function renderSelectionList() {
var _this$props6 = this.props,
dataSet = _this$props6.dataSet,
_this$props6$valueFie = _this$props6.valueField,
valueField = _this$props6$valueFie === void 0 ? '' : _this$props6$valueFie,
_this$props6$textFiel = _this$props6.textField,
textField = _this$props6$textFiel === void 0 ? '' : _this$props6$textFiel,
_this$props6$config = _this$props6.config,
treeFlag = _this$props6$config.treeFlag,
_this$props6$config$t = _this$props6$config.tableProps,
configTableProps = _this$props6$config$t === void 0 ? {} : _this$props6$config$t,
tableProps = _this$props6.tableProps,
multiple = _this$props6.multiple,
viewMode = _this$props6.viewMode,
showSelectedInView = _this$props6.showSelectedInView,
selectionProps = _this$props6.selectionProps;
if (!showSelectedInView || !multiple) {
return null;
}
if (!this.selectionMode) {
var selectionMode = (tableProps === null || tableProps === void 0 ? void 0 : tableProps.selectionMode) || (configTableProps === null || configTableProps === void 0 ? void 0 : configTableProps.selectionMode);
if (!selectionMode) {
this.selectionMode = treeFlag === 'Y' ? SelectionMode.treebox : SelectionMode.rowbox;
} else {
this.selectionMode = selectionMode;
}
}
var selectionsPosition = viewMode === 'drawer' ? SelectionsPosition.side : viewMode === 'modal' ? SelectionsPosition.below : undefined;
return React.createElement(SelectionList, {
dataSet: dataSet,
treeFlag: treeFlag,
valueField: valueField,
textField: textField,
selectionsPosition: selectionsPosition,
selectionProps: selectionProps
});
}
}, {
key: "render",
value: function render() {
var _this$props7 = this.props,
modal = _this$props7.modal,
viewRenderer = _this$props7.viewRenderer,
dataSet = _this$props7.dataSet,
viewMode = _this$props7.viewMode,
lovConfig = _this$props7.config,
textField = _this$props7.textField,
valueField = _this$props7.valueField,
multiple = _this$props7.multiple;
if (modal) {
modal.handleOk(this.handleSelect);
}
return React.createElement(React.Fragment, null, viewMode === 'drawer' && this.renderSelectionList(), React.createElement("div", null, viewRenderer ? toJS(viewRenderer({
dataSet: dataSet,
lovConfig: lovConfig,
textField: textField,
valueField: valueField,
multiple: multiple,
modal: modal
})) : this.renderTable()));
}
}], [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}]);
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