-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormField.tsx
74 lines (67 loc) · 2.44 KB
/
FormField.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 { FormFeedback, FormGroup, Input, Label } from 'reactstrap'
import { FieldStore } from 'mobx-binder'
import React from 'react'
import { observer } from 'mobx-react-lite'
import { useStores } from '../../stores'
export interface FormFieldProps {
field: FieldStore<any>
}
export const FormField = observer(({ field }: FormFieldProps) => {
const {
i18n: { translate: t },
} = useStores()
const showValidationResults = !field.validating && field.showValidationResults
const valid = showValidationResults && field.valid === true
const invalid = showValidationResults && field.valid === false
const errorMessage = showValidationResults && field.errorMessage ? field.errorMessage : null
const handleBlur = () => {
field.handleBlur()
}
const handleFocus = () => {
field.handleFocus()
}
const updateTextFieldValue = (event: any) => {
field.updateValue(event.target.value)
}
const updateCheckboxFieldValue = (event: any) => {
field.updateValue(event.target.checked)
}
return (
<FormGroup>
{field.valueType === 'string' ? (
<>
<Label for={field.name}>{t(`form.fields.${field.name}`)}</Label>
<Input
id={field.name}
type='text'
name={field.name}
value={field.value}
readOnly={field.readOnly}
valid={valid}
invalid={invalid}
onChange={updateTextFieldValue}
onFocus={handleFocus}
onBlur={handleBlur}
/>
</>
) : (
<Label for={field.name}>
<Input
id={field.name}
type='checkbox'
name={field.name}
checked={!!field.value}
readOnly={field.readOnly}
valid={valid}
invalid={invalid}
onChange={updateCheckboxFieldValue}
onFocus={handleFocus}
onBlur={handleBlur}
/>
{t(`form.fields.${field.name}`)}
</Label>
)}
<FormFeedback>{errorMessage}</FormFeedback>
</FormGroup>
)
})