forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateAttributeValueField.tsx
74 lines (69 loc) · 2.25 KB
/
DateAttributeValueField.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
64
65
66
67
68
69
70
71
72
73
74
import { Box, Typography } from "@mui/material";
import { styled } from "@mui/material/styles";
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns";
import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import React, { FC } from "react";
import { Control, Controller } from "react-hook-form";
import { UseFormSetValue } from "react-hook-form/dist/types/form";
import { Schema } from "./EntryFormSchema";
const StyledBox = styled(Box)(({}) => ({
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
}));
const StyledTypography = styled(Typography)(({}) => ({
color: "rgba(0, 0, 0, 0.6)",
}));
interface Props {
attrId: number;
control: Control<Schema>;
setValue: UseFormSetValue<Schema>;
isDisabled?: boolean;
}
export const DateAttributeValueField: FC<Props> = ({
attrId,
control,
setValue,
isDisabled = false,
}) => {
return (
<StyledBox>
<StyledTypography variant="caption">月日を選択</StyledTypography>
<Controller
name={`attrs.${attrId}.value.asString`}
control={control}
defaultValue=""
render={({ field, fieldState: { error } }) => (
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DesktopDatePicker
format="yyyy/MM/dd"
value={field.value ? new Date(field.value) : null}
onChange={(date: Date | null) => {
let settingDateValue = "";
if (date !== null) {
settingDateValue = `${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}`;
}
setValue(`attrs.${attrId}.value.asString`, settingDateValue, {
shouldDirty: true,
shouldValidate: true,
});
}}
slotProps={{
textField: {
error: error != null,
helperText: error?.message,
size: "small",
fullWidth: false,
},
}}
disabled={isDisabled}
/>
</LocalizationProvider>
)}
/>
</StyledBox>
);
};