static-cms/src/components/Widgets/ObjectControl.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-10-28 04:51:37 +02:00
import React, { Component, PropTypes } from 'react';
import { Map } from 'immutable';
import { resolveWidget } from '../Widgets';
2016-10-28 10:21:13 +02:00
import controlStyles from '../ControlPanel/ControlPane.css';
import styles from './ObjectControl.css';
2016-10-28 04:51:37 +02:00
export default class ObjectControl extends Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
2017-01-10 22:23:22 -02:00
onAddAsset: PropTypes.func.isRequired,
2017-03-15 14:30:41 -04:00
onRemoveAsset: PropTypes.func.isRequired,
2017-01-10 22:23:22 -02:00
getAsset: PropTypes.func.isRequired,
value: PropTypes.oneOfType([
PropTypes.node,
PropTypes.object,
PropTypes.bool,
]),
2017-01-16 16:52:59 -02:00
field: PropTypes.object,
forID: PropTypes.string,
2017-03-15 14:30:41 -04:00
className: PropTypes.string,
2016-10-28 04:51:37 +02:00
};
controlFor(field) {
2017-01-10 22:23:22 -02:00
const { onAddAsset, onRemoveAsset, getAsset, value, onChange } = this.props;
if (field.get('widget') === 'hidden') {
return null;
}
2016-10-28 04:51:37 +02:00
const widget = resolveWidget(field.get('widget') || 'string');
const fieldValue = value && Map.isMap(value) ? value.get(field.get('name')) : value;
2016-10-28 04:51:37 +02:00
2016-10-30 16:01:10 -07:00
return (<div className={controlStyles.widget} key={field.get('name')}>
2016-10-28 10:21:13 +02:00
<div className={controlStyles.control} key={field.get('name')}>
2017-03-15 14:30:41 -04:00
<label className={controlStyles.label} htmlFor={field.get('name')}>{field.get('label')}</label>
2016-10-28 04:51:37 +02:00
{
React.createElement(widget.control, {
2017-03-15 14:30:41 -04:00
id: field.get('name'),
2016-10-28 04:51:37 +02:00
field,
value: fieldValue,
onChange: (val, metadata) => {
onChange((value || Map()).set(field.get('name'), val), metadata);
2016-10-28 04:51:37 +02:00
},
2017-01-10 22:23:22 -02:00
onAddAsset,
onRemoveAsset,
getAsset,
forID: field.get('name'),
2016-10-28 04:51:37 +02:00
})
}
</div>
2016-10-28 10:21:13 +02:00
</div>);
2016-10-28 04:51:37 +02:00
}
render() {
const { field, forID } = this.props;
const multiFields = field.get('fields');
const singleField = field.get('field');
2017-03-15 14:30:41 -04:00
const className = this.props.className || '';
2016-10-28 04:51:37 +02:00
if (multiFields) {
2017-03-15 14:30:41 -04:00
return (<div id={forID} className={`${ className } ${ styles.root }`}>
{multiFields.map(f => this.controlFor(f))}
</div>);
} else if (singleField) {
return this.controlFor(singleField);
2016-10-28 04:51:37 +02:00
}
return <h3>No field(s) defined for this widget</h3>;
2016-10-28 04:51:37 +02:00
}
}