Skip to content

Commit

Permalink
Add main start, end adorment
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Jul 9, 2024
1 parent e4ec186 commit 66f838a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
49 changes: 48 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,48 @@ 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}
__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}
></Box>
),
};
})}
/>
);
};

export const WithEllipsis = () => {
const values = [
{ value: "color-black", label: "Long black label here" },
Expand Down
8 changes: 8 additions & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ export type SelectProps<T, V> = PropsWithBox<
| "nonce"
> & {
label?: ReactNode;

error?: boolean;
helperText?: ReactNode;
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 +85,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 +151,17 @@ const SelectInner = <T extends Option, V extends Option | string>(
whiteSpace="nowrap"
overflow="hidden"
textOverflow="ellipsis"
display="flex"
>
{startAdornment && typed && <Box>{startAdornment(value)}</Box>}
<Text
size={convertSizeToScale(size)}
color={labelColor}
title={selectedItem?.label}
>
{selectedItem?.label}
</Text>
{endAdornment && typed && <Box>{endAdornment(value)}</Box>}
</Box>
</SelectWrapper>
<Portal asChild ref={refs.setFloating} style={floatingStyles}>
Expand Down

0 comments on commit 66f838a

Please sign in to comment.