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