2026-06-22 18:33:39 +08:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import { OptionCombobox } from "@/components/option-combobox"
|
|
|
|
|
|
2026-06-27 21:27:57 +08:00
|
|
|
import {
|
2026-06-29 18:37:31 +08:00
|
|
|
buildVariableOption,
|
2026-06-27 21:27:57 +08:00
|
|
|
createRefValue,
|
|
|
|
|
refField,
|
|
|
|
|
refNodeId,
|
|
|
|
|
type WorkflowVariableRef,
|
|
|
|
|
type WorkflowVariableSelector,
|
|
|
|
|
} from "./workflow-utils"
|
2026-06-22 18:33:39 +08:00
|
|
|
|
|
|
|
|
export function VariableSelector({
|
|
|
|
|
value,
|
|
|
|
|
variables,
|
|
|
|
|
onChange,
|
2026-06-27 21:27:57 +08:00
|
|
|
placeholder = "选择变量",
|
2026-06-28 19:25:58 +08:00
|
|
|
triggerClassName,
|
2026-06-22 18:33:39 +08:00
|
|
|
}: {
|
|
|
|
|
value?: WorkflowVariableSelector
|
|
|
|
|
variables: WorkflowVariableRef[]
|
|
|
|
|
onChange: (value: WorkflowVariableSelector) => void
|
2026-06-27 21:27:57 +08:00
|
|
|
placeholder?: string
|
2026-06-28 19:25:58 +08:00
|
|
|
triggerClassName?: string
|
2026-06-22 18:33:39 +08:00
|
|
|
}) {
|
2026-06-27 21:27:57 +08:00
|
|
|
const selected = value ? `${refNodeId(value)}.${refField(value)}` : ""
|
2026-06-29 18:37:31 +08:00
|
|
|
const options = variables.map(buildVariableOption)
|
2026-06-22 18:33:39 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<OptionCombobox
|
2026-06-27 21:27:57 +08:00
|
|
|
value={selected}
|
2026-06-22 18:33:39 +08:00
|
|
|
options={options}
|
2026-06-27 21:27:57 +08:00
|
|
|
placeholder={placeholder}
|
2026-06-28 19:25:58 +08:00
|
|
|
triggerClassName={triggerClassName}
|
2026-06-27 21:27:57 +08:00
|
|
|
onChange={(next) => {
|
|
|
|
|
const variable = variables.find((item) => `${item.nodeId}.${item.field}` === next)
|
|
|
|
|
if (variable) {
|
|
|
|
|
onChange(createRefValue(variable.nodeId, variable.field))
|
|
|
|
|
}
|
2026-06-22 18:33:39 +08:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|