From 3eb4b1233e1f5399d88d9b16316d4a241692a883 Mon Sep 17 00:00:00 2001 From: hancandice Date: Sun, 19 May 2024 18:52:37 +0200 Subject: [PATCH] Add thumbTrackingValue, allowing the tracking of thumb movement to update the displayed value dynamically. Additionally, thumbTrackingValueStyle is introduced to customize the appearance of this tracked value. These features enhance user interaction and visual customization options. --- README.md | 2 ++ index.tsx | 25 ++++++++++++++++++++++--- styles.ts | 3 +++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d99d85f..cea3a80 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ const handleValueChange = useCallback((low, high) => { | `onValueChanged` | Will be called when a value was changed.
If `disableRange` is set to true, the second argument should be ignored.
`fromUser` will be true if the value was changed by user's interaction. | `(low: number, high: number, fromUser: boolean) => void` | `undefined` | | `onSliderTouchStart` | Will be called when user starts interaction with slider.
If `disableRange` is set to true, the second argument should be ignored. | `(low: number, high: number) => void` | `undefined` | | `onSliderTouchEnd` | Will be called when user ends interaction with slider.
If `disableRange` is set to true, the second argument should be ignored. | `(low: number, high: number) => void` | `undefined` | +| `thumbTrackingValue` | If enabled, both the `label` and `notch` will be hidden. Instead, the low and high values will update and move along with the thumb as the user slides it.
You can also customize the appearance of the value by configuring `thumbTrackingValueStyle`. | boolean | `false` | +| `thumbTrackingValueStyle` | only functions when `thumbTrackingValue` is enabled, allowing you to customize its appearance. | TextStyle | Initially, it will be `undefined` if not provided. | All the other props (e.g. style) will be passed to root container component. diff --git a/index.tsx b/index.tsx index 37163ae..d739878 100644 --- a/index.tsx +++ b/index.tsx @@ -14,6 +14,8 @@ import { PanResponderGestureState, View, ViewProps, + Text, + TextStyle, } from 'react-native'; import styles from './styles'; @@ -41,6 +43,8 @@ export interface SliderProps extends ViewProps { disableRange?: boolean; disabled?: boolean; floatingLabel?: boolean; + thumbTrackingValue?: boolean; + thumbTrackingValueStyle?: TextStyle; renderLabel?: (value: number) => ReactNode; renderNotch?: (value: number) => ReactNode; renderRail: () => ReactNode; @@ -58,6 +62,8 @@ const Slider: React.FC = ({ low: lowProp, high: highProp, floatingLabel = false, + thumbTrackingValue = false, + thumbTrackingValueStyle, allowLabelOverflow = false, disableRange = false, disabled = false, @@ -297,10 +303,13 @@ const Slider: React.FC = ({ return ( - + {!thumbTrackingValue && + ( {labelView} {notchView} - + ) + } + {renderRail()} @@ -309,10 +318,20 @@ const Slider: React.FC = ({ + {thumbTrackingValue && ( + {inPropsRef.current.low} + )} {lowThumb} + {!disableRange && ( - {highThumb} + + {thumbTrackingValue && ( + {inPropsRef.current.high} + )} + + {highThumb} + )}