Skip to content

Commit b8f0756

Browse files
committed
Add controlled Select stories
1 parent d31277f commit b8f0756

File tree

4 files changed

+144
-7
lines changed

4 files changed

+144
-7
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use client'
2+
3+
import { css, Flex } from '@devup-ui/react'
4+
import { useState } from 'react'
5+
6+
import {
7+
Select,
8+
SelectContainer,
9+
SelectDivider,
10+
SelectOption,
11+
SelectTrigger,
12+
} from '.'
13+
import { IconArrow } from './IconArrow'
14+
15+
export function ControlledCheckbox() {
16+
const [value, setValue] = useState<string[]>([])
17+
const handleChange = (nextValue: string) => {
18+
if (value.includes(nextValue)) {
19+
setValue(value.filter((v) => v !== nextValue))
20+
} else {
21+
setValue([...value, nextValue])
22+
}
23+
}
24+
25+
const [subValue, setSubValue] = useState<string[]>([])
26+
const handleSubChange = (nextValue: string) => {
27+
if (subValue.includes(nextValue)) {
28+
setSubValue(subValue.filter((v) => v !== nextValue))
29+
} else {
30+
setSubValue([...subValue, nextValue])
31+
}
32+
}
33+
34+
return (
35+
<Select onValueChange={handleChange} type="checkbox" value={value}>
36+
<SelectTrigger>Select {value}</SelectTrigger>
37+
<SelectContainer>
38+
<SelectOption value="Option 1">Option 1</SelectOption>
39+
<SelectOption value="Option 2">Option 2</SelectOption>
40+
<SelectDivider />
41+
<SelectOption value="Option 3">Option 3</SelectOption>
42+
<SelectOption value="Option 4">Option 4</SelectOption>
43+
<Select
44+
onValueChange={handleSubChange}
45+
type="checkbox"
46+
value={subValue}
47+
>
48+
<SelectTrigger asChild>
49+
<SelectOption>
50+
<Flex alignItems="center" justifyContent="space-between" w="100%">
51+
Option 5<IconArrow />
52+
</Flex>
53+
</SelectOption>
54+
</SelectTrigger>
55+
<SelectContainer
56+
className={css({
57+
right: '0',
58+
top: '0',
59+
transform: 'translateX(100%)',
60+
})}
61+
>
62+
<SelectOption value="Option 6">Option 6</SelectOption>
63+
<SelectOption value="Option 7">Option 7</SelectOption>
64+
</SelectContainer>
65+
</Select>
66+
</SelectContainer>
67+
</Select>
68+
)
69+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use client'
2+
3+
import { css, Flex } from '@devup-ui/react'
4+
import { useState } from 'react'
5+
6+
import {
7+
Select,
8+
SelectContainer,
9+
SelectDivider,
10+
SelectOption,
11+
SelectTrigger,
12+
} from '.'
13+
import { IconArrow } from './IconArrow'
14+
15+
export function ControlledRadio() {
16+
const [value, setValue] = useState('')
17+
const handleChange = (value: string) => {
18+
setValue(value)
19+
}
20+
return (
21+
<Select onValueChange={handleChange} type="radio" value={value}>
22+
<SelectTrigger>Select {value}</SelectTrigger>
23+
<SelectContainer>
24+
<SelectOption value="Option 1">Option 1</SelectOption>
25+
<SelectOption value="Option 2">Option 2</SelectOption>
26+
<SelectDivider />
27+
<SelectOption value="Option 3">Option 3</SelectOption>
28+
<SelectOption value="Option 4">Option 4</SelectOption>
29+
<Select type="radio">
30+
<SelectTrigger asChild>
31+
<SelectOption>
32+
<Flex alignItems="center" justifyContent="space-between" w="100%">
33+
Option 5<IconArrow />
34+
</Flex>
35+
</SelectOption>
36+
</SelectTrigger>
37+
<SelectContainer
38+
className={css({
39+
right: '0',
40+
top: '0',
41+
transform: 'translateX(100%)',
42+
})}
43+
>
44+
<SelectOption value="Option 6">Option 6</SelectOption>
45+
<SelectOption value="Option 7">Option 7</SelectOption>
46+
</SelectContainer>
47+
</Select>
48+
</SelectContainer>
49+
</Select>
50+
)
51+
}

packages/components/src/components/Select/Select.stories.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
SelectOption,
99
SelectTrigger,
1010
} from '.'
11+
import { ControlledCheckbox } from './ControlledCheckbox'
12+
import { ControlledRadio } from './ControlledRadio'
1113
import { IconArrow } from './IconArrow'
1214

1315
type Story = StoryObj<typeof meta>
@@ -26,16 +28,20 @@ const meta: Meta<typeof Select> = {
2628
}
2729

2830
export const Default: Story = {
29-
args: {},
31+
args: { type: 'radio' },
3032
render: (args) => (
31-
<Select {...args}>
33+
<Select {...args} defaultValue="Option 1">
3234
<SelectTrigger>Select</SelectTrigger>
3335
<SelectContainer>
34-
<SelectOption>Option 1</SelectOption>
35-
<SelectOption>Option 2</SelectOption>
36+
<SelectOption disabled value="Option 1">
37+
Option 1
38+
</SelectOption>
39+
<SelectOption value="Option 2">Option 2</SelectOption>
3640
<SelectDivider />
37-
<SelectOption>Option 3</SelectOption>
38-
<SelectOption disabled>Option 4</SelectOption>
41+
<SelectOption value="Option 3">Option 3</SelectOption>
42+
<SelectOption disabled value="Option 4">
43+
Option 4
44+
</SelectOption>
3945
<Select type="radio">
4046
<SelectTrigger asChild>
4147
<SelectOption>
@@ -60,4 +66,14 @@ export const Default: Story = {
6066
),
6167
}
6268

69+
export const ControlledRadioStory: Story = {
70+
args: {},
71+
render: () => <ControlledRadio />,
72+
}
73+
74+
export const ControlledCheckboxStory: Story = {
75+
args: {},
76+
render: () => <ControlledCheckbox />,
77+
}
78+
6379
export default meta

packages/components/src/components/Select/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type SelectValue<T extends SelectType> = T extends 'radio' ? string : string[]
2424
interface SelectProps extends ComponentProps<'div'> {
2525
defaultValue?: SelectValue<SelectType>
2626
value?: SelectValue<SelectType>
27-
onValueChange?: (value: SelectValue<SelectType>) => void
27+
onValueChange?: (value: string) => void
2828
defaultOpen?: boolean
2929
open?: boolean
3030
onOpenChange?: (open: boolean) => void
@@ -171,6 +171,7 @@ export function SelectContainer({ children, ...props }: ComponentProps<'div'>) {
171171
h="fit-content"
172172
p="10px"
173173
pos="absolute"
174+
styleOrder={1}
174175
transform="translateY(100%)"
175176
userSelect="none"
176177
w="232px"

0 commit comments

Comments
 (0)