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 { FieldUploadMulti } from "./field/TypeUploadMulti"; 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"; export const Field: React.FC = ({ fm, label, name, onLoad, type, placeholder, required, disabled, hidden_label, onChange, className, style, prefix, suffix, allowNew, }) => { 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; ` )} /> )} {after && (
{after}
)}
{error ? (
{error}
) : ( <> )}
); };