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

onValuesChangeFinish() makes slider to stop after one date #278

Open
asjustis opened this issue Feb 26, 2023 · 2 comments
Open

onValuesChangeFinish() makes slider to stop after one date #278

asjustis opened this issue Feb 26, 2023 · 2 comments

Comments

@asjustis
Copy link

I try to implement a multi-slider with two values. It worked fine until I wanted to process and update values after the change.

I started using onValuesChangeFinish(), tried the onValuesChange() as well. If I set these functions and keep them empty, it works. If I start to change my own variable (not used in the slider) as in setPeakStartDate(addDays(minDate, values[0])); it starts to jump back to the initial position after I release the slider. Tried setting the values manually through peakSliderValues variable, but it didn't help as well. Am I missing something?

<MultiSlider
          containerStyle={{ paddingHorizontal: 16, backgroundColor: 'red' }}
          sliderLength={width - 32} //minus the 2 * padding
          snapped
          step={1}
          min={0}
          max={steps}
          enabledOne={true}
          enabledTwo={true}
          enableLabel={true}
          smoothSnapped={true}
          customLabel={props => <SliderLabel startDate={minDate} {...props} />}

          /* HERE THE FUN BEGINS */
          
          //values={[peakSliderValues[0], peakSliderValues[1]]}
          values={[0, 10]}
          // onValuesChange={values => {
          //   setPeakSliderValues(values);
          //   setPeakStartDate(addDays(minDate, values[0]));
          //   setPeakEndDate(addDays(minDate, values[1]));
          // }}

          onValuesChangeFinish={values => {
            //   setPeakSliderValues(values);
            //   setPeakStartDate(addDays(minDate, values[0]));
            //   setPeakEndDate(addDays(minDate, values[1]));
          }}

Thanks

@asjustis
Copy link
Author

Classic. Just after writing down the issue, I found the right combination. I would expect onValuesChangeFinish() could let just do the side-effect action without changing any values behavior internally, but oh well, this will do afterall:

<MultiSlider
          ...
          values={[peakSliderValues[0], peakSliderValues[1]]}

          onValuesChangeFinish={values => {
            setPeakSliderValues(values);
            setPeakStartDate(addDays(minDate, values[0]));
            setPeakEndDate(addDays(minDate, values[1]));
          }}

Cheers

@asjustis
Copy link
Author

asjustis commented Feb 27, 2023

Hm, reopening the issue, seems there is something weird indeed.

const [sliderValues, setSliderValues] = useState([0, 10]);
  
useEffect(() => {
    //debugger;
    setSliderValues(values);
  }, values);

const updateValues = values => {
    //debugger;
    props.onValueChanged(values); // Comes from parent component as param (posting below)
  };

<MultiSlider
        containerStyle={{ paddingHorizontal: 16 }}
        selectedStyle={{ backgroundColor: theme.colors.primary }}
        trackStyle={{ backgroundColor: theme.colors.secondary }}
        sliderLength={width - 32} //minus the 2 * padding
        values={[sliderValues[0], sliderValues[1]]}
        snapped
        step={1}
        min={0}
        max={steps}
        enabledOne={true}
        enabledTwo={true}
        enableLabel={true}
        //smoothSnapped={true}
        customLabel={props => (
          <SliderLabel
            startDate={initialDate}
            labelOne={'Peak starts on'}
            labelTwo={'Peak ends on'}
            {...props}
          />
        )}
        //onValuesChangeFinish={onValueChanged}
        onValuesChangeFinish={values => {
          setSliderValues(values);
          updateValues(values);
          console.log('vlaues', values);
        }}
      />

And this is called from:

<Slider
        // other props ...
        onValueChanged={values => {
          setPeakStartDate(addDays(dateCompStart, values[0])); // just React.useState() setters
          setPeakEndDate(addDays(dateCompStart, values[1]));
        }}
      />

If I comment out updateValues(values); method, the slider works ok. If I keep it uncommented, the sliders are reset back to initial position after releasing the finger.

Any advice?

@asjustis asjustis reopened this Feb 27, 2023
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

1 participant