choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
307 lines (272 loc) • 10.7 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React from 'react';
import { findDOMNode } from 'react-dom';
import noop from 'lodash/noop';
import { action, observable, runInAction } from 'mobx';
import Trigger from '../../../es/trigger/Trigger';
import { Action } from '../../../es/trigger/enum';
import { getIf } from '../data-set/utils';
import { TextField } from '../text-field/TextField';
import autobind from '../_util/autobind';
import Icon from '../icon';
import TaskRunner from '../_util/TaskRunner';
import BUILT_IN_PLACEMENTS from './placements';
import { TriggerViewMode } from './enum';
import { hide } from '../tooltip/singleton';
export { TriggerViewMode };
var TriggerField = /*#__PURE__*/function (_TextField) {
_inherits(TriggerField, _TextField);
var _super = _createSuper(TriggerField);
function TriggerField(props, context) {
var _this;
_classCallCheck(this, TriggerField);
_this = _super.call(this, props, context);
runInAction(function () {
_this.statePopup = false;
});
return _this;
}
_createClass(TriggerField, [{
key: "popup",
get: function get() {
return this.statePopup;
}
}, {
key: "saveTrigger",
value: function saveTrigger(node) {
this.trigger = node;
}
}, {
key: "isValidationMessageHidden",
value: function isValidationMessageHidden(message) {
return _get(_getPrototypeOf(TriggerField.prototype), "isValidationMessageHidden", this).call(this, message) || this.popup;
}
}, {
key: "setPopup",
value: function setPopup(statePopup) {
var _this$props$trigger = this.props.trigger,
trigger = _this$props$trigger === void 0 ? this.getContextConfig('selectTrigger') : _this$props$trigger;
if (statePopup !== this.statePopup) {
this.statePopup = statePopup;
var _this$props$onPopupHi = this.props.onPopupHiddenChange,
onPopupHiddenChange = _this$props$onPopupHi === void 0 ? noop : _this$props$onPopupHi;
onPopupHiddenChange(!statePopup);
if (statePopup && !trigger.includes(Action.hover)) {
hide();
}
}
}
}, {
key: "getRootDomNode",
value: function getRootDomNode() {
return findDOMNode(this);
}
}, {
key: "getOmitPropsKeys",
value: function getOmitPropsKeys() {
return _get(_getPrototypeOf(TriggerField.prototype), "getOmitPropsKeys", this).call(this).concat(['popupContent', 'popupCls', 'popupStyle', 'popupPlacement', 'editable', 'trigger', 'triggerShowDelay', 'triggerHiddenDelay', 'onPopupHiddenChange', 'getPopupContainer', 'getPopupAlignTarget', 'extraFooterPlacement', 'renderExtraFooter', 'treeDefaultExpandedKeys', 'treeDefaultExpandAll', 'tabIntoPopupContent', 'viewMode']);
}
}, {
key: "getObservableProps",
value: function getObservableProps(props, context) {
return _objectSpread(_objectSpread({}, _get(_getPrototypeOf(TriggerField.prototype), "getObservableProps", this).call(this, props, context)), {}, {
viewMode: props.viewMode
});
}
}, {
key: "getPopupProps",
value: function getPopupProps() {
return {
setValue: this.setValue.bind(this),
setPopup: this.setPopup.bind(this),
content: this.getPopupContent()
};
}
}, {
key: "getPopupClassName",
value: function getPopupClassName(defaultClassName) {
return defaultClassName;
}
}, {
key: "getPopupWrapper",
value: function getPopupWrapper() {
var trigger = this.trigger;
if (trigger) {
return trigger.getPopupWrapper();
}
}
}, {
key: "renderPopupContent",
value: function renderPopupContent() {
var popupContent = this.props.popupContent;
if (popupContent === undefined) {
return this.getPopupContent();
}
if (popupContent instanceof Function) {
return popupContent(this.getPopupProps());
}
return popupContent;
}
}, {
key: "getDefaultAction",
value: function getDefaultAction() {
return [Action.focus, Action.click];
}
}, {
key: "getWrappedEditor",
value: function getWrappedEditor(renderedValue) {
var prefixCls = this.prefixCls,
_this$props = this.props,
popupCls = _this$props.popupCls,
popupStyle = _this$props.popupStyle,
popupPlacement = _this$props.popupPlacement,
hidden = _this$props.hidden,
_this$props$trigger2 = _this$props.trigger,
trigger = _this$props$trigger2 === void 0 ? this.getDefaultAction() : _this$props$trigger2,
triggerShowDelay = _this$props.triggerShowDelay,
triggerHiddenDelay = _this$props.triggerHiddenDelay,
getPopupContainer = _this$props.getPopupContainer,
tabIntoPopupContent = _this$props.tabIntoPopupContent,
_this$props$getPopupA = _this$props.getPopupAlignTarget,
getPopupAlignTarget = _this$props$getPopupA === void 0 ? this.getRootDomNode : _this$props$getPopupA;
return /*#__PURE__*/React.createElement(Trigger, {
ref: this.saveTrigger,
action: this.readOnly || this.disabled ? [] : trigger,
focusDelay: triggerShowDelay,
blurDelay: triggerHiddenDelay,
mouseEnterDelay: triggerShowDelay,
mouseLeaveDelay: triggerHiddenDelay,
prefixCls: prefixCls,
popupCls: this.getPopupClassName(popupCls),
popupStyle: popupStyle,
popupContent: this.renderPopupContent,
popupPlacement: popupPlacement,
popupHidden: hidden || !this.popup,
builtinPlacements: BUILT_IN_PLACEMENTS,
onPopupAnimateAppear: this.handlePopupAnimateAppear,
onPopupAnimateEnd: this.handlePopupAnimateEnd,
onPopupHiddenChange: this.handlePopupHiddenChange,
getPopupStyleFromAlign: this.getPopupStyleFromAlign,
getRootDomNode: getPopupAlignTarget,
getPopupContainer: getPopupContainer,
tabIntoPopupContent: tabIntoPopupContent,
childrenProps: renderedValue,
getContextConfig: this.getContextConfig
}, this.getEditor);
}
}, {
key: "getWrapperClassNames",
value: function getWrapperClassNames() {
var _get2, _ref;
var prefixCls = this.prefixCls;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_get2 = _get(_getPrototypeOf(TriggerField.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this].concat(args, [(_ref = {}, _defineProperty(_ref, "".concat(prefixCls, "-expand"), this.popup), _defineProperty(_ref, "".concat(prefixCls, "-not-editable"), !this.disabled && !this.editable), _ref)]));
}
}, {
key: "getDefaultSuffix",
value: function getDefaultSuffix() {
var prefixCls = this.prefixCls;
return /*#__PURE__*/React.createElement(Icon, {
type: this.getTriggerIconFont(),
className: "".concat(prefixCls, "-trigger")
});
}
}, {
key: "handleTagAnimateEnd",
value: function handleTagAnimateEnd() {
this.forcePopupAlign();
}
}, {
key: "handlePopupMouseDown",
value: function handlePopupMouseDown(e) {
e.preventDefault();
var _this$props$onMouseDo = this.props.onMouseDown,
onMouseDown = _this$props$onMouseDo === void 0 ? noop : _this$props$onMouseDo;
onMouseDown(e);
}
}, {
key: "handlePopupHiddenChange",
value: function handlePopupHiddenChange(hidden) {
this.setPopup(!hidden);
}
}, {
key: "forcePositionChanged",
value: function forcePositionChanged() {
this.forcePopupAlign();
}
}, {
key: "forcePopupAlign",
value: function forcePopupAlign() {
if (this.trigger) {
this.trigger.forcePopupAlign();
}
}
}, {
key: "expand",
value: function expand() {
var _this2 = this;
var popupTask = getIf(this, 'popupTask', function () {
return new TaskRunner();
});
popupTask.cancel();
if (!this.readOnly && !this.popup) {
popupTask.delay(this.props.triggerShowDelay, function () {
_this2.setPopup(true);
});
}
}
}, {
key: "collapse",
value: function collapse() {
var _this3 = this;
var popupTask = getIf(this, 'popupTask', function () {
return new TaskRunner();
});
popupTask.cancel();
if (!this.readOnly && this.popup) {
var element = this.element; // 如果当前焦点在popup中,将焦点换给输入框
var triggerHiddenDelay = this.props.triggerHiddenDelay;
if (this.isFocused && element !== document.activeElement) {
element.focus();
var triggerShowDelay = this.props.triggerShowDelay;
triggerHiddenDelay += triggerShowDelay;
}
popupTask.delay(triggerHiddenDelay, function () {
_this3.setPopup(false);
});
}
}
}]);
return TriggerField;
}(TextField);
export { TriggerField as default };
TriggerField.displayName = 'TriggerField';
TriggerField.defaultProps = _objectSpread(_objectSpread({}, TextField.defaultProps), {}, {
suffixCls: 'trigger',
clearButton: true,
popupPlacement: 'bottomLeft',
triggerShowDelay: 150,
triggerHiddenDelay: 50,
viewMode: TriggerViewMode.popup
});
__decorate([observable], TriggerField.prototype, "statePopup", void 0);
__decorate([autobind], TriggerField.prototype, "saveTrigger", null);
__decorate([autobind], TriggerField.prototype, "isValidationMessageHidden", null);
__decorate([action], TriggerField.prototype, "setPopup", null);
__decorate([autobind], TriggerField.prototype, "getRootDomNode", null);
__decorate([autobind], TriggerField.prototype, "getPopupWrapper", null);
__decorate([autobind], TriggerField.prototype, "renderPopupContent", null);
__decorate([autobind], TriggerField.prototype, "handleTagAnimateEnd", null);
__decorate([autobind], TriggerField.prototype, "handlePopupMouseDown", null);
__decorate([autobind], TriggerField.prototype, "handlePopupHiddenChange", null);
//# sourceMappingURL=TriggerField.js.map