2016-09-29 22:17:29 +02:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-09-28 12:46:39 +02:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import { resolveWidget } from '../Widgets';
|
|
|
|
import styles from './ControlPane.css';
|
|
|
|
|
2016-11-11 17:54:58 -02:00
|
|
|
function isHidden(field) {
|
|
|
|
return field.get('widget') === 'hidden';
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:17:29 +02:00
|
|
|
export default class ControlPane extends Component {
|
|
|
|
|
2016-09-28 12:46:39 +02:00
|
|
|
controlFor(field) {
|
2016-11-17 04:08:37 -08:00
|
|
|
const { entry, getMedia, onChange, onAddMedia, onRemoveMedia } = this.props;
|
2016-09-28 12:46:39 +02:00
|
|
|
const widget = resolveWidget(field.get('widget'));
|
2016-10-11 10:28:42 +02:00
|
|
|
const fieldName = field.get('name');
|
2016-11-30 16:52:17 -02:00
|
|
|
const value = entry.getIn(['data', fieldName]);
|
2016-10-19 19:10:14 -02:00
|
|
|
if (entry.size === 0 || entry.get('partial') === true) return null;
|
2016-09-28 12:46:39 +02:00
|
|
|
return (
|
|
|
|
<div className={styles.control}>
|
2016-10-11 10:28:42 +02:00
|
|
|
<label className={styles.label} htmlFor={fieldName}>{field.get('label')}</label>
|
2016-09-29 22:17:29 +02:00
|
|
|
{
|
|
|
|
React.createElement(widget.control, {
|
2016-10-11 10:28:42 +02:00
|
|
|
field,
|
|
|
|
value,
|
|
|
|
onChange: val => onChange(entry.setIn(['data', fieldName], val)),
|
|
|
|
onAddMedia,
|
|
|
|
onRemoveMedia,
|
|
|
|
getMedia,
|
2016-09-29 22:17:29 +02:00
|
|
|
})
|
|
|
|
}
|
2016-09-28 12:46:39 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-10-21 20:42:14 -02:00
|
|
|
const { collection, fields } = this.props;
|
|
|
|
if (!collection || !fields) {
|
2016-09-28 12:46:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-10-21 20:42:14 -02:00
|
|
|
|
2016-09-28 12:46:39 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{
|
2016-11-17 04:08:37 -08:00
|
|
|
fields.map((field) => {
|
|
|
|
if (isHidden(field)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return <div key={field.get('name')} className={styles.widget}>{this.controlFor(field)}</div>;
|
|
|
|
})
|
2016-09-28 12:46:39 +02:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlPane.propTypes = {
|
|
|
|
collection: ImmutablePropTypes.map.isRequired,
|
|
|
|
entry: ImmutablePropTypes.map.isRequired,
|
2016-10-21 20:42:14 -02:00
|
|
|
fields: ImmutablePropTypes.list.isRequired,
|
2016-09-28 12:46:39 +02:00
|
|
|
getMedia: PropTypes.func.isRequired,
|
|
|
|
onAddMedia: PropTypes.func.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onRemoveMedia: PropTypes.func.isRequired,
|
|
|
|
};
|