From aa5ce4a2f17107f625b80ee9e605026ce18aa1c9 Mon Sep 17 00:00:00 2001 From: Bruce Martin Date: Tue, 12 Mar 2019 10:49:29 -0700 Subject: [PATCH] correctly toggle group selection in categorical metadata (#640) --- client/src/components/categorical/category.js | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/client/src/components/categorical/category.js b/client/src/components/categorical/category.js index 6c2a48bf..1cbb73d1 100644 --- a/client/src/components/categorical/category.js +++ b/client/src/components/categorical/category.js @@ -21,38 +21,42 @@ class Category extends React.Component { }; } - componentDidUpdate() { + componentDidUpdate(prevProps) { const { categoricalSelectionState, metadataField } = this.props; - const cat = categoricalSelectionState[metadataField]; - const categoryCount = { - // total number of categories in this dimension - totalCatCount: cat.numCategories, - // number of selected options in this category - selectedCatCount: _.reduce( - cat.categorySelected, - (res, cond) => (cond ? res + 1 : res), - 0 - ) - }; - if (categoryCount.selectedCatCount === categoryCount.totalCatCount) { - /* everything is on, so not indeterminate */ - this.checkbox.indeterminate = false; - } else if (categoryCount.selectedCatCount === 0) { - /* nothing is on, so no */ - this.checkbox.indeterminate = false; - } else if (categoryCount.selectedCatCount < categoryCount.totalCatCount) { - /* to be explicit... */ - this.checkbox.indeterminate = true; + if (categoricalSelectionState !== prevProps.categoricalSelectionState) { + const cat = categoricalSelectionState[metadataField]; + const categoryCount = { + // total number of categories in this dimension + totalCatCount: cat.numCategories, + // number of selected options in this category + selectedCatCount: _.reduce( + cat.categorySelected, + (res, cond) => (cond ? res + 1 : res), + 0 + ) + }; + if (categoryCount.selectedCatCount === categoryCount.totalCatCount) { + /* everything is on, so not indeterminate */ + this.checkbox.indeterminate = false; + this.setState({ isChecked: true }); // eslint-disable-line react/no-did-update-set-state + } else if (categoryCount.selectedCatCount === 0) { + /* nothing is on, so no */ + this.checkbox.indeterminate = false; + this.setState({ isChecked: false }); // eslint-disable-line react/no-did-update-set-state + } else if (categoryCount.selectedCatCount < categoryCount.totalCatCount) { + /* to be explicit... */ + this.checkbox.indeterminate = true; + } } } - handleColorChange() { + handleColorChange = () => { const { dispatch, metadataField } = this.props; dispatch({ type: "color by categorical metadata", colorAccessor: metadataField }); - } + }; toggleAll() { const { dispatch, metadataField } = this.props; @@ -161,10 +165,10 @@ class Category extends React.Component {