Live Code
Storybook
Usage
Slider
Slider is a form control that allows users to select a value from a range of values.
Basic Usage
The Slider component implements the HTML range input element.
import { useState } from 'react';
import { Slider } from '@alma-oss/spirit-web-react';
const [value, setValue] = useState();
const handleChange = (event) => {
setValue(Number(event.target.value));
};
<Slider id="slider" label="Slider" value={value} onChange={handleChange} />;
Slider Steps and Value
You can specify the Slider steps and value range by setting the min, max, and step props.
import { useState } from 'react';
import { Slider } from '@alma-oss/spirit-web-react';
const [value, setValue] = useState();
const handleChange = (event) => {
setValue(Number(event.target.value));
};
<Slider id="slider" label="Slider" min={3} max={12} value={value} onChange={handleChange} />;
Required
ℹ️ As per the HTML specification, the Slider component does not support the required state.
Hidden Label
<Slider id="slider" label="Slider" value={value} onChange={handleChange} isLabelHidden />
Fluid Width
<Slider id="slider" label="Slider" value={value} onChange={handleChange} isFluid />
Helper Text
<Slider id="slider" label="Slider" value={value} onChange={handleChange} helperText="Helper text" />
Validation States
Validation states implement the Validation state dictionary.
<Slider
id="slider-success"
label="Slider"
value={value}
onChange={handleChange}
validationState="success"
validationText="Validation text"
/>
<Slider
hasValidationIcon
id="slider-warning"
label="Slider"
value={value}
onChange={handleChange}
validationState="warning"
validationText="Validation text with icon"
/>
<Slider
id="slider-danger"
label="Slider"
value={value}
onChange={handleChange}
validationState="danger"
validationText={['First validation text', 'Second validation text']}
/>
Disabled State
<Slider id="slider-disabled" label="Slider" value={value} onChange={handleChange} isDisabled />
API
| Attribute | Type | Default | Required | Description |
|---|---|---|---|---|
| hasValidationIcon | bool | false | ✕ | Whether to show validation icon |
| helperText | string | - | ✕ | Custom helper text |
| id | string | - | ✓ | Input and label identification |
| isDisabled | bool | false | ✕ | Whether is the input disabled |
| isFluid | bool | false | ✕ | Whether the element spans to the full width of its parent |
| isLabelHidden | bool | false | ✕ | Whether the label is hidden |
| label | ReactNode | - | ✓ | Label text |
| max | number | 100 | ✕ | Max value of slider input |
| min | number | 0 | ✕ | Min value of slider input |
| onChange | () => void | - | ✓ | On input change callback |
| step | number | 1 | ✕ | Sets the stepping interval |
| validationState | Validation dictionary | - | ✕ | Type of validation state |
| validationText | [ReactNode | ReactNode[]] | - | ✕ | Validation text |
| value | number | - | ✓ | Input value |
On top of the API options, the components accept additional attributes. If you need more control over the styling of a component, you can use style props and escape hatches.
UncontrolledSlider
Basic Usage
import { UncontrolledSlider } from '@alma-oss/spirit-web-react';
<UncontrolledSlider id="slider-uncontrolled" label="UncontrolledSlider" />;
API
| Attribute | Type | Default | Required | Description |
|---|---|---|---|---|
| hasValidationIcon | boolean | false | ✕ | Whether to show validation icon |
| helperText | string | - | ✕ | Custom helper text |
| id | string | - | ✓ | Input and label identification |
| isDisabled | bool | false | ✕ | Whether is the input disabled |
| isFluid | bool | false | ✕ | Whether the element spans to the full width of its parent |
| isLabelHidden | bool | false | ✕ | Whether the label is hidden |
| label | ReactNode | - | ✓ | Label text |
| max | number | 100 | ✕ | Max value of slider input |
| min | number | 0 | ✕ | Min value of slider input |
| onChange | () => void | - | ✕ | On input change callback |
| step | number | 1 | ✕ | Sets the stepping interval |
| validationText | [ReactNode | ReactNode[]] | - | ✕ | Validation text |
| value | number | 0 | ✕ | Input value |
On top of the API options, the components accept additional attributes. If you need more control over the styling of a component, you can use style props and escape hatches.