Skip to content

Commit 9033c54

Browse files
Added feature to add action button to terms
1 parent 41be03e commit 9033c54

File tree

7 files changed

+320
-105
lines changed

7 files changed

+320
-105
lines changed

docs/documentation/docs/controls/ModernTaxonomyPicker.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ import { ModernTaxonomyPicker } from "@pnp/spfx-controls-react/lib/ModernTaxonom
3535

3636
```TypeScript
3737
<ModernTaxonomyPicker allowMultipleSelections={true}
38-
termSetId="f233d4b7-68fb-41ef-8b58-2af0bafc0d38"
39-
panelTitle="Select Term"
40-
label="Taxonomy Picker"
41-
context={this.props.context}
42-
onChange={this.onTaxPickerChange} />
38+
termSetId="f233d4b7-68fb-41ef-8b58-2af0bafc0d38"
39+
panelTitle="Select Term"
40+
label="Taxonomy Picker"
41+
context={this.props.context}
42+
onChange={this.onTaxPickerChange}
43+
/>
4344
```
4445

4546
- With the `onChange` property you can capture the event of when the terms in the picker has changed:
@@ -50,6 +51,78 @@ private onTaxPickerChange(terms : ITermInfo[]) {
5051
}
5152
```
5253

54+
## 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.
56+
57+
```TypeScript
58+
<ModernTaxonomyPicker
59+
allowMultipleSelections={true}
60+
termSetId={"36d21c3f-b83b-4acc-a223-4df6fa8e946d"}
61+
panelTitle="Panel title"
62+
label={"Field title"}
63+
context={this.props.context}
64+
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}]}
66+
onChange={this.onTaxPickerChange}
67+
disabled={false}
68+
customPanelWidth={700}
69+
isLightDismiss={false}
70+
isBlocking={false}
71+
onRenderActionButton={(termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo?: ITermInfo) => {
72+
const menuIcon: IIconProps = { iconName: 'MoreVertical', "aria-label": "More actions", style: { fontSize: "medium" } };
73+
if (termInfo) {
74+
const menuProps: IContextualMenuProps = {
75+
items: [
76+
{
77+
key: 'addTerm',
78+
text: 'Add Term',
79+
iconProps: { iconName: 'Tag' },
80+
onClick: () => onContextualMenuClick(termInfo.id)
81+
},
82+
{
83+
key: 'deleteTerm',
84+
text: 'Delete term',
85+
iconProps: { iconName: 'Untag' },
86+
onClick: () => onContextualMenuClick(termInfo.id)
87+
},
88+
],
89+
};
90+
91+
return (
92+
<IconButton
93+
menuProps={menuProps}
94+
menuIconProps={menuIcon}
95+
style={this.state.clickedActionTerm && this.state.clickedActionTerm.id === termInfo.id ? {opacity: 1} : null}
96+
onMenuClick={(ev?: React.MouseEvent<HTMLElement, MouseEvent> | React.KeyboardEvent<HTMLElement>, button?: IButtonProps) => {
97+
this.setState({clickedActionTerm: termInfo});
98+
}}
99+
onAfterMenuDismiss={() => this.setState({clickedActionTerm: null})}
100+
/>
101+
);
102+
}
103+
else {
104+
const menuProps: IContextualMenuProps = {
105+
items: [
106+
{
107+
key: 'addTerm',
108+
text: 'Add term',
109+
iconProps: { iconName: 'Tag' },
110+
onClick: () => onContextualMenuClick(termSetInfo.id)
111+
},
112+
],
113+
};
114+
return (
115+
<IconButton
116+
menuProps={menuProps}
117+
menuIconProps={menuIcon}
118+
style={{opacity: 1}}
119+
/>
120+
);
121+
}
122+
}}
123+
/>
124+
```
125+
53126
## Implementation
54127

55128
The ModernTaxonomyPicker control can be configured with the following properties:
@@ -70,5 +143,8 @@ The ModernTaxonomyPicker control can be configured with the following properties
70143
| customPanelWidth | number | no | Custom panel width in pixels. |
71144
| termPickerProps | IModernTermPickerProps | no | Custom properties for the term picker (More info: [IBasePickerProps interface](https://developer.microsoft.com/en-us/fluentui#/controls/web/pickers#IBasePickerProps)). |
72145
| themeVariant | IReadonlyTheme | no | The current loaded SharePoint theme/section background (More info: [Supporting section backgrounds](https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/guidance/supporting-section-backgrounds)). |
146+
| isLightDismiss | boolean | no | Whether the panel can be light dismissed. |
147+
| isBlocking | boolean | no | Whether the panel uses a modal overlay or not. |
148+
| onRenderActionButton | function | no | Optional custom renderer for adding e.g. a button with additional actions to the terms in the tree view. |
73149

74150
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/wiki/controls/TaxonomyPicker)

src/controls/modernTaxonomyPicker/ModernTaxonomyPicker.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export interface IModernTaxonomyPickerProps {
6060
customPanelWidth?: number;
6161
themeVariant?: IReadonlyTheme;
6262
termPickerProps?: Optional<IModernTermPickerProps, 'onResolveSuggestions'>;
63+
isLightDismiss?: boolean;
64+
isBlocking?: boolean;
65+
onRenderActionButton?: (termStoreInfo: ITermStoreInfo, termSetInfo: ITermSetInfo, termInfo?: ITermInfo) => JSX.Element;
6366
}
6467

6568
export function ModernTaxonomyPicker(props: IModernTaxonomyPickerProps) {
@@ -235,7 +238,8 @@ export function ModernTaxonomyPicker(props: IModernTaxonomyPickerProps) {
235238
hasCloseButton={true}
236239
closeButtonAriaLabel={strings.ModernTaxonomyPickerPanelCloseButtonText}
237240
onDismiss={onClosePanel}
238-
isLightDismiss={true}
241+
isLightDismiss={props.isLightDismiss}
242+
isBlocking={props.isBlocking}
239243
type={props.customPanelWidth ? PanelType.custom : PanelType.medium}
240244
customWidth={props.customPanelWidth ? `${props.customPanelWidth}px` : undefined}
241245
headerText={props.panelTitle}
@@ -273,6 +277,7 @@ export function ModernTaxonomyPicker(props: IModernTaxonomyPickerProps) {
273277
languageTag={currentLanguageTag}
274278
themeVariant={props.themeVariant}
275279
termPickerProps={props.termPickerProps}
280+
onRenderActionButton={props.onRenderActionButton}
276281
/>
277282
</div>
278283
)

src/controls/modernTaxonomyPicker/taxonomyPanelContents/TaxonomyPanelContents.module.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@
5555
height: 48px;
5656
line-height: 48px;
5757
}
58+
59+
.taxonomyItemFocusZone {
60+
display: flex;
61+
align-items: center;
62+
width: 100%;
63+
}
64+
65+
.taxonomyItemHeader {
66+
width: 100%;
67+
}
68+
69+
.taxonomyItemHeader .actionButtonContainer > * {
70+
opacity: 0;
71+
}
72+
73+
.taxonomyItemHeader:hover .actionButtonContainer > *, .taxonomyItemHeader:focus .actionButtonContainer > *, .taxonomyItemHeader .actionButtonContainer:focus-within > * {
74+
opacity: 1;
75+
}
5876
}

0 commit comments

Comments
 (0)