-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSelect.stories.tsx
More file actions
208 lines (194 loc) · 6.59 KB
/
Copy pathSelect.stories.tsx
File metadata and controls
208 lines (194 loc) · 6.59 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
201
202
203
204
205
206
207
208
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {Button, Collection, Label, ListBox, ListLayout, OverlayArrow, Popover, Select, SelectValue, Virtualizer} from 'react-aria-components';
import {LoadingSpinner, MyListBoxItem} from './utils';
import React from 'react';
import styles from '../example/index.css';
import {UNSTABLE_ListBoxLoadingSentinel} from '../src/ListBox';
import {useAsyncList} from 'react-stately';
export default {
title: 'React Aria Components',
argTypes: {
validationBehavior: {
control: 'select',
options: ['native', 'aria']
}
}
};
export const SelectExample = () => (
<Select data-testid="select-example" id="select-example-id">
<Label style={{display: 'block'}}>Test</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>▼</span>
</Button>
<Popover>
<OverlayArrow>
<svg width={12} height={12}><path d="M0 0,L6 6,L12 0" /></svg>
</OverlayArrow>
<ListBox className={styles.menu}>
<MyListBoxItem>Foo</MyListBoxItem>
<MyListBoxItem>Bar</MyListBoxItem>
<MyListBoxItem>Baz</MyListBoxItem>
<MyListBoxItem href="http://google.com">Google</MyListBoxItem>
</ListBox>
</Popover>
</Select>
);
export const SelectRenderProps = () => (
<Select data-testid="select-render-props">
{({isOpen}) => (
<>
<Label style={{display: 'block'}}>Test</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>{isOpen ? '▲' : '▼'}</span>
</Button>
<Popover>
<ListBox className={styles.menu}>
<MyListBoxItem>Foo</MyListBoxItem>
<MyListBoxItem>Bar</MyListBoxItem>
<MyListBoxItem>Baz</MyListBoxItem>
<MyListBoxItem href="http://google.com">Google</MyListBoxItem>
</ListBox>
</Popover>
</>
)}
</Select>
);
let makeItems = (length: number) => Array.from({length}, (_, i) => ({
id: i,
name: `Item ${i}`
}));
let manyItems = makeItems(100);
export const SelectManyItems = () => (
<Select>
<Label style={{display: 'block'}}>Test</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>▼</span>
</Button>
<Popover>
<OverlayArrow>
<svg width={12} height={12}><path d="M0 0,L6 6,L12 0" /></svg>
</OverlayArrow>
<ListBox items={manyItems} className={styles.menu}>
{item => <MyListBoxItem>{item.name}</MyListBoxItem>}
</ListBox>
</Popover>
</Select>
);
export const VirtualizedSelect = () => (
<Select>
<Label style={{display: 'block'}}>Test</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>▼</span>
</Button>
<Popover>
<OverlayArrow>
<svg width={12} height={12}><path d="M0 0,L6 6,L12 0" /></svg>
</OverlayArrow>
<Virtualizer layout={new ListLayout({rowHeight: 25})}>
<ListBox items={manyItems} className={styles.menu}>
{item => <MyListBoxItem>{item.name}</MyListBoxItem>}
</ListBox>
</Virtualizer>
</Popover>
</Select>
);
interface Character {
name: string,
height: number,
mass: number,
birth_year: number
}
const MyListBoxLoaderIndicator = (props) => {
return (
<UNSTABLE_ListBoxLoadingSentinel style={{height: 30, width: '100%'}} {...props}>
<LoadingSpinner style={{height: 20, width: 20, transform: 'translate(-50%, -50%)'}} />
</UNSTABLE_ListBoxLoadingSentinel>
);
};
export const AsyncVirtualizedCollectionRenderSelect = (args) => {
let list = useAsyncList<Character>({
async load({signal, cursor}) {
if (cursor) {
cursor = cursor.replace(/^http:\/\//i, 'https://');
}
// Slow down load so progress circle can appear
await new Promise(resolve => setTimeout(resolve, args.delay));
let res = await fetch(cursor || 'https://swapi.py4e.com/api/people/?search=', {signal});
let json = await res.json();
return {
items: json.results,
cursor: json.next
};
}
});
return (
<Select>
<Label style={{display: 'block'}}>Async Virtualized Collection render Select</Label>
<Button style={{position: 'relative'}}>
<SelectValue />
{list.isLoading && <LoadingSpinner style={{right: '20px', left: 'unset', top: '0px', height: '100%', width: 20}} />}
<span aria-hidden="true" style={{paddingLeft: 25}}>▼</span>
</Button>
<Virtualizer
layout={ListLayout}
layoutOptions={{
rowHeight: 25,
loaderHeight: 30
}}>
<Popover>
<OverlayArrow>
<svg width={12} height={12}><path d="M0 0,L6 6,L12 0" /></svg>
</OverlayArrow>
<ListBox className={styles.menu}>
<Collection items={list.items}>
{item => (
<MyListBoxItem id={item.name}>{item.name}</MyListBoxItem>
)}
</Collection>
<MyListBoxLoaderIndicator isLoading={list.loadingState === 'loadingMore'} onLoadMore={list.loadMore} />
</ListBox>
</Popover>
</Virtualizer>
</Select>
);
};
AsyncVirtualizedCollectionRenderSelect.story = {
args: {
delay: 50
}
};
// Test case for https://github.com/adobe/react-spectrum/issues/8034
// Required select validation cannot currently be tested in the jsdom environment.
// In jsdom, forms are submitted even when required fields are empty.
// See: https://github.com/jsdom/jsdom/issues/2898
export const RequiredSelectWithManyItems = (props) => (
<form>
<Select {...props} name="select" isRequired>
<Label style={{display: 'block'}}>Required Select with many items</Label>
<Button>
<SelectValue />
<span aria-hidden="true" style={{paddingLeft: 5}}>▼</span>
</Button>
<Popover>
<ListBox items={makeItems(301)} className={styles.menu}>
{item => <MyListBoxItem>{item.name}</MyListBoxItem>}
</ListBox>
</Popover>
</Select>
<Button type="submit">Submit</Button>
</form>
);