/
BaseExtendedPicker.tsx
323 lines (281 loc) · 11.5 KB
/
BaseExtendedPicker.tsx
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import * as React from 'react';
import { BaseComponent, KeyCodes, css } from '../../Utilities';
import { Autofill } from '../../Autofill';
import { IInputProps } from '../../Pickers';
import * as stylesImport from './BaseExtendedPicker.scss';
import { IBaseExtendedPickerProps, IBaseExtendedPicker } from './BaseExtendedPicker.types';
import { IBaseFloatingPickerProps, BaseFloatingPicker } from '../../FloatingPicker';
import { BaseSelectedItemsList, IBaseSelectedItemsListProps } from '../../SelectedItemsList';
import { FocusZone, FocusZoneDirection } from '../../FocusZone';
import { Selection, SelectionMode, SelectionZone } from '../../Selection';
// tslint:disable-next-line:no-any
const styles: any = stylesImport;
export interface IBaseExtendedPickerState<T> {
queryString: string | null;
selectedItems: T[] | null;
suggestionItems: T[] | null;
}
export class BaseExtendedPicker<T, P extends IBaseExtendedPickerProps<T>> extends BaseComponent<P, IBaseExtendedPickerState<T>>
implements IBaseExtendedPicker<T> {
public floatingPicker = React.createRef<BaseFloatingPicker<T, IBaseFloatingPickerProps<T>>>();
public selectedItemsList = React.createRef<BaseSelectedItemsList<T, IBaseSelectedItemsListProps<T>>>();
protected root = React.createRef<HTMLDivElement>();
protected input = React.createRef<Autofill>();
protected selection: Selection;
protected floatingPickerProps: IBaseFloatingPickerProps<T>;
protected selectedItemsListProps: IBaseSelectedItemsListProps<T>;
constructor(basePickerProps: P) {
super(basePickerProps);
this.selection = new Selection({ onSelectionChanged: () => this.onSelectionChange() });
this.state = {
queryString: '',
suggestionItems: this.props.suggestionItems ? (this.props.suggestionItems as T[]) : null,
selectedItems: this.props.defaultSelectedItems
? (this.props.defaultSelectedItems as T[])
: this.props.selectedItems
? (this.props.selectedItems as T[])
: null
};
this.floatingPickerProps = this.props.floatingPickerProps;
this.selectedItemsListProps = this.props.selectedItemsListProps;
}
// tslint:disable-next-line:no-any
public get items(): any {
return this.state.selectedItems
? this.state.selectedItems
: this.selectedItemsList.current
? this.selectedItemsList.current.items
: null;
}
public componentDidMount(): void {
this.forceUpdate();
}
// tslint:disable-next-line function-name
public UNSAFE_componentWillReceiveProps(newProps: P): void {
if (newProps.floatingPickerProps) {
this.floatingPickerProps = newProps.floatingPickerProps;
}
if (newProps.selectedItemsListProps) {
this.selectedItemsListProps = newProps.selectedItemsListProps;
}
if (newProps.selectedItems) {
this.setState({ selectedItems: newProps.selectedItems });
}
}
public focus(): void {
if (this.input.current) {
this.input.current.focus();
}
}
public clearInput(): void {
if (this.input.current) {
this.input.current.clear();
}
}
public get inputElement(): HTMLInputElement | null {
return this.input.current && this.input.current.inputElement;
}
public get highlightedItems(): T[] {
return this.selectedItemsList.current ? this.selectedItemsList.current.highlightedItems() : [];
}
public render(): JSX.Element {
const { className, inputProps, disabled, focusZoneProps } = this.props;
const activeDescendant =
this.floatingPicker.current && this.floatingPicker.current.currentSelectedSuggestionIndex !== -1
? 'sug-' + this.floatingPicker.current.currentSelectedSuggestionIndex
: undefined;
const isExpanded = this.floatingPicker.current ? this.floatingPicker.current.isSuggestionsShown : false;
return (
<div
ref={this.root}
className={css('ms-BasePicker ms-BaseExtendedPicker', className ? className : '')}
onKeyDown={this.onBackspace}
onCopy={this.onCopy}
>
<FocusZone direction={FocusZoneDirection.bidirectional} {...focusZoneProps}>
<SelectionZone selection={this.selection} selectionMode={SelectionMode.multiple}>
<div className={css('ms-BasePicker-text', styles.pickerText)} role={'list'}>
{this.props.headerComponent}
{this.renderSelectedItemsList()}
{this.canAddItems() && (
<Autofill
{...inputProps as IInputProps}
className={css('ms-BasePicker-input', styles.pickerInput)}
ref={this.input}
onFocus={this.onInputFocus}
onClick={this.onInputClick}
onInputValueChange={this.onInputChange}
aria-activedescendant={activeDescendant}
aria-owns={isExpanded ? 'suggestion-list' : undefined}
aria-expanded={isExpanded}
aria-haspopup="true"
role="combobox"
disabled={disabled}
onPaste={this.onPaste}
/>
)}
</div>
</SelectionZone>
</FocusZone>
{this.renderFloatingPicker()}
</div>
);
}
protected onSelectionChange = (): void => {
this.forceUpdate();
};
protected canAddItems(): boolean {
const { itemLimit } = this.props;
return itemLimit === undefined || this.items.length < itemLimit;
}
protected renderFloatingPicker(): JSX.Element {
const FloatingPicker: React.ComponentType<IBaseFloatingPickerProps<T>> = this.props.onRenderFloatingPicker;
return (
<FloatingPicker
componentRef={this.floatingPicker}
onChange={this._onSuggestionSelected}
onSuggestionsHidden={this._onSuggestionsShownOrHidden}
onSuggestionsShown={this._onSuggestionsShownOrHidden}
inputElement={this.input.current ? this.input.current.inputElement : undefined}
selectedItems={this.items}
suggestionItems={this.props.suggestionItems ? this.props.suggestionItems : undefined}
{...this.floatingPickerProps}
/>
);
}
protected renderSelectedItemsList(): JSX.Element {
const SelectedItems: React.ComponentType<IBaseSelectedItemsListProps<T>> = this.props.onRenderSelectedItems;
return (
<SelectedItems
componentRef={this.selectedItemsList}
selection={this.selection}
selectedItems={this.props.selectedItems ? this.props.selectedItems : undefined}
onItemsDeleted={this.props.selectedItems ? this.props.onItemsRemoved : undefined}
{...this.selectedItemsListProps}
/>
);
}
protected onInputChange = (value: string, composing?: boolean): void => {
// We don't want to update the picker's suggestions when the input is still being composed
if (!composing) {
this.setState({ queryString: value });
if (this.floatingPicker.current) {
this.floatingPicker.current.onQueryStringChanged(value);
}
}
};
protected onInputFocus = (ev: React.FocusEvent<HTMLInputElement | Autofill>): void => {
if (this.selectedItemsList.current) {
this.selectedItemsList.current.unselectAll();
}
if (this.props.inputProps && this.props.inputProps.onFocus) {
this.props.inputProps.onFocus(ev as React.FocusEvent<HTMLInputElement>);
}
};
protected onInputClick = (ev: React.MouseEvent<HTMLInputElement | Autofill>): void => {
if (this.selectedItemsList.current) {
this.selectedItemsList.current.unselectAll();
}
if (this.floatingPicker.current && this.inputElement) {
// Update the value if the input value is empty or it is different than the current inputText from the floatingPicker
const shoudUpdateValue = this.inputElement.value === '' || this.inputElement.value !== this.floatingPicker.current.inputText;
this.floatingPicker.current.showPicker(shoudUpdateValue);
}
};
// This is protected because we may expect the backspace key to work differently in a different kind of picker.
// This lets the subclass override it and provide it's own onBackspace. For an example see the BasePickerListBelow
protected onBackspace = (ev: React.KeyboardEvent<HTMLElement>): void => {
if (ev.which !== KeyCodes.backspace) {
return;
}
if (this.selectedItemsList.current && this.items.length) {
if (
this.input.current &&
!this.input.current.isValueSelected &&
this.input.current.inputElement === document.activeElement &&
(this.input.current as Autofill).cursorLocation === 0
) {
if (this.floatingPicker.current) {
this.floatingPicker.current.hidePicker();
}
ev.preventDefault();
this.selectedItemsList.current.removeItemAt(this.items.length - 1);
this._onSelectedItemsChanged();
} else if (this.selectedItemsList.current.hasSelectedItems()) {
if (this.floatingPicker.current) {
this.floatingPicker.current.hidePicker();
}
ev.preventDefault();
this.selectedItemsList.current.removeSelectedItems();
this._onSelectedItemsChanged();
}
}
};
protected onCopy = (ev: React.ClipboardEvent<HTMLElement>): void => {
if (this.selectedItemsList.current) {
// Pass it down into the selected items list
this.selectedItemsList.current.onCopy(ev);
}
};
protected onPaste = (ev: React.ClipboardEvent<Autofill | HTMLInputElement>): void => {
if (this.props.onPaste) {
const inputText = ev.clipboardData.getData('Text');
ev.preventDefault();
this.props.onPaste(inputText);
}
};
protected _onSuggestionSelected = (item: T): void => {
const currentRenderedQueryString = this.props.currentRenderedQueryString;
const queryString = this.state.queryString;
if (currentRenderedQueryString === undefined || currentRenderedQueryString === queryString) {
const processedItem: T | PromiseLike<T> | null = this.props.onItemSelected ? (this.props.onItemSelected as any)(item) : item;
if (processedItem === null) {
return;
}
const processedItemObject: T = processedItem as T;
const processedItemPromiseLike: PromiseLike<T> = processedItem as PromiseLike<T>;
let newItem: T;
if (processedItemPromiseLike && processedItemPromiseLike.then) {
processedItemPromiseLike.then((resolvedProcessedItem: T) => {
newItem = resolvedProcessedItem;
this._addProcessedItem(newItem);
});
} else {
newItem = processedItemObject;
this._addProcessedItem(newItem);
}
}
};
protected _onSelectedItemsChanged = (): void => {
this.focus();
};
/**
* The floating picker is the source of truth for if the menu has been opened or not.
*
* Because this isn't tracked inside the state of this component, we need to
* force an update here to keep the rendered output that depends on the picker being open
* in sync with the state
*
* Called when the suggestions is shown or closed
*/
private _onSuggestionsShownOrHidden = () => {
this.forceUpdate();
};
private _addProcessedItem(newItem: T) {
// If this is a controlled component, call the on item selected callback
// Otherwise add it to the selectedItemsList
if (this.props.onItemAdded) {
this.props.onItemAdded(newItem);
}
if (this.selectedItemsList.current) {
this.selectedItemsList.current.addItems([newItem]);
}
if (this.input.current) {
this.input.current.clear();
}
if (this.floatingPicker.current) {
this.floatingPicker.current.hidePicker();
}
this.focus();
}
}