@@ -2,10 +2,11 @@ import React, { useEffect, useState, useRef } from "react";
2
2
import { action } from "@storybook/addon-actions" ;
3
3
import styled from "styled-components" ;
4
4
import { text , boolean , select } from "@storybook/addon-knobs" ;
5
+ import { GroupBase , OptionProps } from "react-windowed-select" ;
5
6
import { Button , Heading2 , Select , SelectOption } from "../index" ;
6
7
import { Box } from "../Box" ;
7
8
import { Flex } from "../Flex" ;
8
- import { SelectProps } from "./Select" ;
9
+ import { NDSSelectProps } from "./Select" ;
9
10
10
11
const errorList = [ "Error message 1" , "Error message 2" ] ;
11
12
@@ -60,7 +61,10 @@ const getPhotos = async () => {
60
61
return results ;
61
62
} ;
62
63
63
- const SelectWithManyOptions = ( { multiselect, labelText, ...props } : SelectProps ) => {
64
+ const SelectWithManyOptions = < Option , IsMulti extends boolean , Group extends GroupBase < Option > > ( {
65
+ multiselect,
66
+ labelText,
67
+ } : Pick < NDSSelectProps < Option , IsMulti , Group > , "multiselect" | "labelText" > ) => {
64
68
const [ photoList , setPhotoList ] = useState ( [ ] ) ;
65
69
66
70
const setOptions = async ( ) => {
@@ -72,46 +76,35 @@ const SelectWithManyOptions = ({ multiselect, labelText, ...props }: SelectProps
72
76
setOptions ( ) ;
73
77
} , [ ] ) ;
74
78
75
- return < Select multiselect = { multiselect } options = { photoList } labelText = { labelText } { ... props } /> ;
79
+ return < Select multiselect = { multiselect } options = { photoList } labelText = { labelText } /> ;
76
80
} ;
77
81
78
- type SelectWithStateProps = SelectProps & {
79
- selectedValue : string ;
80
- } ;
81
-
82
- class SelectWithState extends React . Component < { selectedValue : string } , SelectWithStateProps > {
83
- constructor ( props ) {
84
- super ( props ) ;
85
-
86
- this . state = { selectedValue : "" } ;
87
- this . handleChange = this . handleChange . bind ( this ) ;
88
- this . clearSelection = this . clearSelection . bind ( this ) ;
89
- }
82
+ function SelectWithState < Option , IsMulti extends boolean , Group extends GroupBase < Option > > (
83
+ props : NDSSelectProps < Option , IsMulti , Group >
84
+ ) {
85
+ const [ selectedValue , setSelectedValue ] = useState ( "" ) ;
90
86
91
- handleChange ( selectedValue ) {
92
- this . setState ( { selectedValue } ) ;
87
+ function handleChange ( selectedValue ) {
88
+ setSelectedValue ( selectedValue ) ;
93
89
}
94
90
95
- clearSelection ( ) {
96
- this . setState ( { selectedValue : "" } ) ;
91
+ function clearSelection ( ) {
92
+ setSelectedValue ( "" ) ;
97
93
}
98
94
99
- render ( ) {
100
- const { selectedValue } = this . state ;
101
- return (
102
- < Flex flexDirection = "column" gap = "x2" alignItems = "flex-start" >
103
- < Select
104
- className = "Select"
105
- classNamePrefix = "SelectTest"
106
- onChange = { this . handleChange }
107
- value = { selectedValue }
108
- options = { options }
109
- { ...this . props }
110
- />
111
- < Button onClick = { this . clearSelection } > Clear selection</ Button >
112
- </ Flex >
113
- ) ;
114
- }
95
+ return (
96
+ < Flex flexDirection = "column" gap = "x2" alignItems = "flex-start" >
97
+ < Select
98
+ className = "Select"
99
+ classNamePrefix = "SelectTest"
100
+ onChange = { handleChange }
101
+ value = { selectedValue }
102
+ options = { options }
103
+ { ...props }
104
+ />
105
+ < Button onClick = { clearSelection } > Clear selection</ Button >
106
+ </ Flex >
107
+ ) ;
115
108
}
116
109
117
110
export default {
@@ -156,7 +149,7 @@ export const WithDifferentSizes = () => {
156
149
< Heading2 > Standard</ Heading2 >
157
150
< Flex gap = "x2" minHeight = "360px" >
158
151
< Select
159
- defaultMenuIsOpen
152
+ initialIsOpen
160
153
placeholder = "Please select inventory status"
161
154
onChange = { action ( "selection changed" ) }
162
155
onBlur = { action ( "blurred" ) }
@@ -166,7 +159,7 @@ export const WithDifferentSizes = () => {
166
159
/>
167
160
< Select
168
161
size = "medium"
169
- defaultMenuIsOpen
162
+ initialIsOpen
170
163
placeholder = "Please select inventory status"
171
164
onChange = { action ( "selection changed" ) }
172
165
onBlur = { action ( "blurred" ) }
@@ -176,7 +169,7 @@ export const WithDifferentSizes = () => {
176
169
/>
177
170
< Select
178
171
size = "large"
179
- defaultMenuIsOpen
172
+ initialIsOpen
180
173
placeholder = "Please select inventory status"
181
174
onChange = { action ( "selection changed" ) }
182
175
onBlur = { action ( "blurred" ) }
@@ -189,7 +182,7 @@ export const WithDifferentSizes = () => {
189
182
< Heading2 > Multi-select</ Heading2 >
190
183
< Flex gap = "x2" alignItems = "flex-start" >
191
184
< Select
192
- defaultMenuIsOpen
185
+ initialIsOpen
193
186
defaultValue = { [ partnerCompanyName [ 0 ] . value , partnerCompanyName [ 2 ] . value ] }
194
187
noOptionsMessage = { ( ) => "No options" }
195
188
placeholder = "Please select inventory status"
@@ -199,7 +192,7 @@ export const WithDifferentSizes = () => {
199
192
/>
200
193
< Select
201
194
size = "medium"
202
- defaultMenuIsOpen
195
+ initialIsOpen
203
196
defaultValue = { [ partnerCompanyName [ 0 ] . value , partnerCompanyName [ 2 ] . value ] }
204
197
noOptionsMessage = { ( ) => "No options" }
205
198
placeholder = "Please select inventory status"
@@ -209,7 +202,7 @@ export const WithDifferentSizes = () => {
209
202
/>
210
203
< Select
211
204
size = "large"
212
- defaultMenuIsOpen
205
+ initialIsOpen
213
206
defaultValue = { [ partnerCompanyName [ 0 ] . value , partnerCompanyName [ 2 ] . value ] }
214
207
noOptionsMessage = { ( ) => "No options" }
215
208
placeholder = "Please select inventory status"
@@ -270,7 +263,6 @@ WithAnOptionSelected.story = {
270
263
} ;
271
264
272
265
export const WithState = ( ) => (
273
- // @ts -ignore
274
266
< SelectWithState placeholder = "Please select inventory status" options = { options } labelText = "Inventory status" />
275
267
) ;
276
268
@@ -575,14 +567,18 @@ export const WithCustomOptionComponent = () => {
575
567
height : "10px" ,
576
568
marginRight : "5px" ,
577
569
} ) ) ;
578
- const CustomOption = ( { children, ...props } ) => {
570
+ const CustomOption = ( { children, ...props } : OptionProps ) => {
579
571
const newChildren = (
580
572
< >
581
573
< Indicator />
582
574
{ children }
583
575
</ >
584
576
) ;
585
- return < SelectOption { ...props } > { newChildren } </ SelectOption > ;
577
+ return (
578
+ < SelectOption size = "medium" { ...props } >
579
+ { newChildren }
580
+ </ SelectOption >
581
+ ) ;
586
582
} ;
587
583
return (
588
584
< >
@@ -631,6 +627,18 @@ export const UsingRefToControlFocus = () => {
631
627
) ;
632
628
} ;
633
629
630
+ export const WithTopMenuPlacement = ( ) => {
631
+ return (
632
+ < Flex height = "100vh" alignItems = "center" justifyContent = "center" >
633
+ < Select options = { options } menuPlacement = "top" />
634
+ </ Flex >
635
+ ) ;
636
+ } ;
637
+
638
+ UsingRefToControlFocus . story = {
639
+ name : "using ref to control focus" ,
640
+ } ;
641
+
634
642
const CustomOption = ( props ) => {
635
643
return < SelectOption { ...props } > { props . selectProps . myCustomProp } </ SelectOption > ;
636
644
} ;
@@ -650,15 +658,3 @@ export const WithCustomProps = () => {
650
658
</ >
651
659
) ;
652
660
} ;
653
-
654
- export const WithTopMenuPlacement = ( ) => {
655
- return (
656
- < Flex height = "100vh" alignItems = "center" justifyContent = "center" >
657
- < Select options = { options } menuPlacement = "top" />
658
- </ Flex >
659
- ) ;
660
- } ;
661
-
662
- UsingRefToControlFocus . story = {
663
- name : "using ref to control focus" ,
664
- } ;
0 commit comments