From 3f79bc19d2904267da80a7b838643a45051fb8a8 Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Wed, 31 Jul 2024 09:31:45 +1000 Subject: [PATCH] Fix serializing conditional fields in content components in `fields.markdoc` where the value is `undefined` --- .changeset/twenty-dolphins-check.md | 5 +++ .../markdoc/editor/tests/markdoc.test.tsx | 39 +++++++++++++++++++ .../keystatic/src/form/serialize-props.ts | 6 +++ 3 files changed, 50 insertions(+) create mode 100644 .changeset/twenty-dolphins-check.md diff --git a/.changeset/twenty-dolphins-check.md b/.changeset/twenty-dolphins-check.md new file mode 100644 index 000000000..4e9d78e8e --- /dev/null +++ b/.changeset/twenty-dolphins-check.md @@ -0,0 +1,5 @@ +--- +'@keystatic/core': patch +--- + +Fix serializing conditional fields in content components in `fields.markdoc` where the value is `undefined` diff --git a/packages/keystatic/src/form/fields/markdoc/editor/tests/markdoc.test.tsx b/packages/keystatic/src/form/fields/markdoc/editor/tests/markdoc.test.tsx index 901f7980b..a794f3c61 100644 --- a/packages/keystatic/src/form/fields/markdoc/editor/tests/markdoc.test.tsx +++ b/packages/keystatic/src/form/fields/markdoc/editor/tests/markdoc.test.tsx @@ -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 ); @@ -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(` + + + + + + `); + expect(toMarkdoc(editor)).toMatchInlineSnapshot(` + "{% string-directly-in-conditional conditional={discriminant: false} /%} + " + `); +}); diff --git a/packages/keystatic/src/form/serialize-props.ts b/packages/keystatic/src/form/serialize-props.ts index 8dc52797f..0b9002e64 100644 --- a/packages/keystatic/src/form/serialize-props.ts +++ b/packages/keystatic/src/form/serialize-props.ts @@ -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; },