Skip to content

Commit f3fb557

Browse files
Fix the step resolution calculation for StepMarker and steps when default 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
1 parent f1c48ad commit f3fb557

File tree

4 files changed

+32042
-7
lines changed

4 files changed

+32042
-7
lines changed

example/src/Examples.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ const MyStepMarker: FC<MarkerProps> = ({stepMarked, currentValue}) => {
275275
<View style={styles.separator} />
276276
<View style={styles.label}>
277277
{currentValue !== undefined ? (
278-
<Text>{currentValue}</Text>
278+
<Text>
279+
{currentValue % 1 === 0 ? currentValue : currentValue.toFixed(2)}
280+
</Text>
279281
) : (
280282
<Text>{'-'}</Text>
281283
)}
@@ -317,6 +319,27 @@ const SliderExampleWithCustomMarker = (props: SliderProps) => {
317319
);
318320
};
319321

322+
const SliderExampleWithCustomMarkerWithDefaultProps = (props: SliderProps) => {
323+
const [value, setValue] = useState(props.value ?? CONSTANTS.MIN_VALUE);
324+
325+
return (
326+
<View>
327+
<Text style={styles.text}>{value && +value.toFixed(3)}</Text>
328+
<Slider
329+
style={[styles.slider, props.style]}
330+
thumbImage={require('./resources/empty.png')}
331+
tapToSeek
332+
{...props}
333+
value={value}
334+
onValueChange={setValue}
335+
StepMarker={MyStepMarker}
336+
minimumTrackTintColor={'#00629A'}
337+
maximumTrackTintColor={'#979EA4'}
338+
/>
339+
</View>
340+
);
341+
};
342+
320343
export default SliderExample;
321344

322345
const styles = StyleSheet.create({
@@ -342,7 +365,7 @@ const styles = StyleSheet.create({
342365
},
343366
label: {
344367
marginTop: 10,
345-
width: 45,
368+
width: 55,
346369
paddingVertical: 5,
347370
paddingHorizontal: 10,
348371
backgroundColor: '#ffffff',
@@ -361,7 +384,7 @@ const styles = StyleSheet.create({
361384
alignItems: 'center',
362385
},
363386
tinyLogo: {
364-
marginVertical: 5,
387+
marginVertical: 2,
365388
aspectRatio: 1,
366389
flex: 1,
367390
height: '100%',
@@ -608,6 +631,12 @@ export const examples: Props[] = [
608631
return <SliderExampleWithCustomMarker />;
609632
},
610633
},
634+
{
635+
title: 'Custom step marker but default step and min/max values',
636+
render() {
637+
return <SliderExampleWithCustomMarkerWithDefaultProps />;
638+
},
639+
},
611640
{
612641
title: 'Inverted slider direction',
613642
render() {

package/src/Slider.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,19 @@ const SliderComponent = (
215215
);
216216
const [width, setWidth] = useState(0);
217217

218-
const step = localProps.step
218+
const stepResolution = localProps.step
219219
? localProps.step
220220
: constants.DEFAULT_STEP_RESOLUTION;
221221

222+
const defaultStep =
223+
(localProps.maximumValue! - localProps.minimumValue!) / stepResolution;
224+
const stepLength = localProps.step || defaultStep;
225+
222226
const options = Array.from(
223227
{
224-
length: (localProps.maximumValue! - localProps.minimumValue!) / step + 1,
228+
length: (localProps.step ? defaultStep : stepResolution) + 1,
225229
},
226-
(_, index) => localProps.minimumValue! + index * step,
230+
(_, index) => localProps.minimumValue! + index * stepLength,
227231
);
228232

229233
const defaultStyle =

0 commit comments

Comments
 (0)