2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2017-01-13 19:30:40 -02:00
|
|
|
import { Map, fromJS } from 'immutable';
|
2016-09-28 12:46:39 +02:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import { resolveWidget } from '../Widgets';
|
2017-01-13 19:30:40 -02:00
|
|
|
import ControlHOC from '../Widgets/ControlHOC';
|
2016-09-28 12:46:39 +02:00
|
|
|
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 {
|
2017-01-13 19:30:40 -02:00
|
|
|
componentValidate = {};
|
|
|
|
processControlRef(fieldName, wrappedControl) {
|
|
|
|
if (!wrappedControl) return;
|
|
|
|
this.componentValidate[fieldName] = wrappedControl.validate;
|
|
|
|
}
|
|
|
|
|
|
|
|
validate = () => {
|
2017-01-31 17:09:45 -02:00
|
|
|
this.props.fields.forEach((field) => {
|
|
|
|
if (isHidden(field)) return;
|
|
|
|
this.componentValidate[field.get("name")]();
|
|
|
|
});
|
2017-01-13 19:30:40 -02:00
|
|
|
};
|
2016-09-29 22:17:29 +02:00
|
|
|
|
2016-09-28 12:46:39 +02:00
|
|
|
controlFor(field) {
|
2017-01-13 19:30:40 -02:00
|
|
|
const { entry, fieldsMetaData, fieldsErrors, getAsset, onChange, onAddAsset, onRemoveAsset } = 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-12-29 17:18:24 -02:00
|
|
|
const metadata = fieldsMetaData.get(fieldName);
|
2017-01-13 19:30:40 -02:00
|
|
|
const errors = fieldsErrors.get(fieldName);
|
|
|
|
const labelClass = errors ? styles.labelWithError : styles.label;
|
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}>
|
2017-01-13 19:30:40 -02:00
|
|
|
<label className={labelClass} htmlFor={fieldName}>{field.get('label')}</label>
|
|
|
|
<ul className={styles.errors}>
|
|
|
|
{
|
|
|
|
errors && errors.map(error => (
|
|
|
|
typeof error === 'string' && <li key={error.trim().replace(/[^a-z0-9]+/gi, '-')}>{error}</li>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
<ControlHOC
|
|
|
|
controlComponent={widget.control}
|
|
|
|
field={field}
|
|
|
|
value={value}
|
|
|
|
metadata={metadata}
|
|
|
|
onChange={(newValue, newMetadata) => onChange(fieldName, newValue, newMetadata)}
|
|
|
|
onValidate={this.props.onValidate.bind(this, fieldName)}
|
|
|
|
onAddAsset={onAddAsset}
|
|
|
|
onRemoveAsset={onRemoveAsset}
|
|
|
|
getAsset={getAsset}
|
|
|
|
ref={this.processControlRef.bind(this, fieldName)}
|
|
|
|
/>
|
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>
|
|
|
|
{
|
2017-06-04 17:21:38 +02:00
|
|
|
fields.map((field, i) => {
|
2016-11-17 04:08:37 -08:00
|
|
|
if (isHidden(field)) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-04 17:21:38 +02:00
|
|
|
return <div key={i} className={styles.widget}>{this.controlFor(field)}</div>;
|
2016-11-17 04:08:37 -08:00
|
|
|
})
|
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-12-29 17:18:24 -02:00
|
|
|
fieldsMetaData: ImmutablePropTypes.map.isRequired,
|
2017-01-13 19:30:40 -02:00
|
|
|
fieldsErrors: ImmutablePropTypes.map.isRequired,
|
2017-01-10 22:23:22 -02:00
|
|
|
getAsset: PropTypes.func.isRequired,
|
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
2016-09-28 12:46:39 +02:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-01-13 19:30:40 -02:00
|
|
|
onValidate: PropTypes.func.isRequired,
|
2017-01-10 22:23:22 -02:00
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
2016-09-28 12:46:39 +02:00
|
|
|
};
|