Skip to content

Commit

Permalink
Fix serializing conditional fields in content components in `fields.m…
Browse files Browse the repository at this point in the history
…arkdoc` where the value is `undefined` (#1252)
  • Loading branch information
emmatown committed Jul 30, 2024
1 parent c8d9539 commit 0c1ed9b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-dolphins-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Fix serializing conditional fields in content components in `fields.markdoc` where the value is `undefined`
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ const schema = createEditorSchema(
something: fields.text({ label: 'Something' }),
},
}),
'string-directly-in-conditional': block({
label: 'String directly in conditional',
schema: {
conditional: fields.conditional(
fields.checkbox({ label: 'Checkbox' }),
{
true: fields.text({ label: 'True' }),
false: fields.text({ label: 'False' }),
}
),
},
}),
},
false
);
Expand Down Expand Up @@ -651,3 +663,30 @@ test('only inline in list item', () => {
"
`);
});

test('undefined in conditional value', () => {
const markdoc = `{% string-directly-in-conditional conditional={discriminant:false} /%}`;
const editor = fromMarkdoc(markdoc);
expect(editor).toMatchInlineSnapshot(`
<doc>
<node_selection>
<string-directly-in-conditional
props={
{
"extraFiles": [],
"value": {
"conditional": {
"discriminant": false,
},
},
}
}
/>
</node_selection>
</doc>
`);
expect(toMarkdoc(editor)).toMatchInlineSnapshot(`
"{% string-directly-in-conditional conditional={discriminant: false} /%}
"
`);
});
6 changes: 6 additions & 0 deletions packages/keystatic/src/form/serialize-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export function serializeProps(
array(_schema, value) {
return value.map(val => (val === undefined ? null : val));
},
conditional(_schema, value) {
if (value.value === undefined) {
return { discriminant: value.discriminant } as any;
}
return value;
},
child() {
return undefined as any;
},
Expand Down

0 comments on commit 0c1ed9b

Please sign in to comment.