|
| 1 | +import React from 'react'; |
| 2 | +import { Controller, FieldValues } from 'react-hook-form'; |
| 3 | +import FormField from '@cloudscape-design/components/form-field'; |
| 4 | +import ToggleCSD from '@cloudscape-design/components/toggle'; |
| 5 | + |
| 6 | +import { FormToggleProps } from './types'; |
| 7 | + |
| 8 | +import styles from './index.module.scss'; |
| 9 | + |
| 10 | +export const FormToggle = <T extends FieldValues>({ |
| 11 | + name, |
| 12 | + control, |
| 13 | + rules, |
| 14 | + label, |
| 15 | + info, |
| 16 | + constraintText, |
| 17 | + description, |
| 18 | + secondaryControl, |
| 19 | + stretch, |
| 20 | + leftContent, |
| 21 | + toggleLabel, |
| 22 | + onChange: onChangeProp, |
| 23 | + toggleDescription, |
| 24 | + toggleInfo, |
| 25 | + ...props |
| 26 | +}: FormToggleProps<T>) => { |
| 27 | + return ( |
| 28 | + <Controller |
| 29 | + name={name} |
| 30 | + control={control} |
| 31 | + rules={rules} |
| 32 | + render={({ field: { onChange, value, ...fieldRest }, fieldState: { error } }) => { |
| 33 | + return ( |
| 34 | + <FormField |
| 35 | + description={description} |
| 36 | + label={label} |
| 37 | + info={info} |
| 38 | + stretch={stretch} |
| 39 | + constraintText={constraintText} |
| 40 | + secondaryControl={secondaryControl} |
| 41 | + errorText={error?.message} |
| 42 | + > |
| 43 | + {leftContent} |
| 44 | + |
| 45 | + <ToggleCSD |
| 46 | + {...fieldRest} |
| 47 | + {...props} |
| 48 | + checked={value} |
| 49 | + onChange={(event) => { |
| 50 | + onChange(event.detail.checked); |
| 51 | + onChangeProp?.(event); |
| 52 | + }} |
| 53 | + description={toggleDescription} |
| 54 | + > |
| 55 | + {(toggleLabel || toggleInfo) && ( |
| 56 | + <span className={styles.labelWithInfo}> |
| 57 | + {toggleLabel} |
| 58 | + {toggleLabel && toggleInfo && <span aria-hidden="true" className={styles.divider} />} |
| 59 | + {toggleInfo && ( |
| 60 | + <span |
| 61 | + className={styles.info} |
| 62 | + onClick={(e) => e.stopPropagation()} |
| 63 | + onMouseDown={(e) => e.stopPropagation()} |
| 64 | + onPointerDown={(e) => e.stopPropagation()} |
| 65 | + onKeyDown={(e) => e.stopPropagation()} |
| 66 | + > |
| 67 | + {toggleInfo} |
| 68 | + </span> |
| 69 | + )} |
| 70 | + </span> |
| 71 | + )} |
| 72 | + </ToggleCSD> |
| 73 | + </FormField> |
| 74 | + ); |
| 75 | + }} |
| 76 | + /> |
| 77 | + ); |
| 78 | +}; |
0 commit comments