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] restrict slider type #1241

Open
wants to merge 3 commits into
base: master
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
35 changes: 26 additions & 9 deletions packages/react/src/slider/root/SliderRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import { sliderStyleHookMapping } from './styleHooks';
import { useSliderRoot } from './useSliderRoot';
import { SliderRootContext } from './SliderRootContext';
import { useFieldRootContext } from '../../field/root/FieldRootContext';
import { fixedForwardRef } from '../utils/fixedForwardRef';

/**
* Groups all parts of the slider.
* Renders a `<div>` element.
*
* Documentation: [Base UI Slider](https://base-ui.com/react/components/slider)
*/
const SliderRoot = React.forwardRef(function SliderRoot(
props: SliderRoot.Props,
const SliderRoot = fixedForwardRef(function SliderRoot<TValue extends number | number[]>(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactForwardRef does not work well with generics.
So we need to create a wrapper function to apply the generic.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seloner I think we can remove generic SliderRoot component by removing generic in Props interfacce.

Check https://github.com/mui/base-ui/pull/1241/files#r1899037802, please

props: SliderRoot.Props<TValue>,
forwardedRef: React.ForwardedRef<HTMLDivElement>,
) {
const {
Expand Down Expand Up @@ -59,8 +60,8 @@ const SliderRoot = React.forwardRef(function SliderRoot(
min,
minStepsBetweenValues,
name,
onValueChange,
onValueCommitted,
onValueChange: onValueChange as useSliderRoot.Parameters['onValueChange'],
onValueCommitted: onValueCommitted as useSliderRoot.Parameters['onValueCommitted'],
orientation,
rootRef: forwardedRef,
step,
Expand Down Expand Up @@ -157,16 +158,14 @@ export namespace SliderRoot {
values: ReadonlyArray<number>;
}

export interface Props
export interface Props<TValue extends number[] | number = number | number[]>
extends Pick<
useSliderRoot.Parameters,
| 'disabled'
| 'max'
| 'min'
| 'minStepsBetweenValues'
| 'name'
| 'onValueChange'
| 'onValueCommitted'
| 'orientation'
| 'largeStep'
| 'step'
Expand All @@ -178,7 +177,7 @@ export namespace SliderRoot {
*
* To render a controlled slider, use the `value` prop instead.
*/
defaultValue?: number | ReadonlyArray<number>;
defaultValue?: TValue;
/**
* Whether the component should ignore user interaction.
* @default false
Expand All @@ -192,12 +191,30 @@ export namespace SliderRoot {
* The value of the slider.
* For ranged sliders, provide an array with two values.
*/
value?: number | ReadonlyArray<number>;
value?: TValue;
/**
* Callback function that is fired when the slider's value changed.
*
* @param {number | number[]} value The new value.
* @param {Event} event The corresponding event that initiated the change.
* You can pull out the new value by accessing `event.target.value` (any).
* @param {number} activeThumbIndex Index of the currently moved thumb.
*/
onValueChange?: (value: TValue, event: Event, activeThumbIndex: number) => void;
/**
* Callback function that is fired when the `pointerup` is triggered.
*
* @param {number | number[]} value The new value.
* @param {Event} event The corresponding event that initiated the change.
* **Warning**: This is a generic event not a change event.
*/
onValueCommitted?: (value: TValue, event: Event) => void;
}
}

export { SliderRoot };

// @ts-expect-error
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to handle this I am getting ts error for proptypes.
Tried the proptypes script but no luck.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you remove fixedForwardRef and replace it with React.forwardRef, then // @ts-expect-error can be removed.

@@ -18,8 +18,8 @@ import { fixedForwardRef } from '../utils';
  *
  * Documentation: [Base UI Slider](https://base-ui.com/react/components/slider)
  */
-const SliderRoot = fixedForwardRef(function SliderRoot<TValue extends number | number[]>(
-  props: SliderRoot.Props<TValue>,
+const SliderRoot = React.forwardRef(function SliderRoot(
+  props: SliderRoot.Props,
   forwardedRef: React.ForwardedRef<HTMLDivElement>,
 ) {
   const {

SliderRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
Expand Down
7 changes: 7 additions & 0 deletions packages/react/src/slider/utils/fixedForwardRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

export function fixedForwardRef<T, P extends React.PropsWithoutRef<any> = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode,
): (props: P & React.RefAttributes<T>) => React.ReactNode {
return React.forwardRef(render) as any;
}
Loading