import React, { useState, type InputHTMLAttributes, type ReactNode } from 'react' import classnames from 'classnames' import { Text } from '~components/Text' import { VisuallyHidden } from '~components/VisuallyHidden' import { type OverrideClassName } from '~components/types/OverrideClassName' import styles from './InputRange.module.scss' export type InputRangeProps = { id: string defaultValue?: number value?: number minLabel: ReactNode maxLabel: ReactNode min?: number max?: number } & OverrideClassName> /** * {@link https://cultureamp.design/?path=/docs/components-input-range--docs Storybook} */ export const InputRange = ({ id, defaultValue, value, minLabel, maxLabel, min = 1, max = 10, onChange, 'aria-describedby': ariaDescribedby, classNameOverride, disabled, readOnly, ...restProps }: InputRangeProps): JSX.Element => { const [step, setStep] = useState(0.5) // Let the dot center between the notch initially const visuallyHiddenHintId = `${id}-helper` const readOnlyWithNoValue = readOnly && !value && !defaultValue // This has been split out into a different variable to allow usage of defaultValue above^ // Plus it lets us use max from props with its default value const defaultValueWithDefault = defaultValue ?? (max + 1) / 2 return ( <> ): void => { setStep(1) // Put the stepper to 1 to avoid floating value onChange?.(e) }} {...restProps} />
{[...Array(max)].map((_, index) => (
))}
{min} is {minLabel}, {max} is {maxLabel}
{!readOnlyWithNoValue && (
{minLabel} {maxLabel}
)}
) } InputRange.displayName = 'InputRange'