MUI Components
NPM

MUISlider

MUISlider wraps the Material UI Slider for a single number or a range (number array), with label, error and helper-text handling.

Usage

import MUISlider, { MUISliderProps } from '@nish1896/mui-components/mui/slider';
const [volume, setVolume] = useState(30);

<MUISlider
  fieldName="volume"
  value={volume}
  onValueChange={({ newValue, activeThumb, event }) => setVolume(newValue)}
  min={0}
  max={100}
/>

value is a number for a single-thumb slider and a number[] for a range slider, and newValue has the same shape as value, so no cast is needed. undefined and null are treated as 0.

onValueChange receives { newValue, activeThumb, event }: the new value, the index of the thumb that is being moved, and the change event.

Range slider

Pass an array of numbers as value to get one thumb per number. The slider announces a range to screen readers as, for example, "20 to 60".

const [range, setRange] = useState<number[]>([20, 60]);

<MUISlider
  fieldName="priceRange"
  value={range}
  onValueChange={({ newValue }) => setRange(newValue)}
  valueLabelDisplay="auto"
/>

activeThumb tells you which thumb moved, which is useful for rules between the thumbs. This keeps them at least 10 apart, and disableSwap stops one thumb from passing the other.

const minDistance = 10;
const [range, setRange] = useState<number[]>([20, 60]);

<MUISlider
  fieldName="priceRange"
  value={range}
  onValueChange={({ newValue, activeThumb }) => {
    if (activeThumb === 0) {
      setRange([Math.min(newValue[0], range[1] - minDistance), range[1]]);
    } else {
      setRange([range[0], Math.max(newValue[1], range[0] + minDistance)]);
    }
  }}
  disableSwap
/>

Steps, marks and value labels

Most of the slider's appearance comes from the underlying Material UI props, which are passed through: min, max and step set the scale, marks adds labelled ticks, and valueLabelDisplay controls the bubble above the thumb: 'auto' on hover or drag, 'on' always, or 'off'.

<MUISlider
  fieldName="temperature"
  value={temperature}
  onValueChange={({ newValue }) => setTemperature(newValue)}
  min={0}
  max={40}
  step={10}
  marks={[
    { value: 0, label: '0°C' },
    { value: 10, label: '10°C' },
    { value: 20, label: '20°C' },
    { value: 30, label: '30°C' },
    { value: 40, label: '40°C' }
  ]}
  valueLabelDisplay="auto"
/>

Label placement

A slider has no inline label, so showLabelAboveFormField defaults to true and the label is shown above the track. Pass false to hide the visible label.

API

MUISliderProps also accepts most SliderProps. Pass a number array as value for a range slider.

Props marked with * are required.

NameTypeDescription
fieldName*string
Name/path of the field. Used to derive the id, the default label, and the name attribute. This prop is required for all components.
valuenumber | number[] | null
Current slider value. Pass a number array for range sliders. undefined/null are treated as 0.
onValueChange*({ newValue, activeThumb, event }) => void
Called on every slide with the next value, the active thumb index (for range sliders), and the original change event.
labelReactNode
Label displayed for the field. Defaults to a human-readable label derived from fieldName, e.g. firstName becomes "First Name".
showLabelAboveFormFieldboolean
Whether the field label renders above the component. Since it has no built-in inline label, the label renders above the component by default; pass false to hide the visible label (the accessible name is still applied). Takes precedence over allLabelsAboveFields from the ConfigProvider.
Default: true
formLabelPropsFormLabelProps
FormLabelProps forwarded to the internal FormLabel. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component.
hideLabelboolean
When true, hides the rendered field label while preserving accessible labeling where possible.
requiredboolean
Indicates that the field is mandatory by adding an asterisk symbol (*) to the form label and setting the relevant accessibility attributes.
errorMessagestring | string[]
Validation error for the field — pass a single message string, or a string[] when the field can fail multiple rules at once (every message is shown together). A non-empty string or array puts the field in an error state; undefined/''/[] clear it.
Normalize your form library's error shape to this at the call site (e.g. an RHF FieldError via its .message). Use renderError to customize how the message(s) are rendered.
renderError(errors: string[]) => ReactNode
Custom renderer for the resolved error message(s), called only when the field is in an error state. Always receives a string[] — use errors[0] for the common single-message case, or map over errors when a field fails several rules. By default a single message renders as text and multiple messages render on separate lines.
hideErrorMessageboolean
If true, hides the error message text while keeping the field in an error state.
helperTextReactNode
Content displayed in the FormHelperText component below the field when there is no visible validation error.
formHelperTextPropsFormHelperTextProps
FormHelperTextProps forwarded to the internal FormHelperText. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component.
customIds{ field, label, helperText, error }
Overrides the default field, label, helper text, and error IDs used for accessibility.

Playground

Driven by plain React state — a single-value slider, a range slider (number array), and SliderProps (min, max, step, marks, valueLabelDisplay).

Single value with step & value label

Drag to set the output volume; volume won't go below 10

Range slider (number array)

Custom marks with submit-time validation

Must be at least 20° to submit

Form State - Values & Errors

Reference

Source Code

View the full implementation of this component on GitHub.