From 5b1dab286036614ed257d1707c77dd4287a6d536 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Sat, 4 Oct 2025 14:53:05 +0700 Subject: [PATCH] feat(FE-40): add onInputChange prop --- src/components/input/SelectInput.tsx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/components/input/SelectInput.tsx b/src/components/input/SelectInput.tsx index 5b6ae098..930b5ed5 100644 --- a/src/components/input/SelectInput.tsx +++ b/src/components/input/SelectInput.tsx @@ -1,8 +1,9 @@ 'use client'; -import { ComponentType, ReactNode, useMemo } from 'react'; -import Select, { OptionProps, GroupBase } from 'react-select'; +import { ComponentType, ReactNode, useEffect, useMemo, useState } from 'react'; +import Select, { OptionProps, GroupBase, InputActionMeta } from 'react-select'; import makeAnimated from 'react-select/animated'; +import { useDebounce } from 'use-debounce'; import { cn } from '@/lib/helper'; @@ -41,6 +42,8 @@ interface SelectInputProps { errorMessage?: string; isAnimated?: boolean; openMenu?: boolean; + delay?: number; + onInputChange?: (search: string) => void; } const animatedComponents = makeAnimated(); @@ -65,7 +68,13 @@ const SelectInput = ({ errorMessage, isAnimated = true, openMenu, + delay = 300, + onInputChange, }: SelectInputProps) => { + const [internalInputValue, setInternalInputValue] = useState(''); + + const [debouncedInputValue] = useDebounce(internalInputValue, delay ?? 300); + const components = useMemo(() => { const base = isAnimated ? animatedComponents : {}; @@ -75,6 +84,14 @@ const SelectInput = ({ }; }, [isAnimated]); + const internalInputChangeHandler = (value: string, meta: InputActionMeta) => { + if (meta.action === 'input-change') setInternalInputValue(value); + if (meta.action === 'menu-close') setInternalInputValue(''); + }; + + useEffect(() => { + onInputChange?.(debouncedInputValue); + }, [debouncedInputValue]); return (
({ onChange={(val) => onChange?.(val as T)} options={options} menuIsOpen={openMenu} + inputValue={internalInputValue} + onInputChange={internalInputChangeHandler} isMulti={isMulti} isDisabled={isDisabled} isLoading={isLoading}