UNPKG

@fruits-chain/react-native-xiaoshu

Version:
153 lines (132 loc) 5.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var helpers = _interopRequireWildcard(require("../helpers")); var _hooks = require("../hooks"); var _theme = _interopRequireDefault(require("../theme")); var _style = require("./style"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Progress 进度条 */ const Progress = _ref => { let { percentage = 0, pivotText, color, trackColor, pivotColor, textColor, strokeWidth, inactive = false, showPivot = true, square = false, animated = false, animationDuration, onAnimationEnd } = _ref; const AnimatedValue = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current; const StartPercentage = (0, _react.useRef)(percentage); const onAnimationEndPersistFn = (0, _hooks.usePersistFn)(n => { onAnimationEnd === null || onAnimationEnd === void 0 ? void 0 : onAnimationEnd(n); }); const TOKENS = _theme.default.useThemeTokens(); const CV = _theme.default.createVar(TOKENS, _style.varCreator); // 默认值 color = (0, helpers.getDefaultValue)(color, CV.progress_color); if (inactive) { color = '#cacaca'; } trackColor = (0, helpers.getDefaultValue)(trackColor, CV.progress_background_color); pivotColor = (0, helpers.getDefaultValue)(pivotColor, color); textColor = (0, helpers.getDefaultValue)(textColor, CV.progress_pivot_text_color); pivotText = (0, helpers.getDefaultValue)(pivotText, `${percentage}%`); strokeWidth = (0, helpers.getDefaultValue)(strokeWidth, CV.progress_height); animationDuration = (0, helpers.getDefaultValue)(animationDuration, TOKENS.animation_duration_base); const borderRadius = square ? 0 : strokeWidth / 2; const [progressLayout, setProgressLayout] = (0, _react.useState)({ width: 0, height: 0 }); const [textLayout, setTextLayout] = (0, _react.useState)({ width: 0, height: 0 }); (0, _react.useEffect)(() => { const action = _reactNative.Animated.timing(AnimatedValue, { toValue: progressLayout.width * percentage / 100, duration: animated ? animationDuration : 0, easing: helpers.easing.easeInCubic, useNativeDriver: false }); action.start(_ref2 => { let { finished } = _ref2; if (finished) { onAnimationEndPersistFn(percentage); } }); return () => { action.stop(); }; }, [AnimatedValue, percentage, animationDuration, progressLayout.width, animated, onAnimationEndPersistFn]); const barStyle = { position: 'absolute', left: 0, top: 0, width: AnimatedValue, height: strokeWidth, backgroundColor: color, borderRadius: borderRadius }; const textBoxStyle = { position: 'absolute', left: AnimatedValue, top: 0, justifyContent: 'center', alignItems: 'center', backgroundColor: pivotColor, paddingHorizontal: CV.progress_pivot_padding_horizontal, borderRadius: TOKENS.border_radius_max, transform: [{ translateX: -textLayout.width / 2 }, { translateY: -(textLayout.height - strokeWidth) / 2 }] }; const onLayoutProgress = (0, _react.useCallback)(e => { AnimatedValue.setValue(e.nativeEvent.layout.width * StartPercentage.current / 100); setProgressLayout(e.nativeEvent.layout); }, [AnimatedValue]); const onLayoutText = (0, _react.useCallback)(e => { setTextLayout(e.nativeEvent.layout); }, []); return /*#__PURE__*/_react.default.createElement(_reactNative.View, { onLayout: onLayoutProgress, style: { height: strokeWidth, backgroundColor: trackColor, borderRadius: borderRadius } }, /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, { style: barStyle }), showPivot ? /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, { onLayout: onLayoutText, style: textBoxStyle }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: { color: textColor, fontSize: CV.progress_pivot_font_size, lineHeight: CV.progress_pivot_line_height_scale * CV.progress_pivot_font_size } }, pivotText)) : null); }; var _default = /*#__PURE__*/(0, _react.memo)(Progress); exports.default = _default; //# sourceMappingURL=progress.js.map