-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #847 from thundersdata-frontend/rn-issue
feat: 为Tabs增加lazy加载的功能
- Loading branch information
Showing
5 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@td-design/react-native-tabs': minor | ||
--- | ||
|
||
feat: 为Tabs增加lazy功能 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useEffect } from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
import { useSafeState } from '@td-design/rn-hooks'; | ||
|
||
export default function SceneView({ | ||
lazy, | ||
currentPage, | ||
index, | ||
children, | ||
}: { | ||
lazy: boolean; | ||
currentPage: number; | ||
index: number; | ||
children: (props: { loading: boolean }) => React.ReactNode; | ||
}) { | ||
const [isLoading, setIsLoading] = useSafeState(lazy && Math.abs(currentPage - index) > 0); | ||
|
||
if (isLoading && Math.abs(currentPage - index) <= 0 && lazy) { | ||
setIsLoading(false); | ||
} | ||
|
||
useEffect(() => { | ||
let timer: NodeJS.Timeout | undefined; | ||
|
||
if (!lazy && isLoading) { | ||
timer = setTimeout(() => { | ||
setIsLoading(false); | ||
}, 0); | ||
} | ||
|
||
return () => { | ||
clearTimeout(timer); | ||
}; | ||
}, [lazy, isLoading]); | ||
|
||
const focused = currentPage === index; | ||
|
||
return ( | ||
<View | ||
accessibilityElementsHidden={!focused} | ||
importantForAccessibility={focused ? 'auto' : 'no-hide-descendants'} | ||
style={{ | ||
flex: 1, | ||
overflow: 'hidden', | ||
}} | ||
> | ||
{children({ loading: isLoading })} | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type Route = { | ||
key: string; | ||
icon?: string; | ||
title?: string; | ||
}; | ||
|
||
export type Scene<T extends Route> = { | ||
route: T; | ||
}; | ||
|
||
export type NavigationState<T extends Route> = { | ||
index: number; | ||
routes: T[]; | ||
}; |