UNPKG

@fruits-chain/react-native-xiaoshu

Version:
124 lines (116 loc) 3.48 kB
import React, { useState, useCallback, useEffect, memo } from 'react'; import { View, Text } from 'react-native'; import Button from '../button'; import { getDefaultValue } from '../helpers'; import { usePersistFn } from '../hooks'; import Locale from '../locale'; import Result from '../result'; import Space from '../space'; import Theme from '../theme'; import Progress from './progress'; import { varCreator, styleCreator } from './style'; const ProgressPage = _ref => { let { children, loading: loadingOut, backgroundColor, defaultPercentage = 10, fail, failMessage, failIcon, onPressReload } = _ref; const locale = Locale.useLocale().ProgressPage; const TOKENS = Theme.useThemeTokens(); const CV = Theme.createVar(TOKENS, varCreator); const STYLES = Theme.createStyle(CV, styleCreator); backgroundColor = getDefaultValue(backgroundColor, CV.progress_page_background_color); const [state, setState] = useState({ loading: loadingOut, percentage: defaultPercentage, duration: 0, animated: false }); const onAnimationEnd = useCallback(n => { if (n === 100) { // 延迟更改状态更顺畅,进度条是完整走完的 setTimeout(() => { setState(s => ({ ...s, loading: false })); }, 0); } }, []); const onPressReloadPersistFn = usePersistFn(() => { onPressReload === null || onPressReload === void 0 ? void 0 : onPressReload(); }); useEffect(() => { setState(s => { const isReload = !s.loading && loadingOut; if (isReload) { return { ...s, loading: loadingOut, percentage: defaultPercentage, duration: 0, animated: false }; } return { ...s }; }); }, [defaultPercentage, loadingOut]); useEffect(() => { // 做一个延迟,避免上下两次操作状态合并在一个中,reload 的时候丢失过渡状态 setTimeout(() => { setState(s => { return { ...s, percentage: loadingOut ? 90 : 100, duration: loadingOut ? 1500 : 100, animated: true }; }); }, 0); }, [loadingOut]); const placeholderStyle = { flex: 1, backgroundColor: backgroundColor }; if (state.loading) { return /*#__PURE__*/React.createElement(View, { style: placeholderStyle }, /*#__PURE__*/React.createElement(Progress, { percentage: state.percentage, showPivot: false, animated: state.animated, animationDuration: state.duration, onAnimationEnd: onAnimationEnd, square: true })); } if (fail) { return /*#__PURE__*/React.createElement(Result, { style: STYLES.fail_page, status: "warning", subtitle: /*#__PURE__*/React.createElement(Space, { head: true, gap: "l", align: "center" }, /*#__PURE__*/React.createElement(Text, { style: STYLES.text }, failMessage || locale.failMessage), onPressReload ? /*#__PURE__*/React.createElement(Button, { style: STYLES.btn, text: locale.labelRefreshText, onPress: onPressReloadPersistFn }) : null), renderIcon: () => { if (failIcon) { return failIcon; } return /*#__PURE__*/React.createElement(Result.IconWarning, null); } }); } return children; }; export default /*#__PURE__*/memo(ProgressPage); //# sourceMappingURL=progress-page.js.map