Skip to content

fix: Boolean/number props not getting parsed correctly #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Changes from 3 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
33 changes: 32 additions & 1 deletion packages/core/src/extensions/Blocks/api/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,38 @@ export function propsToAttributes<
// Props are displayed in kebab-case as HTML attributes. If a prop's
// value is the same as its default, we don't display an HTML
// attribute for it.
parseHTML: (element) => element.getAttribute(camelToDataKebab(name)),
parseHTML: (element) => {
const value = element.getAttribute(camelToDataKebab(name));

if (value === null) {
return null;
}

if (typeof spec.default === "boolean") {
if (value === "true") {
return true;
}

if (value === "false") {
return false;
}

return null;
}

if (typeof spec.default === "number") {
const isNumeric =
!Number.isNaN(parseFloat(value)) && Number.isFinite(Number(value));

if (isNumeric) {
return parseFloat(value);
}

return null;
}

return value;
},
renderHTML: (attributes) =>
attributes[name] !== spec.default
? {
Expand Down