Skip to content
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

fix: scroll date of birth year list on first mount only #23079

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const propTypes = {
};

function BaseSelectionListRadio(props) {
const firstLayoutRef = useRef(true);
const listRef = useRef(null);
const textInputRef = useRef(null);
const focusTimeoutRef = useRef(null);
Expand Down Expand Up @@ -254,7 +255,13 @@ function BaseSelectionListRadio(props) {
maxToRenderPerBatch={5}
windowSize={5}
viewabilityConfig={{viewAreaCoveragePercentThreshold: 95}}
onLayout={() => scrollToIndex(focusedIndex, false)}
onLayout={() => {
if (!firstLayoutRef.current) {
return;
}
Comment on lines +259 to +261
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allroundexperts Let's move the logic inside scrollToIndex method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... Are you sure about this? I mean it would be better if the scrollToIndex function would have single responsibility.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought it was used only for this instance , it seems to be used on ArrowKeyFocusManager as well .

Copy link
Contributor

@fedirjh fedirjh Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we can’t just remove the scrolling onLayout ? it does feel like a workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, removing it would not scroll the list to the last element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can’t we fix that with a useEffect ? something like

    useEffect(() => scrollToIndex(focusedIndex, false), [focusedIndex]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I tried first. It works on every platform except on iOS native 😿

Copy link
Contributor

@aldo-expensify aldo-expensify Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would have been to have a state + useEffect:

const [onLayoutTriggered, setOnLayoutTriggered] = useState(false);
useEffect(() => {
    if (onLayoutTriggered) {
        scrollToIndex(focusedIndex, false);
    }
}, [focusedIndex, onLayoutTriggered]);


...
<SectionList
       onLayout={() => setOnLayoutTriggered(true)}
</SectionList>

but I'm not really sure that that is better than what you did with the ref.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... Are you sure about this? I mean it would be better if the scrollToIndex function would have single responsibility.

Agree, I prefer how it is now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I'm not really sure that that is better than what you did with the ref.

I think ref is better approach as it doesn’t trigger a re-render when it's updated unlike using the state.

scrollToIndex(focusedIndex, false);
firstLayoutRef.current = false;
}}
/>
</View>
)}
Expand Down