Skip to content

Commit

Permalink
fix(widgetsFor): return widgets for variable type lists
Browse files Browse the repository at this point in the history
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 #2307 (comment)
  • Loading branch information
domcleal committed Oct 1, 2024
1 parent 51eb7e8 commit 734403b
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,28 @@ export class PreviewPane extends React.Component {
const { fields, entry, fieldsMetaData } = this.props;
const field = fields.find(f => f.get('name') === name);
const nestedFields = field && field.get('fields');
const variableTypes = field && field.get('types');
const value = entry.getIn(['data', field.get('name')]);
const metadata = fieldsMetaData.get(field.get('name'), Map());

// Variable Type lists
if (List.isList(value) && variableTypes) {
return value.map(val => {
const valueType = variableTypes.find(t => t.get('name') === val.get('type'));
const typeFields = valueType && valueType.get('fields');
const widgets =
typeFields &&
Map(
typeFields.map((f, i) => [
f.get('name'),
<div key={i}>{this.getWidget(f, val, metadata.get(f.get('name')), this.props)}</div>,
]),
);
return Map({ data: val, widgets });
});
}

// List widgets
if (List.isList(value)) {
return value.map(val => {
const widgets =
Expand Down

0 comments on commit 734403b

Please sign in to comment.