fix: add isDebounce prop to Field and TypeInput components for debounced input handling

This commit is contained in:
faisolavolut 2025-03-19 11:31:52 +07:00
parent b7cc6832dd
commit a6be768b68
2 changed files with 25 additions and 3 deletions

View File

@ -71,6 +71,7 @@ export interface FieldProps {
forceDisabled?: boolean; forceDisabled?: boolean;
description?: string | (() => any); description?: string | (() => any);
styleField?: string | null; styleField?: string | null;
isDebounce?: boolean;
} }
export const Field: React.FC<FieldProps> = ({ export const Field: React.FC<FieldProps> = ({
fm, fm,
@ -105,6 +106,7 @@ export const Field: React.FC<FieldProps> = ({
forceDisabled, forceDisabled,
description, description,
styleField, styleField,
isDebounce = false,
}) => { }) => {
let result = null; let result = null;
const field = useLocal({ const field = useLocal({
@ -475,6 +477,7 @@ export const Field: React.FC<FieldProps> = ({
fm={fm} fm={fm}
fields={initField} fields={initField}
name={name} name={name}
isDebounce={isDebounce}
placeholder={placeholder} placeholder={placeholder}
required={required} required={required}
type={type} type={type}

View File

@ -12,6 +12,8 @@ import MaskedInput from "../../ui/MaskedInput";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { getLabel } from "@/lib/utils/getLabel"; import { getLabel } from "@/lib/utils/getLabel";
import { time } from "@/lib/utils/date"; import { time } from "@/lib/utils/date";
import React from "react";
import debounce from "lodash.debounce";
export const TypeInput: React.FC<any> = ({ export const TypeInput: React.FC<any> = ({
name, name,
@ -24,7 +26,10 @@ export const TypeInput: React.FC<any> = ({
onChange, onChange,
className, className,
placeholderDate, placeholderDate,
isDebounce = false,
}) => { }) => {
const [inputValue, setInputValue] = useState(null as any);
const [hover, setHover] = useState(0); // State untuk menyimpan nilai hover const [hover, setHover] = useState(0); // State untuk menyimpan nilai hover
const textareaRef = useRef<HTMLTextAreaElement | null>(null); const textareaRef = useRef<HTMLTextAreaElement | null>(null);
@ -227,7 +232,6 @@ export const TypeInput: React.FC<any> = ({
asSingle={true} asSingle={true}
useRange={false} useRange={false}
onChange={(value) => { onChange={(value) => {
console.log(value);
fm.data[name] = value?.startDate fm.data[name] = value?.startDate
? new Date(value?.startDate) ? new Date(value?.startDate)
: null; : null;
@ -364,6 +368,15 @@ export const TypeInput: React.FC<any> = ({
type = "text"; type = "text";
} }
} }
const debouncedOnChange = React.useMemo(
() =>
debounce((event: React.ChangeEvent<HTMLInputElement>) => {
if (typeof onChange === "function") {
onChange(fm.data[name]);
}
}, 2000),
[]
);
return ( return (
<> <>
<Input <Input
@ -391,8 +404,14 @@ export const TypeInput: React.FC<any> = ({
onChange={(ev) => { onChange={(ev) => {
fm.data[name] = ev.currentTarget.value; fm.data[name] = ev.currentTarget.value;
fm.render(); fm.render();
if (typeof onChange === "function") { if (!isDebounce) {
onChange(fm.data[name]); if (typeof onChange === "function") {
onChange(fm.data[name]);
}
} else {
setInputValue(ev.currentTarget.value);
debouncedOnChange(inputValue);
return () => debouncedOnChange.cancel();
} }
}} }}
/> />