Skip to content

Commit

Permalink
fix: select flex and start icon inline bug (#1786)
Browse files Browse the repository at this point in the history
### What does it do?

Fix small bugs on Select to match styles on Locale Picker from CMS

### Related issue(s)/PR(s)

strapi/strapi#19960
  • Loading branch information
Feranchz authored Sep 13, 2024
2 parents 6f405fa + dc49935 commit b95628d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-garlics-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@strapi/design-system': patch
---

fix select bugs with startIcon
6 changes: 6 additions & 0 deletions docs/stories/03-inputs/Select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ There are two options for sizes, `"S"` or `"M"`, the default is `"M"`.

<Canvas of={SelectStories.Size} />

### Start Icons

Is possible to pass a start icon for general Select or for each option.

<Canvas of={SelectStories.StartIcon} />

### Controlled

All the select variants can be a controlled component by passing a `value` prop and an `onChange` callback, this also
Expand Down
51 changes: 51 additions & 0 deletions docs/stories/03-inputs/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MultiSelectOption,
Field,
} from '@strapi/design-system';
import { Plus } from '@strapi/icons';
import { default as outdent } from 'outdent';

const meta: Meta<typeof SingleSelect> = {
Expand Down Expand Up @@ -140,6 +141,56 @@ export const Size = {
name: 'small size',
} satisfies SingleSelectStory;

export const StartIcon = {
args: {
startIcon: <Plus />,
},
render: ({ label, error, hint, ...props }) => {
const selectRef = React.useRef<HTMLDivElement | null>(null);

return (
<Field.Root error={error} hint={hint}>
<Field.Label onClick={() => selectRef.current?.focus()}>{label}</Field.Label>
<SingleSelect ref={selectRef} {...props}>
<SingleSelectOption value="apple" startIcon={<Plus />}>
Apple
</SingleSelectOption>
<SingleSelectOption value="avocado" startIcon={<Plus />}>
Avocado
</SingleSelectOption>
<SingleSelectOption value="banana" startIcon={<Plus />}>
Banana
</SingleSelectOption>
</SingleSelect>
<Field.Error />
<Field.Hint />
</Field.Root>
);
},
parameters: {
docs: {
code: outdent`
<Field.Root error={error} hint={hint}>
<Field.Label onClick={() => selectRef.current?.focus()}>{label}</Field.Label>
<SingleSelect ref={selectRef} {...props}>
<SingleSelectOption value="apple" startIcon={<Plus />}>
Apple
</SingleSelectOption>
<SingleSelectOption value="avocado" startIcon={<Plus />}>
Avocado
</SingleSelectOption>
<SingleSelectOption value="banana" startIcon={<Plus />}>
Banana
</SingleSelectOption>
</SingleSelect>
<Field.Error />
<Field.Hint />
</Field.Root>
`,
},
},
};

type MultipleSelectStory = StoryObj<typeof MultiSelect>;

export const Multiple = {
Expand Down
8 changes: 5 additions & 3 deletions packages/design-system/src/components/Select/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { stripReactIdOfColon } from '../../helpers/strings';
import { useId } from '../../hooks/useId';
import { useIntersection } from '../../hooks/useIntersection';
import { Box } from '../../primitives/Box';
import { Flex } from '../../primitives/Flex';
import { Typography } from '../../primitives/Typography';
import { useField } from '../Field';

Expand Down Expand Up @@ -169,11 +170,12 @@ export const SingleSelectOption = React.forwardRef<HTMLDivElement, SingleSelectO
return (
<SelectParts.Item ref={ref} value={value.toString()} {...restProps}>
{startIcon && (
<Box tag="span" aria-hidden>
<Flex tag="span" aria-hidden>
{startIcon}
</Box>
</Flex>
)}
<Typography lineHeight="20px">
{/* @TODO: Probably we should refactor this to allow composable option building */}
<Typography lineHeight="20px" width="100%">
<SelectParts.ItemText>{children}</SelectParts.ItemText>
</Typography>
</SelectParts.Item>
Expand Down

0 comments on commit b95628d

Please sign in to comment.