From 73225b166f366bbe5f33b2e6c74a0d02bbe9e995 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Sat, 4 Nov 2023 13:13:07 +0600 Subject: [PATCH] refresh tag autocomplete on each symbol --- src/components/tags/TagAutocomplete/index.tsx | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/components/tags/TagAutocomplete/index.tsx b/src/components/tags/TagAutocomplete/index.tsx index 5a89915b..02570c6c 100644 --- a/src/components/tags/TagAutocomplete/index.tsx +++ b/src/components/tags/TagAutocomplete/index.tsx @@ -1,4 +1,11 @@ -import React, { useCallback, useEffect, useMemo, useRef, useState, VFC } from 'react'; +import React, { + useCallback, + useEffect, + useMemo, + useRef, + useState, + VFC, +} from 'react'; import classNames from 'classnames'; import { usePopper } from 'react-popper'; @@ -29,9 +36,11 @@ const TagAutocomplete: VFC = ({ const [categories, tags] = useMemo( () => separateTagOptions( - options.slice(0, 7).filter(option => option !== search && !exclude.includes(option)) + options + .slice(0, 7) + .filter((option) => option !== search && !exclude.includes(option)), ), - [options, search, exclude] + [options, search, exclude], ); const scroll = useRef(null); const wrapper = useRef(null); @@ -52,7 +61,9 @@ const TagAutocomplete: VFC = ({ }); const onKeyDown = useCallback( - event => { + (event) => { + pop?.update?.(); + const all = [...categories, ...tags]; switch (event.key) { @@ -69,7 +80,7 @@ const TagAutocomplete: VFC = ({ onSelect(selected >= 0 ? all[selected] : search); } }, - [setSelected, selected, categories, tags, onSelect, search] + [pop, categories, tags, selected, onSelect, search], ); useEffect(() => { @@ -83,7 +94,10 @@ const TagAutocomplete: VFC = ({ const { scrollTop, clientHeight } = scroll.current; const { offsetTop } = el; - if (clientHeight - scrollTop + el.clientHeight < offsetTop || offsetTop < scrollTop) { + if ( + clientHeight - scrollTop + el.clientHeight < offsetTop || + offsetTop < scrollTop + ) { scroll.current.scrollTo(0, el.offsetTop - el.clientHeight); } }, [selected]); @@ -95,7 +109,11 @@ const TagAutocomplete: VFC = ({ style={pop.styles.popper} {...pop.attributes.popper} > -
+