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

[Slider] Add ability to pass a ref prop for input element through SliderThumb #3033

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions packages/react/slider/src/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand Down Expand Up @@ -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<React.ElementRef<typeof Primitive.input>>(null);

return (
<form
onSubmit={(event) => {
Expand All @@ -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);
}}
>
Expand All @@ -294,6 +302,19 @@ export const WithinForm = () => {
<br />
<br />

<fieldset>
<legend>Input Ref value: {String(inputRef.current?.value)}</legend>
<Slider.Root name="input" defaultValue={data.single} className={rootClass()}>
<Slider.Track className={trackClass()}>
<Slider.Range className={rangeClass()} />
</Slider.Track>
<Slider.Thumb className={thumbClass()} inputRef={inputRef} />
</Slider.Root>
</fieldset>

<br />
<br />

<fieldset>
<legend>Multiple value: {String(data.multiple)}</legend>
<Slider.Root name="multiple" defaultValue={data.multiple} className={rootClass()}>
Expand Down
17 changes: 13 additions & 4 deletions packages/react/slider/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,13 @@ const SliderThumb = React.forwardRef<SliderThumbElement, SliderThumbProps>(
type SliderThumbImplElement = React.ElementRef<typeof Primitive.span>;
interface SliderThumbImplProps extends PrimitiveSpanProps {
index: number;
inputRef?: React.RefObject<BubbleInputElement> | undefined;
name?: string;
}

const SliderThumbImpl = React.forwardRef<SliderThumbImplElement, SliderThumbImplProps>(
(props: ScopedProps<SliderThumbImplProps>, 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<HTMLSpanElement | null>(null);
Expand Down Expand Up @@ -618,6 +619,7 @@ const SliderThumbImpl = React.forwardRef<SliderThumbImplElement, SliderThumbImpl
name ??
(context.name ? context.name + (context.values.length > 1 ? '[]' : '') : undefined)
}
inputRef={inputRef}
value={value}
/>
)}
Expand All @@ -630,10 +632,17 @@ SliderThumb.displayName = THUMB_NAME;

/* -----------------------------------------------------------------------------------------------*/

const BubbleInput = (props: React.ComponentPropsWithoutRef<'input'>) => {
const { value, ...inputProps } = props;
const ref = React.useRef<HTMLInputElement>(null);
type BubbleInputElement = React.ElementRef<typeof Primitive.input>

interface BubbleInputProps extends React.ComponentPropsWithoutRef<'input'> {
inputRef?: React.RefObject<BubbleInputElement> | undefined;
}

const BubbleInput = (props: BubbleInputProps) => {
const { value, inputRef, ...inputProps } = props;
const prevValue = usePrevious(value);
let ref = React.useRef<BubbleInputElement>(null);
if (inputRef) ref = inputRef;

// Bubble value change to parents (e.g form change event)
React.useEffect(() => {
Expand Down