add shared widget getter for widgetFor and widgetsFor

This commit is contained in:
Shawn Erquhart 2017-03-06 15:22:40 -05:00
parent 9bc80ed5e4
commit e48221a04f
2 changed files with 20 additions and 26 deletions

View File

@ -101,7 +101,7 @@
h('dd', {}, this.props.widgetsFor('posts').getIn(['widgets', 'front_limit']) || 0), h('dd', {}, this.props.widgetsFor('posts').getIn(['widgets', 'front_limit']) || 0),
h('dt', {}, 'Default Author'), 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('dt', {}, 'Default Thumbnail'),
h('dd', {}, thumb && h('img', {src: this.props.getAsset(thumb).toString()})) h('dd', {}, thumb && h('img', {src: this.props.getAsset(thumb).toString()}))

View File

@ -29,54 +29,48 @@ export default class PreviewPane extends React.Component {
if (authorField) this.inferedFields[authorField] = INFERABLE_FIELDS.author; 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) => { widgetFor = (name) => {
const { fields, entry, fieldsMetaData, getAsset } = this.props; const { fields, entry } = this.props;
const field = fields.find(f => f.get('name') === name); const field = fields.find(f => f.get('name') === name);
let value = entry.getIn(['data', field.get('name')]); let value = entry.getIn(['data', field.get('name')]);
const metadata = fieldsMetaData.get(field.get('name'));
const labelledWidgets = ['string', 'text', 'number']; const labelledWidgets = ['string', 'text', 'number'];
if (Object.keys(this.inferedFields).indexOf(name) !== -1) { if (Object.keys(this.inferedFields).indexOf(name) !== -1) {
value = this.inferedFields[name].defaultPreview(value); value = this.inferedFields[name].defaultPreview(value);
} else if (value && labelledWidgets.indexOf(field.get('widget')) !== -1 && value.toString().length < 50) { } else if (value && labelledWidgets.indexOf(field.get('widget')) !== -1 && value.toString().length < 50) {
value = <div><strong>{field.get('label')}:</strong> {value}</div>; value = <div><strong>{field.get('label')}:</strong> {value}</div>;
} }
if (!value) return null;
const widget = resolveWidget(field.get('widget')); return value ? this._getWidget(field, value, this.props) : null;
return React.createElement(widget.preview, {
key: field.get('name'),
value,
field,
metadata,
getAsset,
});
}; };
widgetsFor = (name) => { widgetsFor = (name) => {
const { fields, entry, getAsset } = this.props; const { fields, entry } = this.props;
const field = fields && fields.find(f => f.get('name') === name); const field = fields.find(f => f.get('name') === name);
const nestedFields = field && field.get('fields'); const nestedFields = field && field.get('fields');
const value = entry.getIn(['data', field.get('name')]); const value = entry.getIn(['data', field.get('name')]);
const widgetFor = (field, value) => {
const widget = resolveWidget(field.get('widget'));
return (<div key={field.get('name')}>{React.createElement(widget.preview, {
key: field.get('name'),
value: value && List.isList(value) ? value.get(field.get('name')) : value,
field,
getAsset,
})}</div>);
};
if (List.isList(value)) { if (List.isList(value)) {
return value.map((val, index) => { 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'), <div key={i}>{this._getWidget(f, val, this.props)}</div>]));
return Map({ data: val, widgets }); return Map({ data: val, widgets });
}); });
} }
return Map({ return Map({
data: value, 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)])),
}); });
}; };