import { memo, useCallback, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiArrowDownSLine } from '@remixicon/react' import { capitalize } from 'lodash-es' import { useBoolean } from 'ahooks' import { VarType as NumberVarType } from '../../tool/types' import VariableTag from '../../_base/components/variable-tag' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Button from '@/app/components/base/button' import cn from '@/utils/classnames' import VarReferenceVars from '@/app/components/workflow/nodes/_base/components/variable/var-reference-vars' import type { NodeOutPutVar, ValueSelector, } from '@/app/components/workflow/types' import { VarType } from '@/app/components/workflow/types' import { variableTransformer } from '@/app/components/workflow/utils' import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development' const options = [ NumberVarType.variable, NumberVarType.constant, ] type ConditionNumberInputProps = { numberVarType?: NumberVarType onNumberVarTypeChange: (v: NumberVarType) => void value: string onValueChange: (v: string) => void variables: NodeOutPutVar[] isShort?: boolean unit?: string } const ConditionNumberInput = ({ numberVarType = NumberVarType.constant, onNumberVarTypeChange, value, onValueChange, variables, isShort, unit, }: ConditionNumberInputProps) => { const { t } = useTranslation() const [numberVarTypeVisible, setNumberVarTypeVisible] = useState(false) const [variableSelectorVisible, setVariableSelectorVisible] = useState(false) const [isFocus, { setTrue: setFocus, setFalse: setBlur, }] = useBoolean() const handleSelectVariable = useCallback((valueSelector: ValueSelector) => { onValueChange(variableTransformer(valueSelector) as string) setVariableSelectorVisible(false) }, [onValueChange]) return (
setNumberVarTypeVisible(v => !v)}>
{ options.map(option => (
{ onNumberVarTypeChange(option) setNumberVarTypeVisible(false) }} > {capitalize(option)}
)) }
{ numberVarType === NumberVarType.variable && ( setVariableSelectorVisible(v => !v)}> { value && ( ) } { !value && (
{t('workflow.nodes.ifElse.selectVariable')}
) }
) } { numberVarType === NumberVarType.constant && (
onValueChange(e.target.value)} placeholder={t('workflow.nodes.ifElse.enterValue') || ''} onFocus={setFocus} onBlur={setBlur} /> {!isFocus && unit &&
{unit}
}
) }
) } export default memo(ConditionNumberInput)