import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { Map, fromJS } from 'immutable'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { resolveWidget } from '../Widgets'; import ControlHOC from '../Widgets/ControlHOC'; import styles from './ControlPane.css'; function isHidden(field) { return field.get('widget') === 'hidden'; } export default class ControlPane extends Component { componentValidate = {}; processControlRef(fieldName, wrappedControl) { if (!wrappedControl) return; this.componentValidate[fieldName] = wrappedControl.validate; } validate = () => { this.props.fields.forEach((field) => { if (isHidden(field)) return; this.componentValidate[field.get("name")](); }); }; controlFor(field) { const { entry, fieldsMetaData, fieldsErrors, getAsset, onChange, onAddAsset, onRemoveAsset } = this.props; const widget = resolveWidget(field.get('widget')); const fieldName = field.get('name'); const value = entry.getIn(['data', fieldName]); const metadata = fieldsMetaData.get(fieldName); const errors = fieldsErrors.get(fieldName); const labelClass = errors ? styles.labelWithError : styles.label; if (entry.size === 0 || entry.get('partial') === true) return null; return (
onChange(fieldName, newValue, newMetadata)} onValidate={this.props.onValidate.bind(this, fieldName)} onAddAsset={onAddAsset} onRemoveAsset={onRemoveAsset} getAsset={getAsset} ref={this.processControlRef.bind(this, fieldName)} />
); } render() { const { collection, fields } = this.props; if (!collection || !fields) { return null; } return (
{ fields.map((field, i) => { if (isHidden(field)) { return null; } return
{this.controlFor(field)}
; }) }
); } } ControlPane.propTypes = { collection: ImmutablePropTypes.map.isRequired, entry: ImmutablePropTypes.map.isRequired, fields: ImmutablePropTypes.list.isRequired, fieldsMetaData: ImmutablePropTypes.map.isRequired, fieldsErrors: ImmutablePropTypes.map.isRequired, getAsset: PropTypes.func.isRequired, onAddAsset: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, onValidate: PropTypes.func.isRequired, onRemoveAsset: PropTypes.func.isRequired, };