-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCascadeddropdownWebPart.ts
More file actions
133 lines (118 loc) · 4.14 KB
/
CascadeddropdownWebPart.ts
File metadata and controls
133 lines (118 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import { BaseClientSideWebPart, PropertyPaneDropdown } from '@microsoft/sp-webpart-base';
import {
IPropertyPaneConfiguration,
PropertyPaneTextField,
IPropertyPaneDropdownOption
} from '@microsoft/sp-property-pane';
import * as strings from 'CascadeddropdownWebPartStrings';
import Cascadeddropdown from './components/Cascadeddropdown';
import { ICascadeddropdownProps } from './components/ICascadeddropdownProps';
import { IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
import { sp } from "@pnp/sp/presets/all";
export interface ICascadeddropdownWebPartProps {
description: string;
selectedList: string;
selectedItem: string;
}
export default class CascadeddropdownWebPart extends BaseClientSideWebPart<ICascadeddropdownWebPartProps> {
private listOptions: IPropertyPaneDropdownOption[] = [];
private itemOptions: IPropertyPaneDropdownOption[] = [];
public render(): void {
if (!this.context.propertyPane.isPropertyPaneOpen() && this.properties.selectedList === undefined) {
this.context.propertyPane.open();
}
const element: React.ReactElement<ICascadeddropdownProps> = React.createElement(
Cascadeddropdown,
{
description: this.properties.description,
selectedList: this.properties.selectedList,
selectedItem: this.properties.selectedItem
}
);
ReactDom.render(element, this.domElement);
}
protected onPropertyPaneConfigurationStart(): void {
this.getAllLists().then((allLists) => {
this.listOptions = allLists;
this.render();
this.context.propertyPane.refresh();
})
}
protected onPropertyPaneFieldChanged(propertyPath: string, oldValue: any, newValue: any): void {
if (propertyPath === "selectedList") {
super.onPropertyPaneFieldChanged(propertyPath, oldValue, newValue);
this.getItems().then((itemOptions) => {
this.itemOptions = itemOptions;
this.render();
// refresh the item selector control by repainting the property pane
this.context.propertyPane.refresh();
});
}
}
private getItems(): Promise<IPropertyPaneDropdownOption[]> {
let itemOptions: IPropertyPaneDropdownOption[] = [];
return sp.web.lists.getById(this.properties.selectedList).items.get().then((listItems) => {
listItems.forEach(item => {
itemOptions.push({
key: item.Id,
text: item.Title
});
});
return Promise.resolve(itemOptions);
})
}
private getAllLists(): Promise<IPropertyPaneDropdownOption[]> {
let listOptions: IPropertyPaneDropdownOption[] = [];
try {
return sp.web.lists.filter(`Hidden eq false`).get().then((allLists) => {
allLists.forEach(list => {
listOptions.push({
key: list.Id,
text: list.Title
});
});
return Promise.resolve(listOptions);
});
} catch (error) {
console.error(error);
}
}
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
}),
PropertyPaneDropdown('selectedList', {
label: `Please select list`,
options: this.listOptions
}),
PropertyPaneDropdown('selectedItem', {
label: `Please select Item`,
options: this.itemOptions
})
]
}
]
}
]
};
}
}