From 03b87020df3fc0d494c8d9adfddd22ec4440cc60 Mon Sep 17 00:00:00 2001 From: Matthew Robertson Date: Tue, 23 Jul 2024 19:28:40 +0100 Subject: [PATCH] Slider Support for an InputRef to give more control to consumer around the hidden input --- packages/react/slider/src/Slider.stories.tsx | 21 ++++++++++++++++++++ packages/react/slider/src/Slider.tsx | 17 ++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/react/slider/src/Slider.stories.tsx b/packages/react/slider/src/Slider.stories.tsx index 529bcc78f..c1f386377 100644 --- a/packages/react/slider/src/Slider.stories.tsx +++ b/packages/react/slider/src/Slider.stories.tsx @@ -3,6 +3,7 @@ import { DirectionProvider } from '@radix-ui/react-direction'; import { css } from '../../../../stitches.config'; import serialize from 'form-serialize'; import * as Slider from '@radix-ui/react-slider'; +import { Primitive } from '@radix-ui/react-primitive'; export default { title: 'Components/Slider' }; @@ -264,12 +265,16 @@ export const SmallSteps = () => { export const WithinForm = () => { const [data, setData] = React.useState({ single: [0], + input: [0], multiple: [10, 15, 20, 80], price: { min: 30, max: 70, }, }); + + const inputRef = React.useRef>(null); + return (
{ @@ -278,6 +283,9 @@ export const WithinForm = () => { }} onChange={(event) => { const formData = serialize(event.currentTarget, { hash: true }); + if (event.target !== inputRef.current) { + console.log("This is the event from our input reference"); + } setData(formData as any); }} > @@ -294,6 +302,19 @@ export const WithinForm = () => {

+
+ Input Ref value: {String(inputRef.current?.value)} + + + + + + +
+ +
+
+
Multiple value: {String(data.multiple)} diff --git a/packages/react/slider/src/Slider.tsx b/packages/react/slider/src/Slider.tsx index f758343d1..0af1d11ae 100644 --- a/packages/react/slider/src/Slider.tsx +++ b/packages/react/slider/src/Slider.tsx @@ -545,12 +545,13 @@ const SliderThumb = React.forwardRef( type SliderThumbImplElement = React.ElementRef; interface SliderThumbImplProps extends PrimitiveSpanProps { index: number; + inputRef?: React.RefObject | undefined; name?: string; } const SliderThumbImpl = React.forwardRef( (props: ScopedProps, forwardedRef) => { - const { __scopeSlider, index, name, ...thumbProps } = props; + const { __scopeSlider, index, inputRef, name, ...thumbProps } = props; const context = useSliderContext(THUMB_NAME, __scopeSlider); const orientation = useSliderOrientationContext(THUMB_NAME, __scopeSlider); const [thumb, setThumb] = React.useState(null); @@ -618,6 +619,7 @@ const SliderThumbImpl = React.forwardRef 1 ? '[]' : '') : undefined) } + inputRef={inputRef} value={value} /> )} @@ -630,10 +632,17 @@ SliderThumb.displayName = THUMB_NAME; /* -----------------------------------------------------------------------------------------------*/ -const BubbleInput = (props: React.ComponentPropsWithoutRef<'input'>) => { - const { value, ...inputProps } = props; - const ref = React.useRef(null); +type BubbleInputElement = React.ElementRef + +interface BubbleInputProps extends React.ComponentPropsWithoutRef<'input'> { + inputRef?: React.RefObject | undefined; +} + +const BubbleInput = (props: BubbleInputProps) => { + const { value, inputRef, ...inputProps } = props; const prevValue = usePrevious(value); + let ref = React.useRef(null); + if (inputRef) ref = inputRef; // Bubble value change to parents (e.g form change event) React.useEffect(() => {