-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomOptionsModal.tsx
More file actions
157 lines (153 loc) · 4.23 KB
/
Copy pathCustomOptionsModal.tsx
File metadata and controls
157 lines (153 loc) · 4.23 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { type FC } from 'react'
import { Controller, type Control, type UseFormHandleSubmit, type SubmitHandler, type FieldError } from 'react-hook-form'
import {
Modal,
Box,
Typography,
Button,
FormControl,
InputLabel,
Select,
MenuItem,
FormHelperText,
TextField,
capitalize
} from '@mui/material'
import { useTranslation } from 'react-i18next'
interface CustomTextModalProps {
onClose: () => void
handleSubmit: UseFormHandleSubmit<any>
onSubmit: SubmitHandler<any>
open: boolean
icon: JSX.Element
control: Control<any>
firstError: FieldError | undefined
secondError: FieldError | undefined
thirdError?: FieldError | undefined
name: string
firstLabel: string
secondLabel: string
options: Option[]
thirdLabel?: string
editText?: string
title?: string
}
const CustomTextModal: FC<CustomTextModalProps> = ({
onClose,
handleSubmit,
onSubmit,
open,
icon,
control,
firstError,
secondError,
name,
firstLabel,
secondLabel,
thirdError,
thirdLabel,
editText,
options,
title
}) => {
const { t } = useTranslation()
return (
<Modal
open={open}
onClose={onClose}
aria-labelledby="edit-modal"
>
<Box
sx={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 400,
bgcolor: 'background.paper',
boxShadow: 24,
p: 4
}}
>
<Typography id="edit-modal" variant="h6" component="h2">
{title ?? t('editModal.title')}
</Typography>
{/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
<form onSubmit={handleSubmit(onSubmit)} id={name}>
{
(thirdLabel !== undefined) && <Controller
name={thirdLabel}
control={control}
render={({ field }) => (
<TextField
{...field}
label={capitalize(thirdLabel)}
variant="outlined"
margin="normal"
fullWidth
error={Boolean(thirdError)}
helperText={thirdError?.message ?? ''}
/>
)}
/>
}
<Controller
name={firstLabel}
control={control}
render={({ field }) => (
<>
<FormControl fullWidth variant="outlined" margin="normal">
<InputLabel id="select-label-1">{firstLabel}</InputLabel>
<Select
{...field}
labelId="select-label-1"
label={firstLabel}
error={Boolean(firstError)}
>
{options.map((option, index) => (
<MenuItem key={index} value={String(option.id)}>{option.name}</MenuItem>
))}
</Select>
</FormControl>
{Boolean(firstError) && (
<FormHelperText error>{firstError?.message ?? ''}</FormHelperText>
)}
</>
)}
/>
<Controller
name={secondLabel}
control={control}
render={({ field }) => <FormControl fullWidth variant="outlined" margin="normal">
<InputLabel id="select-label-2">{secondLabel}</InputLabel>
<Select
{...field}
labelId="select-label-2"
label={secondLabel}
error={Boolean(secondError)}
>
{options.map((option, index) => (
<MenuItem key={index} value={String(option.id)}>{option.name}</MenuItem>
))}
</Select>
{Boolean(secondError) && (
<FormHelperText error>{secondError?.message ?? ''}</FormHelperText>
)}
</FormControl>
}
/>
<Button
type="submit"
fullWidth
variant="contained"
sx={{ mt: 3, mb: 2 }}
endIcon={icon}
>
{editText ?? t('editModal.edit')}
</Button>
</form>
</Box>
</Modal >
)
}
export default CustomTextModal