UNPKG

@ant-design/react-native

Version:

基于蚂蚁金服移动设计规范的 React Native 组件库

111 lines (100 loc) 4.16 kB
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 { Animated, Dimensions, View } from 'react-native'; import { WithTheme } from '../style'; import ProgressStyles from './style/index'; var Progress = function (_React$Component) { _inherits(Progress, _React$Component); function Progress(props) { _classCallCheck(this, Progress); var _this = _possibleConstructorReturn(this, (Progress.__proto__ || Object.getPrototypeOf(Progress)).call(this, props)); _this.onLayout = function (e) { _this.setState({ wrapWidth: e.nativeEvent.layout.width }); }; _this.normalPercent = function (percent) { var widthPercent = 0; if (percent !== undefined && percent > 0) { widthPercent = percent > 100 ? 100 : percent; } return widthPercent; }; _this.getWidth = function () { var percent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.percent; return _this.state.wrapWidth * (_this.normalPercent(percent) / 100); }; _this.state = { wrapWidth: props.wrapWidth || Dimensions.get('window').width, percentage: new Animated.Value(0) }; return _this; } _createClass(Progress, [{ key: 'UNSAFE_componentWillReceiveProps', value: function UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.wrapWidth !== this.props.wrapWidth) { this.setState({ wrapWidth: nextProps.wrapWidth }); } if (this.props.appearTransition && nextProps.percent !== this.props.percent) { this.setState({ percentage: new Animated.Value(this.getWidth(nextProps.percent)) }); } } }, { key: 'componentDidMount', value: function componentDidMount() { if (this.props.appearTransition) { this.state.percentage.setValue(0); Animated.timing(this.state.percentage, { toValue: this.getWidth(), duration: 1000, useNativeDriver: true }).start(); } } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, position = _props.position, unfilled = _props.unfilled, style = _props.style, barStyle = _props.barStyle; var percentStyle = { width: this.getWidth(), height: 0 }; return React.createElement( WithTheme, { styles: this.props.styles, themeStyles: ProgressStyles }, function (styles) { var child = React.createElement(View, { style: [styles.progressBar, percentStyle, barStyle] }); if (_this2.props.appearTransition) { percentStyle.width = _this2.state.percentage; child = React.createElement(Animated.View, { style: [styles.progressBar, percentStyle, barStyle] }); } var outerStyle = [styles.progressOuter, position === 'fixed' ? { position: 'absolute', top: 0 } : {}, !unfilled ? { backgroundColor: 'transparent' } : {}, style]; return React.createElement( View, { onLayout: _this2.onLayout, style: outerStyle }, child ); } ); } }]); return Progress; }(React.Component); export default Progress; Progress.defaultProps = { percent: 0, position: 'normal', unfilled: true, appearTransition: false };