|
| 1 | +/* eslint-disable no-nested-ternary */ |
| 2 | +import { css } from '@rocket.chat/css-in-js'; |
| 3 | +import type { AriaAttributes, ReactElement } from 'react'; |
| 4 | +import React, { useMemo, useRef } from 'react'; |
| 5 | +import type { AriaSliderProps } from 'react-aria'; |
| 6 | +import { useNumberFormatter, useSlider } from 'react-aria'; |
| 7 | +import { useSliderState } from 'react-stately'; |
| 8 | + |
| 9 | +import { useStyle } from '../../hooks/useStyle'; |
| 10 | +import { SliderHead } from './SliderHead'; |
| 11 | +import { SliderThumb } from './SliderThumb'; |
| 12 | +import { SliderTrack } from './SliderTrack'; |
| 13 | + |
| 14 | +type SliderProps<T extends number | number[]> = AriaAttributes & { |
| 15 | + /** |
| 16 | + * The display format of the value output. |
| 17 | + */ |
| 18 | + formatOptions?: Intl.NumberFormatOptions; |
| 19 | + label?: string; |
| 20 | + showOutput?: boolean; |
| 21 | + /** |
| 22 | + * Slider with multiple thumbs. |
| 23 | + * @default false |
| 24 | + */ |
| 25 | + multiThumb?: T extends number[] ? true : false; |
| 26 | + step?: number; |
| 27 | + /** |
| 28 | + * @default 0 |
| 29 | + */ |
| 30 | + minValue?: number; |
| 31 | + /** |
| 32 | + * @default 100 |
| 33 | + */ |
| 34 | + maxValue?: number; |
| 35 | + orientation?: 'horizontal' | 'vertical'; |
| 36 | + disabled?: boolean; |
| 37 | + defaultValue?: T; |
| 38 | + small?: boolean; |
| 39 | + /** |
| 40 | + * 100% of parent's dimention |
| 41 | + */ |
| 42 | + large?: boolean; |
| 43 | +} & ( |
| 44 | + | { |
| 45 | + value: T; |
| 46 | + onChange: (value: T) => void; |
| 47 | + } |
| 48 | + | { |
| 49 | + value?: never; |
| 50 | + onChange?: never; |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | +export function Slider<T extends number | [min: number, max: number]>( |
| 55 | + props: SliderProps<T> |
| 56 | +): ReactElement { |
| 57 | + const { |
| 58 | + label, |
| 59 | + formatOptions, |
| 60 | + showOutput = true, |
| 61 | + multiThumb, |
| 62 | + maxValue, |
| 63 | + minValue, |
| 64 | + small, |
| 65 | + large, |
| 66 | + } = props; |
| 67 | + |
| 68 | + // Get a defaultValue in the range for multiThumb |
| 69 | + const getMultiThumbDefaultValue = (): T | undefined => { |
| 70 | + if (multiThumb && !defaultValue) { |
| 71 | + if (minValue && maxValue) { |
| 72 | + return [minValue, maxValue] as T; |
| 73 | + } |
| 74 | + if (minValue) { |
| 75 | + return [minValue, 100] as T; |
| 76 | + } |
| 77 | + if (maxValue) { |
| 78 | + return [0, maxValue] as T; |
| 79 | + } |
| 80 | + return [0, 100] as T; |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + const { defaultValue = getMultiThumbDefaultValue() } = props; |
| 85 | + |
| 86 | + const sliderProps = { |
| 87 | + ...props, |
| 88 | + isDisabled: props.disabled, |
| 89 | + } as AriaSliderProps<number | number[]>; |
| 90 | + |
| 91 | + const trackRef = useRef(null); |
| 92 | + const numberFormatter = useNumberFormatter(formatOptions); |
| 93 | + const sliderState = useSliderState({ |
| 94 | + defaultValue, |
| 95 | + ...sliderProps, |
| 96 | + numberFormatter, |
| 97 | + }); |
| 98 | + |
| 99 | + const { groupProps, trackProps, labelProps, outputProps } = useSlider( |
| 100 | + sliderProps, |
| 101 | + sliderState, |
| 102 | + trackRef |
| 103 | + ); |
| 104 | + |
| 105 | + const isHorizontal = useMemo( |
| 106 | + () => sliderState.orientation === 'horizontal', |
| 107 | + [sliderState.orientation] |
| 108 | + ); |
| 109 | + const isVertical = useMemo( |
| 110 | + () => sliderState.orientation === 'vertical', |
| 111 | + [sliderState.orientation] |
| 112 | + ); |
| 113 | + |
| 114 | + const slider = useStyle( |
| 115 | + css` |
| 116 | + display: flex; |
| 117 | + ${isHorizontal && |
| 118 | + css` |
| 119 | + flex-direction: column; |
| 120 | + width: ${small ? '150px' : large ? '100%' : '300px'}; |
| 121 | + `}; |
| 122 | + ${isVertical && |
| 123 | + css` |
| 124 | + flex-direction: row-reverse; |
| 125 | + height: ${small ? '50px' : large ? '100%' : '100px'}; |
| 126 | + `} |
| 127 | + `, |
| 128 | + sliderState |
| 129 | + ); |
| 130 | + |
| 131 | + return ( |
| 132 | + <div {...groupProps} className={slider}> |
| 133 | + <SliderHead |
| 134 | + labelProps={labelProps} |
| 135 | + outputProps={outputProps} |
| 136 | + state={sliderState} |
| 137 | + showOutput={showOutput} |
| 138 | + label={label} |
| 139 | + multiThumb={multiThumb} |
| 140 | + /> |
| 141 | + <SliderTrack |
| 142 | + state={sliderState} |
| 143 | + trackProps={trackProps} |
| 144 | + trackRef={trackRef} |
| 145 | + multiThumb={multiThumb} |
| 146 | + > |
| 147 | + <SliderThumb index={0} state={sliderState} trackRef={trackRef} /> |
| 148 | + {multiThumb && ( |
| 149 | + <SliderThumb index={1} state={sliderState} trackRef={trackRef} /> |
| 150 | + )} |
| 151 | + </SliderTrack> |
| 152 | + </div> |
| 153 | + ); |
| 154 | +} |
0 commit comments