Skip to content

Commit 00ac355

Browse files
committed
TaxonomyPicker - ability to automatically select children (#765)
1 parent cabf489 commit 00ac355

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

docs/documentation/docs/controls/TaxonomyPicker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ The TaxonomyPicker control can be configured with the following properties:
173173
| required | boolean | no | Specifies if to display an asterisk near the label. Note that error message should be specified in `onGetErrorMessage` or `errorMessage` |
174174
| useSessionStorage | boolean | no | Specify if the control uses session storage. Default is set to true for better performance. |
175175
| 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. |
176+
| selectChildrenIfParentSelected | boolean | no | Specifies if the children should be selected when parent item is selected (defaults to false).|
176177

177178

178179
Interface `IPickerTerm`

src/controls/taxonomyPicker/ITaxonomyPicker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ export interface ITaxonomyPickerProps {
131131
* Panel selection change handler. Can be used to interact with the control while selecting items in the panel, before Click or Cancel is clicked.
132132
*/
133133
onPanelSelectionChange?: (prevValue: IPickerTerms, newValue: IPickerTerms) => void;
134+
135+
/**
136+
* Specifies if the childrens should be selected when parent is selected.
137+
* By default this is set to false.
138+
*/
139+
selectChildrenIfParentSelected?: boolean;
134140
}
135141

136142
/**

src/controls/taxonomyPicker/TaxonomyPicker.tsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
236236
return;
237237
}
238238

239+
const {
240+
allowMultipleSelections,
241+
selectChildrenIfParentSelected
242+
} = this.props;
243+
244+
const {
245+
termSetAndTerms
246+
} = this.state;
247+
239248
// Term item to add to the active nodes array
240249
const termItem = {
241250
name: term.Name,
@@ -244,27 +253,59 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
244253
termSet: term.TermSet.Id
245254
};
246255

256+
//
257+
// checking if we need to process child terms
258+
//
259+
let children: ITerm[] = [];
260+
if (allowMultipleSelections && selectChildrenIfParentSelected) {
261+
if (term.Id === term.TermSet.Id) { // term set selected
262+
children = termSetAndTerms.Terms || [];
263+
}
264+
else {
265+
children = termSetAndTerms.Terms ? termSetAndTerms.Terms.filter(t => {
266+
return t.PathOfTerm.indexOf(`${term.PathOfTerm};`) !== -1;
267+
}) : [];
268+
}
269+
}
270+
247271
// Check if the term is checked or unchecked
248272
if (checked) {
249273
// Check if it is allowed to select multiple terms
250-
if (this.props.allowMultipleSelections) {
274+
if (allowMultipleSelections) {
251275
// Add the checked term
252276
activeNodes.push(termItem);
277+
253278
// Filter out the duplicate terms
254279
activeNodes = uniqBy(activeNodes, 'key');
255280
} else {
256281
// Only store the current selected item
257282
activeNodes = [termItem];
258283
}
284+
285+
if (children.length) {
286+
activeNodes.push(...children.map(c => {
287+
return {
288+
name: c.Name,
289+
key: c.Id,
290+
path: c.PathOfTerm,
291+
termSet: c.TermSet.Id
292+
};
293+
}));
294+
}
259295
} else {
260296
// Remove the term from the list of active nodes
261297
activeNodes = activeNodes.filter(item => item.key !== term.Id);
298+
299+
if (children.length) {
300+
const childIds = children.map(c => c.Id);
301+
activeNodes = activeNodes.filter(item => childIds.indexOf(item.key) === -1);
302+
}
262303
}
263304
// Sort all active nodes
264305
activeNodes = sortBy(activeNodes, 'path');
265306

266307
if (this.props.onPanelSelectionChange) {
267-
this.props.onPanelSelectionChange(this.state.activeNodes.slice(), activeNodes)
308+
this.props.onPanelSelectionChange(this.state.activeNodes.slice(), activeNodes);
268309
}
269310

270311
// Update the current state

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
717717
label="Service Picker with custom actions"
718718
context={this.props.context}
719719
onChange={this.onServicePickerChange}
720-
isTermSetSelectable={false}
720+
isTermSetSelectable={true}
721721
termActions={{
722722
actions: [{
723723
title: "Get term labels",

0 commit comments

Comments
 (0)