Skip to content

Commit d7d10d2

Browse files
Added callback to update taxonomy tree view with changed terms
1 parent dfe3c29 commit d7d10d2

File tree

3 files changed

+182
-32
lines changed

3 files changed

+182
-32
lines changed

docs/documentation/docs/controls/ModernTaxonomyPicker.md

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,53 @@ private onTaxPickerChange(terms : ITermInfo[]) {
5252
```
5353

5454
## Advanced example
55-
Custom rendering of a More actions button that displays a context menu for each term in the term set and the term set itself and with different options for the terms and the term set. This could for example be used to add terms to an open term set. It also shows how to set the initialsValues property when just knowing the name and the id of the term, the rest of the term properties must be provided but doesn't need to be the correct values.
55+
Custom rendering of a More actions button that displays a context menu for each term in the term set and the term set itself and with different options for the terms and the term set. This could for example be used to add terms to an open term set. It also shows how to set the initialsValues property when just knowing the name and the id of the term.
5656

5757
```TypeScript
58+
const termSetId = "36d21c3f-b83b-4acc-a223-4df6fa8e946d";
59+
const [clickedActionTerm, setClickedActionTerm] = React.useState<ITermInfo>();
60+
61+
const addChildTerm = (parentTermId, updateTaxonomyTreeViewCallback): void => {
62+
spPost(sp.termStore.sets.getById(termSetId).terms.getById(parentTermId).children, {
63+
body: JSON.stringify({
64+
"labels": [
65+
{
66+
"languageTag": "en-US",
67+
"name": "Test",
68+
"isDefault": true
69+
}
70+
]
71+
}),
72+
})
73+
.then(addedTerm => {
74+
return sp.termStore.sets.getById(termSetId).terms.getById(addedTerm.id).expand("parent")();
75+
})
76+
.then(term => {
77+
updateTaxonomyTreeViewCallback([term], null, null);
78+
});
79+
}
80+
81+
...
82+
5883
<ModernTaxonomyPicker
5984
allowMultipleSelections={true}
60-
termSetId={"36d21c3f-b83b-4acc-a223-4df6fa8e946d"}
85+
termSetId={termSetId}
6186
panelTitle="Panel title"
6287
label={"Field title"}
6388
context={this.props.context}
6489
required={false}
65-
initialValues={[{labels: [{name: "Subprocess A1", isDefault: true, languageTag: "en-US"}], id: "29eced8f-cf08-454b-bd9e-6443bc0a0f5e", childrenCount: 0, createdDateTime: "", lastModifiedDateTime: "", descriptions: [], customSortOrder: [], properties: [], localProperties: [], isDeprecated: false, isAvailableForTagging: [], topicRequested: false}]}
90+
initialValues={[{labels: [{name: "Subprocess A1", isDefault: true, languageTag: "en-US"}], id: "29eced8f-cf08-454b-bd9e-6443bc0a0f5e"}]}
6691
onChange={this.onTaxPickerChange}
6792
disabled={false}
6893
customPanelWidth={700}
6994
isLightDismiss={false}
7095
isBlocking={false}
71-
onRenderActionButton={(termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo?: ITermInfo): JSX.Element => {
96+
onRenderActionButton={(
97+
termStoreInfo: ITermStoreInfo,
98+
termSetInfo: ITermSetInfo,
99+
termInfo: ITermInfo
100+
updateTaxonomyTreeViewCallback?: (newTermItems?: ITermInfo[], updatedTermItems?: ITermInfo[], deletedTermItems?: ITermInfo[]) => void
101+
): JSX.Element => {
72102
const menuIcon: IIconProps = { iconName: 'MoreVertical', "aria-label": "More actions", style: { fontSize: "medium" } };
73103
if (termInfo) {
74104
const menuProps: IContextualMenuProps = {
@@ -77,13 +107,13 @@ Custom rendering of a More actions button that displays a context menu for each
77107
key: 'addTerm',
78108
text: 'Add Term',
79109
iconProps: { iconName: 'Tag' },
80-
onClick: () => onContextualMenuClick(termInfo.id)
110+
onClick: () => addChildTerm(termInfo.id, updateTaxonomyTreeViewCallback)
81111
},
82112
{
83113
key: 'deleteTerm',
84114
text: 'Delete term',
85115
iconProps: { iconName: 'Untag' },
86-
onClick: () => onContextualMenuClick(termInfo.id)
116+
onClick: () => deleteTerm(termInfo.id, updateTaxonomyTreeViewCallback)
87117
},
88118
],
89119
};
@@ -92,11 +122,11 @@ Custom rendering of a More actions button that displays a context menu for each
92122
<IconButton
93123
menuProps={menuProps}
94124
menuIconProps={menuIcon}
95-
style={this.state.clickedActionTerm && this.state.clickedActionTerm.id === termInfo.id ? {opacity: 1} : null}
125+
style={clickedActionTerm && clickedActionTerm.id === termInfo.id ? {opacity: 1} : null}
96126
onMenuClick={(ev?: React.MouseEvent<HTMLElement, MouseEvent> | React.KeyboardEvent<HTMLElement>, button?: IButtonProps) => {
97-
this.setState({clickedActionTerm: termInfo});
127+
setClickedActionTerm(termInfo));
98128
}}
99-
onAfterMenuDismiss={() => this.setState({clickedActionTerm: null})}
129+
onAfterMenuDismiss={() => setClickedActionTerm(null)}
100130
/>
101131
);
102132
}
@@ -107,7 +137,7 @@ Custom rendering of a More actions button that displays a context menu for each
107137
key: 'addTerm',
108138
text: 'Add term',
109139
iconProps: { iconName: 'Tag' },
110-
onClick: () => onContextualMenuClick(termSetInfo.id)
140+
onClick: () => addTerm(termInfo.id, updateTaxonomyTreeViewCallback)
111141
},
112142
],
113143
};
@@ -177,7 +207,7 @@ You can also use the `TaxonomyTree` control separately to just render a stand-al
177207
}, []);
178208

179209
return (
180-
{currentTermSetInfo && (
210+
{currentTermStoreInfo && currentTermSetInfo && currentLanguageTag && (
181211
<TaxonomyTree
182212
languageTag={currentLanguageTag}
183213
onLoadMoreData={taxonomyService.getTerms}

src/controls/modernTaxonomyPicker/taxonomyPanelContents/TaxonomyPanelContents.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface ITaxonomyPanelContentsProps {
3838
languageTag: string;
3939
themeVariant?: IReadonlyTheme;
4040
termPickerProps?: Optional<IModernTermPickerProps, 'onResolveSuggestions'>;
41-
onRenderActionButton?: (termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo?: ITermInfo) => JSX.Element;
41+
onRenderActionButton?: (termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo: ITermInfo, updateTaxonomyTreeViewCallback?: (newTermItems?: ITermInfo[], updatedTermItems?: ITermInfo[], deletedTermItems?: ITermInfo[]) => void) => JSX.Element;
4242
}
4343

4444
export function TaxonomyPanelContents(props: ITaxonomyPanelContentsProps): React.ReactElement<ITaxonomyPanelContentsProps> {

0 commit comments

Comments
 (0)