-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioGroupInput.tsx
More file actions
66 lines (60 loc) · 1.92 KB
/
RadioGroupInput.tsx
File metadata and controls
66 lines (60 loc) · 1.92 KB
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
import { ComponentPropsWithoutRef } from 'react';
import { IonRadioGroup, IonText, RadioGroupCustomEvent } from '@ionic/react';
import { useField } from 'formik';
import classNames from 'classnames';
import './RadioGroupInput.scss';
import { PropsWithTestId } from '../types';
/**
* Properties for the `RadioGroupInput` component.
* @see {@link PropsWithTestId}
* @see {@link IonRadioGroup}
*/
interface RadioGroupInputProps
extends PropsWithTestId,
Omit<ComponentPropsWithoutRef<typeof IonRadioGroup>, 'name'>,
Required<Pick<ComponentPropsWithoutRef<typeof IonRadioGroup>, 'name'>> {}
/**
* The `RadioGroupInput` component renders a standardized `IonRadioGroup` which
* is integrated with Formik.
*
* Use one to many `IonRadio` components as the `children` to specify the
* available options.
*
* @param {RadioGroupInputProps} props - Component properties.
* @returns {JSX.Element} JSX
*/
const RadioGroupInput = ({
className,
name,
onIonChange,
testid = 'input-radiogroup',
...radioGroupProps
}: RadioGroupInputProps): JSX.Element => {
const [field, meta, helpers] = useField({ name });
/**
* Handles changes to the field value as a result of user action.
* @param {RadioGroupCustomEvent} event - The event
*/
const onChange = async (event: RadioGroupCustomEvent): Promise<void> => {
await helpers.setValue(event.detail.value);
await helpers.setTouched(true);
onIonChange?.(event);
};
return (
<div className="ls-radiogroup-wrapper" data-testid={`${testid}-wrapper`}>
<IonRadioGroup
className={classNames('ls-radiogroup-input', className)}
data-testid={testid}
onIonChange={onChange}
{...field}
{...radioGroupProps}
/>
{!!meta.error && (
<IonText className="ls-radiogroup-error" color="danger" data-testid={`${testid}-error`}>
{meta.error}
</IonText>
)}
</div>
);
};
export default RadioGroupInput;