Only update search filters based on query if the query changed

This commit is contained in:
Albert Juhé Lluveras 2018-12-14 17:38:31 +01:00
parent 302ff934d1
commit d972ebf672
1 changed files with 6 additions and 8 deletions

View File

@ -4,7 +4,7 @@
*/
import { Component } from '@wordpress/element';
import { SelectControl } from '@wordpress/components';
import { find, partial } from 'lodash';
import { find, isEqual, partial } from 'lodash';
import PropTypes from 'prop-types';
import interpolateComponents from 'interpolate-components';
import classnames from 'classnames';
@ -29,14 +29,12 @@ class SearchFilter extends Component {
}
}
// Given that `getLabels` is async, we can't use `getDerivedStateFromProps` here.
// `componentDidUpdate` will trigger an extra render once the labels are loaded,
// but that's a small trade-off.
componentDidUpdate( props ) {
const { filter, config, query } = props;
componentDidUpdate( prevProps ) {
const { query } = this.props;
const { filter, config, query: prevQuery } = prevProps;
if ( filter.value.length ) {
config.input.getLabels( filter.value, query ).then( this.updateLabels );
if ( filter.value.length && ! isEqual( prevQuery, query ) ) {
config.input.getLabels( filter.value, prevQuery ).then( this.updateLabels );
}
}