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