Skip to content

Commit 16c334c

Browse files
Select: add maxHeight prop (#673)
1 parent 6d1a4f2 commit 16c334c

File tree

5 files changed

+77
-19
lines changed

5 files changed

+77
-19
lines changed

src/components/Select/MultiSelect.stories.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Meta, StoryObj } from "@storybook/react-vite";
22
import { MultiSelect } from "./MultiSelect";
33
import { selectOptions } from "./selectOptions";
44
import { useEffect, useState } from "react";
5+
import { Container } from "../Container/Container";
6+
import { Panel } from "../Panel/Panel";
57

68
const meta: Meta<typeof MultiSelect> = {
79
component: MultiSelect,
@@ -79,3 +81,26 @@ export const OptionsAsProp: StoryObj<typeof MultiSelect> = {
7981
);
8082
},
8183
};
84+
85+
export const MaxHeight = {
86+
args: {},
87+
render: () => {
88+
return (
89+
<Container fillWidth>
90+
<Panel>
91+
<MultiSelect maxHeight="200px">
92+
<MultiSelect.Item value="item 1">Fish</MultiSelect.Item>
93+
<MultiSelect.Item value="item 2">Bread</MultiSelect.Item>
94+
<MultiSelect.Item value="item 3">Rocks</MultiSelect.Item>
95+
<MultiSelect.Item value="item 4">Snakes</MultiSelect.Item>
96+
<MultiSelect.Item value="item 5">Boats</MultiSelect.Item>
97+
<MultiSelect.Item value="item 6">Sandals</MultiSelect.Item>
98+
<MultiSelect.Item value="item 7">Potatoes</MultiSelect.Item>
99+
<MultiSelect.Item value="item 8">Rabbits</MultiSelect.Item>
100+
</MultiSelect>
101+
</Panel>
102+
</Container>
103+
);
104+
},
105+
tags: ["form-field", "select", "autodocs"],
106+
};

src/components/Select/SingleSelect.stories.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,30 @@ export const UseFullWidth = {
124124
tags: ["form-field", "select", "autodocs"],
125125
};
126126

127+
export const MaxHeight = {
128+
args: {},
129+
render: () => {
130+
return (
131+
<Container fillWidth>
132+
<Panel>
133+
<Title type="h2">Select with max height</Title>
134+
<Select maxHeight="200px">
135+
<Select.Item value="item 1">Fish</Select.Item>
136+
<Select.Item value="item 2">Bread</Select.Item>
137+
<Select.Item value="item 3">Rocks</Select.Item>
138+
<Select.Item value="item 4">Snakes</Select.Item>
139+
<Select.Item value="item 5">Boats</Select.Item>
140+
<Select.Item value="item 6">Sandals</Select.Item>
141+
<Select.Item value="item 7">Potatoes</Select.Item>
142+
<Select.Item value="item 8">Rabbits</Select.Item>
143+
</Select>
144+
</Panel>
145+
</Container>
146+
);
147+
},
148+
tags: ["form-field", "select", "autodocs"],
149+
};
150+
127151
export const NoOptions: StoryObj<typeof Select> = {
128152
// prettier-ignore
129153
args: { label: "Label", customText: "No results for \"{search}\"" },
@@ -202,6 +226,7 @@ const NoOptionsComponent = ({
202226
</Container>
203227
);
204228
};
229+
205230
export const NoOptionCustomNode: StoryObj<typeof Select> = {
206231
// prettier-ignore
207232
args: { },

src/components/Select/common/InternalSelect.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export const InternalSelect = ({
215215
options,
216216
sortable = false,
217217
placeholder = "Select an option",
218+
maxHeight,
218219
multiple,
219220
checkbox,
220221
selectLabel,
@@ -507,7 +508,7 @@ export const InternalSelect = ({
507508
size="xs"
508509
/>
509510
</SearchBarContainer>
510-
<SelectListContent>
511+
<SelectListContent $maxHeight={maxHeight}>
511512
<OptionContext.Provider value={optionContextValue}>
512513
{options && options.length > 0
513514
? options.map((props, index) => {

src/components/Select/common/SelectStyled.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,16 @@ export const SelectList = styled.div`
178178
width: inherit;
179179
max-height: calc(var(--radix-popover-content-available-height) - 1rem);
180180
`;
181-
export const SelectListContent = styled.div`
181+
export const SelectListContent = styled.div<{ $maxHeight?: string }>`
182182
width: inherit;
183183
overflow: auto;
184184
flex: 1;
185+
186+
${({ $maxHeight }) =>
187+
$maxHeight &&
188+
`
189+
max-height: ${$maxHeight};
190+
`}
185191
`;
186192

187193
export const HiddenSelectElement = styled.select`

src/components/Select/common/types.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,41 @@ export type SelectionType = "custom" | "default";
7171
interface InternalSelectProps
7272
extends PopoverProps,
7373
Omit<HTMLAttributes<HTMLDivElement>, "onChange" | "dir" | "onSelect" | "children"> {
74-
label?: ReactNode;
75-
error?: ReactNode;
76-
disabled?: boolean;
77-
name?: string;
78-
form?: string;
79-
dir?: "start" | "end";
80-
orientation?: "horizontal" | "vertical";
81-
allowCreateOption?: boolean;
8274
onChange: (selectedValues: Array<string>) => void;
83-
open: boolean;
8475
onOpenChange: (open: boolean) => void;
85-
value: Array<string>;
86-
sortable?: boolean;
8776
onSelect: (
8877
value: string,
8978
type?: SelectionType,
9079
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
9180
) => void;
92-
multiple?: boolean;
81+
open: boolean;
82+
value: Array<string>;
83+
allowCreateOption?: boolean;
9384
checkbox?: boolean;
94-
selectLabel?: string;
95-
placeholder?: string;
96-
showSearch?: boolean;
97-
customText?: string;
9885
container?: HTMLElement;
99-
useFullWidthItems?: boolean;
86+
customText?: string;
87+
dir?: "start" | "end";
88+
disabled?: boolean;
89+
error?: ReactNode;
90+
form?: string;
10091
itemCharacterLimit?: string;
92+
label?: ReactNode;
93+
maxHeight?: string;
94+
multiple?: boolean;
95+
name?: string;
10196
/**
10297
* Controls rendering when no options are available.
10398
* - false: renders nothing
10499
* - true: renders default message 'No Options found' with search term if present
105100
* - ({ search: string, onClose: () => void }) => ReactNode: renders the returned node allowing dynamic content based on current search string
106101
*/
107102
noAvailableOptions?: boolean | ((props: NoAvailableOptionsFactoryProps) => ReactNode);
103+
orientation?: "horizontal" | "vertical";
104+
placeholder?: string;
105+
selectLabel?: string;
106+
showSearch?: boolean;
107+
sortable?: boolean;
108+
useFullWidthItems?: boolean;
108109
}
109110

110111
export type SelectOptionProp = SelectOptionType | SelectChildrenType;

0 commit comments

Comments
 (0)