-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathServiceLabelForm.tsx
64 lines (59 loc) · 1.74 KB
/
ServiceLabelForm.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
import React from 'react'
import { FormContainer, FormField } from '../forms'
import Grid from '@mui/material/Grid'
import TextField from '@mui/material/TextField'
import { LabelKeySelect } from '../selection/LabelKeySelect'
import { Config } from '../util/RequireConfig'
import { Label } from '../../schema'
function validateKey(value: string): Error | undefined {
const parts = value.split('/')
if (parts.length !== 2)
return new Error('Must be in the format: "example/KeyName".')
}
interface LabelFormProps {
value: Label
errors: Error[]
onChange: (value: Label) => void
editValueOnly?: boolean
disabled?: boolean
create?: boolean
}
export default function LabelForm(props: LabelFormProps): React.JSX.Element {
const { editValueOnly = false, create, ...otherProps } = props
return (
<FormContainer {...otherProps} optionalLabels>
<Grid container spacing={2}>
<Grid item xs={12}>
<Config>
{(cfg) => (
<FormField
fullWidth
disabled={editValueOnly}
component={LabelKeySelect}
label='Key'
name='key'
required
onCreate={
!cfg['General.DisableLabelCreation']
? (key: string) =>
otherProps.onChange({ ...otherProps.value, key })
: undefined
}
validate={validateKey}
/>
)}
</Config>
</Grid>
<Grid item xs={12}>
<FormField
fullWidth
component={TextField}
label='Value'
name='value'
required
/>
</Grid>
</Grid>
</FormContainer>
)
}