(
props: SliderBaseProps<T> & {sliderRef: RefObject<HTMLDivElement | null>}
)
| 358 | }); |
| 359 | |
| 360 | export function SliderBase<T extends number | number[]>( |
| 361 | props: SliderBaseProps<T> & {sliderRef: RefObject<HTMLDivElement | null>} |
| 362 | ): ReactNode { |
| 363 | let formContext = useContext(FormContext); |
| 364 | // oxlint-disable-next-line react/react-compiler |
| 365 | props = useFormProps(props); |
| 366 | let { |
| 367 | label, |
| 368 | labelPosition = 'top', |
| 369 | labelAlign = 'start', |
| 370 | size = 'M', |
| 371 | minValue = 0, |
| 372 | maxValue = 100 |
| 373 | } = props; |
| 374 | let {direction} = useLocale(); |
| 375 | |
| 376 | // oxlint-disable react/react-compiler |
| 377 | return ( |
| 378 | <AriaSlider |
| 379 | {...props} |
| 380 | ref={props.sliderRef} |
| 381 | className={renderProps => |
| 382 | (props.UNSAFE_className || '') + |
| 383 | mergeStyles( |
| 384 | style(field())({labelPosition, isInForm: !!formContext}), |
| 385 | slider({...renderProps, labelPosition, size, isInForm: !!formContext}, props.styles) |
| 386 | ) |
| 387 | }> |
| 388 | {({state}) => { |
| 389 | let maxLabelLength = 0; |
| 390 | switch (state.values.length) { |
| 391 | case 1: |
| 392 | maxLabelLength = Math.max( |
| 393 | [...state.getFormattedValue(minValue)].length, |
| 394 | [...state.getFormattedValue(maxValue)].length |
| 395 | ); |
| 396 | break; |
| 397 | case 2: |
| 398 | maxLabelLength = Math.max( |
| 399 | [...state.getFormattedValue([minValue, minValue + (props.step || 1)])].length, |
| 400 | [...state.getFormattedValue([maxValue - (props.step || 1), maxValue])].length |
| 401 | ); |
| 402 | break; |
| 403 | default: |
| 404 | throw new Error('Only sliders with 1 or 2 handles are supported!'); |
| 405 | } |
| 406 | |
| 407 | let outputValue = ( |
| 408 | <SliderOutput |
| 409 | style={{ |
| 410 | width: `${maxLabelLength}ch`, |
| 411 | minWidth: `${maxLabelLength}ch`, |
| 412 | fontVariantNumeric: 'tabular-nums' |
| 413 | }} |
| 414 | className={output({direction, labelPosition, isInForm: !!formContext})} |
| 415 | /> |
| 416 | ); |
| 417 |
nothing calls this directly
no test coverage detected