Skip to content

Commit d7f1703

Browse files
authored
fix: Upgrade react-windowed-select
1 parent f0afe91 commit d7f1703

File tree

14 files changed

+237
-234
lines changed

14 files changed

+237
-234
lines changed

cypress/e2e/components/Select.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,4 @@ describe("Select", () => {
176176
assertDropDownIsOpen();
177177
});
178178
});
179-
describe("Knobs", () => {});
180179
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"react-popper": "1.3.11",
161161
"react-popper-2": "npm:[email protected]",
162162
"react-resize-detector": "^9.1.0",
163-
"react-windowed-select": "2.0.5",
163+
"react-windowed-select": "^5.2.0",
164164
"smoothscroll-polyfill": "^0.4.4",
165165
"react-select": "^5.8.0",
166166
"styled-system": "^5.1.4",

src/AsyncSelect/AsyncSelect.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { InlineValidation } from "../Validation";
1212
import customStyles from "../Select/customReactSelectStyles";
1313
import { SelectOption } from "../Select/SelectOption";
1414
import { getSubset } from "../utils/subset";
15+
import { ComponentSize, useComponentSize } from "../NDSProvider/ComponentSizeContext";
1516
import {
1617
SelectControl,
1718
SelectMultiValue,
@@ -25,6 +26,7 @@ import {
2526
type AsyncCustomProps<Option, IsMulti extends boolean, Group extends GroupBase<Option>> = {
2627
autocomplete?: AsyncProps<Option, IsMulti, Group>["isSearchable"];
2728
labelText?: string;
29+
size?: ComponentSize;
2830
requirementText?: string;
2931
helpText?: ReactNode;
3032
disabled?: AsyncProps<Option, IsMulti, Group>["isDisabled"];
@@ -36,7 +38,7 @@ type AsyncCustomProps<Option, IsMulti extends boolean, Group extends GroupBase<O
3638
defaultValue?: AsyncProps<Option, IsMulti, Group>["defaultInputValue"];
3739
};
3840

39-
type AsyncSelectProps<Option, IsMulti extends boolean, Group extends GroupBase<Option>> = Omit<
41+
export type AsyncSelectProps<Option, IsMulti extends boolean, Group extends GroupBase<Option>> = Omit<
4042
AsyncProps<Option, IsMulti, Group>,
4143
"isSearchable" | "isDisabled" | "isMulti" | "defaultMenuIsOpen" | "defaultInputValue"
4244
> &
@@ -77,6 +79,7 @@ const AsyncSelect = forwardRef(
7779
defaultOptions,
7880
loadOptions,
7981
isClearable,
82+
size,
8083
...props
8184
}: AsyncSelectProps<Option, IsMulti, Group>,
8285
ref:
@@ -89,6 +92,8 @@ const AsyncSelect = forwardRef(
8992
const spaceProps = getSubset(props, propTypes.space);
9093
const error = !!(errorMessage || errorList);
9194

95+
const componentSize = useComponentSize(size);
96+
9297
return (
9398
<Field {...spaceProps}>
9499
<MaybeFieldLabel labelText={labelText} requirementText={requirementText} helpText={helpText}>
@@ -100,15 +105,13 @@ const AsyncSelect = forwardRef(
100105
ref={ref}
101106
defaultInputValue={defaultValue}
102107
placeholder={placeholder || t("start typing")}
103-
styles={
104-
customStyles({
105-
theme,
106-
error,
107-
maxHeight,
108-
windowed: false,
109-
hasDefaultOptions: Boolean(defaultOptions),
110-
}) as any
111-
}
108+
styles={customStyles<Option, IsMulti, Group>({
109+
theme,
110+
error,
111+
maxHeight,
112+
windowed: false,
113+
hasDefaultOptions: Boolean(defaultOptions),
114+
})}
112115
isDisabled={disabled}
113116
isSearchable={autocomplete}
114117
aria-required={required}
@@ -127,7 +130,7 @@ const AsyncSelect = forwardRef(
127130
onInputChange={onInputChange}
128131
theme={theme as any}
129132
components={{
130-
Option: SelectOption,
133+
Option: (props) => <SelectOption size={componentSize} {...props} />,
131134
Control: SelectControl,
132135
MultiValue: SelectMultiValue,
133136
ClearIndicator: SelectClearIndicator,

src/Layout/Sidebar.story.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import {
1212
PrimaryButton,
1313
Box,
1414
Textarea,
15-
Text,
16-
Flex,
17-
Button,
1815
Heading3,
1916
} from "..";
2017

@@ -435,7 +432,7 @@ export const WithDifferentWidths = () => {
435432
<Select
436433
value={width.value}
437434
options={options}
438-
onChange={(s: string) => {
435+
onChange={(s) => {
439436
const [option] = options.filter(({ value }) => value === s);
440437
setWidth(option);
441438
}}

src/Select/Select.spec-utils.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Select/Select.spec-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { fireEvent, Matcher, SelectorMatcherOptions } from "@testing-library/react";
2+
3+
type QueryByText = (id: Matcher, options?: SelectorMatcherOptions) => HTMLElement;
4+
5+
export const openDropdown = (element: HTMLElement, i: number) => {
6+
fireEvent.focus(element.querySelectorAll("input")[i]);
7+
8+
fireEvent.keyDown(element.querySelectorAll("input")[i], {
9+
key: "ArrowDown",
10+
code: 40,
11+
});
12+
};
13+
14+
export const selectOption = (optionText: string, container: HTMLElement, queryByText: QueryByText, i = 0) => {
15+
expect(queryByText(optionText)).toBeNull();
16+
17+
openDropdown(container, i);
18+
19+
expect(queryByText(optionText)).not.toBeNull();
20+
21+
fireEvent.click(queryByText(optionText));
22+
};

src/Select/Select.story.tsx

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React, { useEffect, useState, useRef } from "react";
22
import { action } from "@storybook/addon-actions";
33
import styled from "styled-components";
44
import { text, boolean, select } from "@storybook/addon-knobs";
5+
import { GroupBase, OptionProps } from "react-windowed-select";
56
import { Button, Heading2, Select, SelectOption } from "../index";
67
import { Box } from "../Box";
78
import { Flex } from "../Flex";
8-
import { SelectProps } from "./Select";
9+
import { NDSSelectProps } from "./Select";
910

1011
const errorList = ["Error message 1", "Error message 2"];
1112

@@ -60,7 +61,10 @@ const getPhotos = async () => {
6061
return results;
6162
};
6263

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">) => {
6468
const [photoList, setPhotoList] = useState([]);
6569

6670
const setOptions = async () => {
@@ -72,46 +76,35 @@ const SelectWithManyOptions = ({ multiselect, labelText, ...props }: SelectProps
7276
setOptions();
7377
}, []);
7478

75-
return <Select multiselect={multiselect} options={photoList} labelText={labelText} {...props} />;
79+
return <Select multiselect={multiselect} options={photoList} labelText={labelText} />;
7680
};
7781

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("");
9086

91-
handleChange(selectedValue) {
92-
this.setState({ selectedValue });
87+
function handleChange(selectedValue) {
88+
setSelectedValue(selectedValue);
9389
}
9490

95-
clearSelection() {
96-
this.setState({ selectedValue: "" });
91+
function clearSelection() {
92+
setSelectedValue("");
9793
}
9894

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+
);
115108
}
116109

117110
export default {
@@ -156,7 +149,7 @@ export const WithDifferentSizes = () => {
156149
<Heading2>Standard</Heading2>
157150
<Flex gap="x2" minHeight="360px">
158151
<Select
159-
defaultMenuIsOpen
152+
initialIsOpen
160153
placeholder="Please select inventory status"
161154
onChange={action("selection changed")}
162155
onBlur={action("blurred")}
@@ -166,7 +159,7 @@ export const WithDifferentSizes = () => {
166159
/>
167160
<Select
168161
size="medium"
169-
defaultMenuIsOpen
162+
initialIsOpen
170163
placeholder="Please select inventory status"
171164
onChange={action("selection changed")}
172165
onBlur={action("blurred")}
@@ -176,7 +169,7 @@ export const WithDifferentSizes = () => {
176169
/>
177170
<Select
178171
size="large"
179-
defaultMenuIsOpen
172+
initialIsOpen
180173
placeholder="Please select inventory status"
181174
onChange={action("selection changed")}
182175
onBlur={action("blurred")}
@@ -189,7 +182,7 @@ export const WithDifferentSizes = () => {
189182
<Heading2>Multi-select</Heading2>
190183
<Flex gap="x2" alignItems="flex-start">
191184
<Select
192-
defaultMenuIsOpen
185+
initialIsOpen
193186
defaultValue={[partnerCompanyName[0].value, partnerCompanyName[2].value]}
194187
noOptionsMessage={() => "No options"}
195188
placeholder="Please select inventory status"
@@ -199,7 +192,7 @@ export const WithDifferentSizes = () => {
199192
/>
200193
<Select
201194
size="medium"
202-
defaultMenuIsOpen
195+
initialIsOpen
203196
defaultValue={[partnerCompanyName[0].value, partnerCompanyName[2].value]}
204197
noOptionsMessage={() => "No options"}
205198
placeholder="Please select inventory status"
@@ -209,7 +202,7 @@ export const WithDifferentSizes = () => {
209202
/>
210203
<Select
211204
size="large"
212-
defaultMenuIsOpen
205+
initialIsOpen
213206
defaultValue={[partnerCompanyName[0].value, partnerCompanyName[2].value]}
214207
noOptionsMessage={() => "No options"}
215208
placeholder="Please select inventory status"
@@ -270,7 +263,6 @@ WithAnOptionSelected.story = {
270263
};
271264

272265
export const WithState = () => (
273-
// @ts-ignore
274266
<SelectWithState placeholder="Please select inventory status" options={options} labelText="Inventory status" />
275267
);
276268

@@ -575,14 +567,18 @@ export const WithCustomOptionComponent = () => {
575567
height: "10px",
576568
marginRight: "5px",
577569
}));
578-
const CustomOption = ({ children, ...props }) => {
570+
const CustomOption = ({ children, ...props }: OptionProps) => {
579571
const newChildren = (
580572
<>
581573
<Indicator />
582574
{children}
583575
</>
584576
);
585-
return <SelectOption {...props}>{newChildren}</SelectOption>;
577+
return (
578+
<SelectOption size="medium" {...props}>
579+
{newChildren}
580+
</SelectOption>
581+
);
586582
};
587583
return (
588584
<>
@@ -631,6 +627,18 @@ export const UsingRefToControlFocus = () => {
631627
);
632628
};
633629

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+
634642
const CustomOption = (props) => {
635643
return <SelectOption {...props}>{props.selectProps.myCustomProp}</SelectOption>;
636644
};
@@ -650,15 +658,3 @@ export const WithCustomProps = () => {
650658
</>
651659
);
652660
};
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

Comments
 (0)