@ant-design/react-native
Version:
基于蚂蚁金服移动设计规范的 React Native 组件库
136 lines (124 loc) • 5.01 kB
JavaScript
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';
import { ActivityIndicator, Animated, Text, View } from 'react-native';
import Icon from '../icon';
import { WithTheme } from '../style';
import ToastStyles from './style/index';
var ToastContainer = function (_React$Component) {
_inherits(ToastContainer, _React$Component);
function ToastContainer(props) {
_classCallCheck(this, ToastContainer);
var _this = _possibleConstructorReturn(this, (ToastContainer.__proto__ || Object.getPrototypeOf(ToastContainer)).call(this, props));
_this.state = {
fadeAnim: new Animated.Value(0)
};
return _this;
}
_createClass(ToastContainer, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
var _props = this.props,
onClose = _props.onClose,
onAnimationEnd = _props.onAnimationEnd;
var duration = this.props.duration;
var timing = Animated.timing;
if (this.anim) {
this.anim = null;
}
var animArr = [timing(this.state.fadeAnim, {
toValue: 1,
duration: 200,
useNativeDriver: true
}), Animated.delay(duration * 1000)];
if (duration > 0) {
animArr.push(timing(this.state.fadeAnim, {
toValue: 0,
duration: 200,
useNativeDriver: true
}));
}
this.anim = Animated.sequence(animArr);
this.anim.start(function () {
if (duration > 0) {
_this2.anim = null;
if (onClose) {
onClose();
}
if (onAnimationEnd) {
onAnimationEnd();
}
}
});
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.anim) {
this.anim.stop();
this.anim = null;
}
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _props2 = this.props,
_props2$type = _props2.type,
type = _props2$type === undefined ? '' : _props2$type,
content = _props2.content,
mask = _props2.mask;
return React.createElement(
WithTheme,
{ styles: this.props.styles, themeStyles: ToastStyles },
function (styles) {
var iconType = {
success: 'check-circle',
fail: 'close-circle',
offline: 'frown'
};
var iconDom = null;
if (type === 'loading') {
iconDom = React.createElement(ActivityIndicator, { animating: true, style: [styles.centering], color: 'white', size: 'large' });
} else if (type === 'info') {
iconDom = null;
} else {
iconDom = React.createElement(Icon, { name: iconType[type], style: styles.image, color: 'white', size: 36 });
}
return React.createElement(
View,
{ style: [styles.container], pointerEvents: mask ? undefined : 'box-none' },
React.createElement(
View,
{ style: [styles.innerContainer] },
React.createElement(
Animated.View,
{ style: { opacity: _this3.state.fadeAnim } },
React.createElement(
View,
{ style: [styles.innerWrap, iconDom ? styles.iconToast : styles.textToast] },
iconDom,
React.createElement(
Text,
{ style: styles.content },
content
)
)
)
)
);
}
);
}
}]);
return ToastContainer;
}(React.Component);
export default ToastContainer;
ToastContainer.defaultProps = {
duration: 3,
mask: true,
onClose: function onClose() {}
};