diff --git a/example/src/Examples.tsx b/example/src/Examples.tsx index dcead190..969407e0 100644 --- a/example/src/Examples.tsx +++ b/example/src/Examples.tsx @@ -1,6 +1,6 @@ -import React, {useState} from 'react'; -import {Text, View, StyleSheet} from 'react-native'; -import Slider, {SliderProps} from '@react-native-community/slider'; +import React, {FC, useState} from 'react'; +import {Text, View, StyleSheet, Image} from 'react-native'; +import Slider, {MarkerProps, SliderProps} from '@react-native-community/slider'; export interface Props { title: string; @@ -8,6 +8,13 @@ export interface Props { platform?: string; } +const CONSTANTS = { + MAX_VALUE: 100, + MIN_VALUE: 10, + STEP: 10, + DEFAULT_STEP_RESOLUTION: 100, +} as const; + const SliderExample = (props: SliderProps) => { const [value, setValue] = useState(props.value ?? 0); return ( @@ -262,20 +269,113 @@ const SlidingCustomStepsThumbImageWithNumbersAndDifferentWidth = ( ); }; +const MyStepMarker: FC = ({stepMarked, currentValue}) => { + return stepMarked ? ( + + + + {currentValue !== undefined ? ( + {currentValue} + ) : ( + {'-'} + )} + + + + ) : ( + + ); +}; + +const SliderExampleWithCustomMarker = (props: SliderProps) => { + const [value, setValue] = useState(props.value ?? CONSTANTS.MIN_VALUE); + + return ( + + {value && +value.toFixed(3)} + + + ); +}; + export default SliderExample; const styles = StyleSheet.create({ - slider: { - width: 300, - opacity: 1, - marginTop: 10, - }, text: { fontSize: 14, textAlign: 'center', fontWeight: '500', margin: 0, }, + divider: { + width: 2, + height: 20, + backgroundColor: '#ffffff', + justifyContent: 'center', + alignItems: 'center', + }, + separator: { + width: 2, + height: 20, + backgroundColor: '#00629A', + justifyContent: 'center', + alignItems: 'center', + }, + label: { + marginTop: 10, + width: 45, + paddingVertical: 5, + paddingHorizontal: 10, + backgroundColor: '#ffffff', + shadowColor: '#000000', + shadowOffset: { + width: 0, + height: 1, + }, + shadowOpacity: 0.4, + shadowRadius: 4, + justifyContent: 'center', + alignItems: 'center', + }, + background: { + justifyContent: 'center', + alignItems: 'center', + }, + tinyLogo: { + marginVertical: 5, + aspectRatio: 1, + flex: 1, + height: '100%', + width: '100%', + }, + minMaxLabel: { + flexDirection: 'row', + zIndex: -1, + }, + slider: { + width: 300, + opacity: 1, + marginTop: 10, + }, outer: { width: 20, height: 20, @@ -502,6 +602,12 @@ export const examples: Props[] = [ return ; }, }, + { + title: 'Custom step marker settings', + render() { + return ; + }, + }, { title: 'Inverted slider direction', render() { diff --git a/example/src/resources/empty.png b/example/src/resources/empty.png new file mode 100644 index 00000000..503d58bb Binary files /dev/null and b/example/src/resources/empty.png differ diff --git a/package/src/Slider.tsx b/package/src/Slider.tsx index 7b4d0502..d77f28b2 100644 --- a/package/src/Slider.tsx +++ b/package/src/Slider.tsx @@ -216,16 +216,15 @@ const SliderComponent = ( ); const [width, setWidth] = useState(0); + const step = localProps.step + ? localProps.step + : constants.DEFAULT_STEP_RESOLUTION; + const options = Array.from( { - length: - (localProps.maximumValue! - localProps.minimumValue!) / - (localProps.step - ? localProps.step - : constants.DEFAULT_STEP_RESOLUTION) + - 1, + length: (localProps.maximumValue! - localProps.minimumValue!) / step + 1, }, - (_, index) => index, + (_, index) => localProps.minimumValue! + index * step, ); const defaultStyle = diff --git a/package/src/__tests__/Slider.test.tsx b/package/src/__tests__/Slider.test.tsx index e4e12298..1e5efab3 100644 --- a/package/src/__tests__/Slider.test.tsx +++ b/package/src/__tests__/Slider.test.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import renderer from 'react-test-renderer'; import Slider from '../Slider'; +import {View} from 'react-native'; describe('', () => { it('renders enabled slider', () => { @@ -60,4 +61,29 @@ describe('', () => { expect(tree).toMatchSnapshot(); }); + + it('renders a slider with custom stepMaker', () => { + const tree = renderer + .create( + { + return stepMarked ? ( + + + + ) : ( + + + + ); + }} + />, + ) + .toJSON(); + + expect(tree).toMatchSnapshot(); + }); }); diff --git a/package/src/__tests__/__snapshots__/Slider.test.tsx.snap b/package/src/__tests__/__snapshots__/Slider.test.tsx.snap index 2cdd29e5..622a70b1 100644 --- a/package/src/__tests__/__snapshots__/Slider.test.tsx.snap +++ b/package/src/__tests__/__snapshots__/Slider.test.tsx.snap @@ -443,6 +443,163 @@ exports[` renders a slider with custom props 1`] = ` `; +exports[` renders a slider with custom stepMaker 1`] = ` + + + + + + + + + + + + +`; + exports[` renders disabled slider 1`] = ` {renderStepNumber ? ( ; + currentValue?: number; }; export const SliderTrackMark = ({ isTrue, thumbImage, StepMarker, + currentValue, }: TrackMarksProps) => { return ( - {StepMarker ? : null} + {StepMarker ? ( + + ) : null} {thumbImage && isTrue ? ( diff --git a/package/typings/index.d.ts b/package/typings/index.d.ts index 56db19a6..abae2dcb 100644 --- a/package/typings/index.d.ts +++ b/package/typings/index.d.ts @@ -24,11 +24,13 @@ export type TrackMarksProps = { isTrue: boolean; thumbImage?: ImageURISource; StepMarker?: FC | boolean; -} + currentValue?: number; +}; export type MarkerProps = { stepMarked: boolean; -} + currentValue?: number; +}; export interface SliderPropsIOS extends ReactNative.ViewProps { /**