/**
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
* SPDX-License-Identifier: MIT
*/
import React, { useMemo } from 'react';
import { TriggerRenderProps } from '@douyinfe/semi-ui/lib/es/treeSelect';
import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
import { IconChevronDownStroked, IconIssueStroked } from '@douyinfe/semi-icons';
import { IJsonSchema } from '../../typings/json-schema';
import { useVariableTree } from './use-variable-tree';
import { UIRootTitle, UITag, UITreeSelect, UIVarName } from './styles';
interface PropTypes {
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 type VariableSelectorProps = PropTypes;
export { useVariableTree };
export const VariableSelector = ({
value,
config = {},
onChange,
style,
readonly = false,
includeSchema,
excludeSchema,
hasError,
triggerRender,
}: PropTypes) => {
const treeData = useVariableTree({ includeSchema, excludeSchema });
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'}
);
}
return (
onChange(undefined)}
>
{_option.rootMeta?.title ? `${_option.rootMeta?.title} -` : null}
{_option.label}
);
}}
showClear={false}
arrowIcon={}
triggerRender={triggerRender}
placeholder={config?.placeholder ?? 'Select Variable...'}
/>
>
);
};