2022-01-28 05:20:03 +01:00
|
|
|
// @flow
|
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-03-01 03:20:29 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2022-01-28 05:20:03 +01:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import useDebounce from 'effects/use-debounce';
|
2022-03-01 03:20:29 +01:00
|
|
|
import classnames from 'classnames';
|
2022-01-28 05:20:03 +01:00
|
|
|
|
|
|
|
const FILTER_DEBOUNCE_MS = 300;
|
|
|
|
|
|
|
|
interface Props {
|
2022-01-28 05:25:31 +01:00
|
|
|
defaultValue?: string;
|
2022-01-28 05:20:03 +01:00
|
|
|
icon?: string;
|
|
|
|
placeholder?: string;
|
2022-03-01 03:20:29 +01:00
|
|
|
inline?: boolean;
|
2022-01-28 05:20:03 +01:00
|
|
|
onChange: (newValue: string) => any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function DebouncedInput(props: Props) {
|
2022-03-01 03:20:29 +01:00
|
|
|
const { defaultValue = '', icon, placeholder = '', inline, onChange } = props;
|
2022-01-28 05:25:31 +01:00
|
|
|
const [rawValue, setRawValue] = useState(defaultValue);
|
2022-01-28 05:20:03 +01:00
|
|
|
const debouncedValue: string = useDebounce(rawValue, FILTER_DEBOUNCE_MS);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
onChange(debouncedValue);
|
|
|
|
}, [onChange, debouncedValue]);
|
|
|
|
|
|
|
|
return (
|
2022-03-01 03:20:29 +01:00
|
|
|
<div className={classnames({ wunderbar: !inline, 'wunderbar--inline': inline })}>
|
2022-01-28 05:20:03 +01:00
|
|
|
{icon && <Icon icon={icon} />}
|
2022-03-01 03:20:29 +01:00
|
|
|
<FormField
|
|
|
|
className={classnames({ wunderbar__input: !inline, 'wunderbar__input--inline': inline })}
|
|
|
|
type="text"
|
|
|
|
name="debounced_search"
|
2022-01-28 05:20:03 +01:00
|
|
|
spellCheck={false}
|
|
|
|
placeholder={placeholder}
|
|
|
|
value={rawValue}
|
|
|
|
onChange={(e) => setRawValue(e.target.value.trim())}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|