-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
RangeWidget.tsx
63 lines (60 loc) · 1.55 KB
/
RangeWidget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from "react";
import FormLabel from "@mui/material/FormLabel";
import Slider from "@mui/material/Slider";
import {
ariaDescribedByIds,
FormContextType,
RJSFSchema,
StrictRJSFSchema,
WidgetProps,
rangeSpec,
} from "@rjsf/utils";
/** The `RangeWidget` component uses the `BaseInputTemplate` changing the type to `range` and wrapping the result
* in a div, with the value along side it.
*
* @param props - The `WidgetProps` for this component
*/
export default function RangeWidget<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(props: WidgetProps<T, S, F>) {
const {
value,
readonly,
disabled,
onBlur,
onFocus,
options,
schema,
onChange,
required,
label,
id,
} = props;
const sliderProps = { value, label, id, name: id, ...rangeSpec<S>(schema) };
const _onChange = (_: any, value?: number | number[]) => {
onChange(value ? value : options.emptyValue);
};
const _onBlur = ({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
onBlur(id, value);
const _onFocus = ({
target: { value },
}: React.FocusEvent<HTMLInputElement>) => onFocus(id, value);
return (
<>
<FormLabel required={required} htmlFor={id}>
{label || schema.title}
</FormLabel>
<Slider
disabled={disabled || readonly}
onChange={_onChange}
onBlur={_onBlur}
onFocus={_onFocus}
valueLabelDisplay="auto"
{...sliderProps}
aria-describedby={ariaDescribedByIds<T>(id)}
/>
</>
);
}