feat: to add tag when tag input is unfocus (#21555)

This commit is contained in:
CorrectRoad
2025-06-27 10:36:01 +08:00
committed by GitHub
parent fd41645f95
commit 7d9d670f90
2 changed files with 28 additions and 16 deletions

View File

@@ -1,6 +1,5 @@
import { useState } from 'react' import { useCallback, useState } from 'react'
import type { ChangeEvent, FC, KeyboardEvent } from 'react' import type { ChangeEvent, FC, KeyboardEvent } from 'react'
import { } from 'use-context-selector'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import AutosizeInput from 'react-18-input-autosize' import AutosizeInput from 'react-18-input-autosize'
import { RiAddLine, RiCloseLine } from '@remixicon/react' import { RiAddLine, RiCloseLine } from '@remixicon/react'
@@ -40,17 +39,17 @@ const TagInput: FC<TagInputProps> = ({
onChange(copyItems) onChange(copyItems)
} }
const handleKeyDown = (e: KeyboardEvent) => { const handleNewTag = useCallback((value: string) => {
if (isSpecialMode && e.key === 'Enter')
setValue(`${value}`)
if (e.key === customizedConfirmKey) {
if (isSpecialMode)
e.preventDefault()
const valueTrimmed = value.trim() const valueTrimmed = value.trim()
if (!valueTrimmed || (items.find(item => item === valueTrimmed))) if (!valueTrimmed) {
notify({ type: 'error', message: t('datasetDocuments.segment.keywordEmpty') })
return return
}
if ((items.find(item => item === valueTrimmed))) {
notify({ type: 'error', message: t('datasetDocuments.segment.keywordDuplicate') })
return
}
if (valueTrimmed.length > 20) { if (valueTrimmed.length > 20) {
notify({ type: 'error', message: t('datasetDocuments.segment.keywordError') }) notify({ type: 'error', message: t('datasetDocuments.segment.keywordError') })
@@ -61,11 +60,22 @@ const TagInput: FC<TagInputProps> = ({
setTimeout(() => { setTimeout(() => {
setValue('') setValue('')
}) })
}, [items, onChange, notify, t])
const handleKeyDown = (e: KeyboardEvent) => {
if (isSpecialMode && e.key === 'Enter')
setValue(`${value}`)
if (e.key === customizedConfirmKey) {
if (isSpecialMode)
e.preventDefault()
handleNewTag(value)
} }
} }
const handleBlur = () => { const handleBlur = () => {
setValue('') handleNewTag(value)
setFocused(false) setFocused(false)
} }

View File

@@ -355,7 +355,9 @@ const translation = {
newChildChunk: 'New Child Chunk', newChildChunk: 'New Child Chunk',
keywords: 'KEYWORDS', keywords: 'KEYWORDS',
addKeyWord: 'Add keyword', addKeyWord: 'Add keyword',
keywordEmpty: 'The keyword cannot be empty',
keywordError: 'The maximum length of keyword is 20', keywordError: 'The maximum length of keyword is 20',
keywordDuplicate: 'The keyword already exists',
characters_one: 'character', characters_one: 'character',
characters_other: 'characters', characters_other: 'characters',
hitCount: 'Retrieval count', hitCount: 'Retrieval count',