-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRatingInput.tsx
More file actions
79 lines (75 loc) · 2.28 KB
/
RatingInput.tsx
File metadata and controls
79 lines (75 loc) · 2.28 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Rating, { RatingProps } from "@mui/material/Rating";
import { Controller, FieldValues } from "react-hook-form";
import { FormGroupLayout, FormGroupLayoutProps } from "./FormGroupLayout";
import { useSafeNameId } from "./hooks/useSafeNameId";
import { useFormContext } from "./context/FormContext";
interface RatingInputProps<T extends FieldValues>
extends Omit<RatingProps, "name">,
Omit<FormGroupLayoutProps<T, never>, "onBlur" | "onChange" | "onKeyDown" | "layout" | "addonLeft" | "addonRight" | "addonProps"> {}
const RatingInput = <T extends FieldValues>(props: RatingInputProps<T>) => {
const {
helpText,
label,
labelToolTip,
inputOnly,
hideValidationMessage,
labelStyle,
formGroupId,
inputGroupStyle,
sx,
disabled,
readOnly,
...rest
} = props;
const { name, id } = useSafeNameId(props.name, props.id);
const { disabled: formDisabled, control } = useFormContext<T>();
return (
<Controller
control={control}
shouldUnregister={disabled || formDisabled}
name={props.name}
render={({ field: { value, onBlur, onChange, ...fieldRest } }) => (
<FormGroupLayout
name={name}
id={id}
helpText={helpText}
label={label}
labelToolTip={labelToolTip}
inputOnly={inputOnly}
hideValidationMessage={hideValidationMessage}
labelStyle={labelStyle}
formGroupId={formGroupId}
inputGroupStyle={inputGroupStyle}
>
<Rating
{...rest}
{...fieldRest}
value={value ?? null}
sx={{
...sx,
label: {
// unset the inline-block set by reboot
display: "unset",
},
}}
disabled={disabled || formDisabled}
readOnly={readOnly || formDisabled}
onBlur={(e) => {
if (props.onBlur) {
props.onBlur(e);
}
onBlur();
}}
onChange={(e, value) => {
if (props.onChange) {
props.onChange(e, value);
}
onChange(value);
}}
/>
</FormGroupLayout>
)}
/>
);
};
export { RatingInput, RatingInputProps };