1
1
# Use cascading dropdowns in web part properties
2
2
3
- > Note. This article has not yet been verified with SPFx GA version, so you might have challenges on making this work as such with the latest release.
4
-
5
3
When designing the property pane for your SharePoint client-side web parts, you may have one web part property that displays its options based on the value selected in another property. This scenario typically occurs when implementing cascading dropdown controls. In this article, you will learn how to create cascading dropdown controls in the web part property pane without developing a custom property pane control.
6
4
7
5
![ Item dropdown disabled and web part placeholder communicating loading updated list of item options] ( ../../../../images/react-cascading-dropdowns-loading-indicator-when-loading-items.png )
@@ -34,9 +32,9 @@ When prompted, enter the following values:
34
32
35
33
- ** react-cascadingdropdowns** as your solution name
36
34
- ** Use the current folder** for the ___location to place the files
35
+ - ** React** as the starting point to build the web part
37
36
- ** List items** as your web part name
38
37
- ** Shows list items from the selected list** as your web part description
39
- - ** React** as the starting point to build the web part
40
38
41
39
![ SharePoint Framework Yeoman generator with the default choices] ( ../../../../images/react-cascading-dropdowns-yo-sharepoint.png )
42
40
@@ -81,7 +79,7 @@ Update the **propertyPaneSettings** getter to:
81
79
``` ts
82
80
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
83
81
// ...
84
- protected get propertyPaneSettings (): IPropertyPaneSettings {
82
+ protected getPropertyPaneConfiguration (): IPropertyPaneConfiguration {
85
83
return {
86
84
pages: [
87
85
{
@@ -132,26 +130,17 @@ In the **src/webparts/listItems/components/ListItems.tsx** file, change the cont
132
130
133
131
``` tsx
134
132
export default class ListItems extends React .Component <IListItemsProps , {}> {
135
- public render(): JSX .Element {
133
+ public render(): JSX .Element {
136
134
return (
137
- <div className = { styles .listItems } >
135
+ <div className = { styles .helloWorld } >
138
136
<div className = { styles .container } >
139
- <div className = { css (' ms-Grid-row ms-bgColor-themeDark ms-fontColor-white' , styles .row )} >
140
- <div className = ' ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1' >
141
- <span className = ' ms-font-xl ms-fontColor-white' >
142
- Welcome to SharePoint!
143
- </span >
144
- <p className = ' ms-font-l ms-fontColor-white' >
145
- Customize SharePoint experiences using web parts.
146
- </p >
147
- <p className = ' ms-font-l ms-fontColor-white' >
148
- { this .props .listName }
149
- </p >
150
- <a
151
- className = { css (' ms-Button' , styles .button )}
152
- href = ' https://github.com/SharePoint/sp-dev-docs/wiki'
153
- >
154
- <span className = ' ms-Button-label' >Learn more</span >
137
+ <div className = { ` ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles .row } ` } >
138
+ <div className = " ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1" >
139
+ <span className = " ms-font-xl ms-fontColor-white" >Welcome to SharePoint!</span >
140
+ <p className = " ms-font-l ms-fontColor-white" >Customize SharePoint experiences using Web Parts.</p >
141
+ <p className = " ms-font-l ms-fontColor-white" >{ escape (this .props .listName )} </p >
142
+ <a href = " https://aka.ms/spfx" className = { styles .button } >
143
+ <span className = { styles .label } >Learn more</span >
155
144
</a >
156
145
</div >
157
146
</div >
@@ -161,6 +150,13 @@ export default class ListItems extends React.Component<IListItemsProps, {}> {
161
150
}
162
151
}
163
152
```
153
+ In the ** src/webparts/listItems/components/IListItemsProps.ts** file, change the ** IListItemsProps** interface to:
154
+
155
+ ``` ts
156
+ export interface IListItemsProps {
157
+ listName: string ;
158
+ }
159
+ ```
164
160
165
161
Run the following command to verify that the project is running:
166
162
@@ -183,23 +179,18 @@ In the **ListItemsWebPart** class add a reference to the **PropertyPaneDropdown*
183
179
``` ts
184
180
import {
185
181
BaseClientSideWebPart ,
186
- IPropertyPaneSettings ,
187
- IWebPartContext ,
188
- PropertyPaneDropdown
182
+ IPropertyPaneConfiguration ,
183
+ PropertyPaneTextField ,
184
+ PropertyPaneDropdown ,
185
+ IPropertyPaneDropdownOption
189
186
} from ' @microsoft/sp-webpart-base' ;
190
187
```
191
188
192
- Right after this import statement, add a reference to the ** IDropdownOption** interface.
193
-
194
- ``` ts
195
- import { IDropdownOption } from ' office-ui-fabric-react' ;
196
- ```
197
-
198
189
In the ** ListItemsWebPart** class, add a new variable named ** lists** to store information about all available lists in the current site.
199
190
200
191
``` ts
201
192
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
202
- private lists: IDropdownOption [];
193
+ private lists: IPropertyPaneDropdownOption [];
203
194
// ...
204
195
}
205
196
```
@@ -233,7 +224,7 @@ export default class ListItemsWebPart extends BaseClientSideWebPart<IListItemsWe
233
224
PropertyPaneDropdown (' listName' , {
234
225
label: strings .ListNameFieldLabel ,
235
226
options: this .lists ,
236
- isDisabled : this .listsDropdownDisabled
227
+ disabled : this .listsDropdownDisabled
237
228
})
238
229
]
239
230
}
@@ -264,17 +255,17 @@ In the **ListItemsWebPart** class, add a method to load available lists. In this
264
255
``` ts
265
256
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
266
257
// ...
267
- private loadLists(): Promise <IDropdownOption []> {
268
- return new Promise <IDropdownOption []>((resolve : (options : IDropdownOption []) => void , reject : (error : any ) => void ) => {
258
+ private loadLists(): Promise <IPropertyPaneDropdownOption []> {
259
+ return new Promise <IPropertyPaneDropdownOption []>((resolve : (options : IPropertyPaneDropdownOption []) => void , reject : (error : any ) => void ) => {
269
260
setTimeout ((): void => {
270
261
resolve ([{
271
262
key: ' sharedDocuments' ,
272
263
text: ' Shared Documents'
273
264
},
274
- {
275
- key: ' myDocuments' ,
276
- text: ' My Documents'
277
- }]);
265
+ {
266
+ key: ' myDocuments' ,
267
+ text: ' My Documents'
268
+ }]);
278
269
}, 2000 );
279
270
});
280
271
}
@@ -298,7 +289,7 @@ export default class ListItemsWebPart extends BaseClientSideWebPart<IListItemsWe
298
289
this .context .statusRenderer .displayLoadingIndicator (this .domElement , ' lists' );
299
290
300
291
this .loadLists ()
301
- .then ((listOptions : IDropdownOption []): void => {
292
+ .then ((listOptions : IPropertyPaneDropdownOption []): void => {
302
293
this .lists = listOptions ;
303
294
this .listsDropdownDisabled = false ;
304
295
this .context .propertyPane .refresh ();
@@ -358,6 +349,15 @@ export interface IListItemsWebPartProps {
358
349
}
359
350
```
360
351
352
+ Change the code in the ** src/webparts/listItems/components/IListItemsProps.ts** file to:
353
+
354
+ ``` ts
355
+ export interface IListItemsProps {
356
+ listName: string ;
357
+ itemName: string ;
358
+ }
359
+ ```
360
+
361
361
In the ** src/webparts/listItems/ListItemsWebPart.ts** file, change the code of the ** render** method to:
362
362
363
363
``` ts
@@ -407,27 +407,16 @@ In the **src/webparts/listItems/components/ListItems.tsx** file, change the **re
407
407
export default class ListItems extends React .Component <IListItemsProps , {}> {
408
408
public render(): JSX .Element {
409
409
return (
410
- <div className = { styles .listItems } >
410
+ <div className = { styles .helloWorld } >
411
411
<div className = { styles .container } >
412
- <div className = { css (' ms-Grid-row ms-bgColor-themeDark ms-fontColor-white' , styles .row )} >
413
- <div className = ' ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1' >
414
- <span className = ' ms-font-xl ms-fontColor-white' >
415
- Welcome to SharePoint!
416
- </span >
417
- <p className = ' ms-font-l ms-fontColor-white' >
418
- Customize SharePoint experiences using web parts.
419
- </p >
420
- <p className = ' ms-font-l ms-fontColor-white' >
421
- { this .props .listName }
422
- </p >
423
- <p className = ' ms-font-l ms-fontColor-white' >
424
- { this .props .itemName }
425
- </p >
426
- <a
427
- className = { css (' ms-Button' , styles .button )}
428
- href = ' https://github.com/SharePoint/sp-dev-docs/wiki'
429
- >
430
- <span className = ' ms-Button-label' >Learn more</span >
412
+ <div className = { ` ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles .row } ` } >
413
+ <div className = " ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1" >
414
+ <span className = " ms-font-xl ms-fontColor-white" >Welcome to SharePoint!</span >
415
+ <p className = " ms-font-l ms-fontColor-white" >Customize SharePoint experiences using Web Parts.</p >
416
+ <p className = " ms-font-l ms-fontColor-white" >{ escape (this .props .listName )} </p >
417
+ <p className = " ms-font-l ms-fontColor-white" >{ escape (this .props .itemName )} </p >
418
+ <a href = " https://aka.ms/spfx" className = { styles .button } >
419
+ <span className = { styles .label } >Learn more</span >
431
420
</a >
432
421
</div >
433
422
</div >
@@ -447,7 +436,7 @@ In the **ListItemsWebPart** class, add a new variable named **items** which you
447
436
``` ts
448
437
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
449
438
// ...
450
- private items: IDropdownOption [];
439
+ private items: IPropertyPaneDropdownOption [];
451
440
// ...
452
441
}
453
442
```
@@ -467,7 +456,7 @@ Change the **propertyPaneSettings** getter to use the dropdown control to render
467
456
``` ts
468
457
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
469
458
// ...
470
- protected get propertyPaneSettings (): IPropertyPaneSettings {
459
+ protected getPropertyPaneConfiguration (): IPropertyPaneConfiguration {
471
460
return {
472
461
pages: [
473
462
{
@@ -481,12 +470,12 @@ export default class ListItemsWebPart extends BaseClientSideWebPart<IListItemsWe
481
470
PropertyPaneDropdown (' listName' , {
482
471
label: strings .ListNameFieldLabel ,
483
472
options: this .lists ,
484
- isDisabled : this .listsDropdownDisabled
473
+ disabled : this .listsDropdownDisabled
485
474
}),
486
475
PropertyPaneDropdown (' itemName' , {
487
476
label: strings .ItemNameFieldLabel ,
488
477
options: this .items ,
489
- isDisabled : this .itemsDropdownDisabled
478
+ disabled : this .itemsDropdownDisabled
490
479
})
491
480
]
492
481
}
@@ -517,15 +506,15 @@ In the **src/webparts/listItems/ListItemsWebPart.ts** file, in the **ListItemsWe
517
506
``` ts
518
507
export default class ListItemsWebPart extends BaseClientSideWebPart <IListItemsWebPartProps > {
519
508
// ...
520
- private loadItems(): Promise <IDropdownOption []> {
509
+ private loadItems(): Promise <IPropertyPaneDropdownOption []> {
521
510
if (! this .properties .listName ) {
522
511
// resolve to empty options since no list has been selected
523
512
return Promise .resolve ();
524
513
}
525
514
526
515
const wp: ListItemsWebPart = this ;
527
516
528
- return new Promise <IDropdownOption []>((resolve : (options : IDropdownOption []) => void , reject : (error : any ) => void ) => {
517
+ return new Promise <IPropertyPaneDropdownOption []>((resolve : (options : IPropertyPaneDropdownOption []) => void , reject : (error : any ) => void ) => {
529
518
setTimeout (() => {
530
519
const items = {
531
520
sharedDocuments: [
0 commit comments