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