UNPKG

choerodon-ui

Version:

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

150 lines (132 loc) 6.25 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _cachedTypeIntlMap; import React, { useCallback, useContext, useEffect } from 'react'; import { observer } from 'mobx-react-lite'; import { action } from 'mobx'; import { Size } from '../core/enum'; import { RecordCachedType } from '../data-set/enum'; import { $l } from '../locale-context'; import TableContext from './TableContext'; import Tooltip from '../tooltip'; import Button from '../button/Button'; import ObserverSelect from '../select/Select'; import { FuncType } from '../button/enum'; import { getCount } from './utils'; var Option = ObserverSelect.Option; export var cachedTypeIntlMap = (_cachedTypeIntlMap = {}, _defineProperty(_cachedTypeIntlMap, RecordCachedType.selected, 'cached_type_selected'), _defineProperty(_cachedTypeIntlMap, RecordCachedType.add, 'cached_type_created'), _defineProperty(_cachedTypeIntlMap, RecordCachedType.update, 'cached_type_updated'), _defineProperty(_cachedTypeIntlMap, RecordCachedType["delete"], 'cached_type_destroyed'), _cachedTypeIntlMap); var CachedTips = function CachedTip(props) { var dataSet = props.dataSet, tableStore = props.tableStore, prefixCls = props.prefixCls; var recordCachedType = tableStore.recordCachedType, defaultRecordCachedType = tableStore.defaultRecordCachedType; var options = []; var selectedLength = dataSet.selected.length, createdLength = dataSet.cachedCreated.length, updatedLength = dataSet.cachedUpdated.length, destroyedLength = dataSet.cachedDestroyed.length; var lengths = new Map([[RecordCachedType.selected, selectedLength], [RecordCachedType.add, createdLength], [RecordCachedType.update, updatedLength], [RecordCachedType["delete"], destroyedLength]]); lengths.forEach(function (length, key) { if (length) { options.push( /*#__PURE__*/React.createElement(Option, { key: String(key), value: key }, $l('Table', cachedTypeIntlMap[key]))); if (!defaultRecordCachedType || key === RecordCachedType.add) { defaultRecordCachedType = key; } } else { if (recordCachedType === key) { recordCachedType = undefined; } if (defaultRecordCachedType === key) { defaultRecordCachedType = undefined; } } }); var optionsRenderer = useCallback(function (_ref) { var text = _ref.text, value = _ref.value; return /*#__PURE__*/React.createElement(React.Fragment, null, text, /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-cached-type-count") }, getCount(dataSet, value))); }, [prefixCls, dataSet]); var handleChangeRecordCachedType = useCallback(action(function (newRecordCachedType) { tableStore.recordCachedType = newRecordCachedType; }), [tableStore]); useEffect(action(function () { if (tableStore.recordCachedType !== recordCachedType) { tableStore.recordCachedType = recordCachedType; } }), [tableStore, recordCachedType]); useEffect(action(function () { if (tableStore.defaultRecordCachedType !== defaultRecordCachedType) { tableStore.defaultRecordCachedType = defaultRecordCachedType; if (defaultRecordCachedType === RecordCachedType.add) { tableStore.showCachedSelection = true; } else if (!defaultRecordCachedType) { tableStore.showCachedSelection = false; } } }), [tableStore, defaultRecordCachedType]); if (options.length) { return /*#__PURE__*/React.createElement("span", null, $l('Table', 'cached_tips', { type: options.length > 1 ? /*#__PURE__*/React.createElement(ObserverSelect, { className: "".concat(prefixCls, "-cached-type"), optionRenderer: optionsRenderer, isFlat: true, value: recordCachedType || defaultRecordCachedType, onChange: handleChangeRecordCachedType, size: Size.small, clearButton: false }, options) : $l('Table', cachedTypeIntlMap[recordCachedType || defaultRecordCachedType || RecordCachedType.selected]), count: getCount(dataSet, recordCachedType || defaultRecordCachedType) })); } return null; }; var ObserverCachedTips = observer(CachedTips); var SelectionTips = function SelectionTips() { var _useContext = useContext(TableContext), prefixCls = _useContext.prefixCls, dataSet = _useContext.dataSet, tableStore = _useContext.tableStore, showSelectionCachedButton = _useContext.showSelectionCachedButton, onShowCachedSelectionChange = _useContext.onShowCachedSelectionChange; var showCachedSelection = tableStore.showCachedSelection; var getTitle = function getTitle() { return $l('Table', showCachedSelection ? 'hide_cached_records' : 'show_cached_records'); }; var cachedButton = showSelectionCachedButton && (dataSet.cacheSelectionKeys || dataSet.cacheModifiedKeys) && dataSet.cachedRecords.length > 0 ? /*#__PURE__*/React.createElement(Tooltip, { title: getTitle }, /*#__PURE__*/React.createElement(Button, { size: Size.small, className: "".concat(prefixCls, "-switch"), funcType: FuncType.flat, icon: showCachedSelection ? 'visibility_off' : 'visibility', onClick: action(function () { var newShowCachedSelection = !showCachedSelection; tableStore.showCachedSelection = newShowCachedSelection; if (onShowCachedSelectionChange) { onShowCachedSelectionChange(newShowCachedSelection); } }) })) : null; if (tableStore.showCachedTips) { return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-selection-tips") }, /*#__PURE__*/React.createElement(ObserverCachedTips, { dataSet: dataSet, tableStore: tableStore, prefixCls: prefixCls }), cachedButton); } return tableStore.showSelectionTips ? /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-selection-tips") }, /*#__PURE__*/React.createElement("span", null, $l('Table', 'selection_tips', { count: getCount(dataSet, RecordCachedType.selected) })), cachedButton) : cachedButton; }; SelectionTips.displayName = 'SelectionTips'; export default observer(SelectionTips); //# sourceMappingURL=SelectionTips.js.map