1
+ import * as React from 'react' ;
2
+ import { IDropdownOption , IDropdownProps , Dropdown } from 'office-ui-fabric-react/lib/components/Dropdown' ;
3
+ import { Spinner , SpinnerSize } from 'office-ui-fabric-react/lib/components/Spinner' ;
4
+
5
+ import { IListPickerProps , IListPickerState } from './IListPicker' ;
6
+ import { ISPService } from '../../services/ISPService' ;
7
+ import { SPServiceFactory } from '../../services/SPServiceFactory' ;
8
+
9
+ import styles from './ListPicker.module.scss' ;
10
+
11
+ /**
12
+ * Empty list value, to be checked for single list selection
13
+ */
14
+ const EMPTY_LIST_KEY = 'NO_LIST_SELECTED' ;
15
+
16
+ /**
17
+ * Renders the controls for the ListPicker component
18
+ */
19
+ export class ListPicker extends React . Component < IListPickerProps , IListPickerState > {
20
+ private _options : IDropdownOption [ ] = [ ] ;
21
+ private _selectedList : string | string [ ] ;
22
+
23
+ /**
24
+ * Constructor method
25
+ */
26
+ constructor ( props : IListPickerProps ) {
27
+ super ( props ) ;
28
+
29
+ console . debug ( 'selectedList' , this . props . selectedList ) ;
30
+
31
+ this . state = {
32
+ options : this . _options ,
33
+ loading : false
34
+ } ;
35
+
36
+ this . onChanged = this . onChanged . bind ( this ) ;
37
+ }
38
+
39
+ /**
40
+ * Lifecycle hook when component is mounted
41
+ */
42
+ public componentDidMount ( ) {
43
+ this . loadLists ( ) ;
44
+ }
45
+
46
+ /**
47
+ * Loads the list from SharePoint current web site
48
+ */
49
+ private loadLists ( ) {
50
+ const { context, baseTemplate, includeHidden, orderBy, multiSelect, selectedList } = this . props ;
51
+
52
+ // Show the loading indicator and disable the dropdown
53
+ this . setState ( { loading : true } ) ;
54
+
55
+ const service : ISPService = SPServiceFactory . createService ( context , true , 5000 ) ;
56
+ service . getLibs ( {
57
+ baseTemplate : baseTemplate ,
58
+ includeHidden : includeHidden ,
59
+ orderBy : orderBy
60
+ } ) . then ( ( results ) => {
61
+ // Start mapping the lists to the dropdown option
62
+ results . value . map ( list => {
63
+ this . _options . push ( {
64
+ key : list . Id ,
65
+ text : list . Title
66
+ } ) ;
67
+ } ) ;
68
+
69
+ if ( multiSelect !== true ) {
70
+ // Add option to unselct list
71
+ this . _options . unshift ( {
72
+ key : EMPTY_LIST_KEY ,
73
+ text : ''
74
+ } ) ;
75
+ }
76
+
77
+ this . _selectedList = this . props . selectedList ;
78
+
79
+ // Hide the loading indicator and set the dropdown options and enable the dropdown
80
+ this . setState ( {
81
+ loading : false ,
82
+ options : this . _options ,
83
+ selectedList : this . _selectedList
84
+ } ) ;
85
+ } ) ;
86
+ }
87
+
88
+ /**
89
+ * Raises when a list has been selected
90
+ * @param option the new selection
91
+ * @param index the index of the selection
92
+ */
93
+ private onChanged ( option : IDropdownOption , index ?: number ) : void {
94
+ const { multiSelect, onSelectionChanged } = this . props ;
95
+
96
+ if ( multiSelect === true ) {
97
+ if ( this . _selectedList === undefined ) {
98
+ this . _selectedList = new Array < string > ( ) ;
99
+ }
100
+ ( this . _selectedList as string [ ] ) . push ( option . key as string ) ;
101
+ } else {
102
+ this . _selectedList = option . key as string ;
103
+ }
104
+
105
+ if ( onSelectionChanged ) {
106
+ onSelectionChanged ( this . _selectedList ) ;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Renders the ListPicker controls with Office UI Fabric
112
+ */
113
+ public render ( ) : JSX . Element {
114
+ const { loading, options, selectedList } = this . state ;
115
+ const { className, disabled, multiSelect, label, placeHolder } = this . props ;
116
+
117
+ const dropdownOptions : IDropdownProps = {
118
+ className : className ,
119
+ options : options ,
120
+ disabled : ( loading || disabled ) ,
121
+ label : label ,
122
+ placeHolder : placeHolder ,
123
+ onChanged : this . onChanged
124
+ } ;
125
+
126
+ if ( multiSelect === true ) {
127
+ dropdownOptions . multiSelect = true ;
128
+ dropdownOptions . selectedKeys = selectedList as string [ ] ;
129
+ } else {
130
+ dropdownOptions . selectedKey = selectedList as string ;
131
+ }
132
+
133
+ return (
134
+ < div className = { styles . listPicker } >
135
+ { loading && < Spinner className = { styles . spinner } size = { SpinnerSize . xSmall } /> }
136
+ < Dropdown { ...dropdownOptions } />
137
+ </ div >
138
+ ) ;
139
+ }
140
+ }
141
+
142
+ export default ListPicker ;
0 commit comments