-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Open
seloner
wants to merge
3
commits into
mui:master
Choose a base branch
from
seloner:fix/slider-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+34
−12
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,8 @@ import { useFieldRootContext } from '../../field/root/FieldRootContext'; | |
* | ||
* Documentation: [Base UI Slider](https://base-ui.com/react/components/slider) | ||
*/ | ||
const SliderRoot = React.forwardRef(function SliderRoot( | ||
props: SliderRoot.Props, | ||
const SliderRoot = React.forwardRef(function SliderRoot<Value extends number | readonly number[]>( | ||
props: SliderRoot.Props<Value>, | ||
forwardedRef: React.ForwardedRef<HTMLDivElement>, | ||
) { | ||
const { | ||
|
@@ -59,8 +59,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, | ||
orientation, | ||
rootRef: forwardedRef, | ||
step, | ||
|
@@ -118,7 +119,14 @@ const SliderRoot = React.forwardRef(function SliderRoot( | |
<CompositeList elementsRef={slider.thumbRefs}>{renderElement()}</CompositeList> | ||
</SliderRootContext.Provider> | ||
); | ||
}); | ||
}) as { | ||
<Value extends number | readonly number[]>( | ||
props: SliderRoot.Props<Value> & { | ||
ref?: React.RefObject<HTMLDivElement>; | ||
}, | ||
): React.JSX.Element; | ||
propTypes?: any; | ||
}; | ||
|
||
export namespace SliderRoot { | ||
export interface State extends FieldRoot.State { | ||
|
@@ -157,7 +165,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 +174,6 @@ export namespace SliderRoot { | |
| 'min' | ||
| 'minStepsBetweenValues' | ||
| 'name' | ||
| 'onValueChange' | ||
| 'onValueCommitted' | ||
| 'orientation' | ||
| 'largeStep' | ||
| 'step' | ||
|
@@ -179,7 +185,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,10 +203,26 @@ 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 }; | ||
|
||
SliderRoot.propTypes /* remove-proptypes */ = { | ||
|
@@ -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. | ||
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. This was auto generated. |
||
* @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. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@mj12albert
This was needed because ts error
Parameter props of call signature from exported interface has or is using private name
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.
I managed to bypass this with
1b5a14d