Skip to content

Commit 507fc92

Browse files
ovflowdcanerakdas
andauthored
fix: select should have scrollbar (#6452)
* fix: select should have scrollbar * test: ResizeObserver mock added --------- Co-authored-by: Caner Akdas <[email protected]>
1 parent 503d265 commit 507fc92

File tree

4 files changed

+84
-36
lines changed

4 files changed

+84
-36
lines changed

components/Common/Select/__tests__/index.test.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe('Select', () => {
1010
const label = 'Option label';
1111
const onChange = jest.fn();
1212

13+
global.ResizeObserver = jest.fn().mockImplementation(() => ({
14+
observe: jest.fn(),
15+
unobserve: jest.fn(),
16+
disconnect: jest.fn(),
17+
}));
18+
1319
it('renders the label when provided', () => {
1420
const { getByLabelText } = render(
1521
<Select

components/Common/Select/index.tsx

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22

33
import { ChevronDownIcon } from '@heroicons/react/24/outline';
4-
import * as Primitive from '@radix-ui/react-select';
4+
import * as ScrollPrimitive from '@radix-ui/react-scroll-area';
5+
import * as SelectPrimitive from '@radix-ui/react-select';
56
import classNames from 'classnames';
67
import { useId, useMemo } from 'react';
78
import type { FC } from 'react';
@@ -77,50 +78,58 @@ const Select: FC<SelectProps> = ({
7778
</label>
7879
)}
7980

80-
<Primitive.Root value={defaultValue} onValueChange={onChange}>
81-
<Primitive.Trigger
81+
<SelectPrimitive.Root value={defaultValue} onValueChange={onChange}>
82+
<SelectPrimitive.Trigger
8283
className={styles.trigger}
8384
aria-label={label}
8485
id={id}
8586
>
86-
<Primitive.Value placeholder={placeholder} />
87+
<SelectPrimitive.Value placeholder={placeholder} />
8788
<ChevronDownIcon className={styles.icon} />
88-
</Primitive.Trigger>
89-
<Primitive.Portal>
90-
<Primitive.Content
89+
</SelectPrimitive.Trigger>
90+
91+
<SelectPrimitive.Portal>
92+
<SelectPrimitive.Content
9193
position={inline ? 'popper' : 'item-aligned'}
9294
className={classNames(styles.dropdown, { [styles.inline]: inline })}
9395
>
94-
<Primitive.Viewport>
95-
{mappedValues.map(({ label, items }, key) => (
96-
<Primitive.Group key={label?.toString() || key}>
97-
{label && (
98-
<Primitive.Label
99-
className={classNames(styles.item, styles.label)}
100-
>
101-
{label}
102-
</Primitive.Label>
103-
)}
104-
105-
{items.map(({ value, label, iconImage, disabled }) => (
106-
<Primitive.Item
107-
key={value}
108-
value={value}
109-
disabled={disabled}
110-
className={classNames(styles.item, styles.text)}
111-
>
112-
<Primitive.ItemText>
113-
{iconImage}
114-
<span>{label}</span>
115-
</Primitive.ItemText>
116-
</Primitive.Item>
96+
<ScrollPrimitive.Root type="auto">
97+
<SelectPrimitive.Viewport>
98+
<ScrollPrimitive.Viewport>
99+
{mappedValues.map(({ label, items }, key) => (
100+
<SelectPrimitive.Group key={label?.toString() ?? key}>
101+
{label && (
102+
<SelectPrimitive.Label
103+
className={classNames(styles.item, styles.label)}
104+
>
105+
{label}
106+
</SelectPrimitive.Label>
107+
)}
108+
109+
{items.map(({ value, label, iconImage, disabled }) => (
110+
<SelectPrimitive.Item
111+
key={value}
112+
value={value}
113+
disabled={disabled}
114+
className={classNames(styles.item, styles.text)}
115+
>
116+
<SelectPrimitive.ItemText>
117+
{iconImage}
118+
<span>{label}</span>
119+
</SelectPrimitive.ItemText>
120+
</SelectPrimitive.Item>
121+
))}
122+
</SelectPrimitive.Group>
117123
))}
118-
</Primitive.Group>
119-
))}
120-
</Primitive.Viewport>
121-
</Primitive.Content>
122-
</Primitive.Portal>
123-
</Primitive.Root>
124+
</ScrollPrimitive.Viewport>
125+
</SelectPrimitive.Viewport>
126+
<ScrollPrimitive.Scrollbar orientation="vertical">
127+
<ScrollPrimitive.Thumb />
128+
</ScrollPrimitive.Scrollbar>
129+
</ScrollPrimitive.Root>
130+
</SelectPrimitive.Content>
131+
</SelectPrimitive.Portal>
132+
</SelectPrimitive.Root>
124133
</span>
125134
);
126135
};

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@radix-ui/react-dialog": "^1.0.5",
4848
"@radix-ui/react-dropdown-menu": "^2.0.6",
4949
"@radix-ui/react-label": "^2.0.2",
50+
"@radix-ui/react-scroll-area": "^1.0.5",
5051
"@radix-ui/react-select": "^2.0.0",
5152
"@radix-ui/react-tabs": "^1.0.4",
5253
"@radix-ui/react-toast": "^1.1.5",

0 commit comments

Comments
 (0)