UNPKG

@nutui/nutui-react-taro

Version:

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

152 lines (151 loc) 5.6 kB
import { a as __awaiter } from "./tslib.es6-iWu3F_1J.js"; import React__default, { useRef, useState } from "react"; import classNames from "classnames"; import { View } from "@tarojs/components"; import { Loading, More } from "@nutui/icons-react-taro"; import Taro from "@tarojs/taro"; import { u as useConfig } from "./configprovider.taro-DpK4IiCE.js"; import { u as useTouch } from "./use-touch-BeiLHDO9.js"; import { C as ComponentDefaults } from "./typings-DV9RBfhj.js"; import { a as getDeviceInfo } from "./get-system-info-D27xNglo.js"; function bound(position, min, max) { let ret = position; { ret = Math.max(position, min); } { ret = Math.min(ret, max); } return ret; } function rubberband(distance, dimension, constant) { return distance * dimension * constant / (dimension + constant * distance); } function rubberbandIfOutOfBounds(position, min, max, dimension, constant = 0.15) { if (constant === 0) return bound(position, min, max); if (position < min) return -rubberband(min - position, dimension, constant) + min; if (position > max) return +rubberband(position - max, dimension, constant) + max; return position; } const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); const defaultProps = Object.assign(Object.assign({}, ComponentDefaults), { type: "default", pullingText: "", canReleaseText: "", refreshingText: "", completeText: "", completeDelay: 500, disabled: false, headHeight: 50, threshold: 60, scrollTop: 0, onRefresh: () => { } }); const PullToRefresh = (p) => { const classPrefix = "nut-pulltorefresh"; const { locale } = useConfig(); const touch = useTouch(); const props = Object.assign(Object.assign(Object.assign({}, defaultProps), p), { pullingText: p.pullingText || locale.pullToRefresh.pullingText, canReleaseText: p.canReleaseText || locale.pullToRefresh.canReleaseText, refreshingText: p.refreshingText || locale.pullToRefresh.refreshingText, completeText: p.completeText || locale.pullToRefresh.completeText }); const classes = classNames(classPrefix, props.className, `${classPrefix}-${props.type}`); const headHeight = props.headHeight; const threshold = props.threshold; const pullingRef = useRef(false); const [status, setStatus] = useState("pulling"); const [height, setHeight] = useState(0); const renderIcons = (status2) => { return React__default.createElement( React__default.Fragment, null, (status2 === "pulling" || status2 === "complete") && React__default.createElement(Loading, null), (status2 === "canRelease" || status2 === "refreshing") && React__default.createElement(More, null) ); }; const renderStatusIcon = () => { var _a; if (props.renderIcon) { return (_a = props.renderIcon) === null || _a === void 0 ? void 0 : _a.call(props, status); } return renderIcons(status); }; const renderStatusText = () => { var _a; if (props.renderText) { return (_a = props.renderText) === null || _a === void 0 ? void 0 : _a.call(props, status); } if (status === "pulling") return props.pullingText; if (status === "canRelease") return props.canReleaseText; if (status === "refreshing") return props.refreshingText; if (status === "complete") return props.completeText; return ""; }; const handleTouchStart = (e) => { if (props.disabled) return; touch.start(e); }; const handleTouchMove = (e) => { if (props.scrollTop > 0 || props.disabled) { return; } if (status === "refreshing" || status === "complete") return; touch.move(e); if (touch.isVertical()) { pullingRef.current = true; setHeight(Math.max(rubberbandIfOutOfBounds(touch.deltaY.current, 0, 0, headHeight * 5, 0.5), 0)); setStatus(height > threshold ? "canRelease" : "pulling"); } }; function doRefresh() { return __awaiter(this, void 0, void 0, function* () { setHeight(headHeight); setStatus("refreshing"); try { yield props.onRefresh(); setStatus("complete"); } catch (e) { setHeight(0); setStatus("pulling"); throw e; } if (props.completeDelay > 0) { yield sleep(props.completeDelay); } setHeight(0); setStatus("pulling"); }); } const handleTouchEnd = () => { if (props.disabled) return; pullingRef.current = false; if (status === "canRelease") { doRefresh(); } else { setHeight(0); setStatus("pulling"); } }; const isAndroidWeApp = getDeviceInfo().platform === "android" && Taro.getEnv() === "WEAPP"; const springStyles = Object.assign({ height: `${height}px` }, !pullingRef.current || isAndroidWeApp ? { transition: "height .3s ease" } : {}); return React__default.createElement( View, { className: classes, style: props.style, onTouchStart: handleTouchStart, onTouchMove: handleTouchMove, onTouchEnd: handleTouchEnd }, React__default.createElement( View, { style: springStyles, className: `${classPrefix}-head` }, React__default.createElement( "div", { className: `${classPrefix}-head-content`, style: { height: headHeight } }, React__default.createElement("div", null, renderStatusIcon()), React__default.createElement("div", null, renderStatusText()) ) ), React__default.createElement("div", { className: `${classPrefix}-content` }, props.children) ); }; PullToRefresh.displayName = "NutPullToRefresh"; export { PullToRefresh as P };