Skip to content

Commit

Permalink
Add adornments to Select input (#820)
Browse files Browse the repository at this point in the history
* Add addorments to Select input

* Add changeset

* update changeset

* Add main start, end adorment

* make text full width

* Fix empty space

* Fix story

* Remove box
  • Loading branch information
poulch authored Jul 9, 2024
1 parent d9896bf commit 9c969e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-spies-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/macaw-ui": patch
---

You can now see start and end adornment in select input options
51 changes: 50 additions & 1 deletion src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meta, StoryObj } from "@storybook/react";
import { useState } from "react";
import { Option } from "~/components/BaseSelect";

import { Box } from "../Box";
import { Select } from ".";
Expand Down Expand Up @@ -41,7 +42,11 @@ const SelectTemplate: Story = {
const [value, setValue] = useState(options[0]);

return (
<Select {...args} value={value} onChange={(value) => setValue(value)} />
<Select
{...args}
value={value}
onChange={(value) => setValue(value as Option)}
/>
);
},
};
Expand Down Expand Up @@ -155,6 +160,50 @@ export const WithStringValue = () => {
);
};

export const WithStartAdornment = () => {
const [value, setValue] = useState("color-black");

return (
<Select
label="Pick a color"
size="large"
value={value}
onChange={(value) => setValue(value)}
startAdornment={(value) => {
if (!value) {
return null;
}

return (
<Box
width={4}
height={4}
marginRight={2}
__flexShrink={0}
__backgroundColor={value}
></Box>
);
}}
options={options.map((option) => {
const value = option.value.split("color-")[1];
return {
...option,
value,
startAdornment: (
<Box
__backgroundColor={value}
marginRight={2}
width={4}
height={4}
__flexShrink={0}
></Box>
),
};
})}
/>
);
};

export const WithEllipsis = () => {
const values = [
{ value: "color-black", label: "Long black label here" },
Expand Down
14 changes: 13 additions & 1 deletion src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type SelectProps<T, V> = PropsWithBox<
options: T[];
onChange?: SingleChangeHandler<V>;
value: V | null;
startAdornment?: (inputValue: V | null) => ReactNode;
endAdornment?: (inputValue: V | null) => ReactNode;
children?: ReactNode;
}
> &
Expand Down Expand Up @@ -82,6 +84,8 @@ const SelectInner = <T extends Option, V extends Option | string>(
onChange,
onFocus,
onBlur,
startAdornment,
endAdornment,
children,
...props
}: SelectProps<T, V>,
Expand Down Expand Up @@ -146,14 +150,18 @@ const SelectInner = <T extends Option, V extends Option | string>(
whiteSpace="nowrap"
overflow="hidden"
textOverflow="ellipsis"
display="flex"
>
{startAdornment && typed && startAdornment(value)}
<Text
size={convertSizeToScale(size)}
color={labelColor}
title={selectedItem?.label}
width="100%"
>
{selectedItem?.label}
</Text>
{endAdornment && typed && endAdornment(value)}
</Box>
</SelectWrapper>
<Portal asChild ref={refs.setFloating} style={floatingStyles}>
Expand Down Expand Up @@ -185,7 +193,11 @@ const SelectInner = <T extends Option, V extends Option | string>(
})}
active={highlightedIndex === index}
>
<Text size={getListTextSize(size)}>{item.label}</Text>
{item?.startAdornment}
<Text size={getListTextSize(size)} width="100%">
{item.label}
</Text>
{item?.endAdornment}
</List.Item>
))}

Expand Down

0 comments on commit 9c969e2

Please sign in to comment.