-
Notifications
You must be signed in to change notification settings - Fork 1
feat: fix scroll behavior on asset lists [PERA-3277] #394
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
apps/mobile/src/components/SearchableList/SearchableList.tsx
This file contains hidden or 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,182 @@ | ||
| /* | ||
| Copyright 2022-2025 Pera Wallet, LDA | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License | ||
| */ | ||
|
|
||
| import React, { createElement, forwardRef, useCallback, useMemo } from 'react' | ||
| import type { LegendListRenderItemProps } from '@legendapp/list' | ||
|
|
||
| import { PWFlatList, PWView } from '@components/core' | ||
| import type { PWFlatListProps, PWFlatListRef } from '@components/core' | ||
| import { SearchInput } from '@components/SearchInput' | ||
| import { | ||
| isSearchSentinel, | ||
| useSearchableList, | ||
| type AugmentedItem, | ||
| } from './useSearchableList' | ||
| import { DEFAULT_SNAP_THRESHOLD, SCROLL_EVENT_THROTTLE } from '@constants/ui' | ||
| import { Maybe } from '@perawallet/wallet-core-shared' | ||
|
|
||
| type RenderItem<T> = (props: LegendListRenderItemProps<T>) => React.ReactNode | ||
|
|
||
| const renderHeaderNode = ( | ||
| component: Maybe<React.ComponentType | React.ReactElement>, | ||
| ): React.ReactNode => { | ||
| if (component == null) { | ||
| return null | ||
| } | ||
| if (typeof component === 'function') { | ||
| const Component = component | ||
| return <Component /> | ||
| } | ||
| return component | ||
| } | ||
|
|
||
| export type SearchableListProps<T> = Omit< | ||
| PWFlatListProps<T>, | ||
| 'stickyIndices' | 'renderItem' | ||
| > & { | ||
| renderItem?: RenderItem<T> | ||
| searchValue?: string | ||
| searchPlaceholder?: string | ||
| onSearchChange?: (value: string) => void | ||
| /** | ||
| * Fraction of the header (`ListHeaderComponent`) revealed during a drag | ||
| * required to snap to fully expanded; otherwise snap to fully collapsed | ||
| * (search bar pinned). Defaults to 0.25. | ||
| */ | ||
| snapThreshold?: number | ||
| } | ||
|
|
||
| const SearchableListInner = <T,>( | ||
| props: SearchableListProps<T>, | ||
| ref: React.ForwardedRef<PWFlatListRef>, | ||
| ) => { | ||
| const { | ||
| ListHeaderComponent, | ||
| ListFooterComponent, | ||
| data, | ||
| renderItem, | ||
| keyExtractor, | ||
| searchValue, | ||
| searchPlaceholder, | ||
| onSearchChange, | ||
| snapThreshold = DEFAULT_SNAP_THRESHOLD, | ||
| onScroll, | ||
| onScrollEndDrag, | ||
| // children is part of the React props type but not used by the list. | ||
| children: _children, | ||
| ...listProps | ||
| } = props | ||
|
|
||
| const { | ||
| listRef, | ||
| augmentedData, | ||
| augmentedKeyExtractor, | ||
| toUserIndex, | ||
| searchFooterHeight, | ||
| handleHeaderLayout, | ||
| handleListLayout, | ||
| handleContentSizeChange, | ||
| handleSearchFocus, | ||
| handleScroll, | ||
| handleScrollEndDrag, | ||
| } = useSearchableList<T>({ | ||
| forwardedRef: ref, | ||
| data, | ||
| keyExtractor, | ||
| snapThreshold, | ||
| itemHeightEstimate: listProps.estimatedItemSize, | ||
| onScroll, | ||
| onScrollEndDrag, | ||
| }) | ||
|
|
||
| const augmentedHeader = useMemo( | ||
| () => ( | ||
| <PWView onLayout={handleHeaderLayout}> | ||
| {renderHeaderNode(ListHeaderComponent)} | ||
| </PWView> | ||
| ), | ||
| [ListHeaderComponent, handleHeaderLayout], | ||
| ) | ||
|
|
||
| const augmentedFooter = useMemo(() => { | ||
| const callerFooter = renderHeaderNode(ListFooterComponent) | ||
| if (searchFooterHeight <= 0 && callerFooter == null) { | ||
| return null | ||
| } | ||
| return ( | ||
| <> | ||
| {callerFooter} | ||
| {searchFooterHeight > 0 && ( | ||
| <PWView style={{ height: searchFooterHeight }} /> | ||
| )} | ||
| </> | ||
| ) | ||
| }, [ListFooterComponent, searchFooterHeight]) | ||
|
|
||
| const augmentedRenderItem = useCallback<RenderItem<AugmentedItem<T>>>( | ||
| info => { | ||
| if (isSearchSentinel(info.item)) { | ||
| return ( | ||
| <SearchInput | ||
| value={searchValue} | ||
| onFocus={handleSearchFocus} | ||
| placeholder={searchPlaceholder} | ||
| onChangeText={onSearchChange} | ||
| /> | ||
| ) | ||
| } | ||
| return ( | ||
| renderItem?.({ | ||
| ...info, | ||
| item: info.item, | ||
| index: toUserIndex(info.index), | ||
| data: (info.data?.slice(1) ?? []) as readonly T[], | ||
| }) ?? null | ||
| ) | ||
| }, | ||
| [ | ||
| renderItem, | ||
| searchValue, | ||
| searchPlaceholder, | ||
| onSearchChange, | ||
| handleSearchFocus, | ||
| toUserIndex, | ||
| ], | ||
| ) | ||
|
|
||
| // LegendList's data-mode prop type uses `children: never`, while React's | ||
| // intrinsic component types always add `children?: ReactNode` — so any | ||
| // structural cast at this boundary fails. The single `any` here is | ||
| // strictly to bridge that mismatch; everything we *write* (data, | ||
| // renderItem, keyExtractor, etc.) is properly typed above. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| return createElement(PWFlatList as any, { | ||
| ...listProps, | ||
| ref: listRef, | ||
| data: augmentedData, | ||
| renderItem: augmentedRenderItem, | ||
| keyExtractor: augmentedKeyExtractor, | ||
| ListHeaderComponent: augmentedHeader, | ||
| ListFooterComponent: augmentedFooter, | ||
| stickyIndices: [0], | ||
| maintainVisibleContentPosition: true, | ||
| onLayout: handleListLayout, | ||
| onContentSizeChange: handleContentSizeChange, | ||
| onScroll: handleScroll, | ||
| onScrollEndDrag: handleScrollEndDrag, | ||
| scrollEventThrottle: SCROLL_EVENT_THROTTLE, | ||
| }) | ||
| } | ||
|
|
||
| export const SearchableList = forwardRef(SearchableListInner) as <T>( | ||
| props: SearchableListProps<T> & React.RefAttributes<PWFlatListRef>, | ||
| ) => React.ReactElement | ||
This file contains hidden or 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 @@ | ||
| /* | ||
| Copyright 2022-2025 Pera Wallet, LDA | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License | ||
| */ | ||
|
|
||
| export { SearchableList } from './SearchableList' | ||
| export type { SearchableListProps } from './SearchableList' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I don't like much this pattern because this is basically returning a React node every time, which means it's a component. But it's not written as a component. And since it's not a component, you cannot use react features which might be interesting sometimes like
React.memo, etc.I'd rather design that as a sub-component instead and use it in the more React idiomatic way.
But there's no issue here since you already mitigated the risks of recalculating the result unnecessarily with the useMemo down the line. So I'm really just expressing an personal opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is required because of the way the type for this prop is defined on LegendList - it's not really up to us.