-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathindex.tsx
More file actions
80 lines (76 loc) · 2.91 KB
/
index.tsx
File metadata and controls
80 lines (76 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react';
import { Controller, FieldValues } from 'react-hook-form';
import FormField from '@cloudscape-design/components/form-field';
import ToggleCSD from '@cloudscape-design/components/toggle';
import { FormToggleProps } from './types';
import styles from './index.module.scss';
export const FormToggle = <T extends FieldValues>({
name,
control,
rules,
defaultValue,
label,
info,
constraintText,
description,
secondaryControl,
stretch,
leftContent,
toggleLabel,
onChange: onChangeProp,
toggleDescription,
toggleInfo,
...props
}: FormToggleProps<T>) => {
return (
<Controller
name={name}
control={control}
rules={rules}
defaultValue={defaultValue}
render={({ field: { onChange, value, ...fieldRest }, fieldState: { error } }) => {
return (
<FormField
description={description}
label={label}
info={info}
stretch={stretch}
constraintText={constraintText}
secondaryControl={secondaryControl}
errorText={error?.message}
>
{leftContent}
<ToggleCSD
{...fieldRest}
{...props}
checked={value}
onChange={(event) => {
onChange(event.detail.checked);
onChangeProp?.(event);
}}
description={toggleDescription}
>
{(toggleLabel || toggleInfo) && (
<span className={styles.labelWithInfo}>
{toggleLabel}
{toggleLabel && toggleInfo && <span aria-hidden="true" className={styles.divider} />}
{toggleInfo && (
<span
className={styles.info}
onClick={(e) => e.stopPropagation()}
onMouseDown={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
onKeyDown={(e) => e.stopPropagation()}
>
{toggleInfo}
</span>
)}
</span>
)}
</ToggleCSD>
</FormField>
);
}}
/>
);
};