Slider

Slider control allows users to select a specific value or a range of values from a set. Usually, slider presents a relatively large dataset and the way that the user interacts with it is helpful to explore the multiple options swiftly.

Props

NameTypeDescriptionDefault
defaultValuenumberInitial value of the slider, only when it is uncontrolled.-
valuenumberThe selected value. If undefined, the component will be uncontrolled and the value will be managed internally by the component.-
labelstringText to be placed above the slider.-
namestringName attribute of the input element.-
helperTextstringHelper text to be placed above the slider.-
minValuenumberThe minimum value available for selection.0
maxValuenumberThe maximum value available for selection.100
stepnumberThe step interval between values available for selection.1
showLimitsValuesbooleanWhether the min/max value labels should be displayed next to the slider.false
showInputbooleanWhether the input element for displaying/controlling the slider value should be displayed next to the slider.false
disabledbooleanIf true, the component will be disabled.false
marksbooleanWhether the marks between each step should be shown or not.false
onChange(value: number) => voidThis function will be called when the slider changes its value, as it's being dragged. The new value will be passed as a parameter when this function is executed.-
onDragEnd(value: number) => voidThis function will be called when the slider changes its value, but only when the thumb is released. The new value will be passed as a parameter when this function is executed.-
labelFormatCallback(value: number) => stringThis function will be used to format the labels displayed next to the slider. The value will be passed as parameter and the function must return the formatted value.-
margin'xxsmall' | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'xxlarge' | MarginSize of the margin to be applied to the component. You can pass an object with 'top', 'bottom', 'left' and 'right' properties in order to specify different margin sizes.-
size'medium' | 'large' | 'fillParent'Size of the component.'fillParent'
tabIndexnumberValue of the tabindex attribute.0
refReact.Ref<HTMLDivElement>Reference to the component.-

Examples

Controlled

() => {
  const [value, changeValue] = useState(25);
  const onChange = (newValue) => {
    changeValue(newValue);
  };

  return (
    <DxcInset space="2rem">
      <DxcSlider
        label="Select a value"
        value={value}
        onChange={onChange}
      />
    </DxcInset>
  );
}

Uncontrolled

() => {
  const sliderRef = useRef();

  const handleSubmit = () => {
    const slider = sliderRef.current.getElementsByTagName("input")[0];
    console.log(slider.value);
  };

  return (
    <DxcInset space="2rem">
      <DxcFlex direction="column" gap="2rem">
        <DxcSlider
          label="Select a value"
          defaultValue={40}
          ref={sliderRef}
        />
        <DxcButton label="Submit" type="submit" onClick={handleSubmit} />
      </DxcFlex>
    </DxcInset>
  );
}

Format label

() => {
  const [value, changeValue] = useState(25);
  const onChange = (newValue) => {
    changeValue(newValue);
  };
  const labelFormat = (value) => {
    return `${value}°C`;
  };

  return (
    <DxcInset space="2rem">
      <DxcSlider
        label="Select a value"
        value={value}
        onChange={onChange}
        minValue={0}
        maxValue={100}
        showLimitsValues={true}
        labelFormatCallback={labelFormat}
      />
    </DxcInset>
  );
}