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

React-native-wheely infinite scroll #42

Open
GiorgosKatsinoulas opened this issue Nov 15, 2022 · 5 comments
Open

React-native-wheely infinite scroll #42

GiorgosKatsinoulas opened this issue Nov 15, 2022 · 5 comments

Comments

@GiorgosKatsinoulas
Copy link

Hi, is it possible when I reach the last item on my list to show again the first item? Like infinite scroll at the picker wheely?

@erksch
Copy link
Owner

erksch commented Nov 16, 2022

Cool idea, but I will not be able to implement it, you can give it a try and make PR :)

@Mahmood-AlHajjo
Copy link

Mahmood-AlHajjo commented Aug 3, 2024

@erksch
you can make an infinite scroll like this:

const InfiniteWheelPicker = ({
  options,
  selectedIndex,
  onChange,
  ...rest
}) => {
  const extendedOptions = [...options, ...options, ...options];
  const middleIndex = Math.floor(extendedOptions.length / 3) + selectedIndex;
  const handleIndexChange = index => {
    const newIndex = index % options.length;
    onChange(newIndex);
  };

  return (
    <WheelPicker
      selectedIndex={middleIndex}
      options={extendedOptions}
      onChange={handleIndexChange}
      flatListProps={{
        initialNumToRender: extendedOptions.length,
        getItemLayout: (_, index) => ({ length: 45, offset: 45 * index, index }),
      }}
      visibleRest={4}
      itemHeight={45}
      {...rest}
    />
  );
};

and you can call it like this:

 const [selectedHour, setSelectedHour] = useState(12);
  const hours = Array.from({ length: 12 }, (_, i) => (i + 1).toString());
  
  <InfiniteWheelPicker
            selectedIndex={hours.indexOf(selectedHour.toString())}
            options={hours}
            onChange={index => setSelectedHour(hours[index])}
            itemStyle={styles.itemStyle}
            itemTextStyle={{
              fontSize: 18,
              fontWeight: 'bold',
            }}
            containerStyle={{ flex: 1, alignItems: 'flex-end' }}
            selectedIndicatorStyle={{
              backgroundColor: '#F4F4F5',
              borderRadius: 0,
              borderTopLeftRadius: 8,
              borderBottomLeftRadius: 8,
            }}
          />
video_2024-08-03_07-00-55.mp4

I hope that is helpful.

@haerim95
Copy link

haerim95 commented Aug 8, 2024

@Mahmood-AlHajjo

YOU GENIUS 🤩 Just what I needed

@erksch
Copy link
Owner

erksch commented Aug 8, 2024

@Mahmood-AlHajjo Pretty cool trick 😄

@haerim95
Copy link

@Mahmood-AlHajjo

I have a question about your trick.
I only want to give visibleRest a value of 1, because I only want to show one value above and one below.

  const [selectedMinute, setSelectedMinute] = useState<number>(Number(minute));
  const hours = Array.from({ length: 60 }, (_, i) => (i + 1).toString());
  
  <InfiniteWheelPicker
            selectedIndex={hours.indexOf(selectedHour.toString())}
            options={hours}
            onChange={index => setSelectedHour(hours[index])}
            itemStyle={styles.itemStyle}
            itemTextStyle={{
              fontSize: 18,
              fontWeight: 'bold',
            }}
            containerStyle={{ flex: 1, alignItems: 'flex-end' }}
            selectedIndicatorStyle={{
              backgroundColor: '#F4F4F5',
              borderRadius: 0,
              borderTopLeftRadius: 8,
              borderBottomLeftRadius: 8,
            }}
          />
const InfiniteWheelPicker = ({
  options,
  selectedIndex,
  onChange,
  ...rest
}) => {
  const extendedOptions = [...options, ...options, ...options];
  const middleIndex = Math.floor(extendedOptions.length / 3) + selectedIndex;
  const handleIndexChange = index => {
    const newIndex = index % options.length;
    onChange(newIndex);
  };

  return (
    <WheelPicker
      selectedIndex={middleIndex}
      options={extendedOptions}
      onChange={handleIndexChange}
      flatListProps={{
        initialNumToRender: extendedOptions.length,
        getItemLayout: (_, index) => ({ length: 70, offset: 70 * index, index }),
      }}
      visibleRest={1}
      itemHeight={70}
      {...rest}
    />
  );
};

Aug-14-2024 14-40-31

But visibleRest={1} is not available. The option disappears like this, and then you scroll back to see it again..
If I give visibleRest a value of 4, this doesn't happen, but I only want to show three.

Is there any solution to this problem?
This issue only occurs when implementing minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants