-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix: stabilize reopen ordering in basic pickers #85447
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
marufsharifi
wants to merge
11
commits into
Expensify:main
Choose a base branch
from
marufsharifi:fix/initial-selection-ordering-basic-pickers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6189320
fix: stabilize reopen ordering in basic pickers
marufsharifi c3b3cd0
Merge branch 'main' into fix/initial-selection-ordering-basic-pickers
marufsharifi 4b893c1
Merge branch 'main' into fix/initial-selection-ordering-basic-pickers
marufsharifi be13995
refactor: rename useInitialSelection hook and simplify single-value c…
marufsharifi 2be0c60
fix: keep picker focus sync aligned with debounced search state
marufsharifi 3acb38b
fix: extend basic picker reopen ordering to dynamic country and state…
marufsharifi bd70341
refactor: limit new picker memoization and preserve existing behavior
marufsharifi c9189b5
fix: align SelectCountryStep country options with selection list types
marufsharifi d36078f
Merge branch 'main' into fix/initial-selection-ordering-basic-pickers
marufsharifi 0138ea8
refactor: simplify initial selection hook and memo deps
marufsharifi 00a7f22
fixed lint error
marufsharifi 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
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
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
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
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
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
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
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
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
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
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,50 @@ | ||
| import {useFocusEffect} from '@react-navigation/native'; | ||
| import type {DependencyList} from 'react'; | ||
| import {useCallback, useEffect, useRef, useState} from 'react'; | ||
|
|
||
| type UseInitialSelectionOptions = { | ||
| /** Dependencies that should trigger refreshing the snapshot (e.g., when a modal opens) */ | ||
| resetDeps?: DependencyList; | ||
|
|
||
| /** Whether to refresh the snapshot whenever the screen gains focus */ | ||
| resetOnFocus?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Keeps an immutable snapshot of the initial selection for the current open/focus cycle. | ||
| * Callers can refresh the snapshot by changing `resetDeps` or via screen focus. | ||
| */ | ||
| function useInitialSelection<T>(selection: T, options: UseInitialSelectionOptions = {}) { | ||
| const {resetDeps = [], resetOnFocus = false} = options; | ||
| const [initialSelection, setInitialSelection] = useState(selection); | ||
| const latestSelectionRef = useRef(selection); | ||
|
|
||
| const updateInitialSelection = useCallback((nextSelection: T) => { | ||
| setInitialSelection((previousSelection) => (Object.is(previousSelection, nextSelection) ? previousSelection : nextSelection)); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| latestSelectionRef.current = selection; | ||
| }, [selection]); | ||
|
|
||
| useEffect(() => { | ||
| // Intentionally refresh the snapshot only when the caller marks a new open/focus cycle. | ||
| // Live selection changes while the picker stays open should not repin or refocus the list. | ||
| updateInitialSelection(selection); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
marufsharifi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, resetDeps); | ||
|
|
||
| useFocusEffect( | ||
| useCallback(() => { | ||
| if (!resetOnFocus) { | ||
| return; | ||
| } | ||
|
|
||
| updateInitialSelection(latestSelectionRef.current); | ||
| }, [resetOnFocus, updateInitialSelection]), | ||
| ); | ||
|
|
||
| return initialSelection; | ||
| } | ||
|
|
||
| export default useInitialSelection; | ||
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,45 @@ | ||
| import CONST from '@src/CONST'; | ||
|
|
||
| function moveInitialSelectionToTopByKey(keys: string[], initialSelectedKeys: string[]): string[] { | ||
| if (initialSelectedKeys.length === 0 || keys.length <= CONST.MOVE_SELECTED_ITEMS_TO_TOP_OF_LIST_THRESHOLD) { | ||
| return keys; | ||
| } | ||
|
|
||
| const selectedKeys = new Set(initialSelectedKeys); | ||
| const selected: string[] = []; | ||
| const remaining: string[] = []; | ||
|
|
||
| for (const key of keys) { | ||
| if (selectedKeys.has(key)) { | ||
| selected.push(key); | ||
| continue; | ||
| } | ||
|
|
||
| remaining.push(key); | ||
| } | ||
|
|
||
| return [...selected, ...remaining]; | ||
| } | ||
|
|
||
| function moveInitialSelectionToTopByValue<T extends {value: string}>(items: T[], initialSelectedValues: string[]): T[] { | ||
| if (initialSelectedValues.length === 0 || items.length <= CONST.MOVE_SELECTED_ITEMS_TO_TOP_OF_LIST_THRESHOLD) { | ||
| return items; | ||
| } | ||
|
|
||
| const selectedValues = new Set(initialSelectedValues); | ||
| const selected: T[] = []; | ||
| const remaining: T[] = []; | ||
|
|
||
| for (const item of items) { | ||
| if (selectedValues.has(item.value)) { | ||
| selected.push(item); | ||
| continue; | ||
| } | ||
|
|
||
| remaining.push(item); | ||
| } | ||
|
|
||
| return [...selected, ...remaining]; | ||
| } | ||
|
|
||
| export {moveInitialSelectionToTopByKey, moveInitialSelectionToTopByValue}; |
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.
Uh oh!
There was an error while loading. Please reload this page.