slider icon indicating copy to clipboard operation
slider copied to clipboard

How do I reset values for slider

Open doshisunny opened this issue 9 years ago • 13 comments

I have a slider as below which uses marks for custom dates. Sliding functionality is working. Once slider moves and certain point I want to reset slider to defult values after some action (For e.g Button click).

There is not reset or custom method option in props which I can use. What is the best way to do that?

Original Slider : screen shot 2017-01-10 at 3 13 36 pm

User Changes Slider to : screen shot 2017-01-10 at 3 20 26 pm

After reset button click it need to go back to : screen shot 2017-01-10 at 3 13 36 pm

doshisunny avatar Jan 10 '17 21:01 doshisunny

Use prop value to convert Slider to a controlled component.

yesmeck avatar Jan 11 '17 02:01 yesmeck

I have the same issue. As soon as I use the value prop, I am unable to grab the slider handles.

@yesmeck Do you perhaps have an example solution for this use case specific to rc-slider?

sandervanhooft avatar Jan 19 '17 17:01 sandervanhooft

@sandervanhooft See https://github.com/ant-design/ant-design/wiki/FAQ#i-set-the-value-of-inputselectetc-and-then-it-cannot-be-changed-by-users-action

yesmeck avatar Jan 20 '17 01:01 yesmeck

@yesmeck This is a little bit confusing. Changing defaultValue makes a lot of sense, but the component's componentWillReceiveProps() ignores changes to defaultValue. Thus the component won't rerender. Is this a bug, or expected behavior? If it's expected, then I'm confused on how to accomplish the goal outlined by the OP.

icambron avatar Apr 05 '17 20:04 icambron

defaultValue is not considered to change, if you want change value of slider, use value. See

https://facebook.github.io/react/docs/forms.html#controlled-components https://facebook.github.io/react/docs/uncontrolled-components.html

yesmeck avatar Apr 06 '17 02:04 yesmeck

@yesmeck but if I set value the user can't make further edits. Here is a fiddle: if I change defaultValue to value then slider is locked in place. Perhaps I'm being dense, but I don't see how using an uncontrolled component would help, since then I wouldn't be able to actually set it. (Another fiddle). What am I missing?

icambron avatar Apr 06 '17 06:04 icambron

but I don't see how using an uncontrolled component would help

Then you should use controlled component.

yesmeck avatar Apr 06 '17 06:04 yesmeck

@yesmeck that's my first fiddle though, right? I think I must be missing something more basic here.

icambron avatar Apr 06 '17 06:04 icambron

No, your first fiddle is still a uncontrolled component. A controlled component means you pass value to the component and use onChange to collect the component value to the state.

See http://codepen.io/yesmeck/pen/evaJPP?editors=0010

yesmeck avatar Apr 06 '17 06:04 yesmeck

Ah, OK, I get it now. Thanks for your time!

icambron avatar Apr 06 '17 06:04 icambron

@yesmeck thanks, I implemented and it works so fine!

linatoquica93 avatar Jul 26 '17 02:07 linatoquica93

No, your first fiddle is still a uncontrolled component. A controlled component means you pass value to the component and use onChange to collect the component value to the state.

See http://codepen.io/yesmeck/pen/evaJPP?editors=0010

awesome, thanks!

bodfaj13 avatar Feb 07 '20 18:02 bodfaj13

I just faced this issue in my project. Following solution may help you.

Scenario: The project is in React-nextJS framework. We have to use recommended client components. There is price slider component called as RangeSelector on one page. The issue was that on resetting that RangeSelector, functionally wise it works well but on slider it was showing same last searched values and slider point tip. that is it was showing previous min max values.

Solution: RangeSelector is in child component. In RangeSelector component, two JSX attributes - key & onAfterChange - used like: key={props.onReseTrue} & onAfterChange(e, props.min_val, props.max_val) }}. These are defined in main component as follows: function onAfterChange(data,minVal,maxVal) { data.minval=minVal; data.maxval=maxVal; }
Important : We used a prop "key" in RangeSelector slider component and set its value true on clicking reset button. Pass both these values as props to child component. This way solved Range Slider reset issue.

udayanbk avatar Apr 21 '21 09:04 udayanbk