const { condition, positive, negative, once } = oper
const [frag, push] = buildCodeFragment()
- const conditionExpr: CodeFragment[] = [
+ const codes: CodeFragment[] = [
'() => (',
...genExpression(condition, context),
')',
let negativeArg: false | CodeFragment[] = false
if (negative) {
+ positiveArg.unshift(' ? ')
+ negativeArg = [' : ']
if (negative.type === IRNodeTypes.BLOCK) {
- negativeArg = genBlock(negative, context)
+ negativeArg.push(...genBlock(negative, context))
} else {
- negativeArg = ['() => ', ...genIf(negative!, context, true)]
+ negativeArg.push(...genIf(negative!, context, true))
}
+ } else {
+ positiveArg.unshift(' && (')
+ positiveArg.push(')')
}
- if (!isNested) push(NEWLINE, `const n${oper.id} = `)
- push(
- ...genCall(
- helper('createIf'),
- conditionExpr,
- positiveArg,
- negativeArg,
- once && 'true',
- ),
- )
+ codes.push(...positiveArg)
+ if (negativeArg) codes.push(...negativeArg)
+
+ if (isNested) {
+ push(...codes)
+ } else {
+ push(NEWLINE, `const n${oper.id} = `)
+ push(...genCall(helper('createIf'), codes, once && 'true'))
+ }
return frag
}
import { renderEffect } from './renderEffect'
export function createIf(
- condition: () => any,
- b1: BlockFn,
- b2?: BlockFn,
+ ifBlockFn: () => BlockFn,
once?: boolean,
// hydrationNode?: Node,
): DynamicFragment {
const frag = __DEV__ ? new DynamicFragment('if') : new DynamicFragment()
if (once) {
- frag.update(condition() ? b1 : b2)
+ frag.update(ifBlockFn())
} else {
- renderEffect(() => frag.update(condition() ? b1 : b2))
+ renderEffect(() => frag.update(ifBlockFn()))
}
return frag
}