This commit is contained in:
rizky 2024-08-13 03:42:11 -07:00
parent 8ec266d47c
commit d14e07a1fc
7 changed files with 67 additions and 7 deletions

View File

@ -24,6 +24,7 @@ export const Form: FC<FMProps> = (props) => {
reload: async () => {
formReload(fm);
},
save_status: "init",
fields: {},
events: {
on_change(name: string, new_value: any) {},
@ -81,7 +82,9 @@ export const Form: FC<FMProps> = (props) => {
fm.soft_delete.field = null;
}
}
}, []);
const ref = useRef({
el: null as null | HTMLFormElement,
rob: new ResizeObserver(([e]) => {

View File

@ -6,6 +6,7 @@ import { useField } from "../utils/use-field";
import { validate } from "../utils/validate";
import { FieldInput } from "./FieldInput";
import { Label } from "./Label";
import { hashSum } from "lib/utils/hash-sum";
export const Field: FC<FieldProp> = (arg) => {
const showlabel = arg.show_label || "y";
@ -19,7 +20,33 @@ export const Field: FC<FieldProp> = (arg) => {
const w = field.width;
useEffect(() => {
if (fm.save_status === "init" || fm.status !== "ready") return;
if (local.prev_val === undefined) {
if (typeof fm.data[name] === "object") {
const sfied = hashSum(fm.data[name]);
if (sfied !== local.prev_val) {
local.prev_val = sfied;
} else {
return;
}
} else {
local.prev_val = fm.data[name];
}
return;
}
if (local.prev_val !== fm.data[name]) {
if (typeof fm.data[name] === "object") {
const sfied = hashSum(fm.data[name]);
if (sfied !== local.prev_val) {
local.prev_val = sfied;
} else {
return;
}
} else {
local.prev_val = fm.data[name];
}
if (
(!local.prev_val && fm.data[name]) ||
(local.prev_val && !fm.data[name])
@ -30,6 +57,9 @@ export const Field: FC<FieldProp> = (arg) => {
if (arg.on_change) {
arg.on_change({ value: fm.data[name], name, fm });
}
fm.save_status = "unsaved";
if (!fm.events) {
fm.events = {
on_change(name, new_value) {},

View File

@ -5,19 +5,30 @@ import { useEffect, useRef } from "react";
export const KeyValue = ({
value,
onChange,
index,
}: {
value: any;
onChange: (val: any) => void;
index?: "preserve" | "auto-sort";
}) => {
const local = useLocal({
entries: Object.entries(value),
entries: [] as [string, string][],
new: { idx: -1, key: "", value: "" },
});
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
let entries: any[] = [];
if (index === "preserve") {
if (Array.isArray(value)) {
local.entries = value;
} else {
local.entries = Object.entries(value);
}
}
if (local.entries.length > 0) {
Object.entries(value).forEach(([k, v], idx) => {
entries.forEach(([k, v], idx) => {
const found = local.entries.find((e) => {
if (e[0] === k) return true;
return false;
@ -30,8 +41,9 @@ export const KeyValue = ({
}
});
} else {
local.entries = Object.entries(value);
local.entries = entries;
}
local.render();
}, [value]);
@ -87,7 +99,11 @@ export const KeyValue = ({
local.render();
}}
onBlur={() => {
if (index === "preserve") {
onChange([...local.entries]);
} else {
onChange(reverseEntries(local.entries));
}
}}
/>
))}
@ -106,7 +122,11 @@ export const KeyValue = ({
local.new.key = "";
local.new.value = "";
local.render();
if (index === "preserve") {
onChange([...local.entries]);
} else {
onChange(reverseEntries(local.entries));
}
setTimeout(() => {
(
ref?.current?.querySelector(
@ -186,7 +206,7 @@ const KVRow = ({
onChange={(e) => {
update(idx, k, e.currentTarget.value || "");
}}
onKeyDown={(e) => {
onKeyUp={(e) => {
if (e.key === "Backspace" && !e.currentTarget.value) {
keyref.current?.focus();
}

View File

@ -217,6 +217,7 @@ export const FieldTypeInput: FC<{
case "key-value":
return (
<KeyValue
index={field.prop.kv?.index}
value={
Object.keys(fm.data[field.name] || {}).length === 0
? field.prop.kv?.default

View File

@ -65,6 +65,8 @@ ${
data: v,
fk: rel.fk,
});
} else {
record[k] = v;
}
} else {
record[k] = v;

View File

@ -48,7 +48,7 @@ export type FieldProp = {
label: string;
desc?: string;
props?: any;
kv?: { default: any };
kv?: { default: any; index: "preserve" | "auto-sort" };
upload?: {
mode: "single-file" | "multi-file";
accept: string;
@ -137,6 +137,7 @@ export type FMInternal = {
events: {
on_change: (name: string, new_value: any) => void;
};
save_status: "init" | "unsaved" | "saved";
fields: Record<string, FieldLocal>;
field_def: Record<string, GFCol>;
error: {

View File

@ -23,6 +23,8 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
const toastSuccess = (opt: { addNewText: string }) => {
const md = fm.deps.md as MDLocal;
fm.save_status = "saved";
if (md) {
toast.success(
<div
@ -184,6 +186,7 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
}
fm.data = result;
fm.save_status = "saved";
if (result === undefined) fm.data = {};