diff --git a/components/form/Field.tsx b/components/form/Field.tsx index 82cb548..4e28c37 100644 --- a/components/form/Field.tsx +++ b/components/form/Field.tsx @@ -71,6 +71,7 @@ export interface FieldProps { forceDisabled?: boolean; description?: string | (() => any); styleField?: string | null; + isDebounce?: boolean; } export const Field: React.FC = ({ fm, @@ -105,6 +106,7 @@ export const Field: React.FC = ({ forceDisabled, description, styleField, + isDebounce = false, }) => { let result = null; const field = useLocal({ @@ -475,6 +477,7 @@ export const Field: React.FC = ({ fm={fm} fields={initField} name={name} + isDebounce={isDebounce} placeholder={placeholder} required={required} type={type} diff --git a/components/form/field/TypeInput.tsx b/components/form/field/TypeInput.tsx index 189101c..0e7d190 100644 --- a/components/form/field/TypeInput.tsx +++ b/components/form/field/TypeInput.tsx @@ -12,6 +12,8 @@ import MaskedInput from "../../ui/MaskedInput"; import { cn } from "@/lib/utils"; import { getLabel } from "@/lib/utils/getLabel"; import { time } from "@/lib/utils/date"; +import React from "react"; +import debounce from "lodash.debounce"; export const TypeInput: React.FC = ({ name, @@ -24,7 +26,10 @@ export const TypeInput: React.FC = ({ onChange, className, placeholderDate, + isDebounce = false, }) => { + const [inputValue, setInputValue] = useState(null as any); + const [hover, setHover] = useState(0); // State untuk menyimpan nilai hover const textareaRef = useRef(null); @@ -227,7 +232,6 @@ export const TypeInput: React.FC = ({ asSingle={true} useRange={false} onChange={(value) => { - console.log(value); fm.data[name] = value?.startDate ? new Date(value?.startDate) : null; @@ -364,6 +368,15 @@ export const TypeInput: React.FC = ({ type = "text"; } } + const debouncedOnChange = React.useMemo( + () => + debounce((event: React.ChangeEvent) => { + if (typeof onChange === "function") { + onChange(fm.data[name]); + } + }, 2000), + [] + ); return ( <> = ({ onChange={(ev) => { fm.data[name] = ev.currentTarget.value; fm.render(); - if (typeof onChange === "function") { - onChange(fm.data[name]); + if (!isDebounce) { + if (typeof onChange === "function") { + onChange(fm.data[name]); + } + } else { + setInputValue(ev.currentTarget.value); + debouncedOnChange(inputValue); + return () => debouncedOnChange.cancel(); } }} />