UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

231 lines (230 loc) 9.13 kB
import { _ as _define_property } from "@swc/helpers/_/_define_property"; import { _ as _object_spread } from "@swc/helpers/_/_object_spread"; import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props"; import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties"; import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array"; import React, { useEffect, useRef, useState } from "react"; import classNames from "classnames"; import Taro from "@tarojs/taro"; import { View } from "@tarojs/components"; import { pxTransform } from "../../utils/taro/px-transform"; import { ComponentDefaults } from "../../utils/typings"; import { useRtl } from "../configprovider/index"; import { useUuid } from "../../hooks/use-uuid"; import { harmony, web } from "../../utils/taro/platform"; var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), { percent: 0, showText: false, animated: false, lazy: false, delay: 0 }); export var Progress = function(props) { var rtl = useRtl(); var _ref = _object_spread({}, defaultProps, props), className = _ref.className, style = _ref.style, percent = _ref.percent, background = _ref.background, color = _ref.color, strokeWidth = _ref.strokeWidth, showText = _ref.showText, animated = _ref.animated, children = _ref.children, lazy = _ref.lazy, delay = _ref.delay, // tc showInfo = _ref.showInfo, borderRadius = _ref.borderRadius, fontSize = _ref.fontSize, activeColor = _ref.activeColor, backgroundColor = _ref.backgroundColor, active = _ref.active, activeMode = _ref.activeMode, duration = _ref.duration, ariaLabel = _ref.ariaLabel, onActiveEnd = _ref.onActiveEnd, rest = _object_without_properties(_ref, [ "className", "style", "percent", "background", "color", "strokeWidth", "showText", "animated", "children", "lazy", "delay", "showInfo", "borderRadius", "fontSize", "activeColor", "backgroundColor", "active", "activeMode", "duration", "ariaLabel", "onActiveEnd" ]); var classPrefix = 'nut-progress'; var _props_showText, _ref1; var effectiveShowText = (_ref1 = (_props_showText = props.showText) !== null && _props_showText !== void 0 ? _props_showText : showInfo) !== null && _ref1 !== void 0 ? _ref1 : defaultProps.showText; var _props_color, _ref2; var effectiveColor = (_ref2 = (_props_color = props.color) !== null && _props_color !== void 0 ? _props_color : activeColor) !== null && _ref2 !== void 0 ? _ref2 : defaultProps.color; var _props_background, _ref3; var effectiveBgColor = (_ref3 = (_props_background = props.background) !== null && _props_background !== void 0 ? _props_background : backgroundColor) !== null && _ref3 !== void 0 ? _ref3 : defaultProps.background; var _props_animated, _ref4; var effectiveAnimated = (_ref4 = (_props_animated = props.animated) !== null && _props_animated !== void 0 ? _props_animated : active) !== null && _ref4 !== void 0 ? _ref4 : defaultProps.animated; var _obj; var classesInner = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-inner"), true), _define_property(_obj, "".concat(classPrefix, "-active"), effectiveAnimated), _obj)); var _useState = _sliced_to_array(useState(percent), 2), displayPercent = _useState[0], setDispalyPercent = _useState[1]; var getStyles = function() { // 基础样式 var baseStyles = { height: strokeWidth && pxTransform(Number(strokeWidth)), borderRadius: borderRadius && pxTransform(parseInt(borderRadius.toString())) }; var transitionStyle = { transition: "width ".concat(duration || 300, "ms ease-in-out") }; return { outer: _object_spread({ width: '100%', backgroundColor: effectiveBgColor }, baseStyles), inner: _object_spread({ width: "".concat(displayPercent, "%"), background: effectiveColor || '#FF0F23' }, baseStyles, transitionStyle) }; }; var _getStyles = getStyles(), stylesOuter = _getStyles.outer, stylesInner = _getStyles.inner; var handlePercent = function() { var timer = null; if (delay) { setDispalyPercent(0); timer = setTimeout(function() { setDispalyPercent(percent); }, delay); } return function() { lazy && resetObserver(); timer && clearTimeout(timer); }; }; useEffect(function() { var timer = null; if (activeMode === 'backwards') { setDispalyPercent(0); timer = setTimeout(function() { setDispalyPercent(percent); }, duration || 300); } else { setDispalyPercent(percent); } return function() { if (timer) { clearTimeout(timer); } }; }, [ percent, activeMode, duration ]); var _useState1 = _sliced_to_array(useState(false), 2), intersecting = _useState1[0], setIntersecting = _useState1[1]; var progressRef = useRef(null); var webObserver = useRef(null); var uuid = useUuid(); var selector = "".concat(classPrefix, "-lazy-").concat(uuid); var resetObserver = function() { var observer = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null; if (web()) { webObserver.current.disconnect && webObserver.current.disconnect(); } else { observer && observer.disconnect(); } }; useEffect(function() { if (lazy) { setTimeout(function() { if (intersecting) { setDispalyPercent(percent); } else { setDispalyPercent(0.01); } }, delay); } }, [ intersecting ]); var handleWebObserver = function() { /// web环境 if (lazy) { webObserver.current = new IntersectionObserver(function(entires, self) { entires.forEach(function(item) { setIntersecting(item.isIntersecting); }); }, { threshold: [ 0 ], rootMargin: '0px' }); webObserver.current.observe(progressRef.current); } handlePercent(); }; var handleOtherObserver = function() { // 非web环境 var observer = null; if (lazy) { observer = Taro.createIntersectionObserver(Taro.getCurrentInstance().page, { thresholds: [ 0 ], observeAll: true }); observer.relativeToViewport({ top: 0 }).observe("#".concat(selector), function(res) { setIntersecting(res.intersectionRatio > 0); }); } handlePercent(); }; useEffect(function() { if (web()) { handleWebObserver(); } else if (!harmony()) { handleOtherObserver(); } }, []); var getTextStyle = function() { return rtl ? { right: '100%' } : { left: '100%' }; }; var computeRight = function() { if (children) { return 0; } if (!harmony()) { return Math.floor("".concat(percent, "%").length * 9 / 2); } return Math.floor(("".concat(percent, "%").length * 9 + 4) / 2); }; var computeInnerStyle = function() { var style = { backgroundColor: effectiveColor || '#ff0f23', fontSize: fontSize && pxTransform(parseInt(fontSize.toString())) }; if (harmony()) { style.width = harmony() ? pxTransform("".concat(percent, "%").length * 9 + 4) : "".concat(percent, "%").length * 9 + 4; } return style; }; return /*#__PURE__*/ React.createElement(View, _object_spread({ ref: progressRef, id: selector, className: classNames(classPrefix, className), style: style, "aria-label": ariaLabel }, rest), /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-outer"), style: stylesOuter }, /*#__PURE__*/ React.createElement(View, { className: classesInner, style: _object_spread_props(_object_spread({}, stylesInner), { position: 'relative' }), onTransitionEnd: onActiveEnd })), effectiveShowText && /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-text"), style: { fontSize: fontSize && parseInt(fontSize.toString()) } }, children || "".concat(percent, "%"))); }; Progress.displayName = 'NutProgress';