|
| 1 | +import * as telemetry from '../../common/telemetry'; |
| 2 | +import React from 'react'; |
| 3 | +import { IContentTypePickerProps, IContentTypePickerState } from './IContentTypePicker'; |
| 4 | +import { ISPService } from '../../services/ISPService'; |
| 5 | +import { SPServiceFactory } from '../../services/SPServiceFactory'; |
| 6 | +import { cloneDeep } from 'lodash'; |
| 7 | +import { Dropdown, IDropdownOption, IDropdownProps, Spinner, SpinnerSize } from 'office-ui-fabric-react'; |
| 8 | + |
| 9 | +const EMPTY_CONTENTTYPE_KEY = 'NO_CONTENTTYPE_SELECTED'; |
| 10 | + |
| 11 | +export class ContentTypePicker extends React.Component<IContentTypePickerProps, IContentTypePickerState> { |
| 12 | + |
| 13 | + private _selectedContentTypes: string | string[] = null; |
| 14 | + |
| 15 | + constructor(props: IContentTypePickerProps) { |
| 16 | + super(props); |
| 17 | + |
| 18 | + telemetry.track('ReactContentTypePicker'); |
| 19 | + |
| 20 | + this.state = { |
| 21 | + contentTypes: [], |
| 22 | + loading: false, |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + public componentDidMount(): void { |
| 27 | + this.loadContentTypes(); |
| 28 | + } |
| 29 | + |
| 30 | + private async loadContentTypes(): Promise<void> { |
| 31 | + const { |
| 32 | + context, |
| 33 | + listId, |
| 34 | + includeHidden, |
| 35 | + includeReadOnly, |
| 36 | + orderBy, |
| 37 | + filter, |
| 38 | + group, |
| 39 | + webAbsoluteUrl, |
| 40 | + filterItems, |
| 41 | + } = this.props; |
| 42 | + |
| 43 | + // Show the loading indicator and disable the dropdown |
| 44 | + this.setState({ loading: true }); |
| 45 | + |
| 46 | + const service: ISPService = SPServiceFactory.createService(context, true, 5000, webAbsoluteUrl); |
| 47 | + let results = await service.getContentTypes( |
| 48 | + { |
| 49 | + listId, |
| 50 | + filter, |
| 51 | + includeHidden, |
| 52 | + includeReadOnly, |
| 53 | + orderBy, |
| 54 | + group, |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + // Check if custom filter is specified |
| 59 | + if (filterItems) { |
| 60 | + results = filterItems(results); |
| 61 | + } |
| 62 | + |
| 63 | + // Hide the loading indicator and set the dropdown options |
| 64 | + this.setState({ |
| 65 | + loading: false, |
| 66 | + contentTypes: results, |
| 67 | + }); |
| 68 | + |
| 69 | + this.setSelectedContentTypes(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Set the currently selected content type(s). |
| 74 | + */ |
| 75 | + private setSelectedContentTypes(): void { |
| 76 | + this._selectedContentTypes = cloneDeep(this.props.selectedContentTypes); |
| 77 | + |
| 78 | + this.setState({ |
| 79 | + selectedContentTypes: this._selectedContentTypes, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public componentDidUpdate(prevProps: Readonly<IContentTypePickerProps>, prevState: Readonly<IContentTypePickerState>): void { |
| 84 | + const { |
| 85 | + includeHidden, |
| 86 | + includeReadOnly, |
| 87 | + orderBy, |
| 88 | + webAbsoluteUrl, |
| 89 | + selectedContentTypes, |
| 90 | + listId, |
| 91 | + } = this.props; |
| 92 | + |
| 93 | + if ( |
| 94 | + prevProps.includeHidden !== includeHidden || |
| 95 | + prevProps.includeReadOnly !== includeReadOnly || |
| 96 | + prevProps.orderBy !== orderBy || |
| 97 | + prevProps.webAbsoluteUrl !== webAbsoluteUrl || |
| 98 | + prevProps.listId !== listId |
| 99 | + ) { |
| 100 | + this.loadContentTypes(); |
| 101 | + } |
| 102 | + |
| 103 | + if (prevProps.selectedContentTypes !== selectedContentTypes) { |
| 104 | + this.setSelectedContentTypes(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Fires when an item has been selected from the dropdown. |
| 110 | + * @param event Event that has been fired. |
| 111 | + * @param option The new selection. |
| 112 | + * @param index Index of the selection. |
| 113 | + */ |
| 114 | + private onChange = (event: React.FormEvent<HTMLDivElement>, option: IDropdownOption, index?: number): void => { |
| 115 | + const { multiSelect, onSelectionChanged } = this.props; |
| 116 | + const { contentTypes } = this.state; |
| 117 | + |
| 118 | + if (multiSelect) { |
| 119 | + let selectedContentTypes = this._selectedContentTypes ? cloneDeep(this._selectedContentTypes) as string[] : []; |
| 120 | + |
| 121 | + if (option.selected) { |
| 122 | + selectedContentTypes.push(option.key.toString()); |
| 123 | + } |
| 124 | + else { |
| 125 | + selectedContentTypes = selectedContentTypes.filter(ct => ct !== option.key); |
| 126 | + } |
| 127 | + this._selectedContentTypes = selectedContentTypes; |
| 128 | + } |
| 129 | + else { |
| 130 | + this._selectedContentTypes = option.key.toString(); |
| 131 | + } |
| 132 | + |
| 133 | + if (onSelectionChanged) { |
| 134 | + if (multiSelect) { |
| 135 | + onSelectionChanged(cloneDeep(contentTypes.filter(ct => (this._selectedContentTypes as string[]).some(sct => ct.StringId === sct)))); |
| 136 | + } |
| 137 | + else { |
| 138 | + onSelectionChanged(cloneDeep(contentTypes.find(ct => ct.StringId === this._selectedContentTypes as string))); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public render(): React.ReactElement<IContentTypePickerProps> { |
| 144 | + const { loading, contentTypes, selectedContentTypes } = this.state; |
| 145 | + const { className, disabled, multiSelect, label, placeholder, showBlankOption } = this.props; |
| 146 | + |
| 147 | + const options: IDropdownOption[] = contentTypes.map(f => ({ |
| 148 | + key: f.StringId, |
| 149 | + text: f.Name, |
| 150 | + })); |
| 151 | + |
| 152 | + if (showBlankOption && !multiSelect) { |
| 153 | + // Provide empty option |
| 154 | + options.unshift({ |
| 155 | + key: EMPTY_CONTENTTYPE_KEY, |
| 156 | + text: '', |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + const dropdownProps: IDropdownProps = { |
| 161 | + className, |
| 162 | + options, |
| 163 | + disabled: loading || disabled, |
| 164 | + label, |
| 165 | + placeholder, |
| 166 | + onChange: this.onChange, |
| 167 | + }; |
| 168 | + |
| 169 | + if (multiSelect) { |
| 170 | + dropdownProps.multiSelect = true; |
| 171 | + dropdownProps.selectedKeys = selectedContentTypes as string[]; |
| 172 | + } |
| 173 | + else { |
| 174 | + dropdownProps.selectedKey = selectedContentTypes as string; |
| 175 | + } |
| 176 | + |
| 177 | + return ( |
| 178 | + <> |
| 179 | + {loading && <Spinner size={SpinnerSize.xSmall} />} |
| 180 | + <Dropdown {...dropdownProps} /> |
| 181 | + </> |
| 182 | + ); |
| 183 | + } |
| 184 | +} |
0 commit comments