From 9df6372ea8d4fdebefcdab04a324cb7a481aeda3 Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 24 Jun 2024 03:46:05 -0700 Subject: [PATCH] fix --- comps/custom/ShowHidePanel.tsx | 4 +- comps/form/field/Field.tsx | 2 +- comps/form/field/type/TypeCheckbox.tsx | 1 - comps/form/field/type/TypeDropdown.tsx | 14 +- comps/form/field/type/TypeMoney.tsx | 4 +- comps/form/field/type/TypeSingleCheckbox.tsx | 76 ++++++++++ comps/form/field/type/TypeSingleOption.tsx | 5 +- comps/form/field/type/TypeToggle.tsx | 21 ++- comps/list/TableList.tsx | 152 +++++++++++++++---- gen/prop/gen_prop_fields.ts | 12 +- 10 files changed, 241 insertions(+), 50 deletions(-) create mode 100755 comps/form/field/type/TypeSingleCheckbox.tsx diff --git a/comps/custom/ShowHidePanel.tsx b/comps/custom/ShowHidePanel.tsx index 1ee9be7..0f83ddb 100755 --- a/comps/custom/ShowHidePanel.tsx +++ b/comps/custom/ShowHidePanel.tsx @@ -7,7 +7,8 @@ export const ShowHidePanel: FC<{ body: ReactNode; open: string; PassProp: any; -}> = ({ head, body, open, PassProp }) => { + on_init: (e?: any) => void +}> = ({ head, body, open, PassProp, on_init}) => { const local = useLocal( { open: true, @@ -22,6 +23,7 @@ export const ShowHidePanel: FC<{ useEffect(() => { local.open = open === "true" ? true : false; local.render(); + // on_init(local); }, [open]); return ( diff --git a/comps/form/field/Field.tsx b/comps/form/field/Field.tsx index 974bbfb..31da4e5 100755 --- a/comps/form/field/Field.tsx +++ b/comps/form/field/Field.tsx @@ -38,7 +38,7 @@ export const Field: FC = (arg) => { className={cx( "field", "c-flex", - css` + type === "single-option" && sub_type === "checkbox" ? css`padding: 5px 0px 0px 7.5px;` : css` padding: 5px 0px 0px 10px; `, w === "auto" && fm.size.field === "full" && "c-w-full", diff --git a/comps/form/field/type/TypeCheckbox.tsx b/comps/form/field/type/TypeCheckbox.tsx index 010890f..09044c7 100755 --- a/comps/form/field/type/TypeCheckbox.tsx +++ b/comps/form/field/type/TypeCheckbox.tsx @@ -8,7 +8,6 @@ export const FieldCheckbox: FC<{ fm: FMLocal; arg: FieldProp; }> = ({ field, fm, arg }) => { - // console.log({field, fm, arg}) const local = useLocal({ list: [] as any[], }); diff --git a/comps/form/field/type/TypeDropdown.tsx b/comps/form/field/type/TypeDropdown.tsx index 5c0efc3..3a658c6 100755 --- a/comps/form/field/type/TypeDropdown.tsx +++ b/comps/form/field/type/TypeDropdown.tsx @@ -26,20 +26,22 @@ export const TypeDropdown: FC<{ useEffect(() => { if (typeof arg.on_load === "function") { const options = arg.on_load({}); + // console.log(field.name, {options}) if (options instanceof Promise) { options.then((res) => { + // console.log(field.name, {res}) if (Array.isArray(res)) { const list: any = res.map((e: any) => { return { label: arg.opt_get_label(e), value: e.value, + data: e.data }; }); local.options = list; } else { local.options = res; } - if ( field.type === "single-option" && !value && @@ -53,6 +55,14 @@ export const TypeDropdown: FC<{ options: local.options, selected: [local.options[0]?.value], }); + } else if (field.type === "single-option" && value) { + arg.opt_set_value({ + fm, + name: field.name, + type: field.type, + options: local.options, + selected: [value], + }); } local.loaded = true; @@ -60,7 +70,7 @@ export const TypeDropdown: FC<{ }); } else { local.loaded = true; - local.options = []; + local.options = Array.isArray(options) ? options : [] as any; local.render(); } } diff --git a/comps/form/field/type/TypeMoney.tsx b/comps/form/field/type/TypeMoney.tsx index 264c198..7bef551 100755 --- a/comps/form/field/type/TypeMoney.tsx +++ b/comps/form/field/type/TypeMoney.tsx @@ -40,11 +40,11 @@ export const FieldMoney: FC<{ type={"number"} onClick={() => {}} onChange={(ev) => { - fm.data[field.name] = ev.currentTarget.value; + fm.data[field.name] = Number(ev.currentTarget.value); fm.render(); if (field.on_change) { field.on_change({ - value: fm.data[field.name], + value: Number(fm.data[field.name]), name: field.name, fm, }); diff --git a/comps/form/field/type/TypeSingleCheckbox.tsx b/comps/form/field/type/TypeSingleCheckbox.tsx new file mode 100755 index 0000000..6f9c1a4 --- /dev/null +++ b/comps/form/field/type/TypeSingleCheckbox.tsx @@ -0,0 +1,76 @@ +import { useLocal } from "@/utils/use-local"; +import get from "lodash.get"; +import { FC, useEffect } from "react"; +import { FMLocal, FieldLocal, FieldProp } from "../../typings"; + +export const FieldSingleCheckbox: FC<{ + field: FieldLocal; + fm: FMLocal; + arg: FieldProp; +}> = ({ field, fm, arg }) => { + const local = useLocal({ + list: [] as any[], + }); + useEffect(() => { + const callback = (res: any[]) => { + if (Array.isArray(res)) { + const list: any = res.map((e: any) => { + return { + label: arg.opt_get_label(e), + value: e.value, + }; + }); + local.list = list; + } else { + local.list = []; + } + local.render(); + }; + const res = arg.on_load({}); + if (res instanceof Promise) res.then(callback); + else callback(res); + }, []); + let value: boolean =fm.data[field.name]; + return ( + <> +
+
+
{ + fm.data[field.name] = !value; + fm.render(); + }} + className="c-flex c-flex-row c-space-x-1 cursor-pointer c-items-center rounded-full p-0.5" + > + {value ? ( + + + + ) : ( + + + + )} +
+
+
+ + ); +}; diff --git a/comps/form/field/type/TypeSingleOption.tsx b/comps/form/field/type/TypeSingleOption.tsx index d805c41..87ebe41 100755 --- a/comps/form/field/type/TypeSingleOption.tsx +++ b/comps/form/field/type/TypeSingleOption.tsx @@ -4,6 +4,7 @@ import { FieldButton } from "./TypeButton"; import { TypeDropdown } from "./TypeDropdown"; import { FieldRadio } from "./TypeRadio"; import { FieldToggle } from "./TypeToggle"; +import { FieldSingleCheckbox } from "./TypeSingleCheckbox"; export const SingleOption: FC<{ field: FieldLocal; @@ -20,7 +21,9 @@ export const SingleOption: FC<{ ) : arg.sub_type === "radio" ? ( - ) : ( + ) : arg.sub_type === "checkbox" ? ( + + ) :( <> )} diff --git a/comps/form/field/type/TypeToggle.tsx b/comps/form/field/type/TypeToggle.tsx index 5af5807..c2b4677 100755 --- a/comps/form/field/type/TypeToggle.tsx +++ b/comps/form/field/type/TypeToggle.tsx @@ -11,7 +11,9 @@ export const FieldToggle: FC<{ const local = useLocal({ list: [] as any[], value: [] as any[], + ref: null as any, }); + useEffect(() => { const callback = (res: any[]) => { local.list = res; @@ -30,12 +32,21 @@ export const FieldToggle: FC<{ options: local.list, type: field.type, }); - let checked = local.value.indexOf(value) > 0 ? true : false; + let checked = + typeof value === "boolean" + ? value + : local.value.indexOf(value) > 0 + ? true + : false; return ( <>
-