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

feat: add slider upper and lower limits #410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ AppRegistry.registerComponent('SliderExample', () => SliderExample);
| minimumTrackStyle | [style](http://facebook.github.io/react-native/docs/view.html#style) | Yes | | The style applied to the track left of the thumb |
| maximumTrackStyle | [style](http://facebook.github.io/react-native/docs/view.html#style) | Yes | | The style applied to the track right of the thumb |
| value | number or Array | Yes | 0 | Initial value of the slider. The value should be a number or array of numbers between minimumValue and maximumValue, which default to 0 and 1 respectively. Default value is 0. _This is not a controlled component_, e.g. if you don't update the value, the component won't be reset to its inital value. |
| lowerLimit | number | Yes | undefined | Slide lower limit. The user won't be able to slide below this limit. |
| upperLimit | number | Yes | undefined | Slide upper limit. The user won't be able to slide above this limit. |

---

Expand Down
8 changes: 5 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class Slider extends PureComponent<SliderProps, SliderState> {
value: 0,
vertical: false,
startFromZero: false,
lowerLimit: undefined,
Copy link

Choose a reason for hiding this comment

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

there is no sense to specify default values

upperLimit: undefined,
};

static getDerivedStateFromProps(props: SliderProps, state: SliderState) {
Expand Down Expand Up @@ -405,15 +407,15 @@ export class Slider extends PureComponent<SliderProps, SliderState> {
};
_getValue = (gestureState: {dx: number; dy: number}) => {
const {containerSize, thumbSize, values} = this.state;
const {maximumValue, minimumValue, step, vertical} = this.props;
const {lowerLimit, upperLimit, maximumValue, minimumValue, step, vertical} = this.props;
const length = containerSize.width - thumbSize.width;
const thumbLeft = vertical
? this._previousLeft + gestureState.dy * -1
: this._previousLeft + gestureState.dx;
const nonRtlRatio = thumbLeft / length;
const ratio = I18nManager.isRTL ? 1 - nonRtlRatio : nonRtlRatio;
let minValue = minimumValue;
let maxValue = maximumValue;
let minValue = !!lowerLimit || lowerLimit === 0 ? lowerLimit : minimumValue;
let maxValue = !!upperLimit || upperLimit === 0 ? upperLimit : maximumValue;

const rawValues = this._getRawValues(values);

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export type SliderProps = {
*/
startFromZero?: boolean;
vertical?: boolean;
lowerLimit?: number;
upperLimit?: number;
};

export type SliderState = {
Expand Down
Loading