diff --git a/example/index.html b/example/index.html index f3e5affb..6ab62374 100644 --- a/example/index.html +++ b/example/index.html @@ -101,7 +101,7 @@ h('dd', {}, this.props.widgetsFor('posts').getIn(['widgets', 'front_limit']) || 0), h('dt', {}, 'Default Author'), - h('dd', {}, this.props.widgetsFor('posts').getIn(['data', 'front_limit']) || 'None'), + h('dd', {}, this.props.widgetsFor('posts').getIn(['data', 'author']) || 'None'), h('dt', {}, 'Default Thumbnail'), h('dd', {}, thumb && h('img', {src: this.props.getAsset(thumb).toString()})) diff --git a/src/components/PreviewPane/PreviewPane.js b/src/components/PreviewPane/PreviewPane.js index b7da6e36..33171a9e 100644 --- a/src/components/PreviewPane/PreviewPane.js +++ b/src/components/PreviewPane/PreviewPane.js @@ -29,54 +29,48 @@ export default class PreviewPane extends React.Component { if (authorField) this.inferedFields[authorField] = INFERABLE_FIELDS.author; } + _getWidget = (field, value, props) => { + const { fieldsMetaData, getAsset } = props; + const widget = resolveWidget(field.get('widget')); + return React.createElement(widget.preview, { + field, + key: field.get('name'), + value: value && Map.isMap(value) ? value.get(field.get('name')) : value, + metadata: fieldsMetaData && fieldsMetaData.get(field.get('name')), + getAsset, + }); + }; + widgetFor = (name) => { - const { fields, entry, fieldsMetaData, getAsset } = this.props; + const { fields, entry } = this.props; const field = fields.find(f => f.get('name') === name); let value = entry.getIn(['data', field.get('name')]); - const metadata = fieldsMetaData.get(field.get('name')); const labelledWidgets = ['string', 'text', 'number']; if (Object.keys(this.inferedFields).indexOf(name) !== -1) { value = this.inferedFields[name].defaultPreview(value); } else if (value && labelledWidgets.indexOf(field.get('widget')) !== -1 && value.toString().length < 50) { value =
{field.get('label')}: {value}
; } - if (!value) return null; - const widget = resolveWidget(field.get('widget')); - return React.createElement(widget.preview, { - key: field.get('name'), - value, - field, - metadata, - getAsset, - }); + + return value ? this._getWidget(field, value, this.props) : null; }; widgetsFor = (name) => { - const { fields, entry, getAsset } = this.props; - const field = fields && fields.find(f => f.get('name') === name); + const { fields, entry } = this.props; + const field = fields.find(f => f.get('name') === name); const nestedFields = field && field.get('fields'); const value = entry.getIn(['data', field.get('name')]); - const widgetFor = (field, value) => { - const widget = resolveWidget(field.get('widget')); - return (
{React.createElement(widget.preview, { - key: field.get('name'), - value: value && List.isList(value) ? value.get(field.get('name')) : value, - field, - getAsset, - })}
); - }; - if (List.isList(value)) { return value.map((val, index) => { - const widgets = nestedFields && Map(nestedFields.map(f => [f.get('name'), widgetFor(f, val)])); + const widgets = nestedFields && Map(nestedFields.map((f, i) => [f.get('name'),
{this._getWidget(f, val, this.props)}
])); return Map({ data: val, widgets }); }); } return Map({ data: value, - widgets: nestedFields && Map(nestedFields.map(f => [f.get('name'), widgetFor(f, value)])), + widgets: nestedFields && Map(nestedFields.map(f => [f.get('name'), this._getWidget(f, value, this.props)])), }); };