This commit is contained in:
rizky 2024-04-13 00:04:05 -07:00
parent 26c1402bd1
commit 0400830458
5 changed files with 72 additions and 43 deletions

View File

@ -12,7 +12,8 @@ export const RawDropdown: FC<{
onFocus?: () => void; onFocus?: () => void;
onBlur?: () => void; onBlur?: () => void;
onChange?: (value: string) => void; onChange?: (value: string) => void;
}> = ({ value, options, className, onFocus, onBlur, onChange }) => { disabled?: boolean;
}> = ({ value, options, className, onFocus, onBlur, onChange, disabled }) => {
const local = useLocal({ const local = useLocal({
open: false, open: false,
input: { input: {
@ -36,7 +37,7 @@ export const RawDropdown: FC<{
return ( return (
<Popover <Popover
open={local.open} open={disabled ? false : local.open}
onOpenChange={() => { onOpenChange={() => {
local.open = false; local.open = false;
local.render(); local.render();
@ -113,9 +114,11 @@ export const RawDropdown: FC<{
value={local.open ? local.input.value : "Halo"} value={local.open ? local.input.value : "Halo"}
className={cx( className={cx(
"c-absolute c-inset-0 c-w-full c-h-full c-outline-none c-p-0", "c-absolute c-inset-0 c-w-full c-h-full c-outline-none c-p-0",
local.open disabled
? "c-cursor-pointer" ? "c-invisible"
: "c-pointer-events-none c-invisible" : local.open
? "c-cursor-pointer"
: "c-pointer-events-none c-invisible"
)} )}
onChange={(e) => { onChange={(e) => {
local.input.value = e.currentTarget.value; local.input.value = e.currentTarget.value;
@ -139,7 +142,8 @@ export const RawDropdown: FC<{
<div <div
className={cx( className={cx(
"c-absolute c-pointer-events-none c-z-10 c-inset-0 c-left-auto c-flex c-items-center ", "c-absolute c-pointer-events-none c-z-10 c-inset-0 c-left-auto c-flex c-items-center ",
"c-bg-white c-justify-center c-w-6 c-mr-1 c-my-2" "c-bg-white c-justify-center c-w-6 c-mr-1 c-my-2",
disabled && "c-hidden"
)} )}
> >
<ChevronDown size={14} /> <ChevronDown size={14} />

View File

@ -1,36 +1,54 @@
import { useLocal } from "@/utils/use-local"; import { useLocal } from "@/utils/use-local";
import { FC } from "react"; import { FC, useEffect } from "react";
import { FMLocal, FieldLocal } from "../../typings"; import { FMLocal, FieldLocal } from "../../typings";
import { RawDropdown } from "../raw/Dropdown"; import { RawDropdown } from "../raw/Dropdown";
import { Loader2 } from "lucide-react";
export type PropTypeRelation = { export type PropTypeRelation = {
type: "has-one" | "has-many"; type: "has-one" | "has-many";
on_load: () => Promise<any[]>;
}; };
export const FieldTypeRelation: FC<{ export const FieldTypeRelation: FC<{
field: FieldLocal; field: FieldLocal;
fm: FMLocal; fm: FMLocal;
prop: PropTypeRelation; prop: PropTypeRelation;
}> = ({ field, fm, prop }) => { }> = ({ field, fm, prop }) => {
const input = useLocal({}); const input = useLocal({
list: null as null | any[],
});
const value = fm.data[field.name]; const value = fm.data[field.name];
field.input = input; field.input = input;
field.prop = prop; field.prop = prop;
useEffect(() => {
if (input.list === null) {
field.status = "loading";
field.render();
}
}, []);
return ( return (
<> <>
<RawDropdown {field.status === "loading" ? (
options={[{ label: "Halo", value: "halo" }]} <div className="c-w-full c-h-full c-items-center c-flex c-px-2">
value={"halo"} <Loader2 className="c-h-4 c-w-4 c-animate-spin" />
className="c-flex-1 c-bg-transparent c-outline-none c-px-2 c-text-sm c-w-full c-h-full" </div>
onFocus={() => { ) : (
field.focused = true; <RawDropdown
field.render(); options={[{ label: "Halo", value: "halo" }]}
}} value={"halo"}
onBlur={() => { className="c-flex-1 c-bg-transparent c-outline-none c-px-2 c-text-sm c-w-full c-h-full"
field.focused = false; disabled={field.disabled}
field.render(); onFocus={() => {
}} field.focused = true;
/> field.render();
}}
onBlur={() => {
field.focused = false;
field.render();
}}
/>
)}
</> </>
); );
}; };

View File

@ -156,5 +156,10 @@ export const formType = (active: { item_id: string }, meta: any) => {
}; };
}; };
props: any; props: any;
size: {
width: number;
height: number;
field: "full" | "half";
};
}`; }`;
}; };

View File

@ -51,15 +51,15 @@ export const gen_table_list = async (
if (data["child"]) { if (data["child"]) {
result["child"] = data["child"]; result["child"] = data["child"];
result["child"].content.childs = result["child"].content.childs.filter(
(e: any) => {
return e.name !== arg.mode;
}
);
let sub_name = "fields"; let sub_name = "fields";
if (arg.mode === "table") sub_name = "columns"; if (arg.mode === "table") sub_name = "columns";
result["child"].content.childs = result["child"].content.childs.filter(
(e: any) => {
return e.name !== sub_name;
}
);
const child = createItem({ const child = createItem({
name: sub_name, name: sub_name,
childs: fields childs: fields
@ -71,24 +71,28 @@ export const gen_table_list = async (
props: { props: {
name: e.name, name: e.name,
title: formatName(e.name), title: formatName(e.name),
child: { child: createItem({
name: "cell", childs: [
padding: { createItem({
l: 8, name: "cell",
b: 0, padding: {
t: 0, l: 8,
r: 8, b: 0,
}, t: 0,
adv: { r: 8,
js: `\ },
adv: {
js: `\
<div {...props} className={cx(props.className, "")}> <div {...props} className={cx(props.className, "")}>
<FormatValue value={col.value} name={col.name} gen_fields={gen_fields} /> <FormatValue value={col.value} name={col.name} gen_fields={gen_fields} />
</div>`, </div>`,
jsBuilt: `\ jsBuilt: `\
render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, "") }),React.createElement(FormatValue, { value: col.value, name: col.name, gen_fields: gen_fields }))); render(React.createElement("div", Object.assign({}, props, { className: cx(props.className, "") }),React.createElement(FormatValue, { value: col.value, name: col.name, gen_fields: gen_fields })));
`, `,
}, },
}, }),
],
}),
}, },
}, },
}; };

View File

@ -128,9 +128,7 @@ export const createItem = (arg: SimplifiedItem): any => {
name: arg.name || "item", name: arg.name || "item",
type: "item", type: "item",
component, component,
script: { adv: arg.adv,
...arg.adv,
},
childs: arg.childs?.map(createItem), childs: arg.childs?.map(createItem),
}; };
}; };