-
-
Notifications
You must be signed in to change notification settings - Fork 86
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] Narrow the type of value
in callbacks
#1241
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,24 @@ import { useSliderRoot } from './useSliderRoot'; | |
import { SliderRootContext } from './SliderRootContext'; | ||
import { useFieldRootContext } from '../../field/root/FieldRootContext'; | ||
|
||
type SliderRootType = { | ||
<Value extends number | readonly number[]>( | ||
props: SliderRoot.Props<Value> & { | ||
ref?: React.RefObject<HTMLDivElement>; | ||
}, | ||
): React.JSX.Element; | ||
propTypes?: any; | ||
}; | ||
|
||
/** | ||
* 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, | ||
forwardedRef: React.ForwardedRef<HTMLDivElement>, | ||
) { | ||
export const SliderRoot = React.forwardRef(function SliderRoot< | ||
Value extends number | readonly number[], | ||
>(props: SliderRoot.Props<Value>, forwardedRef: React.ForwardedRef<HTMLDivElement>) { | ||
const { | ||
'aria-labelledby': ariaLabelledby, | ||
className, | ||
|
@@ -59,8 +67,9 @@ const SliderRoot = React.forwardRef(function SliderRoot( | |
min, | ||
minStepsBetweenValues, | ||
name: nameProp ?? '', | ||
onValueChange: onValueChangeProp ?? NOOP, | ||
onValueCommitted: onValueCommittedProp ?? NOOP, | ||
onValueChange: (onValueChangeProp as useSliderRoot.Parameters['onValueChange']) ?? NOOP, | ||
onValueCommitted: | ||
(onValueCommittedProp as useSliderRoot.Parameters['onValueCommitted']) ?? NOOP, | ||
Comment on lines
+62
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seloner It should be possible without casting these two props (the legacy
Let me know if you need any help ~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seloner See if this helps: https://github.com/mj12albert/base-ui/commits/slider-value-type/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried yesterday and I was not able to make it work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mj12albert |
||
orientation, | ||
rootRef: forwardedRef, | ||
step, | ||
|
@@ -118,7 +127,7 @@ const SliderRoot = React.forwardRef(function SliderRoot( | |
<CompositeList elementsRef={slider.thumbRefs}>{renderElement()}</CompositeList> | ||
</SliderRootContext.Provider> | ||
); | ||
}); | ||
}) as SliderRootType; | ||
|
||
export namespace SliderRoot { | ||
export interface State extends FieldRoot.State { | ||
|
@@ -157,7 +166,7 @@ export namespace SliderRoot { | |
values: readonly number[]; | ||
} | ||
|
||
export interface Props | ||
export interface Props<Value extends number | readonly number[] = number | readonly number[]> | ||
extends Partial< | ||
Pick< | ||
useSliderRoot.Parameters, | ||
|
@@ -166,8 +175,6 @@ export namespace SliderRoot { | |
| 'min' | ||
| 'minStepsBetweenValues' | ||
| 'name' | ||
| 'onValueChange' | ||
| 'onValueCommitted' | ||
| 'orientation' | ||
| 'largeStep' | ||
| 'step' | ||
|
@@ -179,7 +186,7 @@ export namespace SliderRoot { | |
* | ||
* To render a controlled slider, use the `value` prop instead. | ||
*/ | ||
defaultValue?: number | readonly number[]; | ||
defaultValue?: Value; | ||
/** | ||
* Whether the component should ignore user interaction. | ||
* @default false | ||
|
@@ -197,12 +204,27 @@ export namespace SliderRoot { | |
* The value of the slider. | ||
* For ranged sliders, provide an array with two values. | ||
*/ | ||
value?: number | readonly number[]; | ||
value?: Value; | ||
/** | ||
* 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: Value, 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: Value, event: Event) => void; | ||
} | ||
} | ||
|
||
export { SliderRoot }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mj12albert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I managed to bypass this with |
||
|
||
SliderRoot.propTypes /* remove-proptypes */ = { | ||
// ┌────────────────────────────── Warning ──────────────────────────────┐ | ||
// │ These PropTypes are generated from the TypeScript type definitions. │ | ||
|
@@ -288,7 +310,7 @@ SliderRoot.propTypes /* remove-proptypes */ = { | |
/** | ||
* Callback function that is fired when the slider's value changed. | ||
* | ||
* @param {number | readonly number[]} value The new value. | ||
* @param {number | number[]} value The new value. | ||
mj12albert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was needed to include the ref into types @mj12albert