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] value type should narrow onValueChange value type #1230

Closed
aarongarciah opened this issue Dec 25, 2024 · 0 comments · Fixed by #1241
Closed

[Slider] value type should narrow onValueChange value type #1230

aarongarciah opened this issue Dec 25, 2024 · 0 comments · Fixed by #1241
Assignees
Labels
component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature typescript

Comments

@aarongarciah
Copy link
Member

aarongarciah commented Dec 25, 2024

Feature request

Summary

The Slider accepts values of type Array<number> | number but the onValueChange and onValueCommitted callbacks don't narrow the type of the value parameter to number or Array<number> and users need to type cast it manually.

Example of code throwing a TS error:

//  ❌ Argument of type 'number | number[]' is not assignable to parameter of type 'SetStateAction<number>'.
//    Type 'number[]' is not assignable to type 'SetStateAction<number>'.

const [value, setValue] = React.useState(25);
<Slider.Root value={value} onValueChange={(value) => setValue(value}>

Example of type casting needed to overcome the TS error:

const [value, setValue] = React.useState(25);
<Slider.Root value={value} onValueChange={(value) => setValue(value as number}>

Motivation

Avoid users needing to do manual type casting.

@aarongarciah aarongarciah added component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature labels Dec 25, 2024
@github-project-automation github-project-automation bot moved this to Backlog in Base UI Jan 13, 2025
@colmtuite colmtuite moved this from Backlog to In progress in Base UI Jan 13, 2025
@mj12albert mj12albert assigned mj12albert and unassigned mj12albert Jan 14, 2025
@github-project-automation github-project-automation bot moved this from In progress to Done in Base UI Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature typescript
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants