Skip to content

Commit

Permalink
fix nested widget shouldComponetUpdate override
Browse files Browse the repository at this point in the history
broken by rebase after #254 merge
  • Loading branch information
erquhart committed Sep 7, 2017
1 parent 98d3d3e commit 5a9d621
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/Widgets/ControlHOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ControlHOC extends Component {
mediaPaths: ImmutablePropTypes.map.isRequired,
metadata: ImmutablePropTypes.map,
onChange: PropTypes.func.isRequired,
onValidate: PropTypes.func.isRequired,
onValidate: PropTypes.func,
onOpenMediaLibrary: PropTypes.func.isRequired,
onAddAsset: PropTypes.func.isRequired,
onRemoveAsset: PropTypes.func.isRequired,
Expand Down
10 changes: 10 additions & 0 deletions src/components/Widgets/ListControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export default class ListControl extends Component {
this.valueType = null;
}

/**
* 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;
}

componentDidMount() {
const { field } = this.props;
if (field.get('fields')) {
Expand Down
46 changes: 30 additions & 16 deletions src/components/Widgets/ObjectControl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component, PropTypes } from 'react';
import { Map } from 'immutable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ControlHOC from './ControlHOC';
import { resolveWidget } from '../Widgets';
import controlStyles from '../ControlPanel/ControlPane.css';
import styles from './ObjectControl.css';
Expand All @@ -23,6 +24,23 @@ export default class ObjectControl extends Component {
className: PropTypes.string,
};

/**
* 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;
}

onChange = (fieldName, newValue, newMetadata) => {
const { value, onChange } = this.props;
const objectValue = value || Map();
const newObjectValue = objectValue.set(fieldName, newValue);
return this.props.onChange(newObjectValue, newMetadata);
};

controlFor(field) {
const { onAddAsset, onOpenMediaLibrary, mediaPaths, onRemoveAsset, getAsset, value, onChange } = this.props;
if (field.get('widget') === 'hidden') {
Expand All @@ -34,22 +52,18 @@ export default class ObjectControl extends Component {
return (<div className={controlStyles.widget} key={field.get('name')}>
<div className={controlStyles.control} key={field.get('name')}>
<label className={controlStyles.label} htmlFor={field.get('name')}>{field.get('label')}</label>
{
React.createElement(widget.control, {
id: field.get('name'),
field,
value: fieldValue,
onChange: (val, metadata) => {
onChange((value || Map()).set(field.get('name'), val), metadata);
},
onOpenMediaLibrary,
mediaPaths,
onAddAsset,
onRemoveAsset,
getAsset,
forID: field.get('name'),
})
}
<ControlHOC
controlComponent={widget.control}
field={field}
value={fieldValue}
onChange={this.onChange.bind(this, field.get('name'))}
mediaPaths={mediaPaths}
onOpenMediaLibrary={onOpenMediaLibrary}
onAddAsset={onAddAsset}
onRemoveAsset={onRemoveAsset}
getAsset={getAsset}
forID={field.get('name')}
/>
</div>
</div>);
}
Expand Down

0 comments on commit 5a9d621

Please sign in to comment.