PowerScrollView
PowerScrollView 下拉刷新&加载更多的 ScrollView
代码示例
import { BfPowerScrollView } from '@bud-fe/react-taro-ui';import { Text, View } from '@tarojs/components';import { useEffect, useState } from 'react';import './index.module.less';const mockRequest = async (_startIndex, isRefresh, name) => {const sleep = (t) =>new Promise((resolve) => {setTimeout(() => {resolve();}, t);});const TOTAL = 100;let startIndex = _startIndex;if (isRefresh) {startIndex = 0;}if (startIndex >= TOTAL) {return [];}console.log(`${name}:请求~`, 'isRefresh:', isRefresh);await sleep(1200);const list = [];for (let i = 0; i < 20; i++) {list.push(`${name}:`);}return list;};export default function () {const [state, changeState] = useState({basicsList: [],basicsFinished: false,});const setState = (newState) => {changeState({...state,...newState,});};// 基础用法const basicsDoRefresh = async (event = 0) => {const append = await mockRequest(state.basicsList.length, true, '基础用法');setState({basicsList: append.map((item, index) => (<View key={`${state.basicsList.length + index}append`} style={{ height: '40px', lineHeight: '40px' }}>{item}<Text>{`index:${state.basicsList.length + index + 1}`}</Text>{/* <Tag type="success">{`index:${state.basicsList.length +index +1}`}</Tag> */}</View>)),basicsFinished: append.length === 0,});};const basicsLoadMore = async (event = 0, isRefresh = false) => {let append = await mockRequest(state.basicsList.length, isRefresh, '基础用法');append = append.map((item, index) => (<View key={`${state.basicsList.length + index}append`} style={{ height: '40px', lineHeight: '40px' }}>{item}<Text>{`index:${state.basicsList.length + index + 1}`}</Text>{/* <Tag type="success"></Tag> */}</View>));setState({basicsList: [...state.basicsList, ...append],basicsFinished: append.length === 0,});};useEffect(() => {basicsLoadMore();}, []);return (<><BfPowerScrollViewfinishedText="没有更多了"successText="刷新成功"// refresherEnabled={false}onScrollToUpper={basicsDoRefresh}onScrollToLower={basicsLoadMore}current={state.basicsList.length}finished={state.basicsFinished}header={<View style={{ lineHeight: '50px' }}>Head</View>}footer={<View style={{ lineHeight: '50px' }}>Footer</View>}lowerThreshold={300}>{state.basicsList.map((e, i) => (<View key={i}>{e}</View>))}</BfPowerScrollView>{/* */}</>);}
API
| Name | Description | Type | Default |
|---|---|---|---|
| total | 列表总个数 | any | -- |
| children | 子元素 | ReactNode | (required) |
| current | 当前列表个数 | number | children.length |
| pageSize | 一页个数 | number | 20 |
| minTriggerTopDistance | 最小触发下拉距离顶部距离 | number | 150 |
| finished | 是否已加载完成,加载完成后不再触发 load 事件 | boolean | false |
| errorText | 加载失败后的提示文案 | string | -- |
| loadingText | 加载过程提示文案 | string | 加载中... |
| finishedText | 加载完成后的提示文案 | string | '没有更多了' |
| renderFinished | 自定义加载完成后的提示文案 | ReactNode | -- |
| renderLoading | 自定义底部加载中提示 | ReactNode | -- |
| renderError | 自定义加载失败后的提示文案 | ReactNode | -- |
| onScrollToUpper | 下拉刷新时触发 | (event: number | eventType) => Promise<void> | -- |
| onScrollToLower | 滚动条与底部距离小于 lowerThreshold 时触发 | (event: number | eventType) => Promise<void> | -- |
| header | 头部 | ReactNode | -- |
| footer | 尾部 | ReactNode | -- |
| id | 组件的唯一标示, 保持整个页面唯一 | string | -- |
| className | 同 `class`,在 React/Nerv 里一般使用 `className` 作为 `class` 的代称 | string | -- |
| style | 组件的内联样式, 可以动态设置的内联样式 | string | CSSProperties | -- |
| key | 如果列表中项目的位置会动态改变或者有新的项目添加到列表中, 需要使用 `wx:key` 来指定列表中项目的唯一的标识符。 | string | number | -- |
| hidden | 组件是否显示, 所有组件默认显示 | boolean | -- |
| animation | 动画属性 | any | -- |
| ref | 引用 | LegacyRef<any> | -- |
| dangerouslySetInnerHTML | 渲染 HTML @see /docs/html | { __html: string; } | -- |
| onClick | 手指触摸后马上离开 | (event: ITouchEvent) => void | -- |
| onLongPress | 手指触摸后,超过350ms再离开,如果指定了事件回调函数并触发了这个事件,tap事件将不被触发 | (event: CommonEvent<any>) => void | -- |
| onLongClick | 手指触摸后,超过350ms再离开(推荐使用 longpress 事件代替) | (event: CommonEvent<any>) => void | -- |
| onTransitionEnd | 会在 WXSS transition 或 Taro.createAnimation 动画结束后触发 | (event: CommonEvent<any>) => void | -- |
| onAnimationStart | 会在一个 WXSS animation 动画开始时触发 | (event: CommonEvent<any>) => void | -- |
| onAnimationIteration | 会在一个 WXSS animation 一次迭代结束时触发 | (event: CommonEvent<any>) => void | -- |
| onAnimationEnd | 会在一个 WXSS animation 动画完成时触发 | (event: CommonEvent<any>) => void | -- |
| onTouchForceChange | 在支持 3D Touch 的 iPhone 设备,重按时会触发 | (event: CommonEvent<any>) => void | -- |
| headHeight | 顶部内容高度 | string | number | -- |
| successText | 刷新成功提示文案 | string | -- |
| pullingText | 下拉过程提示文案 | string | '下拉即可刷新...' |
| loosingText | 释放过程提示文案 | string | '释放即可刷新...' |
| pullDistance | 触发下拉刷新的距离 | string | number | 与 refresherThreshold || pullDistance 一致 |
| successDuration | 刷新成功提示展示时长(ms) | string | number | 500 |
| animationDuration | 动画时长 | string | number | 300 |
| renderHead | 自定义下拉刷新区域 {status: 下拉状态, distance: 当前下拉距离} | (params: { status: PullRefreshStatus; distance: number; }) => void | ReactNode | -- |
| onTouchStart | 触摸动作开始。 @supported alipay | CommonEventFunction<any> | -- |
| onTouchEnd | 触摸动作结束。 @supported alipay | CommonEventFunction<any> | -- |
| onTouchCancel | 触摸动作被打断,如来电提醒、弹窗。 @supported alipay | CommonEventFunction<any> | -- |
| onTouchMove | 触摸后移动。 @supported alipay | CommonEventFunction<any> | -- |
| scrollX | 允许横向滚动 @supported weapp, alipay, swan, tt, qq, jd, h5, rn @rn 二选一 | boolean | false |
| scrollY | 允许纵向滚动 @supported weapp, alipay, swan, tt, qq, jd, h5, rn @rn 二选一 | boolean | false |
| upperThreshold | 距顶部/左边多远时(单位px),触发 scrolltoupper 事件 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | number | 50 |
| lowerThreshold | 距底部/右边多远时(单位px),触发 scrolltolower 事件 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | number | 50 |
| scrollTop | 设置竖向滚动条位置 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | number | -- |
| scrollLeft | 设置横向滚动条位置 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | number | -- |
| scrollIntoView | 值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素 @supported weapp, alipay, swan, tt, qq, jd, h5 | string | -- |
| scrollWithAnimation | 在设置滚动条位置时使用动画过渡 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | boolean | false |
| enableBackToTop | iOS 点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向 @supported weapp, alipay, swan, qq, jd, rn | boolean | false |
| enableFlex | 启用 flexbox 布局。开启后,当前节点声明了 `display: flex` 就会成为 flex container,并作用于其孩子节点。 @supported weapp, jd | boolean | false |
| scrollAnchoring | 开启 scroll anchoring 特性,即控制滚动位置不随内容变化而抖动,仅在 iOS 下生效,安卓下可参考 CSS `overflow-anchor` 属性。 @supported weapp | boolean | false |
| refresherEnabled | 开启自定义下拉刷新 @supported weapp | boolean | false |
| refresherThreshold | 设置自定义下拉刷新阈值 @supported weapp | number | 45 |
| refresherDefaultStyle | 设置自定义下拉刷新默认样式,支持设置 `black | white | none`, none 表示不使用默认样式 @supported weapp | string | 'black' |
| refresherBackground | 设置自定义下拉刷新区域背景颜色 @supported weapp | string | '#FFF' |
| refresherTriggered | 设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发 @supported weapp | boolean | false |
| enhanced | 启用 scroll-view 增强特性 @supported weapp | boolean | false |
| bounces | iOS 下 scroll-view 边界弹性控制 (同时开启 enhanced 属性后生效) @supported weapp | boolean | true |
| showScrollbar | 滚动条显隐控制 (同时开启 enhanced 属性后生效) @supported weapp | boolean | true |
| pagingEnabled | 分页滑动效果 (同时开启 enhanced 属性后生效) @supported weapp | boolean | false |
| fastDeceleration | boolean false 滑动减速速率控制 (同时开启 enhanced 属性后生效) @supported weapp | boolean | false |
| scrollAnimationDuration | 当 scroll-with-animation设置为 true 时,可以设置 scroll-animation-duration 来控制动画的执行时间,单位 ms。 @supported alipay | string | -- |
| trapScroll | 纵向滚动时,当滚动到顶部或底部时,强制禁止触发页面滚动,仍然只触发 scroll-view 自身的滚动。 @supported alipay | string | false |
| disableLowerScroll | 发生滚动前,对滚动方向进行判断,当方向是顶部/左边时,如果值为 always 将始终禁止滚动,如果值为 out-of-bounds 且当前已经滚动到顶部/左边,禁止滚动。 @supported alipay | string | -- |
| disableUpperScroll | 发生滚动前,对滚动方向进行判断,当方向是底部/右边时,如果值为 always 将始终禁止滚动,如果值为 out-of-bounds 且当前已经滚动到底部/右边,禁止滚动。 @supported alipay | string | -- |
| ariaLabel | 无障碍访问,(属性)元素的额外描述 @supported qq | string | -- |
| enablePassive | 开启 passive 特性,能优化一定的滚动性能 @supported weapp | string | false |
| onScroll | 滚动时触发 @supported weapp, alipay, swan, tt, qq, jd, h5, rn | BaseEventOrigFunction<onScrollDetail> | -- |
| onRefresherPulling | 自定义下拉刷新控件被下拉 @supported weapp | CommonEventFunction<any> | -- |
| onRefresherRefresh | 自定义下拉刷新被触发 @supported weapp | CommonEventFunction<any> | -- |
| onRefresherRestore | 自定义下拉刷新被复位 @supported weapp | CommonEventFunction<any> | -- |
| onRefresherAbort | 自定义下拉刷新被中止 @supported weapp | CommonEventFunction<any> | -- |
| onDragStart | 滑动开始事件 (同时开启 enhanced 属性后生效) @supported weapp | CommonEventFunction<onDragDetail> | -- |
| onDragging | 滑动事件 (同时开启 enhanced 属性后生效) @supported weapp | CommonEventFunction<onDragDetail> | -- |
| onDragEnd | 滑动结束事件 (同时开启 enhanced 属性后生效) @supported weapp | CommonEventFunction<onDragDetail> | -- |