Skip to content

Commit 449294d

Browse files
authored
Merge pull request #158 from input-output-hk/fix/select-full-width
fix: add full-width support to Select component
2 parents 751a1bd + 04d30e8 commit 449294d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/design-system/select/select-root.component.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33

44
import ChevronDownIcon from '@icons/ChevronDownComponent';
55
import * as Select from '@radix-ui/react-select';
6+
import cn from 'classnames';
67

78
import { Item, ItemRoot } from './select-item';
89
import * as cx from './select-root.component.css';
@@ -26,6 +27,8 @@ export type SelectRootProps = Pick<
2627
portalContainer?: HTMLElement;
2728
triggerTestId?: string;
2829
zIndex?: number;
30+
fullWidth?: boolean;
31+
className?: string;
2932
};
3033

3134
const isValidSelectRootChild = (
@@ -56,6 +59,8 @@ const isValidSelectRootChild = (
5659
* @param variant The style variant.
5760
* @param triggerTestId The `data-testid` attribute, passed to the input trigger / root element.
5861
* @param zIndex The `z-index` applied to the `<Select.Content />`.
62+
* @param fullWidth If `true`, the select trigger and content will take the full width of the parent container.
63+
* @param className The CSS class name applied to the root element.
5964
*/
6065
export const Root = ({
6166
align = 'selected',
@@ -73,6 +78,8 @@ export const Root = ({
7378
variant = 'plain',
7479
triggerTestId,
7580
zIndex,
81+
fullWidth = false,
82+
className,
7683
}: Readonly<SelectRootProps>): JSX.Element => {
7784
const [isOpen, setIsOpen] = React.useState(defaultOpen);
7885

@@ -87,9 +94,10 @@ export const Root = ({
8794
onValueChange={onChange}
8895
>
8996
<Select.Trigger
90-
className={cx.trigger[variant]}
9197
id={id}
9298
data-testid={triggerTestId}
99+
className={cn(cx.trigger[variant], className)}
100+
style={fullWidth ? { width: '100%' } : undefined}
93101
>
94102
<Select.Value placeholder={placeholder} />
95103
{showArrow && (
@@ -100,7 +108,12 @@ export const Root = ({
100108
</Select.Trigger>
101109
<Select.Portal container={portalContainer}>
102110
<Select.Content
103-
style={{ zIndex }}
111+
style={{
112+
zIndex,
113+
...(fullWidth
114+
? { width: 'var(--radix-select-trigger-width)' }
115+
: {}),
116+
}}
104117
className={cx.content[variant]}
105118
position={align === 'selected' ? 'item-aligned' : 'popper'}
106119
>

src/design-system/select/select.stories.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,34 @@ const SelectRootVariants = (): JSX.Element => {
275275
);
276276
};
277277

278+
const FullWidth = (): JSX.Element => (
279+
<Section
280+
title="Full width"
281+
subtitle="The select stretches to fill its container."
282+
>
283+
<div style={{ width: '100%', marginBottom: '30px' }}>
284+
<Select.Root
285+
variant="outline"
286+
align="bottom"
287+
value={undefined}
288+
onChange={(): void => void 0}
289+
placeholder={placeholder}
290+
showArrow
291+
fullWidth
292+
>
293+
{options.map(option => (
294+
<Select.Item
295+
key={option.value}
296+
value={option.value}
297+
title={option.label}
298+
disabled={option.disabled}
299+
/>
300+
))}
301+
</Select.Root>
302+
</div>
303+
</Section>
304+
);
305+
278306
export const Overview = (): JSX.Element => (
279307
<Grid>
280308
<Cell>
@@ -283,6 +311,8 @@ export const Overview = (): JSX.Element => (
283311
<SelectVariants />
284312
<Divider my="$64" />
285313
<SelectRootVariants />
314+
<Divider my="$64" />
315+
<FullWidth />
286316
</Cell>
287317
</Grid>
288318
);

0 commit comments

Comments
 (0)