@@ -12,7 +12,7 @@ import {
12
12
Grid ,
13
13
} from '@rocket.chat/fuselage' ;
14
14
import type { ReactElement } from 'react' ;
15
- import type { SubmitHandler } from 'react-hook-form' ;
15
+ import type { SubmitHandler , Validate } from 'react-hook-form' ;
16
16
import { useForm , Controller } from 'react-hook-form' ;
17
17
import { useTranslation , Trans } from 'react-i18next' ;
18
18
@@ -31,20 +31,23 @@ type CreateCloudWorkspaceFormPayload = {
31
31
} ;
32
32
33
33
type CreateCloudWorkspaceFormProps = {
34
+ defaultValues ?: CreateCloudWorkspaceFormPayload ;
34
35
onSubmit : SubmitHandler < CreateCloudWorkspaceFormPayload > ;
35
36
serverRegionOptions : SelectOption [ ] ;
36
37
languageOptions : SelectOption [ ] ;
37
38
domain : string ;
38
- onBackButtonClick : ( ) => void ;
39
- validateUrl : ( url : string ) => Promise < boolean > ;
40
- validateEmail : ( url : string ) => Promise < boolean > ;
39
+ onBackButtonClick ? : ( ) => void ;
40
+ validateUrl : Validate < string > ;
41
+ validateEmail : Validate < string > ;
41
42
} ;
42
43
43
44
const CreateCloudWorkspaceForm = ( {
45
+ defaultValues,
44
46
onSubmit,
45
47
domain,
46
48
serverRegionOptions,
47
49
languageOptions,
50
+ onBackButtonClick,
48
51
validateUrl,
49
52
validateEmail,
50
53
} : CreateCloudWorkspaceFormProps ) : ReactElement => {
@@ -68,20 +71,16 @@ const CreateCloudWorkspaceForm = ({
68
71
</ Field . Label >
69
72
< Field . Row >
70
73
< EmailInput
71
- error = { errors ?. organizationEmail ?. type || undefined }
72
74
{ ...register ( 'organizationEmail' , {
73
75
required : true ,
74
76
validate : validateEmail ,
75
77
} ) }
78
+ defaultValue = { defaultValues ?. organizationEmail }
79
+ error = { errors ?. organizationEmail ?. type || undefined }
76
80
/>
77
81
</ Field . Row >
78
- { errors . organizationEmail ?. type === 'required' && (
79
- < Field . Error > { t ( 'component.form.requiredField' ) } </ Field . Error >
80
- ) }
81
- { errors . organizationEmail ?. type === 'validate' && (
82
- < Field . Error >
83
- { t ( 'form.createCloudWorkspace.fields.orgEmail.invalid' ) }
84
- </ Field . Error >
82
+ { errors ?. organizationEmail && (
83
+ < Field . Error > { errors . organizationEmail . message } </ Field . Error >
85
84
) }
86
85
</ Field >
87
86
@@ -93,8 +92,9 @@ const CreateCloudWorkspaceForm = ({
93
92
</ Field . Label >
94
93
< Field . Row >
95
94
< TextInput
96
- error = { errors ?. workspaceName ?. type || undefined }
97
95
{ ...register ( 'workspaceName' , { required : true } ) }
96
+ defaultValue = { defaultValues ?. workspaceName }
97
+ error = { errors ?. workspaceName ?. type || undefined }
98
98
/>
99
99
</ Field . Row >
100
100
{ errors . workspaceName && (
@@ -115,15 +115,11 @@ const CreateCloudWorkspaceForm = ({
115
115
required : true ,
116
116
validate : validateUrl ,
117
117
} ) }
118
+ defaultValue = { defaultValues ?. workspaceURL }
118
119
/>
119
120
</ Field . Row >
120
- { errors . workspaceURL ?. type === 'required' && (
121
- < Field . Error > { t ( 'component.form.requiredField' ) } </ Field . Error >
122
- ) }
123
- { errors . workspaceURL ?. type === 'validate' && (
124
- < Field . Error >
125
- { t ( 'form.createCloudWorkspace.fields.workspaceUrl.exists' ) }
126
- </ Field . Error >
121
+ { errors ?. workspaceURL && (
122
+ < Field . Error > { errors . workspaceURL . message } </ Field . Error >
127
123
) }
128
124
</ Field >
129
125
@@ -144,6 +140,8 @@ const CreateCloudWorkspaceForm = ({
144
140
< Controller
145
141
name = 'serverRegion'
146
142
control = { control }
143
+ rules = { { required : true } }
144
+ defaultValue = { defaultValues ?. serverRegion }
147
145
render = { ( { field } ) => (
148
146
< Select
149
147
{ ...field }
@@ -172,6 +170,8 @@ const CreateCloudWorkspaceForm = ({
172
170
< Controller
173
171
name = 'language'
174
172
control = { control }
173
+ rules = { { required : true } }
174
+ defaultValue = { defaultValues ?. language }
175
175
render = { ( { field } ) => (
176
176
< Select
177
177
{ ...field }
@@ -228,6 +228,12 @@ const CreateCloudWorkspaceForm = ({
228
228
229
229
< Form . Footer >
230
230
< ButtonGroup >
231
+ { onBackButtonClick && (
232
+ < Button disabled = { isSubmitting } onClick = { onBackButtonClick } >
233
+ { t ( 'component.form.action.back' ) }
234
+ </ Button >
235
+ ) }
236
+
231
237
< Button
232
238
type = 'submit'
233
239
primary
0 commit comments