chore: use new add sub variables

This commit is contained in:
Joel 2024-09-24 11:59:33 +08:00
parent afc9630cd0
commit 66953d57a2
4 changed files with 50 additions and 24 deletions

View File

@ -289,20 +289,26 @@ type PortalSelectProps = {
onSelect: (value: Item) => void onSelect: (value: Item) => void
items: Item[] items: Item[]
placeholder?: string placeholder?: string
renderTrigger?: (value?: Item) => JSX.Element | null
triggerClassName?: string triggerClassName?: string
triggerClassNameFn?: (open: boolean) => string triggerClassNameFn?: (open: boolean) => string
popupClassName?: string popupClassName?: string
popupInnerClassName?: string
readonly?: boolean readonly?: boolean
hideChecked?: boolean
} }
const PortalSelect: FC<PortalSelectProps> = ({ const PortalSelect: FC<PortalSelectProps> = ({
value, value,
onSelect, onSelect,
items, items,
placeholder, placeholder,
renderTrigger,
triggerClassName, triggerClassName,
triggerClassNameFn, triggerClassNameFn,
popupClassName, popupClassName,
popupInnerClassName,
readonly, readonly,
hideChecked,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
@ -317,6 +323,9 @@ const PortalSelect: FC<PortalSelectProps> = ({
offset={4} offset={4}
> >
<PortalToFollowElemTrigger onClick={() => !readonly && setOpen(v => !v)} className='w-full'> <PortalToFollowElemTrigger onClick={() => !readonly && setOpen(v => !v)} className='w-full'>
{renderTrigger
? renderTrigger(selectedItem)
: (
<div <div
className={classNames(` className={classNames(`
flex items-center justify-between px-2.5 h-9 rounded-lg border-0 bg-gray-100 text-sm ${readonly ? 'cursor-not-allowed' : 'cursor-pointer'} flex items-center justify-between px-2.5 h-9 rounded-lg border-0 bg-gray-100 text-sm ${readonly ? 'cursor-not-allowed' : 'cursor-pointer'}
@ -333,10 +342,12 @@ const PortalSelect: FC<PortalSelectProps> = ({
</span> </span>
<ChevronDownIcon className='shrink-0 h-4 w-4 text-gray-400' /> <ChevronDownIcon className='shrink-0 h-4 w-4 text-gray-400' />
</div> </div>
)}
</PortalToFollowElemTrigger> </PortalToFollowElemTrigger>
<PortalToFollowElemContent className={`z-20 ${popupClassName}`}> <PortalToFollowElemContent className={`z-20 ${popupClassName}`}>
<div <div
className='px-1 py-1 max-h-60 overflow-auto rounded-md bg-white text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm' className={classNames('px-1 py-1 max-h-60 overflow-auto rounded-md bg-white text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm', popupInnerClassName)}
> >
{items.map((item: Item) => ( {items.map((item: Item) => (
<div <div
@ -357,7 +368,7 @@ const PortalSelect: FC<PortalSelectProps> = ({
> >
{item.name} {item.name}
</span> </span>
{item.value === value && ( {!hideChecked && item.value === value && (
<CheckIcon className='shrink-0 h-4 w-4 text-text-accent' /> <CheckIcon className='shrink-0 h-4 w-4 text-text-accent' />
)} )}
</div> </div>

View File

@ -12,10 +12,12 @@ import type { CaseItem, HandleAddCondition, HandleAddSubVariableCondition, Handl
import type { Node, NodeOutPutVar, Var } from '../../../types' import type { Node, NodeOutPutVar, Var } from '../../../types'
import { VarType } from '../../../types' import { VarType } from '../../../types'
import { useGetAvailableVars } from '../../variable-assigner/hooks' import { useGetAvailableVars } from '../../variable-assigner/hooks'
import { SUB_VARIABLES } from '../default'
import ConditionList from './condition-list' import ConditionList from './condition-list'
import ConditionAdd from './condition-add' import ConditionAdd from './condition-add'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { PortalSelect as Select } from '@/app/components/base/select'
type Props = { type Props = {
isSubVariable?: boolean isSubVariable?: boolean
@ -73,6 +75,11 @@ const ConditionWrap: FC<Props> = ({
return varPayload.type === VarType.number return varPayload.type === VarType.number
}, []) }, [])
const subVarOptions = SUB_VARIABLES.map(item => ({
name: item,
value: item,
}))
return ( return (
<> <>
<ReactSortable <ReactSortable
@ -151,14 +158,22 @@ const ConditionWrap: FC<Props> = ({
)}> )}>
{isSubVariable {isSubVariable
? ( ? (
<Select
popupInnerClassName='w-[165px] max-h-none'
onSelect={value => handleAddSubVariableCondition?.(caseId!, conditionId!, value.value as string)}
items={subVarOptions}
value=''
renderTrigger={() => (
<Button <Button
size='small' size='small'
disabled={readOnly} disabled={readOnly}
onClick={() => handleAddSubVariableCondition?.(caseId!, conditionId!)}
> >
<RiAddLine className='mr-1 w-3.5 h-3.5' /> <RiAddLine className='mr-1 w-3.5 h-3.5' />
{t('workflow.nodes.ifElse.addSubVariable')} {t('workflow.nodes.ifElse.addSubVariable')}
</Button> </Button>
)}
hideChecked
/>
) )
: ( : (
<ConditionAdd <ConditionAdd

View File

@ -64,7 +64,7 @@ export type HandleRemoveCondition = (caseId: string, conditionId: string) => voi
export type HandleUpdateCondition = (caseId: string, conditionId: string, newCondition: Condition) => void export type HandleUpdateCondition = (caseId: string, conditionId: string, newCondition: Condition) => void
export type HandleToggleConditionLogicalOperator = (caseId: string) => void export type HandleToggleConditionLogicalOperator = (caseId: string) => void
export type HandleAddSubVariableCondition = (caseId: string, conditionId: string) => void export type HandleAddSubVariableCondition = (caseId: string, conditionId: string, key?: string) => void
export type handleRemoveSubVariableCondition = (caseId: string, conditionId: string, subConditionId: string) => void export type handleRemoveSubVariableCondition = (caseId: string, conditionId: string, subConditionId: string) => void
export type HandleUpdateSubVariableCondition = (caseId: string, conditionId: string, subConditionId: string, newSubCondition: Condition) => void export type HandleUpdateSubVariableCondition = (caseId: string, conditionId: string, subConditionId: string, newSubCondition: Condition) => void
export type HandleToggleSubVariableConditionLogicalOperator = (caseId: string, conditionId: string) => void export type HandleToggleSubVariableConditionLogicalOperator = (caseId: string, conditionId: string) => void

View File

@ -180,7 +180,7 @@ const useConfig = (id: string, payload: IfElseNodeType) => {
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [inputs, setInputs])
const handleAddSubVariableCondition = useCallback<HandleAddSubVariableCondition>((caseId: string, conditionId: string) => { const handleAddSubVariableCondition = useCallback<HandleAddSubVariableCondition>((caseId: string, conditionId: string, key?: string) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
const condition = draft.cases?.find(item => item.case_id === caseId)?.conditions.find(item => item.id === conditionId) const condition = draft.cases?.find(item => item.case_id === caseId)?.conditions.find(item => item.id === conditionId)
if (!condition) if (!condition)
@ -199,7 +199,7 @@ const useConfig = (id: string, payload: IfElseNodeType) => {
subVarCondition.conditions.push({ subVarCondition.conditions.push({
id: uuid4(), id: uuid4(),
key: '', key: key || '',
varType: VarType.string, varType: VarType.string,
comparison_operator: undefined, comparison_operator: undefined,
value: '', value: '',