1
|
|
|
import React, { useState } from "react"; |
2
|
|
|
import { FormikErrors, FormikTouched } from "formik"; |
3
|
|
|
import { FormattedMessage } from "react-intl"; |
4
|
|
|
import { inputMessages } from "./Messages"; |
5
|
|
|
|
6
|
|
|
interface RadioGroupProps { |
7
|
|
|
/** HTML ID of the input. */ |
8
|
|
|
id: string; |
9
|
|
|
/** Text for the associated label of the input. */ |
10
|
|
|
label: string; |
11
|
|
|
/** data-clone-grid-item value, see https://designwithclone.ca/#flexbox-grid. */ |
12
|
|
|
grid: string; |
13
|
|
|
/** If this input is required for submission. */ |
14
|
|
|
required: boolean; |
15
|
|
|
/** Error to display. */ |
16
|
|
|
error: string | FormikErrors<any> | undefined; |
17
|
|
|
/** If this group has been affected by user input or a submission. */ |
18
|
|
|
touched: boolean | FormikTouched<any> | undefined; |
19
|
|
|
/** The value set for the contained group of radio inputs */ |
20
|
|
|
value: string | number | undefined; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
const RadioGroup: React.FunctionComponent<RadioGroupProps> = ({ |
24
|
|
|
id, |
25
|
|
|
label, |
26
|
|
|
grid, |
27
|
|
|
required, |
28
|
|
|
error, |
29
|
|
|
touched, |
30
|
|
|
children, |
31
|
|
|
}): React.ReactElement => { |
32
|
|
|
// Add a temporary style when radiogroup is focused, until it's added to clone. |
33
|
|
|
const [focus, setFocus] = useState(false); |
34
|
|
|
const focusStyle = { |
35
|
|
|
boxShadow: |
36
|
|
|
"-3px -3px 0 #0a6cbc, 3px -3px 0 #0a6cbc, 3px 3px 0 #0a6cbc, -3px 3px 0 #0a6cbc", |
37
|
|
|
transition: "all .2s ease", |
38
|
|
|
}; |
39
|
|
|
const hasError = !!error && touched; |
40
|
|
|
return ( |
41
|
|
|
<div |
42
|
|
|
data-c-grid-item={grid} |
43
|
|
|
data-c-input="radio" |
44
|
|
|
data-c-required={required} |
45
|
|
|
data-c-invalid={hasError ? true : null} |
46
|
|
|
> |
47
|
|
|
<label htmlFor={id}>{label}</label> |
48
|
|
|
<span> |
49
|
|
|
<FormattedMessage {...inputMessages.required} /> |
50
|
|
|
</span> |
51
|
|
|
<div |
52
|
|
|
id={id} |
53
|
|
|
role="radiogroup" |
54
|
|
|
onFocus={(): void => setFocus(true)} |
55
|
|
|
onBlur={(): void => setFocus(false)} |
56
|
|
|
style={focus && !!error ? focusStyle : {}} |
57
|
|
|
> |
58
|
|
|
{children} |
59
|
|
|
</div> |
60
|
|
|
<span>{error}</span> |
61
|
|
|
</div> |
62
|
|
|
); |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
export default RadioGroup; |
66
|
|
|
|