-
Notifications
You must be signed in to change notification settings - Fork 2
/
ListPicker.tsx
455 lines (425 loc) · 11.9 KB
/
ListPicker.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Picker
*/
import "./ListPicker.scss";
import classnames from "classnames";
import * as React from "react";
import {
Columns,
GroupColumn,
Panel,
PopupItem,
Title,
} from "@itwin/components-react";
import type { CommonProps, SizeProps } from "@itwin/core-react";
import { Icon, SearchBox } from "@itwin/core-react";
import { ToolbarDragInteractionContext } from "../toolbar/DragInteraction";
import { UiFramework } from "../UiFramework";
import {
SvgChevronDown,
SvgChevronRight,
SvgList,
} from "@itwin/itwinui-icons-react";
/** Enum for the list picker item type
* @beta
*/
export enum ListItemType {
Item = 0,
Separator = 1,
Container = 2,
}
/** List picker item
* @beta
*/
export interface ListItem {
[key: string]: any;
id?: string;
name?: string;
enabled: boolean;
type?: ListItemType;
children?: ListItem[];
}
/** Properties for the [[ListPicker]] component
* @beta
*/
export interface ListPickerProps {
title: string;
items: ListItem[];
iconSpec?: string | React.ReactNode;
setEnabled: (item: ListItem, enabled: boolean) => any;
onExpanded?: (expand: boolean) => void;
onSizeKnown?: (size: SizeProps) => void;
searchBox?: boolean;
onSearchValueChange?: (search: string) => void;
panelOnly?: boolean;
expanded?: boolean;
}
/** Properties for the [[ListPickerItem]] component
* @beta
*/
export interface ListPickerItemProps extends CommonProps {
key: any;
isActive?: boolean;
isFocused?: boolean;
onClick?: () => void;
label?: string;
}
/** List Picker Item React component
* @beta
*/
export class ListPickerItem extends React.PureComponent<ListPickerItemProps> {
/** Renders ListPickerItem */
public override render() {
const itemClassName = classnames(
"ListPicker-item",
this.props.isActive && "is-active",
this.props.isFocused && "is-focused",
this.props.className
);
// TODO - if cut off, show a title
const title: string | undefined =
this.props.label && this.props.label.length > 25
? this.props.label
: undefined;
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div
className={itemClassName}
onClick={this.props.onClick}
role="button"
tabIndex={-1}
>
<div className="label" title={title}>
{this.props.label}
</div>
</div>
);
}
}
/** Properties for the [[ExpandableSection]] component
* @beta
*/
export interface ExpandableSectionProps extends CommonProps {
title?: string;
expanded?: boolean;
/** Content */
children?: React.ReactNode;
}
/** State for the [[ExpandableSection]] component
* @internal
*/
interface ExpandableSectionState {
expanded: boolean;
}
/** Expandable Section React component
* @beta
*/
export class ExpandableSection extends React.PureComponent<
ExpandableSectionProps,
ExpandableSectionState
> {
/** Creates an ExpandableSection */
constructor(props: ExpandableSectionProps) {
super(props);
this.state = { expanded: !!this.props.expanded };
}
public override async componentDidUpdate(
prevProps: Readonly<ExpandableSectionProps>
) {
if (this.props.expanded !== prevProps.expanded) {
this.setState({ expanded: true });
}
}
private _onClick = () => {
this.setState((prevState) => ({ expanded: !prevState.expanded }));
};
/** Renders ExpandableSection */
public override render() {
const className = classnames(
"nz-toolbar-item-expandable-group-group",
this.props.className
);
const icon = this.state.expanded ? (
<Icon iconSpec={<SvgChevronDown />} />
) : (
<Icon iconSpec={<SvgChevronRight />} />
);
return (
<Panel
className={className}
style={this.props.style}
key={this.props.title}
>
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<div
onClick={this._onClick}
className={
this.state.expanded
? "ListPickerInnerContainer-header-expanded"
: "ListPickerInnerContainer-header"
}
role="button"
tabIndex={-1}
aria-expanded={this.state.expanded}
>
<div className="ListPickerInnerContainer-header-content">
<div className="ListPickerInnerContainer-expander">{icon}</div>
<div className="ListPickerInnerContainer-title">
{this.props.title}
</div>
</div>
</div>
{this.state.expanded ? (
<GroupColumn>{this.props.children}</GroupColumn>
) : (
<div />
)}
</Panel>
);
}
}
/** @beta */
export function getListPanel(props: ListPickerProps): React.ReactNode {
const expandSingleSection = (): boolean => {
const populatedContainers = props.items.filter((item: ListItem) => {
return (
item.type === ListItemType.Container && item.children!.length !== 0
);
});
return populatedContainers.length === 1;
};
const listItemToElement = (item: ListItem, itemIndex: number) => {
switch (item.type) {
case ListItemType.Item:
return (
<ListPickerItem
{...props}
key={itemIndex.toString()}
label={item.name}
isActive={item.enabled}
onClick={
// istanbul ignore next
() => {
props.setEnabled(item, !item.enabled);
}
}
/>
);
case ListItemType.Separator:
return (
<div key={itemIndex.toString()} className="ListPicker-separator" />
);
case ListItemType.Container:
if (item.children!.length !== 0) {
return (
<ExpandableSection
key={itemIndex.toString()}
title={item.name}
className="ListPickerInnerContainer"
expanded={
props.expanded === true
? !expandSingleSection()
: expandSingleSection()
}
>
<GroupColumn>{item.children!.map(listItemToElement)}</GroupColumn>
</ExpandableSection>
);
} else {
return <div key={itemIndex.toString()} />;
}
// istanbul ignore next
default:
return <div key={itemIndex.toString()} />;
}
};
return (
<Panel className="ListPickerContainer">
<Title>{props.title}</Title>
{props.searchBox && props.onSearchValueChange && (
// eslint-disable-next-line deprecation/deprecation
<SearchBox
className="ListPickerSearchBox"
onValueChanged={props.onSearchValueChange}
/>
)}
<Columns>
<GroupColumn className="ListPicker-column">
{props.items.map(listItemToElement)}
</GroupColumn>
</Columns>
</Panel>
);
}
/**
* List picker toolbar popup item.
* Used to provide an expandable list of items to enable/disable items.
* @beta
*/
function ListPickerPopupItem(props: ListPickerProps) {
const icon = props.iconSpec ? (
/* istanbul ignore next */ typeof props.iconSpec === "string" ? (
<Icon iconSpec={props.iconSpec} />
) : (
<i className="icon uifw-item-svg-icon">{props.iconSpec}</i>
)
) : (
<Icon iconSpec={<SvgList />} />
);
return (
<ToolbarDragInteractionContext.Consumer>
{(isEnabled) => {
return props.panelOnly === true ? (
getListPanel(props)
) : (
<PopupItem
hideIndicator={isEnabled}
icon={icon}
title={props.title}
panel={getListPanel(props)}
/>
);
}}
</ToolbarDragInteractionContext.Consumer>
);
}
/** Properties for the [[ListPicker]] component
* @beta
*/
export interface ListPickerPropsExtended extends ListPickerProps {
enableAllFunc?: () => void;
disableAllFunc?: () => void;
invertFunc?: () => void;
}
/**
* List Picker that lets the user pick from a list of items to enable/disable
* It also provides options to enable all, disable all and invert selection
* @beta
*/
export class ListPicker extends React.Component<ListPickerPropsExtended> {
public static get Key_All() {
return -3;
}
public static get Key_None() {
return -2;
}
public static get Key_Invert() {
return -1;
}
public static get Key_Separator() {
return -4;
}
/** Creates a ListPicker */
constructor(props: ListPickerPropsExtended) {
super(props);
}
// Creates an array of items containing the separator and special requests (all, none, invert)
private createItems(items: ListItem[]): ListItem[] {
const newItems: ListItem[] = [];
// Create special buttons (All/None/Invert)
if (this.props.enableAllFunc) {
let allEnabled = true;
items.map((item: ListItem) => {
allEnabled = allEnabled && item.enabled;
});
newItems.push({
key: ListPicker.Key_All,
name: UiFramework.translate("pickerButtons.all"),
enabled: allEnabled,
type: ListItemType.Item,
});
}
if (this.props.disableAllFunc) {
let allDisabled = false;
items.map((item: ListItem) => {
allDisabled = allDisabled || item.enabled;
});
newItems.push({
key: ListPicker.Key_None,
name: UiFramework.translate("pickerButtons.none"),
enabled: !allDisabled,
type: ListItemType.Item,
});
}
if (this.props.invertFunc) {
newItems.push({
key: ListPicker.Key_Invert,
name: UiFramework.translate("pickerButtons.invert"),
enabled: false,
type: ListItemType.Item,
});
}
if (
this.props.enableAllFunc ||
this.props.disableAllFunc ||
this.props.invertFunc
) {
newItems.push({
key: ListPicker.Key_Separator,
name: UiFramework.translate("pickerButtons.separator"),
enabled: false,
type: ListItemType.Separator,
});
}
// Push items
items.map((item) => {
newItems.push(item);
});
return newItems;
}
/**
* Checks if item is a special item.
* @param item Item to check
*/
public isSpecialItem(item: ListItem) {
return (
item.key === ListPicker.Key_All ||
item.key === ListPicker.Key_Invert ||
item.key === ListPicker.Key_None ||
item.type !== ListItemType.Item ||
item.key === ListPicker.Key_Separator
);
}
// Handle enabling/disabling the items
// This will call the this.props.setEnabled function to provide the parent with a chance to process it
private _setEnabled = (item: ListItem, enabled: boolean) => {
if (this.isSpecialItem(item)) {
switch (item.key) {
case ListPicker.Key_All: {
// istanbul ignore else
if (this.props.enableAllFunc) this.props.enableAllFunc();
return;
}
case ListPicker.Key_None: {
// istanbul ignore else
if (this.props.disableAllFunc) this.props.disableAllFunc();
return;
}
case ListPicker.Key_Invert: {
// istanbul ignore else
if (this.props.invertFunc) this.props.invertFunc();
return;
}
}
}
// Call on parent to do processing of the item
this.props.setEnabled(item, enabled);
};
/** Renders ListPicker */
public override render() {
return (
<ListPickerPopupItem
{...this.props}
title={this.props.title}
setEnabled={this._setEnabled}
onExpanded={this.props.onExpanded}
items={this.createItems(this.props.items)}
iconSpec={this.props.iconSpec}
/>
);
}
}