Re-written Preview as a functional component. It also re-uses widgetFor prop to get widgets.

This commit is contained in:
Andrey Okonetchnikov 2016-09-30 16:38:10 +02:00
parent f1eb93ee7f
commit 7fde1e811a
2 changed files with 12 additions and 29 deletions

View File

@ -1,39 +1,21 @@
import React, { PropTypes } from 'react'; import React, { PropTypes } from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import { resolveWidget } from '../Widgets';
export default class Preview extends React.Component { export default function Preview({ collection, widgetFor }) {
if (!collection) {
previewFor(field) { return null;
const { entry, getMedia } = this.props;
const widget = resolveWidget(field.get('widget'));
return React.createElement(widget.preview, {
field: field,
value: entry.getIn(['data', field.get('name')]),
getMedia: getMedia,
});
} }
render() { return (
const { collection } = this.props; <div>
if (!collection) { {collection.get('fields').map(field => widgetFor(field.get('name')))}
return null; </div>
} );
return <div>
{
collection.get('fields').map(field => (
<div key={field.get('name')}>
{this.previewFor(field)}
</div>
))
}
</div>;
}
} }
Preview.propTypes = { Preview.propTypes = {
collection: ImmutablePropTypes.map.isRequired, collection: ImmutablePropTypes.map.isRequired,
entry: ImmutablePropTypes.map.isRequired, entry: ImmutablePropTypes.map.isRequired,
getMedia: PropTypes.func.isRequired, getMedia: PropTypes.func.isRequired,
widgetFor: PropTypes.func.isRequired,
}; };

View File

@ -25,9 +25,10 @@ export default class PreviewPane extends React.Component {
const field = collection.get('fields').find((field) => field.get('name') === name); const field = collection.get('fields').find((field) => field.get('name') === name);
const widget = resolveWidget(field.get('widget')); const widget = resolveWidget(field.get('widget'));
return React.createElement(widget.preview, { return React.createElement(widget.preview, {
field: field, key: field.get('name'),
value: entry.getIn(['data', field.get('name')]), value: entry.getIn(['data', field.get('name')]),
getMedia: getMedia, field,
getMedia,
}); });
} }