| 1 |
import { FocusEvent } from 'react'; |
| 2 |
import { FormControl, FormLabel, Slider, SliderFilledTrack, SliderThumb, SliderTrack } from '@chakra-ui/react'; |
| 3 |
import { |
| 4 |
ariaDescribedByIds, |
| 5 |
FormContextType, |
| 6 |
labelValue, |
| 7 |
rangeSpec, |
| 8 |
RJSFSchema, |
| 9 |
StrictRJSFSchema, |
| 10 |
WidgetProps, |
| 11 |
} from '@rjsf/utils'; |
| 12 |
import { getChakra } from '../utils'; |
| 13 |
|
| 14 |
export default function RangeWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>({ |
| 15 |
value, |
| 16 |
readonly, |
| 17 |
disabled, |
| 18 |
onBlur, |
| 19 |
onFocus, |
| 20 |
options, |
| 21 |
schema, |
| 22 |
uiSchema, |
| 23 |
onChange, |
| 24 |
label, |
| 25 |
hideLabel, |
| 26 |
id, |
| 27 |
}: WidgetProps<T, S, F>) { |
| 28 |
const chakraProps = getChakra({ uiSchema }); |
| 29 |
|
| 30 |
const sliderWidgetProps = { value, label, id, ...rangeSpec<S>(schema) }; |
| 31 |
|
| 32 |
const _onChange = (value: undefined | number) => onChange(value === undefined ? options.emptyValue : value); |
| 33 |
const _onBlur = ({ target: { value } }: FocusEvent<HTMLInputElement>) => onBlur(id, value); |
| 34 |
const _onFocus = ({ target: { value } }: FocusEvent<HTMLInputElement>) => onFocus(id, value); |
| 35 |
|
| 36 |
return ( |
| 37 |
<FormControl mb={1} {...chakraProps}> |
| 38 |
{labelValue(<FormLabel htmlFor={id}>{label}</FormLabel>, hideLabel || !label)} |
| 39 |
<Slider |
| 40 |
{...sliderWidgetProps} |
| 41 |
id={id} |
| 42 |
name={id} |
| 43 |
isDisabled={disabled || readonly} |
| 44 |
onChange={_onChange} |
| 45 |
onBlur={_onBlur} |
| 46 |
onFocus={_onFocus} |
| 47 |
aria-describedby={ariaDescribedByIds<T>(id)} |
| 48 |
> |
| 49 |
<SliderTrack> |
| 50 |
<SliderFilledTrack /> |
| 51 |
</SliderTrack> |
| 52 |
<SliderThumb /> |
| 53 |
</Slider> |
| 54 |
</FormControl> |
| 55 |
); |
| 56 |
} |