| Total Complexity | 1 |
| Complexity/F | 0 |
| Lines of Code | 53 |
| Function Count | 0 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import React 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 | const hasError = !!error && touched; |
||
| 33 | return ( |
||
| 34 | <div |
||
| 35 | data-c-grid-item={grid} |
||
| 36 | data-c-input="radio" |
||
| 37 | data-c-required={required} |
||
| 38 | data-c-invalid={hasError ? true : null} |
||
| 39 | > |
||
| 40 | <label htmlFor={id}>{label}</label> |
||
| 41 | <span> |
||
| 42 | <FormattedMessage {...inputMessages.required} /> |
||
| 43 | </span> |
||
| 44 | <div id={id} role="radiogroup"> |
||
| 45 | {children} |
||
| 46 | </div> |
||
| 47 | <span>{error}</span> |
||
| 48 | </div> |
||
| 49 | ); |
||
| 50 | }; |
||
| 51 | |||
| 52 | export default RadioGroup; |
||
| 53 |