@ant-design/react-native
Version:
基于蚂蚁金服移动设计规范的 React Native 组件库
156 lines (147 loc) • 6.27 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React from 'react';
export default function PopupMixin(getModal, platformProps) {
var _a;
return _a = function (_React$Component) {
_inherits(_a, _React$Component);
function _a(props) {
_classCallCheck(this, _a);
var _this = _possibleConstructorReturn(this, (_a.__proto__ || Object.getPrototypeOf(_a)).call(this, props));
_this.onPickerChange = function (pickerValue) {
if (_this.state.pickerValue !== pickerValue) {
_this.setState({
pickerValue: pickerValue
});
var _this$props = _this.props,
picker = _this$props.picker,
pickerValueChangeProp = _this$props.pickerValueChangeProp;
if (picker && picker.props[pickerValueChangeProp]) {
picker.props[pickerValueChangeProp](pickerValue);
}
}
};
_this.saveRef = function (picker) {
_this.picker = picker;
};
_this.onTriggerClick = function (e) {
var child = _this.props.children;
var childProps = child.props || {};
if (childProps[_this.props.triggerType]) {
childProps[_this.props.triggerType](e);
}
_this.fireVisibleChange(!_this.state.visible);
};
_this.onOk = function () {
_this.props.onOk(_this.picker && _this.picker.getValue());
_this.fireVisibleChange(false);
};
_this.getContent = function () {
if (_this.props.picker) {
var _React$cloneElement;
var pickerValue = _this.state.pickerValue;
if (pickerValue === null) {
pickerValue = _this.props.value;
}
return React.cloneElement(_this.props.picker, (_React$cloneElement = {}, _defineProperty(_React$cloneElement, _this.props.pickerValueProp, pickerValue), _defineProperty(_React$cloneElement, _this.props.pickerValueChangeProp, _this.onPickerChange), _defineProperty(_React$cloneElement, 'ref', _this.saveRef), _React$cloneElement));
} else {
return _this.props.content;
}
};
_this.onDismiss = function () {
_this.props.onDismiss();
_this.fireVisibleChange(false);
};
_this.hide = function () {
_this.fireVisibleChange(false);
};
_this.state = {
pickerValue: 'value' in _this.props ? _this.props.value : null,
visible: _this.props.visible || false
};
return _this;
}
_createClass(_a, [{
key: 'UNSAFE_componentWillReceiveProps',
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
pickerValue: nextProps.value
});
}
if ('visible' in nextProps) {
this.setVisibleState(nextProps.visible);
}
}
}, {
key: 'setVisibleState',
value: function setVisibleState(visible) {
this.setState({
visible: visible
});
if (!visible) {
this.setState({
pickerValue: null
});
}
}
}, {
key: 'fireVisibleChange',
value: function fireVisibleChange(visible) {
if (this.state.visible !== visible) {
if (!('visible' in this.props)) {
this.setVisibleState(visible);
}
this.props.onVisibleChange(visible);
}
}
}, {
key: 'getRender',
value: function getRender() {
var props = this.props;
var children = props.children;
if (!children) {
return getModal(props, this.state.visible, {
getContent: this.getContent,
onOk: this.onOk,
hide: this.hide,
onDismiss: this.onDismiss
});
}
var _props = this.props,
WrapComponent = _props.WrapComponent,
disabled = _props.disabled;
var child = children;
var newChildProps = {};
if (!disabled) {
newChildProps[props.triggerType] = this.onTriggerClick;
}
return React.createElement(
WrapComponent,
{ style: props.wrapStyle },
React.cloneElement(child, newChildProps),
getModal(props, this.state.visible, {
getContent: this.getContent,
onOk: this.onOk,
hide: this.hide,
onDismiss: this.onDismiss
})
);
}
}, {
key: 'render',
value: function render() {
return this.getRender();
}
}]);
return _a;
}(React.Component), _a.defaultProps = _extends({
onVisibleChange: function onVisibleChange(_) {},
okText: 'Ok', dismissText: 'Dismiss', title: '', onOk: function onOk(_) {},
onDismiss: function onDismiss() {}
}, platformProps), _a;
}