choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
395 lines (344 loc) • 12.4 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 _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 from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import noop from 'lodash/noop';
import { action, observable, runInAction } from 'mobx';
import { PropTypes as MobxPropTypes } from 'mobx-react';
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 { hide } from '../tooltip/singleton';
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: "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) {
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) {
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({}, _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)
};
}
}, {
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$trigger = _this$props.trigger,
trigger = _this$props$trigger === void 0 ? this.getDefaultAction() : _this$props$trigger,
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 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
}, 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 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);
});
}
}
}, {
key: "popup",
get: function get() {
return this.statePopup;
}
}]);
return TriggerField;
}(TextField);
export { TriggerField as default };
TriggerField.displayName = 'TriggerField';
TriggerField.propTypes = _objectSpread({
/**
* 下拉框的自定义内容
*/
popupContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
/**
* 下拉框的自定义样式名
*/
popupCls: PropTypes.string,
/**
* 下拉框的内链样式
*/
popupStyle: PropTypes.object,
/**
* 下拉框对齐方式
*/
popupPlacement: PropTypes.string,
/**
* 触发下拉框的方式
* 可选值:click | focus | hover | contextMenu
*/
trigger: MobxPropTypes.arrayOrObservableArrayOf(PropTypes.string),
/**
* 下拉框显示延迟
* @defualt 150
*/
triggerShowDelay: PropTypes.number,
/**
* 下拉框隐藏延迟
* @defualt 50
*/
triggerHiddenDelay: PropTypes.number,
/**
* 下拉框变化钩子
*/
onPopupHiddenChange: PropTypes.func,
/**
* 定义浮层的容器,默认为 body
*/
getPopupContainer: PropTypes.func,
/**
* 定义浮层对齐的目标,默认为组件最外层元素
*/
getPopupAlignTarget: PropTypes.func,
/**
* 当popup中有可获取焦点对象时,是否按tab键时获取焦点
*/
tabIntoPopupContent: PropTypes.bool,
viewMode: PropTypes.oneOf(['none', 'popup', 'modal', 'list', 'drawer'])
}, TextField.propTypes);
TriggerField.defaultProps = _objectSpread({}, TextField.defaultProps, {
suffixCls: 'trigger',
clearButton: true,
popupPlacement: 'bottomLeft',
triggerShowDelay: 150,
triggerHiddenDelay: 50,
viewMode: '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