import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import styled, { css, cx } from 'react-emotion'; import { Map } from 'immutable'; import { partial } from 'lodash'; import { ObjectWidgetTopBar, Icon, colors, components } from 'netlify-cms-ui-default'; const styles = { nestedObjectControl: css` padding: 6px 14px 14px; border-top: 0; border-top-left-radius: 0; border-top-right-radius: 0; `, }; export default class ObjectControl extends Component { static propTypes = { onChangeObject: PropTypes.func.isRequired, onOpenMediaLibrary: PropTypes.func.isRequired, mediaPaths: ImmutablePropTypes.map.isRequired, onAddAsset: PropTypes.func.isRequired, onRemoveInsertedMedia: PropTypes.func.isRequired, getAsset: PropTypes.func.isRequired, value: PropTypes.oneOfType([ PropTypes.node, PropTypes.object, PropTypes.bool, ]), field: PropTypes.object, forID: PropTypes.string, classNameWrapper: PropTypes.string.isRequired, forList: PropTypes.bool, editorControl: PropTypes.func.isRequired, resolveWidget: PropTypes.func.isRequired, }; static defaultProps = { value: Map(), }; constructor(props) { super(props); this.state = { collapsed: false, }; } /* * Always update so that each nested widget has the option to update. This is * required because ControlHOC provides a default `shouldComponentUpdate` * which only updates if the value changes, but every widget must be allowed * to override this. */ shouldComponentUpdate() { return true; } controlFor(field, key) { const { onAddAsset, onOpenMediaLibrary, mediaPaths, onRemoveInsertedMedia, getAsset, value, onChangeObject, editorControl: EditorControl, resolveWidget, } = this.props; if (field.get('widget') === 'hidden') { return null; } const widgetName = field.get('widget') || 'string'; const widget = resolveWidget(widgetName); const fieldName = field.get('name'); const fieldValue = value && Map.isMap(value) ? value.get(fieldName) : value; return ( ); } handleCollapseToggle = () => { this.setState({ collapsed: !this.state.collapsed }); } render() { const { field, forID, classNameWrapper, forList } = this.props; const { collapsed } = this.state; const multiFields = field.get('fields'); const singleField = field.get('field'); if (multiFields) { return (
{forList ? null : } {collapsed ? null : multiFields.map((f, idx) => this.controlFor(f, idx))}
); } else if (singleField) { return this.controlFor(singleField); } return

No field(s) defined for this widget

; } }