Skip to content

Commit b8fe337

Browse files
committed
[Slider] Add input class and apply useSlot
1 parent 27d69b8 commit b8fe337

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

packages/mui-joy/src/Slider/Slider.tsx

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
} from '@mui/utils';
88
import { OverridableComponent } from '@mui/types';
99
import { useSlider } from '@mui/base/SliderUnstyled';
10-
import { useSlotProps } from '@mui/base/utils';
1110
import { useThemeProps, styled, Theme } from '../styles';
11+
import useSlot from '../utils/useSlot';
1212
import sliderClasses, { getSliderUtilityClass } from './sliderClasses';
1313
import { SliderProps, SliderTypeMap, SliderOwnerState } from './SliderProps';
1414

@@ -34,13 +34,14 @@ const useUtilityClasses = (ownerState: SliderOwnerState) => {
3434
],
3535
rail: ['rail'],
3636
track: ['track'],
37+
thumb: ['thumb', disabled && 'disabled'],
38+
input: ['input'],
3739
mark: ['mark'],
3840
markActive: ['markActive'],
3941
markLabel: ['markLabel'],
4042
markLabelActive: ['markLabelActive'],
4143
valueLabel: ['valueLabel'],
4244
valueLabelOpen: ['valueLabelOpen'],
43-
thumb: ['thumb', disabled && 'disabled'],
4445
active: ['active'],
4546
focusVisible: ['focusVisible'],
4647
};
@@ -399,7 +400,8 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
399400
const {
400401
'aria-label': ariaLabel,
401402
'aria-valuetext': ariaValuetext,
402-
component,
403+
className,
404+
component = 'span',
403405
componentsProps = {},
404406
classes: classesProp,
405407
disableSwap = false,
@@ -475,76 +477,75 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
475477
};
476478

477479
const classes = useUtilityClasses(ownerState);
480+
const externalForwardedProps = { ...other, component, componentsProps };
478481

