import React from "react"; import { get } from "lodash"; import { Controller, useFormContext } from "react-hook-form"; import { twMerge } from "tailwind-merge"; import { Field, FieldProps } from "./Field"; export const TextField = ( props: FieldProps & React.HTMLProps & { handleChanged?: (props: { name: string }) => void; startIcon?: React.ReactNode; }, ) => { const { name, type = "text", label, required, disabled, className, labelClassName, childrenWrapperClassName, hideErrorMessage = false, startIcon, handleChanged, ...rest } = props; const form = useFormContext(); const { formState: { errors }, } = form; const hasError = !!get(errors, name)?.message; const input = (value: string | number) => { return typeof value === "number" ? isNaN(value) ? "" : value.toString() : value ?? ""; }; const output = type === "number" ? (e: React.ChangeEvent) => { const output = parseFloat(e.target.value); return isNaN(output) ? 0 : output; } : (e: React.ChangeEvent) => e.target.value; return (
(
{startIcon && (
{startIcon}
)} { field.onChange(output(e)); }} value={input(field.value)} onBlur={() => { if (handleChanged) handleChanged({ name }); }} />
)} />
); };