-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathForm.jsx
112 lines (106 loc) · 2.83 KB
/
Form.jsx
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
import { useState } from 'react'
import { useForm, Controller } from 'react-hook-form'
import {
Box,
FormLabel,
FormControl,
Input,
Button,
Radio,
RadioGroup,
Stack,
Tooltip,
} from '@chakra-ui/react'
import { InfoOutlineIcon, ExternalLinkIcon } from '@chakra-ui/icons'
import { pascalStrToSpacedWord } from './helper'
function Form({ fieldInfo, passBackResults }) {
const [canSubmitForm, setCanSubmitForm] = useState(false)
const {
handleSubmit,
register,
getValues,
control,
formState: { errors, isSubmitting },
} = useForm()
const FormInfoLabel = ({ name, info }) => {
const spacedName = pascalStrToSpacedWord(name)
return (
<FormLabel htmlFor={name}>
{spacedName}{' '}
{info && (
<Tooltip label={info}>
<InfoOutlineIcon />
</Tooltip>
)}
</FormLabel>
)
}
return (
<form
onSubmit={handleSubmit(passBackResults)}
onChange={() => {
const resultsAreValid = Object.values(getValues()).every(
(i) => typeof i === 'string' && i.length > 0
)
setCanSubmitForm(resultsAreValid)
}}
>
<FormControl>
{fieldInfo.map((field) => {
const { name, type, description } = field
return (
<Box key={name} m={4}>
<FormInfoLabel name={name} info={description} />
{type === 'date' && (
<Input
id={name}
type="date"
{...register(name, {
required: 'This is required',
})}
/>
)}
{type === 'number' && (
<Input
id={name}
type="number"
placeholder={name}
{...register(name, {
required: 'This is required',
})}
/>
)}
{type === 'boolean' && (
<Controller
name={name}
control={control}
render={({ field: { onChange, value } }) => (
<RadioGroup onChange={onChange} value={value}>
<Stack direction="row">
<Radio value="1">Yes</Radio>
<Radio value="0">No</Radio>
</Stack>
</RadioGroup>
)}
/>
)}
</Box>
)
})}
</FormControl>
<Box key={name} m={4}>
<Button
w="100%"
mt={4}
colorScheme="teal"
isLoading={isSubmitting}
type="submit"
disabled={!canSubmitForm}
>
Claim <ExternalLinkIcon m={2} />
</Button>
</Box>
</form>
)
}
export default Form