diff --git a/frontend/src/components/PostFormTagsArea.tsx b/frontend/src/components/PostFormTagsArea.tsx index b5fac1a..c7775af 100644 --- a/frontend/src/components/PostFormTagsArea.tsx +++ b/frontend/src/components/PostFormTagsArea.tsx @@ -25,8 +25,8 @@ const getTokenAt = (value: string, pos: number) => { } -const replaceToken = (value: string, start: number, end: number, text: string) => ( - `${ value.slice (0, start) }${ text }${ value.slice (end) }`) +const replaceToken = (value: string, start: number, end: number, text: string) => + `${ value.slice (0, start) }${ text }${ value.slice (end) }` type Props = { @@ -38,16 +38,17 @@ export default (({ tags, setTags }: Props) => { const ref = useRef (null) const [bounds, setBounds] = useState<{ start: number; end: number }> ({ start: 0, end: 0 }) + const [focused, setFocused] = useState (false) const [suggestions, setSuggestions] = useState ([]) const [suggestionsVsbl, setSuggestionsVsbl] = useState (false) const handleTagSelect = (tag: Tag) => { setSuggestionsVsbl (false) const textarea = ref.current! - const newValue = replaceToken (tags, bounds.start, bounds.end, tag.name) + const newValue = replaceToken (tags, bounds.start, bounds.end, tag.name + ' ') setTags (newValue) requestAnimationFrame (async () => { - const p = bounds.start + tag.name.length + const p = bounds.start + tag.name.length + 1 textarea.selectionStart = textarea.selectionEnd = p textarea.focus () await recompute (p, newValue) @@ -56,14 +57,21 @@ export default (({ tags, setTags }: Props) => { const recompute = async (pos: number, v: string = tags) => { const { start, end, token } = getTokenAt (v, pos) + if (!(token.trim ())) + { + setSuggestionsVsbl (false) + return + } + setBounds ({ start, end }) - const data = await apiGet ('/tags/autocomplete', { params: { q: token } }) + + const data = await apiGet ('/tags/autocomplete', { params: { q: token, nico: '0' } }) setSuggestions (data.filter (t => t.postCount > 0)) setSuggestionsVsbl (suggestions.length > 0) } return ( -
+