import { useEffect, useRef } from "react"; import { FieldCheckbox } from "./field/TypeCheckbox"; import { TypeDropdown } from "./field/TypeDropdown"; import { TypeInput } from "./field/TypeInput"; import { TypeUpload } from "./field/TypeUpload"; import { TypeRichText } from "./field/TypeRichText"; import { TypeTag } from "./field/TypeTag"; import get from "lodash.get"; import { getNumber } from "@/lib/utils/getNumber"; import { useLocal } from "@/lib/utils/use-local"; import { FieldRadio } from "./field/TypeRadio"; import { cn } from "@/lib/utils"; import { TooltipBetter } from "../ui/tooltip-better"; export const Field: React.FC<{ fm: any; label?: string; name: string; isBetter?: boolean; tooltip?: string; onLoad?: () => Promise | any; type?: | "rating" | "color" | "single-checkbox" | "radio" | "checkbox" | "upload" | "multi-upload" | "dropdown" | "multi-dropdown" | "checkbox" | "radio" | "single-checkbox" | "richtext" | "tag" | "text" | "money" | "textarea" | "time" | "date" | "password" | "email"; placeholder?: string; disabled?: boolean; required?: boolean; hidden_label?: boolean; onChange?: ({ data }: any) => Promise | void; className?: string; classField?: string; style?: string; prefix?: string | any | (() => any); suffix?: string | any | (() => any); allowNew?: boolean; unique?: boolean; }> = ({ fm, label, isBetter = false, name, onLoad, type = "text", placeholder, required, disabled, hidden_label, onChange, className, classField, style, prefix, suffix, allowNew, unique = true, tooltip, }) => { let result = null; const field = useLocal({ focus: false, }); const suffixRef = useRef(null); const prefixRef = useRef(null); const is_disable = fm.mode === "view" ? true : disabled; const error = fm.error?.[name]; useEffect(() => { if (typeof fm.fields?.[name] !== "object") { const fields = fm.fields?.[name]; fm.fields[name] = { ...fields, label, name, onLoad, type, placeholder, required, disabled, hidden_label, onChange, className, style, }; fm.render(); } }, []); const before = typeof prefix === "function" ? prefix() : prefix; const after = typeof suffix === "function" ? suffix() : suffix; return ( <>
{!hidden_label ? ( ) : ( <> )}
{before && (
{before}
)} {["upload"].includes(type) ? ( <> ) : ["multi-upload"].includes(type) ? ( <> ) : ["dropdown"].includes(type) ? ( <> ) : ["multi-dropdown"].includes(type) ? ( <> ) : ["checkbox"].includes(type) ? ( <> ) : ["radio"].includes(type) ? ( <> ) : ["single-checkbox"].includes(type) ? ( <> ) : ["richtext"].includes(type) ? ( <> ) : ["tag"].includes(type) ? ( <> ) : ( <> { field.focus = true; field.render(); }} className={cx( before && css` padding-left: ${getNumber( get(prefixRef, "current.clientWidth") ) + 10}px; `, after && css` padding-right: ${getNumber( get(suffixRef, "current.clientWidth") ) + 10}px; `, className )} /> )} {after && (
{after}
)}
{error ? (
{error}
) : ( <> )}
); };