-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathPickers.tsx
More file actions
200 lines (185 loc) · 6.2 KB
/
Pickers.tsx
File metadata and controls
200 lines (185 loc) · 6.2 KB
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
import React, { cloneElement, useCallback, useState } from 'react';
import {
Checkbox,
ComboBox,
ComboBoxNormalized,
Flex,
Icon,
Item,
type ItemKey,
PICKER_ITEM_HEIGHTS,
PICKER_TOP_OFFSET,
Picker,
PickerNormalized,
Section,
Text,
} from '@deephaven/components';
import { vsPerson } from '@deephaven/icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { getPositionOfSelectedItem } from '@deephaven/react-hooks';
import { generateItemElements, generateNormalizedItems } from './utils';
import SampleSection from './SampleSection';
// Generate enough items to require scrolling
const items = [...generateNormalizedItems(52)];
const itemsWithIcons = [...generateNormalizedItems(52, { icons: true })];
const itemElementsA = [...generateItemElements(0, 51)];
const itemElementsB = [...generateItemElements(52, 103)];
const itemElementsC = [...generateItemElements(104, 155)];
const itemElementsD = [...generateItemElements(156, 207)];
const itemElementsE = [...generateItemElements(208, 259)];
const mixedItemsWithIconsNoDescriptions = [
'String 1',
'String 2',
'String 3',
'',
'Some really long text that should get truncated',
444,
999,
true,
false,
...itemElementsA.map((itemEl, i) =>
i % 5 > 0
? itemEl
: cloneElement(itemEl, {
...itemEl.props,
children: [
<PersonIcon key={`icon-${itemEl.props.children}`} />,
<Text key={`label-${itemEl.props.children}`}>
{itemEl.props.children}
</Text>,
],
})
),
];
function PersonIcon(): JSX.Element {
return (
<Icon>
<FontAwesomeIcon icon={vsPerson} />
</Icon>
);
}
export function Pickers(): JSX.Element {
const [selectedKey, setSelectedKey] = useState<ItemKey | null>(200);
const [showIcons, setShowIcons] = useState(true);
const [filteredItems, setFilteredItems] = useState(itemsWithIcons);
const onSearch = useCallback(
(searchText: string) =>
setFilteredItems(
searchText === ''
? itemsWithIcons
: itemsWithIcons.filter(
({ item }) => item?.textValue?.includes(searchText)
)
),
[]
);
const getInitialScrollPosition = useCallback(
async () =>
getPositionOfSelectedItem({
keyedItems: items,
itemHeight: PICKER_ITEM_HEIGHTS.medium,
selectedKey,
topOffset: PICKER_TOP_OFFSET,
}),
[selectedKey]
);
const onChange = useCallback((key: ItemKey | null): void => {
setSelectedKey(key);
}, []);
return (
// eslint-disable-next-line react/jsx-props-no-spreading
<SampleSection name="pickers">
<h2 className="ui-title">Pickers</h2>
<Flex gap={14} direction="column">
{[Picker, ComboBox].map(Component => {
const label = (suffix: string) =>
`${Component === Picker ? 'Picker' : 'ComboBox'} (${suffix})`;
return (
<Flex key={Component.displayName} direction="row" gap={14}>
<Component
label={label('Single Child')}
tooltip={{ placement: 'bottom-end' }}
>
<Item textValue="Aaa">Aaa</Item>
</Component>
<Component
label={label('Mixed Children Types')}
defaultSelectedKey="999"
tooltip
>
{mixedItemsWithIconsNoDescriptions}
</Component>
<Component label={label('Sections')} tooltip>
{/* eslint-disable react/jsx-curly-brace-presence */}
{'String 1'}
{'String 2'}
{'String 3'}
<Section title="Section">
<Item textValue="Item Aaa">Item Aaa</Item>
<Item textValue="Item Bbb">Item Bbb</Item>
<Item textValue="Complex Ccc">
<PersonIcon />
<Text>Complex Ccc</Text>
</Item>
</Section>
<Section key="Key B">
<Item textValue="Item Ddd">Item Ddd</Item>
<Item textValue="Item Eee">Item Eee</Item>
<Item textValue="Complex Fff">
<PersonIcon />
<Text>Complex Fff</Text>
</Item>
<Item textValue="Ggg">
<PersonIcon />
<Text>Label</Text>
<Text slot="description">Description</Text>
</Item>
<Item textValue="Hhh">
<PersonIcon />
<Text>Label that causes overflow</Text>
<Text slot="description">
Description that causes overflow
</Text>
</Item>
</Section>
<Section title="Section A">{itemElementsA}</Section>
<Section title="Section B">{itemElementsB}</Section>
<Section key="Section C">{itemElementsC}</Section>
<Section key="Section D">{itemElementsD}</Section>
<Section title="Section E">{itemElementsE}</Section>
</Component>
</Flex>
);
})}
<Checkbox
checked={showIcons}
onChange={e => setShowIcons(e.currentTarget.checked)}
>
Show Ions
</Checkbox>
<Flex direction="row" gap={14}>
<PickerNormalized
label="Picker (Controlled)"
getInitialScrollPosition={getInitialScrollPosition}
normalizedItems={itemsWithIcons}
selectedKey={selectedKey}
showItemIcons={showIcons}
onChange={onChange}
/>
<ComboBoxNormalized
label="ComboBox (Controlled)"
getInitialScrollPosition={getInitialScrollPosition}
normalizedItems={filteredItems}
selectedKey={selectedKey}
showItemIcons={showIcons}
onChange={onChange}
validationState={selectedKey == null ? 'invalid' : 'valid'}
errorMessage="Please select an item."
onInputChange={onSearch}
/>
</Flex>
</Flex>
</SampleSection>
);
}
export default Pickers;