Skip to content

WheelPicker (incubator) - fix updates #1693

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 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions src/incubator/WheelPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const WheelPicker = ({
preferredNumVisibleRows: numberOfVisibleRows
});

const prevInitialValue = useRef(initialValue);
const prevIndex = useRef(currentIndex);
const [scrollOffset, setScrollOffset] = useState(currentIndex * itemHeight);
const [flatListWidth, setFlatListWidth] = useState(0);
Expand Down Expand Up @@ -163,7 +164,13 @@ const WheelPicker = ({
// https://github.com/facebook/react-native/issues/26661
if (Constants.isAndroid && prevIndex.current !== index) {
prevIndex.current = index;
onChange?.(items?.[index]?.value, index);

if (prevInitialValue.current !== initialValue) {
// don't invoke 'onChange' if 'initialValue' changed
prevInitialValue.current = initialValue;
Copy link
Collaborator

@ethanshar ethanshar Nov 24, 2021

Choose a reason for hiding this comment

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

I wouldn't set the prevInitialValue value here just because scrollToIndex method is being called in several places.
Instead I'd set in the useEffect that handle the initialValue update (right after triggering the scroll)

Copy link
Collaborator Author

@Inbal-Tish Inbal-Tish Nov 28, 2021

Choose a reason for hiding this comment

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

Not sure I understand. In all other cases, it should not pass the condition...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes but technically the responsibility for re-setting the prevInitialValue is in the relevant useEffect and not in the method it calls to.
If someone will change the implementation of scrollToIndex they might break the functionality of prevInitialValue

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see how it works... Let's talk when you're available

} else {
onChange?.(items?.[index]?.value, index);
}
}
setTimeout(() => scrollToOffset(index, animated), 100);
};
Expand All @@ -176,10 +183,15 @@ const WheelPicker = ({
const onValueChange = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
setScrollOffset(event.nativeEvent.contentOffset.y);

const {index, value} = getRowItemAtOffset(event.nativeEvent.contentOffset.y);
onChange?.(value, index);
if (prevInitialValue.current !== initialValue) {
// don't invoke 'onChange' if 'initialValue' changed
prevInitialValue.current = initialValue;
} else {
const {index, value} = getRowItemAtOffset(event.nativeEvent.contentOffset.y);
onChange?.(value, index);
}
},
[onChange]);
[onChange, initialValue, getRowItemAtOffset]);

const alignmentStyle = useMemo(() => {
return align === WheelPickerAlign.RIGHT
Expand Down
4 changes: 2 additions & 2 deletions src/incubator/WheelPicker/usePresenter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useRef} from 'react';
import _ from 'lodash';
import React from 'react';
import {ItemProps} from './Item';
import useMiddleIndex from './helpers/useListMiddleIndex';

Expand Down Expand Up @@ -44,7 +44,7 @@ const usePresenter = ({
return items || [];
};

const items = useRef<ItemProps[]>(children ? extractItemsFromChildren() : propItems!).current;
const items = children ? extractItemsFromChildren() : propItems || [];
const middleIndex = useMiddleIndex({itemHeight, listSize: items.length});

const getSelectedValueIndex = () => {
Expand Down