Skip to content
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

fix(widgetsFor): return widgets for variable type lists #7296

Merged
merged 3 commits into from
Jan 29, 2025

Conversation

domcleal
Copy link
Contributor

@domcleal domcleal commented Oct 1, 2024

Summary

When using a Variable Type list widget and a custom preview component, the widgetsFor helper would only return a data list with each of the items in the list, not a widgets list, e.g.

{"data" =>
    {"markdown" => "# Title"}
    {"type" => "block_body"}
}
{"widgets" => undefined} 🚫

The widgets list should also be supplied, particularly for nested Markdown widgets, so a fully formatted preview can be rendered:

{"data" =>
    {"markdown" => "# Title"}
    {"type" => "block_body"}
}
{"widgets" =>
    {"markdown" => Object} ✅
}

This extends support in widgetsFor to detect variable type list widgets and correctly construct the widgets return value.

Fixes #2307 (specifically #2307 (comment) which explains it neatly).

Test plan

Using the test backend, add the following custom preview helper:

var KitchenSinkPreview = createClass({
  render: function() {
    return h('div', {},
      h('h1', {}, 'Typed list'),
      this.props.widgetsFor('typed_list').map(function(item, index) {
        return h('div', {key: index},
          h('hr', {}),
          h('strong', {}, item.getIn(['data', 'string']) || "Untitled"),

          h('dl', {},
            h('dt', {}, 'Data'),
            h('dd', {}, item.getIn(['data', 'markdown'])),

            h('dt', {}, 'Widgets'),
            h('dd', {}, item.getIn(['widgets', 'markdown'])),
          ),
        );
      })
    );
  }
});

CMS.registerPreviewTemplate("kitchenSink", KitchenSinkPreview);   

Before:

image

Only the data value is populated, so only the raw Markdown is available. The widgets return value is empty.

After:

image

In the preview pane, the Markdown widget is now able to be used to render the field.

Checklist

Please add a x inside each checkbox:

When using a Variable Type list widget and a custom preview component,
the `widgetsFor` helper would only return a `data` list with each of the
items in the list, not a `widgets` list, e.g.

    {"data" =>
        {"markdown" => "# Title"}
        {"type" => "block_body"}
    }
    {"widgets" => undefined} 🚫

The `widgets` list should also be supplied, particularly for nested
Markdown widgets, so a fully formatted preview can be rendered:

    {"data" =>
        {"markdown" => "# Title"}
        {"type" => "block_body"}
    }
    {"widgets" =>
        {"markdown" => Object} ✅
    }

This extends support in `widgetsFor` to detect variable type list
widgets and correctly construct the `widgets` return value.

As reported at decaporg#2307 (comment)
@domcleal domcleal marked this pull request as ready for review October 1, 2024 10:54
@domcleal domcleal requested a review from a team as a code owner October 1, 2024 10:54
Copy link
Member

@demshy demshy left a comment

Choose a reason for hiding this comment

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

This is great, thanks!

If maybe someone could figure out if this could work with what @fgnass did here #7374 it would be even better

@domcleal
Copy link
Contributor Author

Thanks for reviewing!

If maybe someone could figure out if this could work with what @fgnass did here #7374 it would be even better

I can't really think of any way that they'd interact. This only fixes the existing helper for custom previews, but visual editing and the default preview are separate concerns.

It might be that the visual editing could be extended to work in custom previews (if it doesn't already, I'm unclear) or that default previews should extend to variable type lists, but I believe that's separate to this change to widgetsFor.

@demshy
Copy link
Member

demshy commented Jan 29, 2025

This only fixes the existing helper for custom previews, but visual editing and the default preview are separate concerns.

Yep, I think this PR is a prerequisite for us to even start thinking about extending visual editing triggers to the variable types. Since that feature will start as Beta anyway I don't have a problem releasing both.

@demshy demshy merged commit 9be2693 into decaporg:main Jan 29, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preview Pane Support for List Widget: Variable Types
2 participants