import { useLocal } from "@/utils/use-local"; import get from "lodash.get"; import { FC, ReactNode, useEffect, useRef } from "react"; import { FMLocal } from "../form/Form"; import { createPortal, render } from "react-dom"; import { Label } from "@/comps/ui/label"; import { Input } from "@/comps/ui/input"; type FilterPosition = 'regular' | 'inline' | 'popup'; type FilterForm = { gen_fields: GenField[]; gen_table: string; name: string; value: any; position: FilterPosition; children?: ReactNode; onClose?: () => void; }; type GenField = { name: string, is_pk: boolean, type: string, optional: boolean }; export const MasterFilter: FC = ({ gen_fields, gen_table, name, value, position, children, onClose }): ReactNode => { const local = useLocal({ data: [] as any[], columns: [] as string[], fields: [] as GenField[], tableName: "", isGenerated: false, // fm: {} as FMLocal, argInit: {} as { fm: FMLocal; submit: any; reload: any }, argOnLoad: {} as { arg: { fm: FMLocal } }, argOnSubmit: {} as { arg: { fm: FMLocal; form: any; error: any; } } }); useEffect(() => { if (!isEditor) { const w = window as any; if (typeof w["prasi_filter"] !== "object") w["prasi_filter"] = {}; if (typeof w["prasi_filter"][name] !== "object") w["prasi_filter"][name] = {}; const val = value(); w["prasi_filter"][name] = { ...w["prasi_filter"][name], ...val }; w.prasiContext.render(); } }, []) const renderContent = (): ReactNode => (

Filter

{local.fields.map((field) => (
{field.type === 'varchar' && (
)} {field.type === "number" && (
)}
))}
); const generateFilter = () => { local.isGenerated = true; local.tableName = gen_table; gen_fields.forEach((data: any) => { local.fields.push(JSON.parse(data)); }); local.render(); console.log('tableName', local.tableName); console.log('fields', local.fields); }; if (position === 'popup') { let popup = document.querySelector(".main-content-preview > .portal"); if (!popup) { popup = document.createElement("div"); popup.classList.add("portal"); const main = document.querySelector(".main-content-preview"); if (main) { main.appendChild(popup); } } return ( <> {createPortal(
{renderContent()}
, popup )} ); } if (local.isGenerated) { return renderContent(); } else { return (
) } };