Skip to content

Commit

Permalink
web: core/ListSearch fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed May 16, 2024
1 parent 53d2d1c commit 18dc8a6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions web/src/components/core/ListSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const search = (elements, term) => {
};

/**
* TODO: Rename
* TODO: Rename and/or refactor?
* Input field for searching in a given list of elements.
* @component
*
Expand All @@ -54,23 +54,32 @@ export default function ListSearch({
}) {
const [value, setValue] = useState("");
const [resultSize, setResultSize] = useState(elements.length);
const searchHandler = useDebounce(term => {
const result = search(elements, term);

const updateResult = (result) => {
setResultSize(result.length);
onChangeProp(result);
};

const searchHandler = useDebounce(term => {
updateResult(search(elements, term));
}, 500);

const onChange = (value) => {
setValue(value);
searchHandler(value);
};

const onClear = () => {
setValue("");
updateResult(elements);
};

return (
<SearchInput
placeholder={placeholder}
value={value}
onChange={(_, value) => onChange(value)}
onClear={() => onChangeProp(elements)}
onClear={onClear}
resultsCount={resultSize}
/>
);
Expand Down

0 comments on commit 18dc8a6

Please sign in to comment.