import React, {ReactNode} from 'react'; import clsx from 'clsx'; import {getInputFieldClassNames} from '../input-field/get-input-field-class-names'; import {UseSliderProps, UseSliderReturn} from './use-slider'; export interface BaseSliderProps extends UseSliderProps { slider: UseSliderReturn; children: ReactNode; } export function BaseSlider(props: BaseSliderProps) { const { size = 'md', inline, label, showValueLabel = !!label, className, width = 'w-full', slider, children, trackColor = 'primary', fillColor = 'primary', } = props; const { domProps, trackRef, getThumbPercent, getThumbValueLabel, labelId, groupId, thumbIds, isDisabled, numberFormatter, minValue, maxValue, step, values, getValueLabel, } = slider; let outputValue = ''; let maxLabelLength = Math.max( [...numberFormatter.format(minValue)].length, [...numberFormatter.format(maxValue)].length, [...numberFormatter.format(step)].length ); if (getValueLabel) { outputValue = getValueLabel(values[0]); } else if (values.length === 1) { outputValue = getThumbValueLabel(0); } else if (values.length === 2) { // This should really use the NumberFormat#formatRange proposal... // https://github.com/tc39/ecma402/issues/393 // https://github.com/tc39/proposal-intl-numberformat-v3#formatrange-ecma-402-393 outputValue = `${getThumbValueLabel(0)} – ${getThumbValueLabel(1)}`; maxLabelLength = 3 + 2 * Math.max( maxLabelLength, [...numberFormatter.format(minValue)].length, [...numberFormatter.format(maxValue)].length ); } const style = getInputFieldClassNames({ size, disabled: isDisabled, labelDisplay: 'flex', }); const wrapperClassname = clsx('touch-none', className, width, { 'flex items-center': inline, }); return (