Skip to content

Commit c5d148b

Browse files
fix #794 input bug (#801)
1 parent e5e86fc commit c5d148b

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

web/app/components/app/configuration/config-var/index.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import { DEFAULT_VALUE_MAX_LEN, getMaxVarNameLength } from '@/config'
1616
import { checkKeys, getNewVar } from '@/utils/var'
1717
import Switch from '@/app/components/base/switch'
1818
import Toast from '@/app/components/base/toast'
19+
import { Timeout } from 'ahooks/lib/useRequest/src/types'
1920

2021
export type IConfigVarProps = {
2122
promptVariables: PromptVariable[]
2223
readonly?: boolean
2324
onPromptVariablesChange?: (promptVariables: PromptVariable[]) => void
2425
}
2526

27+
let conflictTimer: Timeout
28+
2629
const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVariablesChange }) => {
2730
const { t } = useTranslation()
2831
const hasVar = promptVariables.length > 0
@@ -34,11 +37,9 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
3437
return obj
3538
})()
3639

37-
const updatePromptVariable = (key: string, updateKey: string, newValue: any) => {
38-
if (!(key in promptVariableObj))
39-
return
40-
const newPromptVariables = promptVariables.map((item) => {
41-
if (item.key === key) {
40+
const updatePromptVariable = (index: number, updateKey: string, newValue: any) => {
41+
const newPromptVariables = promptVariables.map((item, i) => {
42+
if (i === index) {
4243
return {
4344
...item,
4445
[updateKey]: newValue,
@@ -51,11 +52,9 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
5152
onPromptVariablesChange?.(newPromptVariables)
5253
}
5354

54-
const batchUpdatePromptVariable = (key: string, updateKeys: string[], newValues: any[]) => {
55-
if (!(key in promptVariableObj))
56-
return
57-
const newPromptVariables = promptVariables.map((item) => {
58-
if (item.key === key) {
55+
const batchUpdatePromptVariable = (index: number, updateKeys: string[], newValues: any[]) => {
56+
const newPromptVariables = promptVariables.map((item, i) => {
57+
if (i === index) {
5958
const newItem: any = { ...item }
6059
updateKeys.forEach((updateKey, i) => {
6160
newItem[updateKey] = newValues[i]
@@ -68,8 +67,8 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
6867

6968
onPromptVariablesChange?.(newPromptVariables)
7069
}
71-
7270
const updatePromptKey = (index: number, newKey: string) => {
71+
clearTimeout(conflictTimer)
7372
const { isValid, errorKey, errorMessageKey } = checkKeys([newKey], true)
7473
if (!isValid) {
7574
Toast.notify({
@@ -78,17 +77,28 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
7877
})
7978
return
8079
}
80+
8181
const newPromptVariables = promptVariables.map((item, i) => {
8282
if (i === index) {
8383
return {
8484
...item,
8585
key: newKey,
8686
}
8787
}
88-
8988
return item
9089
})
9190

91+
conflictTimer = setTimeout(() => {
92+
const isKeyExists = promptVariables.some(item => item.key.trim() === newKey.trim())
93+
if (isKeyExists) {
94+
Toast.notify({
95+
type: 'error',
96+
message: t(`appDebug.varKeyError.keyAlreadyExists`, { key: newKey }),
97+
})
98+
return
99+
}
100+
},1000)
101+
92102
onPromptVariablesChange?.(newPromptVariables)
93103
}
94104

@@ -196,7 +206,7 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
196206
type="text"
197207
placeholder={key}
198208
value={name}
199-
onChange={e => updatePromptVariable(key, 'name', e.target.value)}
209+
onChange={e => updatePromptVariable(index, 'name', e.target.value)}
200210
maxLength={getMaxVarNameLength(name)}
201211
className="h-6 leading-6 block w-full rounded-md border-0 py-1.5 text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-1 focus:ring-inset focus:ring-gray-200"
202212
/>)

web/i18n/lang/app-debug.en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const translation = {
121121
tooLong: 'Variable key: {{key}} too length. Can not be longer then 16 characters',
122122
notValid: 'Variable key: {{key}} is invalid. Can only contain letters, numbers, and underscores',
123123
notStartWithNumber: 'Variable key: {{key}} can not start with a number',
124+
keyAlreadyExists:'Variable key: :{{key}} already exists',
124125
},
125126
variableConig: {
126127
modalTitle: 'Field settings',

web/i18n/lang/app-debug.zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const translation = {
117117
tooLong: '变量: {{key}} 长度太长。不能超过 16 个字符',
118118
notValid: '变量: {{key}} 非法。只能包含英文字符,数字和下划线',
119119
notStartWithNumber: '变量: {{key}} 不能以数字开头',
120+
keyAlreadyExists:'变量:{{key}} 已存在',
120121
},
121122
variableConig: {
122123
modalTitle: '变量设置',

0 commit comments

Comments
 (0)