Skip to content

feat: infiniteloading default scroll #3010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/packages/infiniteloading/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { InfiniteLoading } from '@nutui/nutui-react'
| onRefresh | 下拉刷新事件回调 | `() => Promise<void>` | `-` |
| onLoadMore | 继续加载的回调函数 | `() => Promise<void>` | `-` |
| onScroll | 实时监听滚动高度 | `(param: number) => void` | `-` |
| defaultScrollTop | 默认滚动距离 | `number` | `-` |

## 主题定制

Expand Down
17 changes: 17 additions & 0 deletions src/packages/infiniteloading/infiniteloading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface InfiniteLoadingProps extends BasicComponent {
onRefresh: () => Promise<void>
onLoadMore: () => Promise<void>
onScroll: (param: number) => void
defaultScrollTop?: number
}

declare let window: Window & { webkitRequestAnimationFrame: any } & {
Expand Down Expand Up @@ -60,6 +61,7 @@ export const InfiniteLoading: FunctionComponent<
onRefresh,
onLoadMore,
onScroll,
defaultScrollTop,
...restProps
} = {
...defaultProps,
Expand Down Expand Up @@ -101,6 +103,21 @@ export const InfiniteLoading: FunctionComponent<
}
}, [])

useEffect(() => {
if (defaultScrollTop) {
const childHeight =
(getRefreshTop().firstElementChild as HTMLElement).offsetHeight || 0
refreshMaxH.current = Math.floor(childHeight * 1 + 10)
setTimeout(() => {
if ((scrollEl.current as any)?.scrollTop !== undefined) {
;(scrollEl.current as any).scrollTop = defaultScrollTop
} else if ((scrollEl.current as any)?.scrollY !== undefined) {
;(scrollEl.current as any).scrollY = defaultScrollTop
}
}, 10)
}
}, [defaultScrollTop])

const getStyle = () => {
return {
height: distance.current < 0 ? `0px` : `${distance.current}px`,
Expand Down