-
Notifications
You must be signed in to change notification settings - Fork 19
/
port-row.tsx
82 lines (76 loc) · 2.68 KB
/
port-row.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
75
76
77
78
79
80
81
82
import { useEffect } from 'react'
import { Controller, useFormContext } from 'react-hook-form'
import { ButtonIcon, ButtonIconStyle, Icon, IconAwesomeEnum, InputText, InputToggle, Tooltip } from '@qovery/shared/ui'
import { PortData } from '../../../../feature/page-application-create-feature/application-creation-flow.interface'
export interface PortRowProps {
index: number
onDelete: (index: number) => void
}
export function PortRow(props: PortRowProps) {
const { index } = props
const { control, watch, setValue, resetField } = useFormContext<PortData>()
const isPublicWatch = watch(`ports.${index}.is_public`)
const externalPortWatch = watch(`ports.${index}.external_port`)
useEffect(() => {
setValue(`ports.${index}.external_port`, isPublicWatch ? 443 : undefined)
}, [isPublicWatch, index, setValue, resetField])
return (
<div data-testid={'port-row'} className="flex gap-3 mb-5 w-full items-center">
<Controller
name={`ports.${index}.application_port`}
control={control}
rules={{
required: 'Please enter a value',
}}
render={({ field }) => (
<InputText
className="flex-grow"
name={field.name}
onChange={field.onChange}
value={field.value}
label={`Application port`}
type="number"
/>
)}
/>
<Controller
name={`ports.${index}.external_port`}
control={control}
render={({ field, fieldState: { error } }) => (
<InputText
className="flex-grow"
name={field.name}
onChange={field.onChange}
value={externalPortWatch} // passing a watch here because setValue with undefined does not work: https://github.com/react-hook-form/react-hook-form/issues/8133
label={`External port`}
error={error?.message}
type="number"
disabled
rightElement={
<Tooltip content="Only HTTP protocol is supported">
<div>
<Icon name={IconAwesomeEnum.CIRCLE_INFO} className="text-text-400" />
</div>
</Tooltip>
}
/>
)}
/>
<Controller
name={`ports.${index}.is_public`}
control={control}
render={({ field }) => (
<InputToggle small onChange={field.onChange} value={field.value} title="Publicly exposed" />
)}
/>
<ButtonIcon
dataTestId="delete-port"
className="text-text-500"
style={ButtonIconStyle.FLAT}
icon={IconAwesomeEnum.TRASH}
onClick={() => props.onDelete(index)}
/>
</div>
)
}
export default PortRow