feat(FE-40): add onInputChange prop

This commit is contained in:
ValdiANS
2025-10-04 14:53:05 +07:00
parent 8ed12578b4
commit 5b1dab2860
+21 -2
View File
@@ -1,8 +1,9 @@
'use client'; 'use client';
import { ComponentType, ReactNode, useMemo } from 'react'; import { ComponentType, ReactNode, useEffect, useMemo, useState } from 'react';
import Select, { OptionProps, GroupBase } from 'react-select'; import Select, { OptionProps, GroupBase, InputActionMeta } from 'react-select';
import makeAnimated from 'react-select/animated'; import makeAnimated from 'react-select/animated';
import { useDebounce } from 'use-debounce';
import { cn } from '@/lib/helper'; import { cn } from '@/lib/helper';
@@ -41,6 +42,8 @@ interface SelectInputProps<T = OptionType> {
errorMessage?: string; errorMessage?: string;
isAnimated?: boolean; isAnimated?: boolean;
openMenu?: boolean; openMenu?: boolean;
delay?: number;
onInputChange?: (search: string) => void;
} }
const animatedComponents = makeAnimated(); const animatedComponents = makeAnimated();
@@ -65,7 +68,13 @@ const SelectInput = <T extends OptionType>({
errorMessage, errorMessage,
isAnimated = true, isAnimated = true,
openMenu, openMenu,
delay = 300,
onInputChange,
}: SelectInputProps) => { }: SelectInputProps) => {
const [internalInputValue, setInternalInputValue] = useState('');
const [debouncedInputValue] = useDebounce(internalInputValue, delay ?? 300);
const components = useMemo(() => { const components = useMemo(() => {
const base = isAnimated ? animatedComponents : {}; const base = isAnimated ? animatedComponents : {};
@@ -75,6 +84,14 @@ const SelectInput = <T extends OptionType>({
}; };
}, [isAnimated]); }, [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 ( return (
<div <div
className={cn( className={cn(
@@ -110,6 +127,8 @@ const SelectInput = <T extends OptionType>({
onChange={(val) => onChange?.(val as T)} onChange={(val) => onChange?.(val as T)}
options={options} options={options}
menuIsOpen={openMenu} menuIsOpen={openMenu}
inputValue={internalInputValue}
onInputChange={internalInputChangeHandler}
isMulti={isMulti} isMulti={isMulti}
isDisabled={isDisabled} isDisabled={isDisabled}
isLoading={isLoading} isLoading={isLoading}