/** * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates * SPDX-License-Identifier: MIT */ import React, { useMemo } from 'react'; import { IJsonSchema } from '@flowgram.ai/json-schema'; import { I18n } from '@flowgram.ai/editor'; import { type TriggerRenderProps } from '@douyinfe/semi-ui/lib/es/treeSelect'; import { type TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree'; import { Popover, Tag, TreeSelect } from '@douyinfe/semi-ui'; import { IconChevronDownStroked, IconIssueStroked } from '@douyinfe/semi-icons'; import { createInjectMaterial } from '@/shared'; import { useVariableTree } from './use-variable-tree'; import { useVariableSelectorContext } from './context'; import './styles.css'; export interface VariableSelectorProps { value?: string[]; config?: { placeholder?: string; notFoundContent?: string; }; onChange: (value?: string[]) => void; includeSchema?: IJsonSchema | IJsonSchema[]; excludeSchema?: IJsonSchema | IJsonSchema[]; readonly?: boolean; hasError?: boolean; style?: React.CSSProperties; triggerRender?: (props: TriggerRenderProps) => React.ReactNode; } export { useVariableTree }; export const VariableSelector = ({ value, config = {}, onChange, style, readonly = false, includeSchema, excludeSchema, hasError, triggerRender, }: VariableSelectorProps) => { const { skipVariable } = useVariableSelectorContext(); const treeData = useVariableTree({ includeSchema, excludeSchema, skipVariable, }); const treeValue = useMemo(() => { if (typeof value === 'string') { console.warn( 'The Value of VariableSelector is a string, it should be an ARRAY. \n', 'Please check the value of VariableSelector \n' ); return value; } return value?.join('.'); }, [value]); const renderIcon = (icon: string | JSX.Element) => { if (typeof icon === 'string') { return ; } return icon; }; return ( <> { onChange((_config as TreeNodeData).keyPath as string[]); }} renderSelectedItem={(_option: TreeNodeData) => { if (!_option?.keyPath) { return ( } color="amber" closable={!readonly} onClose={() => onChange(undefined)} > {config?.notFoundContent ?? 'Undefined'} ); } const rootIcon = renderIcon(_option.rootMeta?.icon || _option?.icon); const rootTitle = ( {_option.rootMeta?.title ? `${_option.rootMeta?.title} ${_option.isRoot ? '' : '-'} ` : null} ); return ( {rootIcon} {rootTitle} {_option.keyPath.slice(1).join('.')} } > onChange(undefined)} > {rootTitle} {!_option.isRoot && ( {_option.label} )} ); }} showClear={false} arrowIcon={} triggerRender={triggerRender} placeholder={config?.placeholder ?? I18n.t('Select Variable')} /> > ); }; VariableSelector.renderKey = 'variable-selector-render-key'; export const InjectVariableSelector = createInjectMaterial(VariableSelector); export { VariableSelectorProvider } from './context';