479-
const rootProps = useSlotProps({
482+
const [SlotRoot, rootProps] = useSlot('root', {
483+
ref,
484+
className: clsx(classes.root, className),
480485
elementType: SliderRoot,
486+
externalForwardedProps,
481487
getSlotProps: getRootProps,
482-
externalSlotProps: componentsProps.root,
483-
externalForwardedProps: other,
484-
additionalProps: {
485-
as: component,
486-
},
487488
ownerState,
488-
className: classes.root,
489489
});
490490

491-
const railProps = useSlotProps({
491+
const [SlotRail, railProps] = useSlot('rail', {
492+
className: classes.rail,
492493
elementType: SliderRail,
493-
externalSlotProps: componentsProps.rail,
494+
externalForwardedProps,
494495
ownerState,
495-
className: classes.rail,
496496
});
497497

498-
const trackProps = useSlotProps({
499-
elementType: SliderTrack,
500-
externalSlotProps: componentsProps.track,
498+
const [SlotTrack, trackProps] = useSlot('track', {
501499
additionalProps: {
502500
style: trackStyle,
503501
},
504-
ownerState,
505502
className: classes.track,
503+
elementType: SliderTrack,
504+
externalForwardedProps,
505+
ownerState,
506506
});
507507

508-
const markProps = useSlotProps({
508+
const [SlotMark, markProps] = useSlot('mark', {
509+
className: classes.mark,
509510
elementType: SliderMark,
510-
externalSlotProps: componentsProps.mark,
511+
externalForwardedProps,
511512
ownerState,
512-
className: classes.mark,
513513
});
514514

515-
const markLabelProps = useSlotProps({
515+
const [SlotMarkLabel, markLabelProps] = useSlot('markLabel', {
516+
className: classes.markLabel,
516517
elementType: SliderMarkLabel,
517-
externalSlotProps: componentsProps.markLabel,
518+
externalForwardedProps,
518519
ownerState,
519-
className: classes.markLabel,
520520
});
521521

522-
const thumbProps = useSlotProps({
522+
const [SlotThumb, thumbProps] = useSlot('thumb', {
523+
className: classes.thumb,
523524
elementType: SliderThumb,
525+
externalForwardedProps,
524526
getSlotProps: getThumbProps,
525-
externalSlotProps: componentsProps.thumb,
526527
ownerState,
527-
className: classes.thumb,
528528
});
529529

530-
const inputProps = useSlotProps({
530+
const [SlotInput, inputProps] = useSlot('input', {
531+
className: classes.input,
531532
elementType: SliderInput,
533+
externalForwardedProps,
532534
getSlotProps: getHiddenInputProps,
533-
externalSlotProps: componentsProps.input,
534535
ownerState,
535536
});
536537

537-
const valueLabelProps = useSlotProps({
538+
const [SlotValueLabel, valueLabelProps] = useSlot('valueLabel', {
539+
className: classes.valueLabel,
538540
elementType: SliderValueLabel,
539-
externalSlotProps: componentsProps.valueLabel,
541+
externalForwardedProps,
540542
ownerState,
541-
className: classes.valueLabel,
542543
});
543544

544545
return (
545-
<SliderRoot {...rootProps}>
546-
<SliderRail {...railProps} />
547-
<SliderTrack {...trackProps} />
546+
<SlotRoot {...rootProps}>
547+
<SlotRail {...railProps} />
548+
<SlotTrack {...trackProps} />
548549
{marks
549550
.filter((mark) => mark.value >= min && mark.value <= max)
550551
.map((mark, index) => {
@@ -568,7 +569,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
568569

569570
return (
570571
<React.Fragment key={mark.value}>
571-
<SliderMark
572+
<SlotMark
572573
data-index={index}
573574
{...markProps}
574575
style={{ ...style, ...markProps.style }}
@@ -578,7 +579,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
578579
})}
579580
/>
580581
{mark.label != null ? (
581-
<SliderMarkLabel
582+
<SlotMarkLabel
582583
aria-hidden
583584
data-index={index}
584585
{...markLabelProps}
@@ -589,7 +590,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
589590
})}
590591
>
591592
{mark.label}
592-
</SliderMarkLabel>
593+
</SlotMarkLabel>
593594
) : null}
594595
</React.Fragment>
595596
);
@@ -598,7 +599,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
598599
const percent = valueToPercent(value, min, max);
599600
const style = axisProps[axis].offset(percent);
600601
return (
601-
<SliderThumb
602+
<SlotThumb
602603
key={index}
603604
data-index={index}
604605
{...thumbProps}
@@ -612,7 +613,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
612613
...thumbProps.style,
613614
}}
614615
>
615-
<SliderInput
616+
<SlotInput
616617
data-index={index}
617618
aria-label={getAriaLabel ? getAriaLabel(index) : ariaLabel}
618619
aria-valuenow={scale(value)}
@@ -623,7 +624,7 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
623624
{...inputProps}
624625
/>
625626
{valueLabelDisplay !== 'off' ? (
626-
<SliderValueLabel
627+
<SlotValueLabel
627628
{...valueLabelProps}
628629
className={clsx(valueLabelProps.className, {
629630
[classes.valueLabelOpen]:
@@ -633,12 +634,12 @@ const Slider = React.forwardRef(function Slider(inProps, ref) {
633634
{typeof valueLabelFormat === 'function'
634635
? valueLabelFormat(scale(value), index)
635636
: valueLabelFormat}
636-
</SliderValueLabel>
637+
</SlotValueLabel>
637638
) : null}
638-
</SliderThumb>
639+
</SlotThumb>
639640
);
640641
})}
641-
</SliderRoot>
642+
</SlotRoot>
642643
);
643644
}) as OverridableComponent<SliderTypeMap>;
644645

@@ -663,6 +664,10 @@ Slider.propTypes /* remove-proptypes */ = {
663664
* Override or extend the styles applied to the component.
664665
*/
665666
classes: PropTypes.object,
667+
/**
668+
* @ignore
669+
*/
670+
className: PropTypes.string,
666671
/**
667672
* The color of the component. It supports those theme colors that make sense for this component.
668673
* @default 'primary'
@@ -677,7 +682,20 @@ Slider.propTypes /* remove-proptypes */ = {
677682
*/
678683
component: PropTypes.elementType,
679684
/**
680-
* The props used for each slot inside the Slider.
685+
* Replace the default slots.
686+
*/
687+
components: PropTypes.shape({
688+
input: PropTypes.elementType,
689+
mark: PropTypes.elementType,
690+
markLabel: PropTypes.elementType,
691+
rail: PropTypes.elementType,
692+
root: PropTypes.elementType,
693+
thumb: PropTypes.elementType,
694+
track: PropTypes.elementType,
695+
valueLabel: PropTypes.elementType,
696+
}),
697+
/**
698+
* The props used for each slot inside the component.
681699
* @default {}
682700
*/
683701
componentsProps: PropTypes.shape({

0 commit comments

Comments
 (0)