From 00ac35578108bc90c5ff88aad3f7fe2386bd7461 Mon Sep 17 00:00:00 2001 From: Alex Terentiev Date: Sat, 26 Dec 2020 18:15:08 -0800 Subject: [PATCH] TaxonomyPicker - ability to automatically select children (#765) --- .../docs/controls/TaxonomyPicker.md | 1 + .../taxonomyPicker/ITaxonomyPicker.ts | 6 +++ .../taxonomyPicker/TaxonomyPicker.tsx | 45 ++++++++++++++++++- .../controlsTest/components/ControlsTest.tsx | 2 +- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/docs/documentation/docs/controls/TaxonomyPicker.md b/docs/documentation/docs/controls/TaxonomyPicker.md index ecf429881..14ba298f1 100644 --- a/docs/documentation/docs/controls/TaxonomyPicker.md +++ b/docs/documentation/docs/controls/TaxonomyPicker.md @@ -173,6 +173,7 @@ The TaxonomyPicker control can be configured with the following properties: | required | boolean | no | Specifies if to display an asterisk near the label. Note that error message should be specified in `onGetErrorMessage` or `errorMessage` | | useSessionStorage | boolean | no | Specify if the control uses session storage. Default is set to true for better performance. | | onPanelSelectionChange | (prevValue: IPickerTerms, newValue: IPickerTerms) => void | no | Panel selection change handler. Can be used to interact with the control while selecting items in the panel, before Click or Cancel is clicked. | +| selectChildrenIfParentSelected | boolean | no | Specifies if the children should be selected when parent item is selected (defaults to false).| Interface `IPickerTerm` diff --git a/src/controls/taxonomyPicker/ITaxonomyPicker.ts b/src/controls/taxonomyPicker/ITaxonomyPicker.ts index 6bade6e8b..d8452247f 100644 --- a/src/controls/taxonomyPicker/ITaxonomyPicker.ts +++ b/src/controls/taxonomyPicker/ITaxonomyPicker.ts @@ -131,6 +131,12 @@ export interface ITaxonomyPickerProps { * Panel selection change handler. Can be used to interact with the control while selecting items in the panel, before Click or Cancel is clicked. */ onPanelSelectionChange?: (prevValue: IPickerTerms, newValue: IPickerTerms) => void; + + /** + * Specifies if the childrens should be selected when parent is selected. + * By default this is set to false. + */ + selectChildrenIfParentSelected?: boolean; } /** diff --git a/src/controls/taxonomyPicker/TaxonomyPicker.tsx b/src/controls/taxonomyPicker/TaxonomyPicker.tsx index 896509c62..8f71dcf7c 100644 --- a/src/controls/taxonomyPicker/TaxonomyPicker.tsx +++ b/src/controls/taxonomyPicker/TaxonomyPicker.tsx @@ -236,6 +236,15 @@ export class TaxonomyPicker extends React.Component { + return t.PathOfTerm.indexOf(`${term.PathOfTerm};`) !== -1; + }) : []; + } + } + // Check if the term is checked or unchecked if (checked) { // Check if it is allowed to select multiple terms - if (this.props.allowMultipleSelections) { + if (allowMultipleSelections) { // Add the checked term activeNodes.push(termItem); + // Filter out the duplicate terms activeNodes = uniqBy(activeNodes, 'key'); } else { // Only store the current selected item activeNodes = [termItem]; } + + if (children.length) { + activeNodes.push(...children.map(c => { + return { + name: c.Name, + key: c.Id, + path: c.PathOfTerm, + termSet: c.TermSet.Id + }; + })); + } } else { // Remove the term from the list of active nodes activeNodes = activeNodes.filter(item => item.key !== term.Id); + + if (children.length) { + const childIds = children.map(c => c.Id); + activeNodes = activeNodes.filter(item => childIds.indexOf(item.key) === -1); + } } // Sort all active nodes activeNodes = sortBy(activeNodes, 'path'); if (this.props.onPanelSelectionChange) { - this.props.onPanelSelectionChange(this.state.activeNodes.slice(), activeNodes) + this.props.onPanelSelectionChange(this.state.activeNodes.slice(), activeNodes); } // Update the current state diff --git a/src/webparts/controlsTest/components/ControlsTest.tsx b/src/webparts/controlsTest/components/ControlsTest.tsx index b5664a6ca..6fd83f6e3 100644 --- a/src/webparts/controlsTest/components/ControlsTest.tsx +++ b/src/webparts/controlsTest/components/ControlsTest.tsx @@ -717,7 +717,7 @@ export default class ControlsTest extends React.Component