mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-40): add onInputChange prop
This commit is contained in:
@@ -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}
|
||||||
|
|||||||
Reference in New Issue
Block a user