dropdowns-js 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -323
- package/dist/dropdowns-js.cjs.js +6 -10
- package/dist/dropdowns-js.es.js +1164 -1163
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,38 +6,20 @@ Searchable dropdown components for React applications.
|
|
|
6
6
|
- DropdownObj - a single selection dropdown whose underlying data is an array of objects.
|
|
7
7
|
- MultiSelectionDropdown - a multi-selection dropdown whose underlying data is a primitive array.
|
|
8
8
|
- MultiSelectionDropdownObj - a multi-selection dropdown whose underlying data is an array of objects.
|
|
9
|
+
The components sort the data provided to them.
|
|
9
10
|
|
|
10
11
|
## Installation
|
|
11
12
|
```
|
|
12
13
|
npm install dropdowns-js
|
|
13
14
|
```
|
|
14
|
-
|
|
15
|
-
## 1. Wrap your Component
|
|
16
|
-
All components must be separately wrapped in the CollectionsProvider to manage the dropdown states.
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
// App.jsx
|
|
20
|
-
import { CollectionsProvider } from "dropdowns-js";
|
|
21
|
-
import MyComponent from "./MyComponent";
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
return (
|
|
25
|
-
<CollectionsProvider>
|
|
26
|
-
<MyComponent />
|
|
27
|
-
</CollectionsProvider>
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
export default App;
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## 2. Import styles and hook
|
|
16
|
+
## 1. Imports
|
|
34
17
|
Inside your component, where you use any of the dropdowns, import as follows:
|
|
35
18
|
|
|
36
19
|
```
|
|
37
20
|
// File: MyComponent.jsx
|
|
38
21
|
import "dropdowns-js/style.css"; // Must not be left out, so as to enforce dropdown styling.
|
|
39
22
|
import {
|
|
40
|
-
useCollectionsContext,
|
|
41
23
|
Dropdown, // If you use it.
|
|
42
24
|
DropdownObj, // If you use it.
|
|
43
25
|
MultiselectionDropdown, // If you use it.
|
|
@@ -45,88 +27,51 @@ import {
|
|
|
45
27
|
} from "dropdowns-js";
|
|
46
28
|
|
|
47
29
|
export default function MyComponent {
|
|
48
|
-
// Inside your component.
|
|
49
|
-
const {
|
|
50
|
-
addCollection, // A must. For pre-populating your dropdown
|
|
51
|
-
collectionExists, // A must. To be called prior to adding a collection.
|
|
52
|
-
getCollectionData, // Depends
|
|
53
|
-
updateCollection, // Depends on whether the collection's data gets updated.
|
|
54
|
-
setSelected, // Depends. Use-case: pre-populated selection.
|
|
55
|
-
getSelected // A must. For obtaining selected items.
|
|
56
|
-
} = useCollectionsContext();
|
|
57
|
-
|
|
58
30
|
// ...
|
|
59
31
|
}
|
|
60
32
|
```
|
|
61
|
-
**Context functions**
|
|
62
|
-
`addCollection(collectionName, anArray, maxNumSelections = null, primitiveType = true, ...sortFields)`
|
|
63
|
-
Add a new collection of data to be displayed in a dropdown for selection.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
`collectionExists(collectionName)`
|
|
67
|
-
Check (true/false) whether the collection with the specified name exists.
|
|
68
|
-
|
|
69
|
-
`updateCollection(collectionName, anArray)`
|
|
70
|
-
Update the specified collection with new data. An error is thrown for a non-existent collection name. A dropdown whose data is updated must be given a key attribute, and the key value must be updated to cause a re-render with new data.
|
|
71
|
-
|
|
72
|
-
`getCollectionData(collectionName)`
|
|
73
|
-
Get the data (array) of the specified collection. An error is thrown for a non-existent collection name.
|
|
74
|
-
|
|
75
|
-
`setSelected(collectionName, selectedItemsArray)`
|
|
76
|
-
Set the selected items in the specified collection. They are ignored if they were are part of the collection. An error is thrown for a non-existent collection name.
|
|
77
|
-
|
|
78
|
-
`getSelected(collectionName)`
|
|
79
|
-
Get the selected items (array) of the specified collection. An error is thrown for a non-existent collection name is specified.
|
|
80
|
-
|
|
81
|
-
`getMaxNumSelections(collectionName)`
|
|
82
|
-
Get the maximum number of items that can be selected on this collection. An error is thrown for a non-existent collection name is specified.
|
|
83
33
|
|
|
84
34
|
## 3. Dropdown Component Attributes
|
|
85
|
-
`label` - to be
|
|
35
|
+
`label` - the name of the data to be displayed displayed in the dropdown. e.g. Cars, Users.
|
|
86
36
|
|
|
87
37
|
`isDisabled` - disables the component when set to true. Default = false.
|
|
88
38
|
|
|
89
|
-
`
|
|
39
|
+
`data` - data to display in the dropdown, for the user to select from.
|
|
40
|
+
|
|
41
|
+
`sortDirection` - for dropdowns using primitive type array input. Specifies the sort order of the dropdown data. 'asc' or 'desc'. Default is 'asc'.
|
|
42
|
+
|
|
43
|
+
`sortFields` - for dropdonws using object type array input. An array. Specifies the field sort orders of the dropdown data. e.g. ['score desc', 'numGames asc']. If a field is to be sorted ascending order, you can ommit asc. .e.g. ['fullName', 'score desc'].
|
|
44
|
+
|
|
45
|
+
`displayName` - for dropdowns using object type array input. The field (name) by which the dropdown items will be displayed.
|
|
46
|
+
|
|
47
|
+
`valueName` - for dropdowns using object type array input. The name of the field that will be used as the underlying unique value of each dropdown item. e.g. 'code', 'id'.
|
|
48
|
+
|
|
49
|
+
`selectedData` - for multi-selection dropdowns. An array of pre-set selection of options. This is an array of multi-selection dropdowns. Optional.
|
|
50
|
+
|
|
51
|
+
`selected` - for single selection dropdowns. A pre-set selected option.
|
|
90
52
|
|
|
91
|
-
`
|
|
53
|
+
`onItemSelected` - for single selection dropdowns. A function to call when the user has made a selection.
|
|
92
54
|
|
|
93
|
-
`
|
|
55
|
+
`onItemsSelected` - for multi-selection dropdowns. A function to call when the user has made a selection. Or removed items from their selection.
|
|
94
56
|
|
|
95
|
-
`
|
|
57
|
+
`dropdownStyle` - CSS styling for the dropdown. Fields: {color, backgroundColor, borderColor (optional)}.
|
|
58
|
+
|
|
59
|
+
`buttonStyle` - for multi-selecton dropdowns. CSS styling for the DONE button (pressed after completing a selection). Fields: {color, backgroundColor}.
|
|
96
60
|
|
|
97
61
|
## 4. Dropdown usage example
|
|
98
62
|
This dropdown is to be used when the underlying data is a primitive type array.
|
|
99
63
|
```
|
|
100
|
-
import { Dropdown
|
|
64
|
+
import { Dropdown } from 'dropdowns-js';
|
|
101
65
|
import 'dropdowns-js/style.css'; // styles must be imported, otherwise the dropdowns do not display properly.
|
|
102
|
-
import {
|
|
66
|
+
import { useState } from 'react';
|
|
103
67
|
|
|
104
68
|
export default function MyComponent() {
|
|
105
69
|
const [output, setOutput] = useState('');
|
|
106
|
-
|
|
107
|
-
const {
|
|
108
|
-
addCollection,
|
|
109
|
-
collectionExists,
|
|
110
|
-
getSelected
|
|
111
|
-
} = useCollectionsContext();
|
|
112
|
-
|
|
113
|
-
useEffect(()=> {
|
|
114
|
-
if (!collectionExists("FRUITS")) {
|
|
115
|
-
const fruits = ["APPLE", "BANANA" "ORANGE", "NAARJIE", "PEACH"]
|
|
116
|
-
// Create an APPLES collection
|
|
117
|
-
addCollection("FRUITS", // collection name
|
|
118
|
-
fruits, // collection data
|
|
119
|
-
1, // Maximum number of allowed selections.
|
|
120
|
-
true, // true = primitive type (string) data collection
|
|
121
|
-
'asc'); // sort order
|
|
122
|
-
}
|
|
123
|
-
}, []);
|
|
70
|
+
const fruits = [ "BANANA" "ORANGE", "NAARJIE", "PEACH", "APPLE" ];
|
|
124
71
|
|
|
125
72
|
/**Respond when the user has chosen a fruit */
|
|
126
|
-
function fruitSelected() {
|
|
127
|
-
|
|
128
|
-
const selectedFruit = getSelected("FRUITS")[0];
|
|
129
|
-
setOutput(selectedFruit);
|
|
73
|
+
function fruitSelected(selFruit) {
|
|
74
|
+
setOutput(selFruit);
|
|
130
75
|
}
|
|
131
76
|
|
|
132
77
|
return (
|
|
@@ -136,9 +81,13 @@ export default function MyComponent() {
|
|
|
136
81
|
<div style={{padding: '2px', display: 'flex'}}>
|
|
137
82
|
<label htmlFor='fruits' style={{width: '70px'}}>Fruit</label>
|
|
138
83
|
|
|
139
|
-
<Dropdown
|
|
140
|
-
|
|
141
|
-
|
|
84
|
+
<Dropdown
|
|
85
|
+
label={'Fruits'}
|
|
86
|
+
data={fruits} sortDirection='asc'
|
|
87
|
+
onItemSelected={fruitSelected}
|
|
88
|
+
selected={"BANANA"}
|
|
89
|
+
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}}
|
|
90
|
+
/>
|
|
142
91
|
|
|
143
92
|
</div>
|
|
144
93
|
<p>{output}</p>
|
|
@@ -149,47 +98,27 @@ export default function MyComponent() {
|
|
|
149
98
|
```
|
|
150
99
|
## 5. DropdownObj usage example
|
|
151
100
|
```
|
|
152
|
-
import { DropdownObj
|
|
101
|
+
import { DropdownObj } from 'dropdowns-js';
|
|
153
102
|
import 'dropdowns-js/style.css'; // Not to be left out.
|
|
154
103
|
|
|
155
|
-
import {
|
|
104
|
+
import { useState } from 'react';
|
|
156
105
|
|
|
157
106
|
export default function MyComponent2() {
|
|
158
107
|
const [output, setOutput] = useState('');
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
// Create a DRIVING_CODES collection, sorted by description ascending
|
|
170
|
-
const drivingCodes = [
|
|
171
|
-
{ code: 'A1', description: 'Light Motorcycles' },
|
|
172
|
-
{ code: 'A', description: 'Heavy Motorcycles' },
|
|
173
|
-
{ code: 'B', description: 'Light Vehicles' },
|
|
174
|
-
{ code: 'EB', description: 'Light Articulated' },
|
|
175
|
-
{ code: 'C1', description: 'Heavy Vehicles' },
|
|
176
|
-
{ code: 'C', description: 'Extra Heavy Vehicles' },
|
|
177
|
-
{ code: 'EC1', description: 'Heavy Articulated' },
|
|
178
|
-
{ code: 'EC', description: 'Extra Heavy Articulated' }
|
|
179
|
-
];
|
|
180
|
-
addCollection("DRIVING_CODES", // collection name
|
|
181
|
-
drivingCodes, // collection data
|
|
182
|
-
1, // allow maximum 1 selection
|
|
183
|
-
false, // non-primitive type, that is, object type data collection
|
|
184
|
-
'description asc'); // sort order
|
|
185
|
-
}
|
|
186
|
-
}, []);
|
|
108
|
+
const drivingCodes = [
|
|
109
|
+
{ code: 'A1', description: 'Light Motorcycles' },
|
|
110
|
+
{ code: 'A', description: 'Heavy Motorcycles' },
|
|
111
|
+
{ code: 'B', description: 'Light Vehicles' },
|
|
112
|
+
{ code: 'EB', description: 'Light Articulated' },
|
|
113
|
+
{ code: 'C1', description: 'Heavy Vehicles' },
|
|
114
|
+
{ code: 'C', description: 'Extra Heavy Vehicles' },
|
|
115
|
+
{ code: 'EC1', description: 'Heavy Articulated' },
|
|
116
|
+
{ code: 'EC', description: 'Extra Heavy Articulated' }
|
|
117
|
+
];
|
|
187
118
|
|
|
188
119
|
/**Respond when the user has chosen a driving code */
|
|
189
|
-
function drivingCodeSelected() {
|
|
190
|
-
|
|
191
|
-
const selectedDrivingCode = [...getSelected(collectionNames.DRIVING_CODES)[0]];
|
|
192
|
-
setOutput(`${selectedDrivingCode.code} => ${selectedDrivingCode.description}`);
|
|
120
|
+
function drivingCodeSelected(selDrivingCode) {
|
|
121
|
+
setOutput(`${selDrivingCode.code} => ${selDrivingCode.description}`);
|
|
193
122
|
}
|
|
194
123
|
|
|
195
124
|
return (
|
|
@@ -199,8 +128,13 @@ export default function MyComponent2() {
|
|
|
199
128
|
<div style={{padding: '2px', display: 'flex'}}>
|
|
200
129
|
<label htmlFor='driving-codes' style={{width: '70px'}}>Driving Codes</label>
|
|
201
130
|
|
|
202
|
-
<DropdownObj
|
|
203
|
-
|
|
131
|
+
<DropdownObj
|
|
132
|
+
label='Driving Codes' data={drivingCodes}
|
|
133
|
+
displayName="description"
|
|
134
|
+
valueName="code"
|
|
135
|
+
sortFields={ ['description'] }
|
|
136
|
+
onItemSelected={drivingCodeSelected}
|
|
137
|
+
selected={drivingCodes[0]}
|
|
204
138
|
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}} />
|
|
205
139
|
</div>
|
|
206
140
|
|
|
@@ -211,40 +145,22 @@ export default function MyComponent2() {
|
|
|
211
145
|
```
|
|
212
146
|
## 6. MultiselectionDropdown usage example
|
|
213
147
|
```
|
|
214
|
-
import { MultiSelectionDropdown
|
|
148
|
+
import { MultiSelectionDropdown } from 'dropdowns-js';
|
|
215
149
|
import 'dropdowns-js/style.css';
|
|
216
150
|
|
|
217
|
-
import {
|
|
151
|
+
import { useState } from 'react';
|
|
218
152
|
|
|
219
153
|
export default function MyComponent3() {
|
|
220
154
|
const [output, setOutput] = useState('');
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
updateCollection,
|
|
226
|
-
getSelected
|
|
227
|
-
} = useCollectionsContext();
|
|
228
|
-
|
|
229
|
-
useEffect(()=> {
|
|
230
|
-
if (!collectionExists("SPORTS")) {
|
|
231
|
-
// Create an SPORT collection sorted in ascending order.
|
|
232
|
-
const sport = [
|
|
233
|
-
"Motor Racing", "Cycling", "Wrestling", "Kung Fu", "Boxing", "Basket Ball",
|
|
234
|
-
"Rugby", "Cricket", "Running", "Soccer", "Netball", "Hockey"
|
|
235
|
-
];
|
|
236
|
-
addCollection("SPORT", // collection name
|
|
237
|
-
sport, // collection data
|
|
238
|
-
4, // maximum number of selections
|
|
239
|
-
true, // primitive type data (string in this case)
|
|
240
|
-
'asc'); // asc - sort order
|
|
241
|
-
}
|
|
242
|
-
}, []);
|
|
155
|
+
const sport = [
|
|
156
|
+
"Motor Racing", "Cycling", "Wrestling", "Kung Fu", "Boxing", "Basket Ball",
|
|
157
|
+
"Rugby", "Cricket", "Running", "Soccer", "Netball", "Hockey"
|
|
158
|
+
];
|
|
243
159
|
|
|
244
160
|
/**Respond when the user has chosen an interest */
|
|
245
|
-
function
|
|
161
|
+
function sportsSelected(selSports) {
|
|
246
162
|
// Obtain the selected items.
|
|
247
|
-
const selectedSport =
|
|
163
|
+
const selectedSport = selSports("SPORT").join(", );
|
|
248
164
|
setOutput(selectedSport);
|
|
249
165
|
}
|
|
250
166
|
|
|
@@ -255,10 +171,10 @@ export default function MyComponent3() {
|
|
|
255
171
|
<div style={{padding: '2px', display: 'flex'}}>
|
|
256
172
|
<label htmlFor='sport' style={{width: '70px'}}>Sport</label>
|
|
257
173
|
|
|
258
|
-
<MultiSelectionDropdown
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
onItemsSelected={
|
|
174
|
+
<MultiSelectionDropdown
|
|
175
|
+
label='Sport'
|
|
176
|
+
data={sport}
|
|
177
|
+
onItemsSelected={sportsSelected}
|
|
262
178
|
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}}
|
|
263
179
|
buttonStyle={{color: '#fff', backgroundColor: '#008000'}} />
|
|
264
180
|
</div>
|
|
@@ -272,58 +188,29 @@ export default function MyComponent3() {
|
|
|
272
188
|
## 7. MultiSelectionDropdownObj usage example
|
|
273
189
|
```
|
|
274
190
|
import 'dropdowns-js/style.css';
|
|
275
|
-
import { MultiSelectionDropdownObj
|
|
191
|
+
import { MultiSelectionDropdownObj } from 'dropdowns-js';
|
|
276
192
|
|
|
277
|
-
import {
|
|
193
|
+
import { useState } from 'react';
|
|
278
194
|
|
|
279
195
|
|
|
280
196
|
export default function MyComponent4() {
|
|
281
197
|
const [output, setOutput] = useState('');
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
DRIVING_CODES: "DRIVING_CODES",
|
|
293
|
-
DRIVERS: "DRIVERS"
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
useEffect(()=> {
|
|
297
|
-
if (!collectionExists(collectionNames.DRIVING_CODES)) {
|
|
298
|
-
// Create DRIVING_CODES collection sorted in ascending order.
|
|
299
|
-
// Create a DRIVING_CODES collection, sorted by description ascending
|
|
300
|
-
const drivingCodes = [
|
|
301
|
-
{ code: 'A1', description: 'Light Motorcycles' },
|
|
302
|
-
{ code: 'A', description: 'Heavy Motorcycles' },
|
|
303
|
-
{ code: 'B', description: 'Light Vehicles' },
|
|
304
|
-
{ code: 'EB', description: 'Light Articulated' },
|
|
305
|
-
{ code: 'C1', description: 'Heavy Vehicles' },
|
|
306
|
-
{ code: 'C', description: 'Extra Heavy Vehicles' },
|
|
307
|
-
{ code: 'EC1', description: 'Heavy Articulated' },
|
|
308
|
-
{ code: 'EC', description: 'Extra Heavy Articulated' }
|
|
309
|
-
];
|
|
310
|
-
|
|
311
|
-
addCollection("DRIVING_CODES", // collection name
|
|
312
|
-
drivingCodes, // collection data
|
|
313
|
-
3, // maximum number of selections
|
|
314
|
-
false, // object type data
|
|
315
|
-
'description asc'); // asc - sort order
|
|
316
|
-
}
|
|
317
|
-
}, []);
|
|
198
|
+
const drivingCodes = [
|
|
199
|
+
{ code: 'A1', description: 'Light Motorcycles' },
|
|
200
|
+
{ code: 'A', description: 'Heavy Motorcycles' },
|
|
201
|
+
{ code: 'B', description: 'Light Vehicles' },
|
|
202
|
+
{ code: 'EB', description: 'Light Articulated' },
|
|
203
|
+
{ code: 'C1', description: 'Heavy Vehicles' },
|
|
204
|
+
{ code: 'C', description: 'Extra Heavy Vehicles' },
|
|
205
|
+
{ code: 'EC1', description: 'Heavy Articulated' },
|
|
206
|
+
{ code: 'EC', description: 'Extra Heavy Articulated' }
|
|
207
|
+
];
|
|
318
208
|
|
|
319
209
|
/**Respond when the user has chosen an interest */
|
|
320
|
-
function
|
|
321
|
-
// Obtain the selected driving codes.
|
|
322
|
-
const selectedDrivingCodes = getSelected("DRIVING_CODES");
|
|
323
|
-
|
|
210
|
+
function drivCodesSelected(selDrivCode) {
|
|
324
211
|
// Create a string array of driving codes.
|
|
325
|
-
const strSelectedCodes =
|
|
326
|
-
.map(
|
|
212
|
+
const strSelectedCodes = selDrivCodes.map((drivCode)=> drivCode.code)
|
|
213
|
+
.map(drivCode => drivCode.code)
|
|
327
214
|
.join(", ");
|
|
328
215
|
setOutput(strSelectedCodes);
|
|
329
216
|
}
|
|
@@ -335,12 +222,12 @@ export default function MyComponent4() {
|
|
|
335
222
|
<div style={{padding: '2px', display: 'flex'}}>
|
|
336
223
|
<label htmlFor={'driving-licence-codes'} style={{width: '100px'}}>Lic. Codes</label>
|
|
337
224
|
<MultiSelectionDropdownObj
|
|
338
|
-
id='driving-licence-codes'
|
|
339
225
|
label='Driving Licence Codes'
|
|
340
|
-
|
|
226
|
+
data={drivingCodes}
|
|
227
|
+
sortFields={ ['description'] }
|
|
341
228
|
valueName='code'
|
|
342
229
|
displayName='description'
|
|
343
|
-
onItemsSelected={
|
|
230
|
+
onItemsSelected={drivCodeSelected}
|
|
344
231
|
isDisabled={false}
|
|
345
232
|
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}}
|
|
346
233
|
buttonStyle={{color: '#fff', backgroundColor: '#008000'}} />
|
|
@@ -356,132 +243,6 @@ export default function MyComponent4() {
|
|
|
356
243
|
);
|
|
357
244
|
}
|
|
358
245
|
```
|
|
359
|
-
## 8. Multiple dropdown usage example
|
|
360
|
-
```
|
|
361
|
-
export default function MyComponent5() {
|
|
362
|
-
const [output, setOutput] = useState();
|
|
363
|
-
/* A dropdown whose collection data gets updated, must be given a key attribute, so as to update
|
|
364
|
-
it (key) to cause re-render upon the update of collection data. */
|
|
365
|
-
const [dropdownKey2, setDropdownKey2] = useState(0);
|
|
366
|
-
const keyStep = .000001;
|
|
367
|
-
|
|
368
|
-
const {
|
|
369
|
-
addCollection,
|
|
370
|
-
collectionExists,
|
|
371
|
-
updateCollection,
|
|
372
|
-
getSelected
|
|
373
|
-
} = useCollectionsContext();
|
|
374
|
-
|
|
375
|
-
const drivers = [
|
|
376
|
-
{ fullName: "Thabo Mokoena", licenceCode: "B", id: "771217" },
|
|
377
|
-
{ fullName: "Naledi Khumalo", licenceCode: "EB", id: "850412" },
|
|
378
|
-
{ fullName: "Sipho Dlamini", licenceCode: "C1", id: "861802" },
|
|
379
|
-
{ fullName: "Ayesha Patel", licenceCode: "B", id: "500101" },
|
|
380
|
-
{ fullName: "Johan van der Merwe", licenceCode: "EC", id: "620401" },
|
|
381
|
-
{ fullName: "Lerato Molefe", licenceCode: "A", id: "901012" },
|
|
382
|
-
{ fullName: "Sibusiso Nkosi", licenceCode: "EC1", id: "910512" },
|
|
383
|
-
{ fullName: "Michelle Adams", licenceCode: "B", id: "901012" },
|
|
384
|
-
{ fullName: "Kagiso Tshepe", licenceCode: "C", id: "920115" },
|
|
385
|
-
{ fullName: "Pieter Botha", licenceCode: "A1", id: "960401" },
|
|
386
|
-
{ fullName: "Nomvula Zungu", licenceCode: "B", id: "0792346789" },
|
|
387
|
-
{ fullName: "Riaan Pretorius", licenceCode: "EB", id: "0824567812" },
|
|
388
|
-
{ fullName: "Mandla Sithole", licenceCode: "C1", id: "0735678910" },
|
|
389
|
-
{ fullName: "Fatima Khan", licenceCode: "B", id: "0846789123" },
|
|
390
|
-
{ fullName: "Tshepo Madiba", licenceCode: "EC1", id: "0817890234" },
|
|
391
|
-
{ fullName: "Andre Nel", licenceCode: "A", id: "0728901345" },
|
|
392
|
-
{ fullName: "Zanele Mthembu", licenceCode: "B", id: "0769012456" },
|
|
393
|
-
{ fullName: "Imran Desai", licenceCode: "C", id: "0740123567" },
|
|
394
|
-
{ fullName: "Charlene Jacobs", licenceCode: "EB", id: "0831234678" },
|
|
395
|
-
{ fullName: "Vusi Hlongwane", licenceCode: "EC", id: "0712345789" }
|
|
396
|
-
];
|
|
397
|
-
|
|
398
|
-
useEffect(()=> {
|
|
399
|
-
if (!collectionExists("DRIVING_CODES")) {
|
|
400
|
-
const drivingCodes = [
|
|
401
|
-
"A", "A1", "EC", "EC1", "C", "C1", "B", "EB"
|
|
402
|
-
];
|
|
403
|
-
// Create DRIVING_CODES collection sorted in ascending order.
|
|
404
|
-
addCollection("DRIVING_CODES", // collection name
|
|
405
|
-
drivingCodes, // collection data
|
|
406
|
-
1, // maximum number of selections
|
|
407
|
-
true, // primitive type data
|
|
408
|
-
'asc'); // asc - sort order
|
|
409
|
-
}
|
|
410
|
-
if (!collectionExists("DRIVERS")) {
|
|
411
|
-
// Create a drivers collection sorted in ascending order. Initially empty pending the selection of a driving code.
|
|
412
|
-
|
|
413
|
-
addCollection("DRIVERS",
|
|
414
|
-
[], // empty
|
|
415
|
-
5, // allow max 5 selections
|
|
416
|
-
false, // object type data.
|
|
417
|
-
'fullName asc');
|
|
418
|
-
}
|
|
419
|
-
}, []);
|
|
420
|
-
|
|
421
|
-
/**Respond when the user has chosen a driving licence code */
|
|
422
|
-
function drivingCodeSelected() {
|
|
423
|
-
// Obtain the selected driving codes.
|
|
424
|
-
const selectedDrivingCodes = getSelected("DRIVING_CODES");
|
|
425
|
-
|
|
426
|
-
// Create a string array of driving codes.
|
|
427
|
-
const strSelectedCodes = selectedDrivingCodes; // Expecting a size 1 array
|
|
428
|
-
const selectedCode = strSelectedCodes[0];
|
|
429
|
-
|
|
430
|
-
/*=========Update collection according to selected driving licence code=====*/
|
|
431
|
-
const updateData = drivers.filter(driver=> {
|
|
432
|
-
return selectedCode === driver.licenceCode;
|
|
433
|
-
});
|
|
434
|
-
updateCollection("DRIVERS", updateData);
|
|
435
|
-
/*============================================================================*/
|
|
436
|
-
|
|
437
|
-
// Force the re-render the drivers MultiselectionDropdownObj, since its data has been updated.
|
|
438
|
-
setDropdownKey2(dropdownKey2 + keyStep);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function driversSelected() {
|
|
442
|
-
const selectedDrivers = [...getSelected(collectionNames.DRIVERS)].map(driver => driver.fullName); // Array
|
|
443
|
-
setOutput(selectedDrivers.join(', ')); // List of selected drivers' names.
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
return (
|
|
447
|
-
<div className='container' style={{padding: '5px', backgroundColor: 'green'}}>
|
|
448
|
-
<h1>MultiSelectionDropdownObj Example</h1>
|
|
449
|
-
<p>Select an interest, and then your topic</p>
|
|
450
|
-
<div style={{padding: '2px', display: 'flex'}}>
|
|
451
|
-
<label style={{width: '100px'}}>Lic. Codes</label>
|
|
452
|
-
<MultiSelectionDropdownObj label='Driving Licence Codes'
|
|
453
|
-
collectionName={collectionNames.DRIVING_CODES}
|
|
454
|
-
valueName='code'
|
|
455
|
-
displayName='description'
|
|
456
|
-
onItemsSelected={drivingCodeSelected}
|
|
457
|
-
isDisabled={false}
|
|
458
|
-
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}}
|
|
459
|
-
buttonStyle={{color: '#fff', backgroundColor: '#008000'}} />
|
|
460
|
-
</div>
|
|
461
|
-
|
|
462
|
-
<div style={{padding: '2px', display: 'flex'}}>
|
|
463
|
-
<label style={{width: '100px'}}>Drivers</label>
|
|
464
|
-
<MultiSelectionDropdownObj
|
|
465
|
-
key={dropdownKey2}
|
|
466
|
-
collectionName={collectionNames.DRIVERS}
|
|
467
|
-
label='Drivers'
|
|
468
|
-
valueName='id'
|
|
469
|
-
displayName='fullName'
|
|
470
|
-
onItemsSelected={driversSelected}
|
|
471
|
-
dropdownStyle={{color: '#000', backgroundColor: '#66ff66'}}
|
|
472
|
-
buttonStyle={{color: '#fff', backgroundColor: '#008000'}} />
|
|
473
|
-
</div>
|
|
474
|
-
|
|
475
|
-
{output &&
|
|
476
|
-
<div style={{ marginTop: '10px', padding: '5px' }}>
|
|
477
|
-
{output}
|
|
478
|
-
</div>
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
</div>
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
```
|
|
485
246
|
|
|
486
247
|
## License
|
|
487
248
|
MIT
|