Skip to content

Commit

Permalink
Fix the step resolution calculation for StepMarker and steps when def…
Browse files Browse the repository at this point in the history
…ault settings (#636)

* Apply the fix for calculating the step resolution

* Add example for StepMarker with default settings

* Update snapshots in tests

* Fix the formatting issues
  • Loading branch information
BartoszKlonowski authored Sep 24, 2024
1 parent f1c48ad commit f3fb557
Show file tree
Hide file tree
Showing 4 changed files with 32,042 additions and 7 deletions.
35 changes: 32 additions & 3 deletions example/src/Examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ const MyStepMarker: FC<MarkerProps> = ({stepMarked, currentValue}) => {
<View style={styles.separator} />
<View style={styles.label}>
{currentValue !== undefined ? (
<Text>{currentValue}</Text>
<Text>
{currentValue % 1 === 0 ? currentValue : currentValue.toFixed(2)}
</Text>
) : (
<Text>{'-'}</Text>
)}
Expand Down Expand Up @@ -317,6 +319,27 @@ const SliderExampleWithCustomMarker = (props: SliderProps) => {
);
};

const SliderExampleWithCustomMarkerWithDefaultProps = (props: SliderProps) => {
const [value, setValue] = useState(props.value ?? CONSTANTS.MIN_VALUE);

return (
<View>
<Text style={styles.text}>{value && +value.toFixed(3)}</Text>
<Slider
style={[styles.slider, props.style]}
thumbImage={require('./resources/empty.png')}
tapToSeek
{...props}
value={value}
onValueChange={setValue}
StepMarker={MyStepMarker}
minimumTrackTintColor={'#00629A'}
maximumTrackTintColor={'#979EA4'}
/>
</View>
);
};

export default SliderExample;

const styles = StyleSheet.create({
Expand All @@ -342,7 +365,7 @@ const styles = StyleSheet.create({
},
label: {
marginTop: 10,
width: 45,
width: 55,
paddingVertical: 5,
paddingHorizontal: 10,
backgroundColor: '#ffffff',
Expand All @@ -361,7 +384,7 @@ const styles = StyleSheet.create({
alignItems: 'center',
},
tinyLogo: {
marginVertical: 5,
marginVertical: 2,
aspectRatio: 1,
flex: 1,
height: '100%',
Expand Down Expand Up @@ -608,6 +631,12 @@ export const examples: Props[] = [
return <SliderExampleWithCustomMarker />;
},
},
{
title: 'Custom step marker but default step and min/max values',
render() {
return <SliderExampleWithCustomMarkerWithDefaultProps />;
},
},
{
title: 'Inverted slider direction',
render() {
Expand Down
10 changes: 7 additions & 3 deletions package/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,19 @@ const SliderComponent = (
);
const [width, setWidth] = useState(0);

const step = localProps.step
const stepResolution = localProps.step
? localProps.step
: constants.DEFAULT_STEP_RESOLUTION;

const defaultStep =
(localProps.maximumValue! - localProps.minimumValue!) / stepResolution;
const stepLength = localProps.step || defaultStep;

const options = Array.from(
{
length: (localProps.maximumValue! - localProps.minimumValue!) / step + 1,
length: (localProps.step ? defaultStep : stepResolution) + 1,
},
(_, index) => localProps.minimumValue! + index * step,
(_, index) => localProps.minimumValue! + index * stepLength,
);

const defaultStyle =
Expand Down
Loading

0 comments on commit f3fb557

Please sign in to comment.