chore: edit filed exists

This commit is contained in:
Joel 2025-08-20 14:00:38 +08:00
parent d4f976270d
commit af6e5e8663
3 changed files with 39 additions and 4 deletions

View File

@ -19,7 +19,7 @@ type Props = {
className?: string
readonly: boolean
payload: InputVar
onChange?: (item: InputVar, moreInfo?: MoreInfo) => void
onChange?: (item: InputVar, moreInfo?: MoreInfo) => boolean
onRemove?: () => void
rightContent?: React.JSX.Element
varKeys?: string[]
@ -31,7 +31,7 @@ const VarItem: FC<Props> = ({
className,
readonly,
payload,
onChange = noop,
onChange = () => true,
onRemove = noop,
rightContent,
varKeys = [],
@ -48,7 +48,9 @@ const VarItem: FC<Props> = ({
}] = useBoolean(false)
const handlePayloadChange = useCallback((payload: InputVar, moreInfo?: MoreInfo) => {
onChange(payload, moreInfo)
const isValid = onChange(payload, moreInfo)
if(!isValid)
return
hideEditVarModal()
}, [onChange, hideEditVarModal])
return (

View File

@ -9,6 +9,8 @@ import { v4 as uuid4 } from 'uuid'
import { ReactSortable } from 'react-sortablejs'
import { RiDraggable } from '@remixicon/react'
import cn from '@/utils/classnames'
import { hasDuplicateStr } from '@/utils/var'
import Toast from '@/app/components/base/toast'
type Props = {
readonly: boolean
@ -28,7 +30,27 @@ const VarList: FC<Props> = ({
const newList = produce(list, (draft) => {
draft[index] = payload
})
let errorMsgKey = ''
let typeName = ''
if(hasDuplicateStr(newList.map(item => item.variable))) {
errorMsgKey = 'appDebug.varKeyError.keyAlreadyExists'
typeName = 'appDebug.variableConfig.varName'
}
if(hasDuplicateStr(newList.map(item => item.label as string))) {
errorMsgKey = 'appDebug.varKeyError.keyAlreadyExists'
typeName = 'appDebug.variableConfig.labelName'
}
if (errorMsgKey) {
Toast.notify({
type: 'error',
message: t(errorMsgKey, { key: t(typeName) }),
})
return false
}
onChange(newList, moreInfo ? { index, payload: moreInfo } : undefined)
return true
}
}, [list, onChange])

View File

@ -45,7 +45,7 @@ export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput)
}
}
export const checkKey = (key: string, canBeEmpty?: boolean) => {
export const checkKey = (key: string, canBeEmpty?: boolean, keys?: string[]) => {
if (key.length === 0 && !canBeEmpty)
return 'canNoBeEmpty'
@ -82,6 +82,17 @@ export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
return { isValid, errorKey, errorMessageKey }
}
export const hasDuplicateStr = (strArr: string[]) => {
const strObj: Record<string, number> = {}
strArr.forEach((str) => {
if (strObj[str])
strObj[str] += 1
else
strObj[str] = 1
})
return !!Object.keys(strObj).find(key => strObj[key] > 1)
}
const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/g
export const getVars = (value: string) => {
if (!value)