From fb557cf21fcbe7735545b7962300a619e368ea24 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Fri, 22 Sep 2023 10:20:08 +1200 Subject: [PATCH] Components: TreeSelectControl - Make sure individuallySelectParent value is preserved (#40301) * make sure individually selected parent value is preserved * Add changefile(s) from automation for the following project(s): @woocommerce/components --------- Co-authored-by: github-actions --- .../fix-tree-select-control-individuallySelectParent | 4 ++++ packages/js/components/src/tree-select-control/index.js | 7 +++++-- .../js/components/src/tree-select-control/stories/index.js | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 packages/js/components/changelog/fix-tree-select-control-individuallySelectParent diff --git a/packages/js/components/changelog/fix-tree-select-control-individuallySelectParent b/packages/js/components/changelog/fix-tree-select-control-individuallySelectParent new file mode 100644 index 00000000000..49dcda57609 --- /dev/null +++ b/packages/js/components/changelog/fix-tree-select-control-individuallySelectParent @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +TreeSelectControl Component - Make sure individuallySelectParent prop is respected diff --git a/packages/js/components/src/tree-select-control/index.js b/packages/js/components/src/tree-select-control/index.js index 86b4026e492..8fb65589454 100644 --- a/packages/js/components/src/tree-select-control/index.js +++ b/packages/js/components/src/tree-select-control/index.js @@ -234,7 +234,10 @@ const TreeSelectControl = ( { * @return {boolean} True if checked, false otherwise. */ get() { - if ( includeParent && this.value !== ROOT_VALUE ) { + if ( + ( includeParent || individuallySelectParent ) && + this.value !== ROOT_VALUE + ) { return cache.selectedValues.includes( this.value ); } if ( this.hasChildren ) { @@ -427,7 +430,7 @@ const TreeSelectControl = ( { const handleParentChange = ( checked, option ) => { let newValue; const changedValues = individuallySelectParent - ? [] + ? [ option.value ] : option.leaves .filter( ( opt ) => opt.checked !== checked ) .map( ( opt ) => opt.value ); diff --git a/packages/js/components/src/tree-select-control/stories/index.js b/packages/js/components/src/tree-select-control/stories/index.js index d30221a7218..eb6a3df1d3d 100644 --- a/packages/js/components/src/tree-select-control/stories/index.js +++ b/packages/js/components/src/tree-select-control/stories/index.js @@ -96,6 +96,7 @@ Base.args = { selectAllLabel: 'All countries', includeParent: false, alwaysShowPlaceholder: false, + individuallySelectParent: false, }; Base.argTypes = {