This commit is contained in:
rizky 2024-11-22 06:26:59 -07:00
parent f458d4e46c
commit 8425b4ea95
5 changed files with 163 additions and 46 deletions

View File

@ -10,7 +10,7 @@ import { getPathname } from "lib/utils/pathname";
import { sofDeleteField as softDeleteField } from "lib/utils/soft-del-rel";
import { toast } from "../ui/toast";
const editorFormWidth = {} as Record<string, { w: number; f: any }>;
export const editorFormWidth = {} as Record<string, { w: number; f: any }>;
export type { FMLocal } from "./typings";

View File

@ -31,6 +31,20 @@ export const BaseField = (prop: {
const errors = fm.error.get(name);
const showlabel = arg.show_label || "y";
const disabled =
typeof field.disabled === "function" ? field.disabled() : field.disabled;
const show =
typeof field.hidden === "function"
? field.hidden()
: typeof field.hidden === "string"
? field.hidden === "n"
? false
: true
: typeof field.hidden === "boolean"
? field.hidden
: true;
return (
<label
className={cx(
@ -48,6 +62,16 @@ export const BaseField = (prop: {
w === "⅓" && "c-w-1/3",
w === "¼" && "c-w-1/4",
"c-flex-col c-space-y-1",
css`
.field-outer {
border: 1px solid ${disabled ? "#ececeb" : "#cecece"};
&.focused {
border: 1px solid #1c4ed8;
outline: 1px solid #1c4ed8;
}
}
`,
field.focused && "focused",
field.disabled && "disabled",
typeof fm.data[name] !== "undefined" &&
@ -56,8 +80,19 @@ export const BaseField = (prop: {
"filled"
)}
>
{arg.show_label !== "n" && <Label field={field} fm={fm} arg={arg}/>}
<div className="field-input c-flex c-flex-1 c-flex-col">
{arg.show_label !== "n" && <Label field={field} fm={fm} arg={arg} />}
<div
className={cx(
"field-input c-flex c-flex-1 c-flex-col",
errors.length > 0 &&
css`
.field-outer {
border-color: red !important;
background: #fff0f0;
}
`
)}
>
<div
className={cx(
!["toggle", "button", "radio", "checkbox"].includes(arg.sub_type)

View File

@ -1,8 +1,10 @@
import { useLocal } from "lib/utils/use-local";
import { ReactNode, useCallback, useEffect } from "react";
import { ReactNode, useCallback, useEffect, useRef } from "react";
import { FieldLocal, FieldProp, FMLocal } from "../typings";
import { BaseFormLocal, default_base_form_local } from "./types";
import { FieldLoading } from "lib/comps/ui/field-loading";
import { editorFormWidth } from "../Form";
import { ConsoleLogWriter } from "drizzle-orm";
export type BaseFormProps<T> = {
data: T;
@ -12,6 +14,7 @@ export type BaseFormProps<T> = {
render?: () => void;
on_change?: (fm: FMLocal, name: string, new_value: any) => any;
is_form?: boolean;
name: string;
};
export const BaseForm = <T extends Record<string, any>>(
props: BaseFormProps<T>
@ -107,10 +110,58 @@ export const BaseForm = <T extends Record<string, any>>(
};
};
useEffect(() => {
// form.data = data;
// form.render();
const ref = useRef({
el: null as null | HTMLFormElement,
timer: null as any,
rob: new ResizeObserver(async ([e]) => {
let fm = form.fm;
if (!fm) {
if (ref.current.timer) {
return;
}
ref.current.timer = await new Promise<void>((done) => {
const ival = setInterval(() => {
if (form.fm) {
ref.current.timer = null;
clearInterval(ival);
done();
}
}, 100);
});
fm = form.fm;
}
if (e.contentRect.width > 0 && fm) {
fm.size.height = e.contentRect.height;
fm.size.width = e.contentRect.width;
// if (fm.status === "ready" && !isEditor) fm.status = "resizing";
if (fm.props.layout === "auto" || !fm.props.layout) {
if (fm.size.width > 650) {
fm.size.field = "half";
} else {
fm.size.field = "full";
}
} else {
if (fm.props.layout === "1-col") fm.size.field = "full";
if (fm.props.layout === "2-col") fm.size.field = "half";
}
if (isEditor) {
editorFormWidth[props.name] = {
w: fm.size.width,
f: fm.size.field,
};
}
fm.status = "ready";
fm.render();
}
}),
});
useEffect(() => {
if (form.internal.width === 0) {
setTimeout(() => {
form.render();
@ -125,6 +176,7 @@ export const BaseForm = <T extends Record<string, any>>(
if (!form.fm) {
form.fm = form.createFm();
}
const fm = form.fm;
if (typeof props.is_form === "boolean") {
if (!props.is_form) {
@ -149,11 +201,25 @@ export const BaseForm = <T extends Record<string, any>>(
e.stopPropagation();
form.submit();
}}
ref={(el) => {
if (el) {
if (!ref.current.el && fm && fm?.status !== "resizing") {
ref.current.el = el;
ref.current.rob.observe(el);
if (fm.status === "ready") {
fm.status = "resizing";
fm.render();
}
}
}
}}
className={cx(
"form c-flex-1 c-flex c-flex-col c-w-full c-h-full c-relative c-overflow-auto",
className
)}
>
{fm?.status === "ready" && (
<>
<div
className={cx(
"form-inner c-flex-1 c-flex c-flex-row c-flex-wrap c-items-start c-content-start c-absolute c-inset-0",
@ -172,6 +238,8 @@ export const BaseForm = <T extends Record<string, any>>(
<>{typeof children === "function" ? children(form) : children}</>
)}
</div>
</>
)}
</form>
);
};

View File

@ -111,7 +111,9 @@ export const Field: FC<FieldProp> = (arg) => {
? field.hidden === "n"
? false
: true
: typeof field.hidden === "boolean"? field.hidden : true;
: typeof field.hidden === "boolean"
? field.hidden
: true;
if (!show) return <></>;
return (
@ -153,8 +155,21 @@ export const Field: FC<FieldProp> = (arg) => {
)}
ref={typeof arg.field_ref === "function" ? arg.field_ref : undefined}
>
{showlabel !== "n" && field.label && <Label field={field} fm={fm} arg={arg}/>}
<div className={cx("field-input c-flex c-flex-1 c-flex-col")}>
{showlabel !== "n" && field.label && (
<Label field={field} fm={fm} arg={arg} />
)}
<div
className={cx(
"field-input c-flex c-flex-1 c-flex-col",
errors.length > 0 &&
css`
.field-outer {
border-color: red !important;
background: #fff0f0;
}
`
)}
>
<FieldInput
field={field}
fm={fm}

View File

@ -1,8 +1,9 @@
import { GFCol, parseGenField } from "lib/gen/utils";
import { cn } from "lib/utils";
import { fields_map } from "lib/utils/format-value";
import { useLocal } from "lib/utils/use-local";
import { call_prasi_events } from "lib/utils/prasi-events";
import { set } from "lib/utils/set";
import { useLocal } from "lib/utils/use-local";
import get from "lodash.get";
import {
AlertTriangle,
@ -23,8 +24,7 @@ import DataGrid, {
ColumnOrColumnGroup,
RenderCellProps,
Row,
SELECT_COLUMN_KEY,
SortColumn,
SortColumn
} from "react-data-grid";
import "react-data-grid/lib/styles.css";
import { createPortal } from "react-dom";
@ -38,7 +38,6 @@ import { TLList } from "./TLList";
import { TLSlider } from "./TLSlider";
import { sortTree } from "./utils/sort-tree";
import { OnRowClick } from "./utils/type";
import { call_prasi_events } from "lib/utils/prasi-events";
let EMPTY_SET = new Set() as ReadonlySet<any>;