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][material][base] Allow generic value type to narrow onChange value type #38127

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function SeparatorButtonGroup() {
max={360}
valueLabelDisplay="on"
valueLabelFormat={(value) => `hsl(${value} 100% 50%)`}
onChange={(event, newValue) => setHue(newValue as number)}
onChange={(event: Event, newValue: number) => setHue(newValue)}
/>
<ButtonGroup
variant="plain"
Expand Down
4 changes: 2 additions & 2 deletions docs/data/joy/components/divider/DividerChildPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Slider from '@mui/joy/Slider';
import Sheet from '@mui/joy/Sheet';

export default function DividerChildPosition() {
const [position, setPosition] = React.useState<number | Array<number>>(50);
const [position, setPosition] = React.useState<number>(50);
return (
<Box sx={{ width: '100%' }}>
<Stack spacing={1} sx={{ fontSize: 'sm' }}>
Expand All @@ -23,7 +23,7 @@ export default function DividerChildPosition() {
step={1}
valueLabelDisplay="on"
valueLabelFormat={(value) => `${value}%`}
onChange={(event, value) => setPosition(value)}
onChange={(event: Event, value: number) => setPosition(value)}
/>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
step={1}
valueLabelDisplay="on"
valueLabelFormat={(value) => `${value}%`}
onChange={(event, value) => setPosition(value)}
onChange={(event: Event, value: number) => setPosition(value)}
/>
4 changes: 2 additions & 2 deletions docs/data/joy/components/slider/RangeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function valueText(value: number) {
export default function RangeSlider() {
const [value, setValue] = React.useState<number[]>([20, 37]);

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

return (
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/slider/MusicPlayerSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default function MusicPlayerSlider() {
min={0}
step={1}
max={duration}
onChange={(_, value) => setPosition(value as number)}
onChange={(_: Event, value: number) => setPosition(value)}
sx={{
color: theme.palette.mode === 'dark' ? '#fff' : 'rgba(0,0,0,0.87)',
height: 4,
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/base-ui/api/use-slider.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"name": { "type": { "name": "string", "description": "string" } },
"onChange": {
"type": {
"name": "(event: Event, value: number | number[], activeThumb: number) =&gt; void",
"description": "(event: Event, value: number | number[], activeThumb: number) =&gt; void"
"name": "SingleValueChange | MultipleValuesChange | DefaultOnChange",
"description": "SingleValueChange | MultipleValuesChange | DefaultOnChange"
}
},
"onChangeCommitted": {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/productMaterial/MaterialHero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function SlideDemo() {
<Slider
aria-labelledby="temperature-slider"
value={value}
onChange={(_, newValue) => setValue(newValue as number[])}
onChange={(_: Event, newValue: number[]) => setValue(newValue)}
/>
<LocalFireDepartment
fontSize="small"
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/useSlider/useSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default function useSlider(parameters: UseSliderParameters): UseSliderRet
value: { value, name },
});

onChange(clonedEvent, value, thumbIndex);
onChange(clonedEvent, value as number & number[], thumbIndex);
});

const range = Array.isArray(valueDerived);
Expand Down
7 changes: 6 additions & 1 deletion packages/mui-base/src/useSlider/useSlider.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';
import { EventHandlers } from '../utils';

export type SingleValueChange = (event: Event, value: number, activeThumb: number) => void;
export type MultipleValuesChange = (event: Event, value: number[], activeThumb: number) => void;
export type DefaultOnChange = (event: Event, value: number | number[], activeThumb: number) => void;

export interface UseSliderParameters {
/**
* The id of the element containing a label for the slider.
Expand Down Expand Up @@ -56,8 +60,9 @@ export interface UseSliderParameters {
* **Warning**: This is a generic event not a change event.
* @param {number | number[]} value The new value.
* @param {number} activeThumb Index of the currently moved thumb.
*
*/
onChange?: (event: Event, value: number | number[], activeThumb: number) => void;
onChange?: SingleValueChange | MultipleValuesChange | DefaultOnChange;
/**
* Callback function that is fired when the `mouseup` is triggered.
*
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-material-next/src/Slider/Slider.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as React from 'react';
import { SlotComponentProps } from '@mui/base';
import { Mark } from '@mui/base/useSlider';
import {
DefaultOnChange,
Mark,
MultipleValuesChange,
SingleValueChange,
} from '@mui/base/useSlider';
import { SxProps } from '@mui/system';
import { OverridableStringUnion, OverrideProps, OverridableComponent } from '@mui/types';
import { Theme } from '../styles';
Expand Down Expand Up @@ -108,7 +113,7 @@ export interface SliderTypeMap<D extends React.ElementType = 'span', P = {}> {
* @param {number | number[]} value The new value.
* @param {number} activeThumb Index of the currently moved thumb.
*/
onChange?: (event: Event, value: number | number[], activeThumb: number) => void;
onChange?: SingleValueChange | MultipleValuesChange | DefaultOnChange;
/**
* Callback function that is fired when the `mouseup` is triggered.
*
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-material/src/Slider/Slider.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as React from 'react';
import { SlotComponentProps } from '@mui/base';
import { Mark } from '@mui/base/useSlider';
import {
DefaultOnChange,
Mark,
MultipleValuesChange,
SingleValueChange,
} from '@mui/base/useSlider';
import { SxProps } from '@mui/system';
import { OverridableStringUnion } from '@mui/types';
import { Theme } from '../styles';
Expand Down Expand Up @@ -151,7 +156,7 @@ export interface SliderTypeMap<D extends React.ElementType = 'span', P = {}> {
* @param {number | number[]} value The new value.
* @param {number} activeThumb Index of the currently moved thumb.
*/
onChange?: (event: Event, value: number | number[], activeThumb: number) => void;
onChange?: SingleValueChange | MultipleValuesChange | DefaultOnChange;
/**
* Callback function that is fired when the `mouseup` is triggered.
*
Expand Down