This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathindex.js
257 lines (234 loc) · 6.54 KB
/
index.js
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
// @flow
import React, {Component} from 'react';
import {storiesOf} from '@storybook/react';
import MultiSelect from '../index.js';
import type {
Option,
} from '../select-item.js';
const shortList = [
{label: "Brian Genisio", value: 1},
{label: "John Doe", value: 2},
{label: "Jane Doe", value: 3},
];
const longList = [...Array(26).keys()]
.map(value => {
const label = String.fromCharCode(97 + value); // A-Z
return {label, value};
});
const states = {
"AL": "Alabama",
"AK": "Alaska",
"AS": "American Samoa",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California",
"CO": "Colorado",
"CT": "Connecticut",
"DE": "Delaware",
"DC": "District Of Columbia",
"FM": "Federated States Of Micronesia",
"FL": "Florida",
"GA": "Georgia",
"GU": "Guam",
"HI": "Hawaii",
"ID": "Idaho",
"IL": "Illinois",
"IN": "Indiana",
"IA": "Iowa",
"KS": "Kansas",
"KY": "Kentucky",
"LA": "Louisiana",
"ME": "Maine",
"MH": "Marshall Islands",
"MD": "Maryland",
"MA": "Massachusetts",
"MI": "Michigan",
"MN": "Minnesota",
"MS": "Mississippi",
"MO": "Missouri",
"MT": "Montana",
"NE": "Nebraska",
"NV": "Nevada",
"NH": "New Hampshire",
"NJ": "New Jersey",
"NM": "New Mexico",
"NY": "New York",
"NC": "North Carolina",
"ND": "North Dakota",
"MP": "Northern Mariana Islands",
"OH": "Ohio",
"OK": "Oklahoma",
"OR": "Oregon",
"PW": "Palau",
"PA": "Pennsylvania",
"PR": "Puerto Rico",
"RI": "Rhode Island",
"SC": "South Carolina",
"SD": "South Dakota",
"TN": "Tennessee",
"TX": "Texas",
"UT": "Utah",
"VT": "Vermont",
"VI": "Virgin Islands",
"VA": "Virginia",
"WA": "Washington",
"WV": "West Virginia",
"WI": "Wisconsin",
"WY": "Wyoming",
};
const statesList = Object.keys(states)
.map(key => ({
value: key,
label: states[key],
}));
const students = [
{id: 0, name: "Zach Morris"},
{id: 1, name: "Kelly Kapowski"},
{id: 2, name: "A.C. Slater"},
{id: 3, name: "Lisa Turtle"},
{id: 4, name: "Jessie Spano"},
{id: 5, name: "Samuel Powers"},
{id: 6, name: "Tori Scott"},
];
const studentsList = students.map(s => ({
value: s,
label: s.name,
}));
type SMSProps = {
options: Option[],
valueRenderer?: (values: Array<any>, options: Array<Option>) => string,
ItemRenderer?: Function,
selectAllLabel?: string,
isLoading?: boolean,
disabled?: boolean,
disableSearch?: boolean,
filterOptions?: (options: Array<Option>, filter: string) => Array<Option>,
overrideStrings?: {[string]: string}
};
type SMSState = {
selected: Array<Option>
};
class StatefulMultiSelect extends Component<SMSProps, SMSState> {
constructor() {
super();
this.state = {
selected: [],
};
}
handleSelectedChanged(selected) {
this.setState({selected});
}
render() {
const {
ItemRenderer,
options,
selectAllLabel,
valueRenderer,
isLoading,
disabled,
disableSearch,
filterOptions,
overrideStrings,
} = this.props;
const {selected} = this.state;
return <div>
<MultiSelect
options={options}
onSelectedChanged={this.handleSelectedChanged.bind(this)}
selected={selected}
valueRenderer={valueRenderer}
ItemRenderer={ItemRenderer}
selectAllLabel={selectAllLabel}
isLoading={isLoading}
disabled={disabled}
disableSearch={disableSearch}
filterOptions={filterOptions}
overrideStrings={overrideStrings}
/>
<h2>Selected:</h2>
{selected.join(', ')}
</div>;
}
}
function studentValueRenderer(selected, options) {
if (selected.length === 0) {
return "Slect some students...";
}
if (selected.length === options.length) {
return "All students selected";
}
return `Selected ${selected.length} Students`;
}
type SIRProps = {
checked: boolean,
option: Option,
onClick: (event: MouseEvent) => void
};
class StudentItemRenderer extends Component<SIRProps> {
render() {
const {checked, option, onClick} = this.props;
return <span>
<span>
{option.label}
</span>
<input
type="checkbox"
onChange={onClick}
checked={checked}
tabIndex="-1"
style={{float: 'right'}}
/>
</span>;
}
}
const customFilter = (options: Array<Option>, filter: string) => {
const optionIncludesText = (option: Option) => {
const label = option.label || "";
return label.toLowerCase().includes(filter);
};
return options.filter(optionIncludesText);
};
storiesOf('MultiSelect', module)
.add('default view', () => <StatefulMultiSelect options={shortList} />)
.add('long list view', () => <StatefulMultiSelect options={longList} />)
.add('United States', () => <StatefulMultiSelect options={statesList} />)
.add('Custom Heading Renderer', () => <StatefulMultiSelect
options={studentsList}
valueRenderer={studentValueRenderer}
selectAllLabel="All students"
/>)
.add('Tabbing test (accessibility)', () => <div>
<input/>
<StatefulMultiSelect options={shortList} />
<input type="checkbox" />
</div>)
.add('Item Renderer Override', () => <StatefulMultiSelect
options={studentsList}
ItemRenderer={StudentItemRenderer}
/>)
.add('With loading indicator', () => <StatefulMultiSelect
options={[]}
isLoading={true}
/>)
.add('Disable Search', () => <StatefulMultiSelect
options={studentsList}
disableSearch={true}
/>)
.add('Disabled', () => <MultiSelect
options={studentsList}
selected={[students[1], students[2]]}
disabled={true}
/>)
.add('Custom Filter', () => <StatefulMultiSelect
options={studentsList}
filterOptions={customFilter}
/>)
.add('Custom Strings', () => <StatefulMultiSelect
options={studentsList}
overrideStrings={{
selectSomeItems: "SeLeCt SoMe iTeMs...",
allItemsAreSelected: "ALl ItEmS aRe SeLeCtEd",
selectAll: "SeLeCt AlL",
search: "SeArCh",
}}
/>);