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"; import { TypeDropdownBetter } from "./field/TypeDropdownBetter"; import { TypeAsyncDropdown } from "./field/TypeAsyncDropdown"; export interface FieldProps { fm: any; label?: string; name: string; isBetter?: boolean; tooltip?: string; valueKey?: string; target?: string; onLoad?: (params?: any) => Promise | any; onCount?: (param?: any) => Promise | any; onDelete?: (item: any) => 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" | "multi-dropdown-better" | "multi-async" | "dropdown-async" | "status"; 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; onLabel?: string | ((item: any) => any); onValue?: string | ((item: any) => any); pagination?: boolean; search?: "api" | "local"; visibleLabel?: boolean; autoRefresh?: boolean; forceDisabled?: boolean; description?: string | (() => any); } export const Field: React.FC = ({ fm, visibleLabel = false, label, isBetter = false, name, onLoad, type = "text", placeholder, required, disabled, hidden_label, onChange, className, classField, style, prefix, suffix, allowNew, unique = true, tooltip, valueKey, onDelete, onCount, onLabel, onValue = "id", target, pagination = true, search = "api", autoRefresh = false, forceDisabled, description, }) => { let result = null; const field = useLocal({ focus: false, }); const suffixRef = useRef(null); const prefixRef = useRef(null); const initField = { label, isBetter, name, onLoad, type, placeholder, required, disabled, hidden_label, onChange, className, classField, style, prefix, suffix, allowNew, unique, tooltip, valueKey, onDelete, onCount, onLabel, onValue, target, pagination, search, }; const is_disable = typeof forceDisabled === "boolean" ? forceDisabled : fm.mode === "view" ? true : disabled; const error = fm.error?.[name]; useEffect(() => { setTimeout(() => { 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(); } }, 1000); }, []); const before = typeof prefix === "function" ? prefix() : prefix; const after = typeof suffix === "function" ? suffix() : suffix; return ( <>
{!hidden_label ? ( ) : ( <> )}
{before && (
{before}
)} {/* "multi-dropdown-better" */} {["upload"].includes(type) ? ( <> ) : ["multi-upload"].includes(type) ? ( <> ) : ["dropdown"].includes(type) ? ( <> ) : ["multi-dropdown"].includes(type) ? ( <> ) : ["multi-async"].includes(type) ? ( <>
) : ["dropdown-async"].includes(type) ? ( <>
) : ["multi-dropdown-better"].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}
)}
{description ? (
{typeof description === "function" ? description() : description}
) : ( <> )} {error ? (
{error}
) : ( <> )}
); };