Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
UIDLAttributeValue,
UIDLEventHandlerStatement,
UIDLElementNode,
UIDLExpressionValue,
UIDLDynamicReference,
} from '@teleporthq/teleport-types'
import { HTMLTemplateGenerationParams, HTMLTemplateSyntax } from './types'
import { createHTMLNode } from '../../builders/hast-builders'
Expand Down Expand Up @@ -195,7 +197,7 @@ const createConditional = (
const stringifyConditionalExpression = (
identifier: string,
operation: string,
value: string | number | boolean
value: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue
) => {
if (typeof value === 'boolean') {
return `${value ? '' : '!'}${identifier}`
Expand All @@ -205,5 +207,17 @@ const stringifyConditionalExpression = (
return `${identifier} ${operation} '${value}'`
}

if (typeof value === 'number') {
return `${identifier} ${operation} ${value}`
}

if (value.type === 'dynamic') {
return `${identifier} ${operation} ${value.content.id}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For dynamic. If i remember correct we need to use options.dynamicPrefix to prefix the value with props by default or with a different name when needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually thinking that we don't even need to treat this flow. In what code generation pipeline is it used? For non React frameworks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even for reach framework too, in content.id we store only the id value. So, using dynamicPrefix we can get generated code like props.id

}

if (value.type === 'expr') {
return `${identifier} ${operation} ${value.content}`
}

return `${identifier} ${operation} ${value}`
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
convertToBinaryOperator,
convertToUnaryOperator,
convertValueToLiteral,
getExpressionFromUIDLExpressionNode,
} from '../../utils/ast-utils'
import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
import {
Expand Down Expand Up @@ -284,7 +285,7 @@ export const createConditionalJSXExpression = (
export const createBinaryExpression = (
condition: {
operation: string
operand?: string | number | boolean
operand?: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue
},
conditionalIdentifier: ConditionalIdentifier,
t = types
Expand All @@ -308,6 +309,28 @@ export const createBinaryExpression = (
}

if (operand !== undefined) {
if (typeof operand === 'object' && 'type' in operand && operand.type === 'expr') {
const exprIdentifier = getExpressionFromUIDLExpressionNode(operand)

return t.binaryExpression(convertToBinaryOperator(operation), identifier, exprIdentifier)
}

if (typeof operand === 'object' && 'type' in operand && operand.type === 'dynamic') {
const dynamicValueIdentifier = createDynamicValueExpression(operand, {
dynamicReferencePrefixMap: {
prop: 'props',
state: '',
local: '',
},
})

return t.binaryExpression(
convertToBinaryOperator(operation),
identifier,
dynamicValueIdentifier
)
}

const stateValueIdentifier = convertValueToLiteral(operand, conditionalIdentifier.type)

return t.binaryExpression(convertToBinaryOperator(operation), identifier, stateValueIdentifier)
Expand Down
2 changes: 1 addition & 1 deletion packages/teleport-types/src/uidl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export interface UIDLConditionalNode {
export interface UIDLConditionalExpression {
conditions: Array<{
operation: string
operand?: string | boolean | number
operand?: string | boolean | number | UIDLDynamicReference | UIDLExpressionValue
}>
// In the code generation phase, we are only supporting 'all' or '||'
// Maybe the type checking for this can be improved.
Expand Down
7 changes: 6 additions & 1 deletion packages/teleport-uidl-validator/src/decoders/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,12 @@ export const conditionalNodeDecoder: Decoder<VUIDLConditionalNode> = object({
condition: optional(
object({
conditions: array(
object({ operation: string(), operand: optional(union(string(), number(), boolean())) })
object({
operation: string(),
operand: optional(
union(string(), number(), boolean(), dynamicValueDecoder, expressionValueDecoder)
),
})
),
matchingCriteria: optional(string()),
})
Expand Down