/** * ToggleField Component * * Renders a toggle switch using Radix UI, integrated with Formik for form state management. * Supports custom child content and displays an error message if validation fails. */ import * as SwitchPrimitive from "@radix-ui/react-switch" import { useField } from "formik" import React, { useState } from "react" import ErrorMessage from "./ErrorMessage" import RequiredAsterisk from "./RequiredAsterisk" import Separator from "./Separator" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" import { faClock } from "@fortawesome/free-regular-svg-icons" /** * Props for ToggleField. * * @param children ReactNode - The content to be displayed alongside the toggle switch. * @extends React.InputHTMLAttributes - Inherits standard input attributes. */ const ToggleField = ({ children, ...props }: { children: React.ReactNode } & React.InputHTMLAttributes) => { const { required, ...restProps } = props // Formik hook for managing form state and validation const [field, meta] = useField({ ...(restProps as { name: string }), type: "checkbox" }) // Local state to manage the checked status of the switch const [checked, setChecked] = useState(false) // Handles checked state change, updates Formik's state accordingly const handleCheckedChange = (isChecked: boolean) => { setChecked(isChecked); field.onChange({ target: { name: field.name, value: isChecked } }); }; return (
handleCheckedChange(isChecked)} >
Toggle ON | Select Tolerance Level
) } export default ToggleField