fix: http attr key rerender

This commit is contained in:
Joel 2024-04-02 13:54:57 +08:00
parent ef39fa3fb2
commit 8be04b57f9
5 changed files with 39 additions and 8 deletions

View File

@ -69,7 +69,7 @@ const KeyValueList: FC<Props> = ({
{ {
list.map((item, index) => ( list.map((item, index) => (
<KeyValueItem <KeyValueItem
key={item.key + index} key={item.id}
nodeId={nodeId} nodeId={nodeId}
payload={item} payload={item}
onChange={handleChange(index)} onChange={handleChange(index)}

View File

@ -1,16 +1,30 @@
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import { useBoolean } from 'ahooks' import { useBoolean } from 'ahooks'
import { uniqueId } from 'lodash'
import type { KeyValue } from '../types' import type { KeyValue } from '../types'
const UNIQUE_ID_PREFIX = 'key-value-'
const strToKeyValueList = (value: string) => { const strToKeyValueList = (value: string) => {
return value.split('\n').map((item) => { return value.split('\n').map((item) => {
const [key, value] = item.split(':') const [key, value] = item.split(':')
return { key: key.trim(), value: value?.trim() } return {
id: uniqueId(UNIQUE_ID_PREFIX),
key: key.trim(),
value: value?.trim(),
}
}) })
} }
const useKeyValueList = (value: string, onChange: (value: string) => void, noFilter?: boolean) => { const useKeyValueList = (value: string, onChange: (value: string) => void, noFilter?: boolean) => {
const [list, setList] = useState<KeyValue[]>(value ? strToKeyValueList(value) : []) const [list, doSetList] = useState<KeyValue[]>(value ? strToKeyValueList(value) : [])
const setList = (l: KeyValue[]) => {
doSetList(l.map((item) => {
return {
...item,
id: item.id || uniqueId(UNIQUE_ID_PREFIX),
}
}))
}
useEffect(() => { useEffect(() => {
if (noFilter) if (noFilter)
return return
@ -21,8 +35,12 @@ const useKeyValueList = (value: string, onChange: (value: string) => void, noFil
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [list, noFilter]) }, [list, noFilter])
const addItem = useCallback(() => { const addItem = useCallback(() => {
setList(prev => [...prev, { key: '', value: '' }]) setList([...list, {
}, []) id: uniqueId(UNIQUE_ID_PREFIX),
key: '',
value: '',
}])
}, [list])
const [isKeyValueEdit, { const [isKeyValueEdit, {
toggle: toggleIsKeyValueEdit, toggle: toggleIsKeyValueEdit,

View File

@ -18,6 +18,7 @@ export enum BodyType {
} }
export type KeyValue = { export type KeyValue = {
id?: string
key: string key: string
value: string value: string
} }

View File

@ -28,6 +28,7 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
inputs, inputs,
toolInputVarSchema, toolInputVarSchema,
setInputVar, setInputVar,
handleOnVarOpen,
filterVar, filterVar,
toolSettingSchema, toolSettingSchema,
toolSettingValue, toolSettingValue,
@ -83,6 +84,7 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
onChange={setInputVar} onChange={setInputVar}
filterVar={filterVar} filterVar={filterVar}
isSupportConstantValue isSupportConstantValue
onOpen={handleOnVarOpen}
/> />
</Field> </Field>
)} )}

View File

@ -94,10 +94,19 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
}, [inputs, setInputs]) }, [inputs, setInputs])
const filterVar = useCallback((varPayload: Var) => { const [currVarIndex, setCurrVarIndex] = useState(-1)
return [VarVarType.string, VarVarType.number].includes(varPayload.type) const currVarType = toolInputVarSchema[currVarIndex]?._type
const handleOnVarOpen = useCallback((index: number) => {
setCurrVarIndex(index)
}, []) }, [])
const filterVar = useCallback((varPayload: Var) => {
if (currVarType)
return varPayload.type === currVarType
return varPayload.type !== VarVarType.arrayFile
}, [currVarType])
const isLoading = currTool && (isBuiltIn ? !currCollection : false) const isLoading = currTool && (isBuiltIn ? !currCollection : false)
// single run // single run
@ -144,7 +153,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const varInputs = getInputVars(hadVarParams.map((p) => { const varInputs = getInputVars(hadVarParams.map((p) => {
if (p.type === VarType.variable) if (p.type === VarType.variable)
return `#${[p.value as ValueSelector].join('.')}#` return `{{#${[p.value as ValueSelector].join('.')}#}}`
return p.value as string return p.value as string
})) }))
@ -176,6 +185,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
setToolSettingValue, setToolSettingValue,
toolInputVarSchema, toolInputVarSchema,
setInputVar, setInputVar,
handleOnVarOpen,
filterVar, filterVar,
currCollection, currCollection,
isShowAuthBtn, isShowAuthBtn,