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] Narrow onChange value type #44777

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

good-jinu
Copy link

@good-jinu good-jinu commented Dec 15, 2024

  • Change value and onChange value param type from number | number[] to generic Value

Closes #37854
Inspired by #38753


What's different from PR #38753

  • Removed component field from SliderType -> props
  • No work was done on the code snippets in the docs.
  • Solved every typescript error (not really sure but as far as I tested, it worked)

- Change value and onChange value param type from `number | number[]` to generic `Value`
@mui-bot
Copy link

mui-bot commented Dec 15, 2024

Netlify deploy preview

https://deploy-preview-44777--material-ui.netlify.app/

Bundle size report

Bundle size will be reported once CircleCI build #782215 finishes.

Generated by 🚫 dangerJS against d67fc23

@zannager zannager added the component: slider This is the name of the generic UI component, not the React module! label Dec 16, 2024
@zannager zannager requested a review from DiegoAndai December 16, 2024 14:30
@mj12albert mj12albert added typescript package: material-ui Specific to @mui/material labels Dec 17, 2024
@mj12albert mj12albert changed the title [mui-material][Slider] Narrow onChange value type [Slider] Narrow onChange value type Dec 17, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@good-jinu Looks good. Can you add a type test in Slider.spec.tsx file?

No work was done on the code snippets in the docs.

We need to update the type in demos. For example in ContinuousSlider demo:

--- a/docs/data/material/components/slider/ContinuousSlider.tsx
+++ b/docs/data/material/components/slider/ContinuousSlider.tsx
@@ -8,8 +8,8 @@ import VolumeUp from '@mui/icons-material/VolumeUp';
 export default function ContinuousSlider() {
   const [value, setValue] = React.useState<number>(30);

-  const handleChange = (event: Event, newValue: number | number[]) => {
-    setValue(newValue as number);
+  const handleChange = (event: Event, newValue: number) => {
+    setValue(newValue);
   };

   return (

Same in other demos.

@good-jinu
Copy link
Author

Thanks for your specific comment! @ZeeshanTamboli
I'll ensure that all the necessary changes are applied to the demos. I'll go ahead and add the type test in the Slider.spec.tsx as well.

- Change value and onChange value param type from `number | number[]` to generic `Value`
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@good-jinu I have pushed few commits to cleanup the code. But there's an issue. The component prop is no longer available on Slider after removing OverridableComponent type from the Slider type. It should be available. Additionally, TypeScript doesn't catch arbitrary props, allowing any prop without error detection.

PR CodeSandbox: https://codesandbox.io/p/sandbox/material-ui-cra-ts-forked-zm74j4
Master: https://codesandbox.io/p/sandbox/material-ui-cra-ts-forked-4ch62x

@DiegoAndai
Copy link
Member

Thanks for working on this @good-jinu. This is a difficult problem, we've tried to solve it a couple of times in the past, so expect more back and forth than usual PRs.

As additional context, Base UI has not implemented this: https://github.com/mui/base-ui/blob/master/packages/react/src/slider/root/useSliderRoot.ts. It makes me wonder if we should do it, given that we're going to use Base UI soon. @aarongarciah what do you think?

@good-jinu
Copy link
Author

Thank you @DiegoAndai for providing the context!

This issue turned out to be more complex and challenging than I initially thought. As you mentioned, if we resolve this problem using Base UI, I hope a better solution will emerge in the future instead of this PR.

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! package: material-ui Specific to @mui/material typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Slider] Allow generic value type to narrow onChange value type
6 participants