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

Fix the step resolution calculation for StepMarker and steps when default settings #636

Merged
merged 4 commits into from
Sep 24, 2024
Merged
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
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
Loading