This commit is contained in:
rizky 2024-04-13 15:35:17 -07:00
parent d54a7817a4
commit 15ac248f19
13 changed files with 91 additions and 54 deletions

View File

@ -113,6 +113,7 @@ export const Form: FC<FMProps> = (props) => {
className={cx( className={cx(
"form c-flex-1 c-w-full c-h-full c-relative c-overflow-auto" "form c-flex-1 c-w-full c-h-full c-relative c-overflow-auto"
)} )}
action="#"
> >
{toaster_el && createPortal(<Toaster cn={cx} />, toaster_el)} {toaster_el && createPortal(<Toaster cn={cx} />, toaster_el)}
<div <div
@ -132,6 +133,7 @@ export const Form: FC<FMProps> = (props) => {
</PassProp> </PassProp>
); );
})} })}
<button type="submit" className="c-hidden"></button>
</div> </div>
</form> </form>
); );

View File

@ -1,11 +1,10 @@
import { Skeleton } from "@/comps/ui/skeleton"; import { createItem } from "@/gen/utils";
import get from "lodash.get"; import get from "lodash.get";
import { FC, useEffect } from "react"; import { FC, useEffect } from "react";
import { FMLocal, FieldLocal } from "../typings"; import { FMLocal, FieldLocal } from "../typings";
import { fieldMapping } from "./mapping";
import { Loader2 } from "lucide-react";
import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem"; import { genFieldMitem, updateFieldMItem } from "../utils/gen-mitem";
import { createItem } from "@/gen/utils"; import { fieldMapping } from "./mapping";
import { FieldLoading } from "./raw/FieldLoading";
const modify = { const modify = {
timeout: null as any, timeout: null as any,
@ -97,28 +96,7 @@ export const FieldInput: FC<{
> >
{prefix && <></>} {prefix && <></>}
{fm.status === "loading" ? ( {fm.status === "loading" ? (
<div className="c-flex c-flex-col c-space-y-1 c-p-1 c-justify-center"> <FieldLoading />
<div className="c-flex c-space-x-1">
<Skeleton
className={css`
width: 50px;
height: 10px;
`}
/>
<Skeleton
className={css`
width: 50px;
height: 10px;
`}
/>
</div>
<Skeleton
className={css`
width: 80px;
height: 10px;
`}
/>
</div>
) : ( ) : (
<div <div
className={cx( className={cx(
@ -126,7 +104,7 @@ export const FieldInput: FC<{
field.disabled && "c-pointer-events-none" field.disabled && "c-pointer-events-none"
)} )}
> >
{!found && <Loader2 className="c-h-4 c-w-4 c-animate-spin" />} {!found && <FieldLoading />}
{found && ( {found && (
<PassProp field={field} fm={fm}> <PassProp field={field} fm={fm}>
{found} {found}

View File

@ -0,0 +1,28 @@
import { Skeleton } from "@/comps/ui/skeleton";
export const FieldLoading = () => {
return (
<div className="c-flex c-flex-col c-space-y-1 c-p-1 c-justify-center">
<div className="c-flex c-space-x-1">
<Skeleton
className={css`
width: 50px;
height: 10px;
`}
/>
<Skeleton
className={css`
width: 50px;
height: 10px;
`}
/>
</div>
<Skeleton
className={css`
width: 80px;
height: 10px;
`}
/>
</div>
);
};

View File

@ -2,7 +2,7 @@ import { useLocal } from "@/utils/use-local";
import { FC, useEffect } from "react"; import { FC, useEffect } from "react";
import { FMLocal, FieldLocal } from "../../typings"; import { FMLocal, FieldLocal } from "../../typings";
import { OptionItem, RawDropdown } from "../raw/Dropdown"; import { OptionItem, RawDropdown } from "../raw/Dropdown";
import { Loader2 } from "lucide-react"; import { FieldLoading } from "../raw/FieldLoading";
export type PropTypeRelation = { export type PropTypeRelation = {
type: "has-one" | "has-many"; type: "has-one" | "has-many";
@ -68,9 +68,7 @@ export const FieldTypeRelation: FC<{
return ( return (
<> <>
{field.status === "loading" ? ( {field.status === "loading" ? (
<div className="c-w-full c-h-full c-items-center c-flex c-px-2"> <FieldLoading />
<Loader2 className="c-h-4 c-w-4 c-animate-spin" />
</div>
) : ( ) : (
<RawDropdown <RawDropdown
options={list} options={list}

View File

@ -25,7 +25,7 @@ export const FieldTypeText: FC<{
}} }}
value={value || ""} value={value || ""}
disabled={field.disabled} disabled={field.disabled}
className="c-flex-1 c-bg-transparent c-outline-none c-px-2 c-text-sm" className="c-flex-1 c-bg-transparent c-outline-none c-px-2 c-text-sm c-w-full"
spellCheck={false} spellCheck={false}
onFocus={() => { onFocus={() => {
field.focused = true; field.focused = true;

View File

@ -9,7 +9,7 @@ import { PropTypeRelation } from "./field/type/TypeRelation";
export type FMProps = { export type FMProps = {
on_init: (arg: { fm: FMLocal; submit: any; reload: any }) => any; on_init: (arg: { fm: FMLocal; submit: any; reload: any }) => any;
on_load: (arg: { fm: FMLocal }) => any; on_load: (arg: { fm: FMLocal }) => any;
on_submit: (arg: { form: any; error: any }) => Promise<any>; on_submit: (arg: { fm: FMLocal; form: any; error: any }) => Promise<any>;
body: any; body: any;
form: FormHook; form: FormHook;
PassProp: any; PassProp: any;
@ -68,6 +68,7 @@ export type FMInternal = {
fields: Record<string, FieldLocal>; fields: Record<string, FieldLocal>;
field_def: Record<string, GFCol>; field_def: Record<string, GFCol>;
error: { error: {
readonly object: Record<string, string>;
readonly list: { name: string; error: string[] }[]; readonly list: { name: string; error: string[] }[];
set: (name: string, error: string[]) => void; set: (name: string, error: string[]) => void;
get: (name: string) => string[]; get: (name: string) => string[];

View File

@ -12,6 +12,13 @@ export const formError = (fm: FMLocal) => {
return res; return res;
}, },
get object() {
const result: any = {};
Object.entries(this._internal).map(([name, error]) => {
result[name] = error.join("\n");
});
return {};
},
clear(name) { clear(name) {
if (name) delete this._internal[name]; if (name) delete this._internal[name];
else this._internal = {}; else this._internal = {};

View File

@ -86,6 +86,12 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
return promise; return promise;
}; };
fm.submit = async () => {}; fm.submit = async () => {
fm.props.on_init({ fm, submit: fm.submit, reload: fm.reload }); if (typeof fm.props.on_submit === "function") {
fm.props.on_submit({ fm, form: fm.data, error: fm.error.object });
}
};
if (typeof fm.props.on_init === "function") {
fm.props.on_init({ fm, submit: fm.submit, reload: fm.reload });
}
}; };

View File

@ -24,6 +24,9 @@ export const gen_form = async (modify: (data: any) => void, data: any) => {
}; };
for (const r of f.relation.fields) { for (const r of f.relation.fields) {
select[f.name].select[r.name] = true; select[f.name].select[r.name] = true;
if (r.is_pk) {
pks[f.name] = r.name;
}
} }
} }

View File

@ -56,20 +56,6 @@ async (opt) => {
select: ${JSON.stringify(select, null, 2).split("\n").join("\n ")}, select: ${JSON.stringify(select, null, 2).split("\n").join("\n ")},
}); });
if (item){
for (const [k, v] of Object.entries(item)) {
${Object.entries(pks)
.map(([k, v]) => {
return `\
if (k === "${k}" && !(v as any)?.connect) {
if (v?.["${v}"]) item[k] = { connect: { ${v}: v?.["${v}"] } } as any;
else delete item[k];
}`;
})
.join("\n")}
}
}
${opt?.after_load ? opt?.after_load : ""} ${opt?.after_load ? opt?.after_load : ""}
return item; return item;

View File

@ -29,7 +29,22 @@ async ({ form, error }: IForm) => {
try { try {
const data = { ...form }; const data = { ...form };
delete data.${id}; delete data.${id};
if (data) {
const pks = ${JSON.stringify(pks)};
for (const [k, v] of Object.entries(pks)) {
if (typeof data[k] === 'object') {
if (data[k][v]) {
data[k] = {
connect: {
[v]: data[k][v]
}
}
}
}
}
}
if (form.${id}) { if (form.${id}) {
await db.${table}.update({ await db.${table}.update({
where: { where: {

View File

@ -38,6 +38,16 @@ export const newField = (arg: GFCol) => {
</div>`, </div>`,
jsBuilt: `render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, "") }), item["${arg.name}"]));`, jsBuilt: `render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, "") }), item["${arg.name}"]));`,
}, },
dim: {
h: "full",
w: "fit",
},
padding: {
l: 0,
b: 0,
t: 0,
r: 10,
},
layout: { layout: {
dir: "row", dir: "row",
align: "left", align: "left",

View File

@ -66,6 +66,7 @@ type SimplifiedItem = {
jsBuilt: string; jsBuilt: string;
}; };
padding?: any; padding?: any;
dim?: any;
layout?: any; layout?: any;
}; };
@ -121,10 +122,12 @@ export const createItem = (arg: SimplifiedItem): any => {
return { return {
id: createId(), id: createId(),
dim: { dim: arg.dim
h: "full", ? arg.dim
w: "full", : {
}, h: "full",
w: "full",
},
padding: arg.padding, padding: arg.padding,
layout: arg.layout, layout: arg.layout,
name: arg.name || "item", name: arg.name || "item",