Commit 7a035882 authored by Joel's avatar Joel

feat: finish choose var

parent 62e2deaf
...@@ -39,6 +39,7 @@ const VarReferencePicker: FC<Props> = ({ ...@@ -39,6 +39,7 @@ const VarReferencePicker: FC<Props> = ({
className, className,
isShowNodeName, isShowNodeName,
value, value,
onChange,
}) => { }) => {
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const hasValue = value.length > 0 const hasValue = value.length > 0
...@@ -87,7 +88,13 @@ const VarReferencePicker: FC<Props> = ({ ...@@ -87,7 +88,13 @@ const VarReferencePicker: FC<Props> = ({
zIndex: 100, zIndex: 100,
minWidth: 227, minWidth: 227,
}}> }}>
<VarReferencePopup vars={mockNodeOutputVars} /> <VarReferencePopup
vars={mockNodeOutputVars}
onChange={(value) => {
onChange(value)
setOpen(false)
}}
/>
</PortalToFollowElemContent> </PortalToFollowElemContent>
</PortalToFollowElem> </PortalToFollowElem>
</div> </div>
......
...@@ -2,43 +2,63 @@ ...@@ -2,43 +2,63 @@
import type { FC } from 'react' import type { FC } from 'react'
import React, { useRef } from 'react' import React, { useRef } from 'react'
import { useHover } from 'ahooks' import { useHover } from 'ahooks'
import type { NodeOutPutVar, Var } from '@/app/components/workflow/types' import cn from 'classnames'
import type { NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types'
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development' import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
type ObjectChildrenProps = { type ObjectChildrenProps = {
nodeId: string
title: string title: string
data: Var[] data: Var[]
objPath: string[] objPath: string[]
onChange: (value: ValueSelector) => void
} }
type ItemProps = { type ItemProps = {
nodeId: string
title: string title: string
objPath: string[] objPath: string[]
itemData: Var itemData: Var
onChange: (value: ValueSelector) => void
} }
const Item: FC<ItemProps> = ({ const Item: FC<ItemProps> = ({
nodeId,
title, title,
objPath, objPath,
itemData, itemData,
onChange,
}) => { }) => {
const isObj = itemData.type === 'object' const isObj = itemData.type === 'object'
const itemRef = useRef(null) const itemRef = useRef(null)
const isItemHovering = useHover(itemRef) const isItemHovering = useHover(itemRef)
const handleChosen = (e: React.MouseEvent) => {
e.stopPropagation()
onChange([nodeId, ...objPath, itemData.variable])
}
return ( return (
<div ref={itemRef} className='relative flex items-center h-6 w-[252px] pl-3 pr-[18px] rounded-md cursor-pointer hover:bg-gray-50'> <div
ref={itemRef}
className={cn(isObj ? 'hover:bg-primary-50 pr-1' : 'hover:bg-gray-50 pr-[18px]', 'relative flex items-center h-6 w-[252px] pl-3 rounded-md cursor-pointer')}
onClick={handleChosen}
>
<div className='flex items-center w-0 grow'> <div className='flex items-center w-0 grow'>
<Variable02 className='shrink-0 w-3.5 h-3.5 text-primary-500' /> <Variable02 className='shrink-0 w-3.5 h-3.5 text-primary-500' />
<div className='ml-1 w-0 grow text-ellipsis text-[13px] font-normal text-gray-900'>{itemData.variable}</div> <div className='ml-1 w-0 grow text-ellipsis text-[13px] font-normal text-gray-900'>{itemData.variable}</div>
</div> </div>
<div className='ml-1 shrink-0 text-xs font-normal text-gray-500'>{itemData.type}</div> <div className='ml-1 shrink-0 text-xs font-normal text-gray-500'>{itemData.type}</div>
{isObj && (
<ChevronRight className='ml-0.5 w-3 h-3 text-gray-500' />
)}
{isObj && isItemHovering && ( {isObj && isItemHovering && (
// eslint-disable-next-line @typescript-eslint/no-use-before-define // eslint-disable-next-line @typescript-eslint/no-use-before-define
<ObjectChildren <ObjectChildren
nodeId={nodeId}
title={title} title={title}
objPath={[...objPath, itemData.variable]} objPath={[...objPath, itemData.variable]}
data={itemData.children as Var[]} data={itemData.children as Var[]}
onChange={onChange}
/> />
)} )}
</div> </div>
...@@ -47,8 +67,10 @@ const Item: FC<ItemProps> = ({ ...@@ -47,8 +67,10 @@ const Item: FC<ItemProps> = ({
const ObjectChildren: FC<ObjectChildrenProps> = ({ const ObjectChildren: FC<ObjectChildrenProps> = ({
title, title,
nodeId,
objPath, objPath,
data, data,
onChange,
}) => { }) => {
const currObjPath = objPath const currObjPath = objPath
...@@ -59,9 +81,11 @@ const ObjectChildren: FC<ObjectChildrenProps> = ({ ...@@ -59,9 +81,11 @@ const ObjectChildren: FC<ObjectChildrenProps> = ({
data.map((v, i) => ( data.map((v, i) => (
<Item <Item
key={i} key={i}
nodeId={nodeId}
title={title} title={title}
objPath={objPath} objPath={objPath}
itemData={v} itemData={v}
onChange={onChange}
/> />
)) ))
} }
...@@ -71,9 +95,12 @@ const ObjectChildren: FC<ObjectChildrenProps> = ({ ...@@ -71,9 +95,12 @@ const ObjectChildren: FC<ObjectChildrenProps> = ({
type Props = { type Props = {
vars: NodeOutPutVar[] vars: NodeOutPutVar[]
onChange: (value: ValueSelector) => void
} }
const VarReferencePopup: FC<Props> = ({ const VarReferencePopup: FC<Props> = ({
vars, vars,
onChange,
}) => { }) => {
return ( return (
<div className='p-1 bg-white rounded-lg border border-gray-200 shadow-lg space-y-1'> <div className='p-1 bg-white rounded-lg border border-gray-200 shadow-lg space-y-1'>
...@@ -84,8 +111,10 @@ const VarReferencePopup: FC<Props> = ({ ...@@ -84,8 +111,10 @@ const VarReferencePopup: FC<Props> = ({
<Item <Item
key={j} key={j}
title={item.title} title={item.title}
nodeId={item.nodeId}
objPath={[]} objPath={[]}
itemData={v} itemData={v}
onChange={onChange}
/> />
))} ))}
</div> </div>
......
...@@ -18,6 +18,7 @@ export const mockNodesData: Record<string, any> = { ...@@ -18,6 +18,7 @@ export const mockNodesData: Record<string, any> = {
export const mockNodeOutputVars: NodeOutPutVar[] = [ export const mockNodeOutputVars: NodeOutPutVar[] = [
{ {
nodeId: 'aaa',
title: 'Start', title: 'Start',
vars: [ vars: [
{ {
...@@ -31,6 +32,7 @@ export const mockNodeOutputVars: NodeOutPutVar[] = [ ...@@ -31,6 +32,7 @@ export const mockNodeOutputVars: NodeOutPutVar[] = [
], ],
}, },
{ {
nodeId: 'bbb',
title: 'LLM', title: 'LLM',
vars: [ vars: [
{ {
......
...@@ -90,6 +90,7 @@ export type Var = { ...@@ -90,6 +90,7 @@ export type Var = {
} }
export type NodeOutPutVar = { export type NodeOutPutVar = {
nodeId: string
title: string title: string
vars: Var[] vars: Var[]
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment