65 lines
1.8 KiB
JavaScript
Raw Normal View History

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';
function isHidden(field) {
return field.get('widget') === 'hidden';
}
export default class ControlPane extends Component {
2016-09-28 12:46:39 +02:00
controlFor(field) {
const { entry, getMedia, onChange, onAddMedia, onRemoveMedia } = this.props;
2016-09-28 12:46:39 +02:00
const widget = resolveWidget(field.get('widget'));
const fieldName = field.get('name');
2016-11-30 16:52:17 -02:00
const value = entry.getIn(['data', fieldName]);
if (entry.size === 0 || entry.get('partial') === true) return null;
2016-09-28 12:46:39 +02:00
return (
<div className={styles.control}>
<label className={styles.label} htmlFor={fieldName}>{field.get('label')}</label>
{
React.createElement(widget.control, {
field,
value,
onChange: val => onChange(entry.setIn(['data', fieldName], val)),
onAddMedia,
onRemoveMedia,
getMedia,
})
}
2016-09-28 12:46:39 +02:00
</div>
);
}
render() {
const { collection, fields } = this.props;
if (!collection || !fields) {
2016-09-28 12:46:39 +02:00
return null;
}
2016-09-28 12:46:39 +02:00
return (
<div>
{
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,
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,
};