Skip to content

Commit

Permalink
allow widgets to override controlHOC shouldComponentUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Aug 30, 2017
1 parent 121bdc2 commit 41fc8fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/Widgets/ControlHOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ class ControlHOC extends Component {
};

shouldComponentUpdate(nextProps) {
/**
* Allow widgets to provide their own `shouldComponentUpdate` method.
*/
if (this.wrappedControlShouldComponentUpdate) {
return this.wrappedControlShouldComponentUpdate(nextProps);
}
return this.props.value !== nextProps.value;
}

processInnerControlRef = (wrappedControl) => {
if (!wrappedControl) return;
this.wrappedControlValid = wrappedControl.isValid || truthy;

/**
* Get the `shouldComponentUpdate` method from the wrapped control, and
* provide the control instance is the `this` binding.
*/
const { shouldComponentUpdate: scu } = wrappedControl;
this.wrappedControlShouldComponentUpdate = scu && scu.bind(wrappedControl);
};

validate = (skipWrapped = false) => {
Expand Down
20 changes: 20 additions & 0 deletions src/components/Widgets/MediaControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ export default class MediaControl extends React.Component {
this.controlID = uuid.v4();
}

shouldComponentUpdate(nextProps) {
/**
* Always update if the value changes.
*/
if (this.props.value !== nextProps.value) {
return true;
}

/**
* If there is a media path for this control in the state object, and that
* path is different than the value in `nextProps`, update.
*/
const mediaPath = nextProps.mediaPaths.get(this.controlID);
if (mediaPath && (nextProps.value !== mediaPath)) {
return true;
}

return false;
}

componentWillReceiveProps(nextProps) {
const { mediaPaths, value } = nextProps;
const mediaPath = mediaPaths.get(this.controlID);
Expand Down

0 comments on commit 41fc8fc

Please sign in to comment.