Skip to content

Commit

Permalink
fix: proper use of type from ui builder backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kpranoto-aws committed Dec 21, 2021
1 parent ce2c57c commit 018f0e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function CustomButton(
<Button
color=\\"#ff0000\\"
width={20}
isDisabled={true}
{...rest}
{...getOverrideProps(overrides, \\"Button\\")}
></Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"width": {
"type": "Number",
"value": "20"
},
"isDisabled": {
"type": "boolean",
"value": "true"
}
}
}
37 changes: 20 additions & 17 deletions packages/codegen-ui-react/lib/react-component-render-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,32 @@ export function buildFixedJsxExpression(prop: FixedStudioComponentProperty): Str
case 'string':
switch (type) {
case undefined:
return factory.createStringLiteral(value as string);
case 'String':
case 'string':
return factory.createStringLiteral(value as string);
case 'Object':
case 'Number':
case 'Boolean':
default:
try {
const parsedValue = JSON.parse(value as string);

if (typeof parsedValue === 'number') {
return factory.createJsxExpression(undefined, factory.createNumericLiteral(parsedValue, undefined));
if (type && typeof parsedValue !== type.toLowerCase()) {
throw new Error(`Parsed value type "${typeof parsedValue}" and type "${type}" mismatch`);
}
if (typeof parsedValue === 'boolean') {
return factory.createJsxExpression(undefined, parsedValue ? factory.createTrue() : factory.createFalse());
switch (typeof parsedValue) {
case 'number':
return factory.createJsxExpression(undefined, factory.createNumericLiteral(parsedValue, undefined));
case 'boolean':
return factory.createJsxExpression(
undefined,
parsedValue ? factory.createTrue() : factory.createFalse(),
);
// object, array, and null
case 'object':
return factory.createJsxExpression(undefined, jsonToLiteral(parsedValue));
default:
throw new Error(`Unsupported value type ${parsedValue} for "${value}"`);
}
// object, array, and null
if (typeof parsedValue === 'object') {
return factory.createJsxExpression(undefined, jsonToLiteral(parsedValue));
}
} catch {} // eslint-disable-line no-empty
throw new Error(`Failed to parse value "${value}" as type ${type}`);
default:
throw new Error(`Invalid type ${type} for "${value}"`);
} catch {
throw new Error(`Failed to parse value "${value}" as type ${type}`);
}
}
default:
throw new Error(`Invalid type ${typeof value} for "${value}"`);
Expand Down

0 comments on commit 018f0e5

Please sign in to comment.