fix(core): clear input value after selecting a tag in page info (#7076)

close AFF-1140
This commit is contained in:
JimmFly
2024-05-27 08:28:12 +00:00
parent cf42faa83b
commit 50dcce891b

View File

@@ -8,7 +8,7 @@ import { DeleteIcon, MoreHorizontalIcon, TagsIcon } from '@blocksuite/icons';
import { useLiveData, useService } from '@toeverything/infra'; import { useLiveData, useService } from '@toeverything/infra';
import clsx from 'clsx'; import clsx from 'clsx';
import type { HTMLAttributes, PropsWithChildren } from 'react'; import type { HTMLAttributes, PropsWithChildren } from 'react';
import { useCallback, useMemo, useReducer, useState } from 'react'; import { useCallback, useMemo, useReducer, useRef, useState } from 'react';
import { TagItem, TempTagItem } from '../../page-list'; import { TagItem, TempTagItem } from '../../page-list';
import { tagColors } from './common'; import { tagColors } from './common';
@@ -23,12 +23,15 @@ interface TagsEditorProps {
interface InlineTagsListProps interface InlineTagsListProps
extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'>, extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'>,
Omit<TagsEditorProps, 'onOptionsChange'> {} Omit<TagsEditorProps, 'onOptionsChange'> {
onRemove?: () => void;
}
const InlineTagsList = ({ const InlineTagsList = ({
pageId, pageId,
readonly, readonly,
children, children,
onRemove,
}: PropsWithChildren<InlineTagsListProps>) => { }: PropsWithChildren<InlineTagsListProps>) => {
const tagList = useService(TagService).tagList; const tagList = useService(TagService).tagList;
const tags = useLiveData(tagList.tags$); const tags = useLiveData(tagList.tags$);
@@ -45,6 +48,7 @@ const InlineTagsList = ({
? undefined ? undefined
: () => { : () => {
tag.untag(pageId); tag.untag(pageId);
onRemove?.();
}; };
return ( return (
<TagItem <TagItem
@@ -175,6 +179,7 @@ export const TagsEditor = ({ pageId, readonly }: TagsEditorProps) => {
const [inputValue, setInputValue] = useState(''); const [inputValue, setInputValue] = useState('');
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [selectedTagIds, setSelectedTagIds] = useState<string[]>([]); const [selectedTagIds, setSelectedTagIds] = useState<string[]>([]);
const inputRef = useRef<HTMLInputElement>(null);
const handleCloseModal = useCallback( const handleCloseModal = useCallback(
(open: boolean) => { (open: boolean) => {
@@ -214,6 +219,10 @@ export const TagsEditor = ({ pageId, readonly }: TagsEditorProps) => {
[pageId, tagIds, tags] [pageId, tagIds, tags]
); );
const focusInput = useCallback(() => {
inputRef.current?.focus();
}, []);
const [nextColor, rotateNextColor] = useReducer( const [nextColor, rotateNextColor] = useReducer(
color => { color => {
const idx = tagColors.findIndex(c => c[1] === color); const idx = tagColors.findIndex(c => c[1] === color);
@@ -234,6 +243,15 @@ export const TagsEditor = ({ pageId, readonly }: TagsEditorProps) => {
[nextColor, pageId, tagList] [nextColor, pageId, tagList]
); );
const onSelectTag = useCallback(
(id: string) => {
onAddTag(id);
setInputValue('');
focusInput();
},
[focusInput, onAddTag]
);
const onInputKeyDown = useCallback( const onInputKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => { (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
@@ -254,8 +272,13 @@ export const TagsEditor = ({ pageId, readonly }: TagsEditorProps) => {
return ( return (
<div data-testid="tags-editor-popup" className={styles.tagsEditorRoot}> <div data-testid="tags-editor-popup" className={styles.tagsEditorRoot}>
<div className={styles.tagsEditorSelectedTags}> <div className={styles.tagsEditorSelectedTags}>
<InlineTagsList pageId={pageId} readonly={readonly}> <InlineTagsList
pageId={pageId}
readonly={readonly}
onRemove={focusInput}
>
<input <input
ref={inputRef}
value={inputValue} value={inputValue}
onChange={onInputChange} onChange={onInputChange}
onKeyDown={onInputKeyDown} onKeyDown={onInputKeyDown}
@@ -282,7 +305,7 @@ export const TagsEditor = ({ pageId, readonly }: TagsEditorProps) => {
data-tag-id={tag.id} data-tag-id={tag.id}
data-tag-value={tag.value$} data-tag-value={tag.value$}
onClick={() => { onClick={() => {
onAddTag(tag.id); onSelectTag(tag.id);
}} }}
> >
<TagItem maxWidth="100%" tag={tag} mode="inline" /> <TagItem maxWidth="100%" tag={tag} mode="inline" />