Skip to content

Commit 8e87b4b

Browse files
authored
Merge pull request #87 from modern-agile-team/feat/woo/QYOG-162
feat(QYOG-162): 셀렉트박스 디자인 시스템 개발
2 parents 7d4a05e + 4c3aeed commit 8e87b4b

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { StoryObj } from "@storybook/react";
2+
import { SelectBox } from ".";
3+
4+
const meta = {
5+
title: "components/SelectBox",
6+
component: SelectBox,
7+
};
8+
9+
type Story = StoryObj<typeof meta>;
10+
11+
interface OptionInterface {
12+
name: string;
13+
value: string;
14+
}
15+
16+
function setSelectOption(result: OptionInterface) {
17+
return result;
18+
}
19+
20+
export const Primary: Story = {
21+
args: {
22+
options: [
23+
{ name: "1학년", value: "1" },
24+
{ name: "2학년", value: "2" },
25+
],
26+
27+
defaultName: "학년",
28+
isRequired: true,
29+
setSelectOption: setSelectOption,
30+
},
31+
};
32+
33+
export default meta;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Created on Sat May 18 2024
3+
*
4+
* Copyright (c) 2024 Your Company
5+
*/
6+
7+
import { useState, useRef, useEffect } from "react";
8+
9+
import * as S from "./emotion";
10+
11+
interface OptionInterface {
12+
name: string;
13+
value: string;
14+
}
15+
interface SelectboxProps {
16+
options: OptionInterface[];
17+
defaultName: string;
18+
isRequired: boolean;
19+
setSelectOption: (result: OptionInterface) => OptionInterface; // setState or object
20+
}
21+
22+
export default function SelectBox({
23+
options,
24+
defaultName = "값을 선택하세요.",
25+
isRequired = false,
26+
setSelectOption,
27+
}: SelectboxProps) {
28+
const [isOpen, setIsOpen] = useState<boolean>(false);
29+
const [selectedName, setSelectedName] = useState(defaultName);
30+
const [selectedIndex, setSelectedIndex] = useState<number>(-1);
31+
32+
function handleOnChangeOption(e: any) {
33+
setSelectedName(e.target.innerHTML);
34+
setSelectedIndex(e.target.id);
35+
}
36+
37+
useEffect(() => {
38+
if (selectedIndex >= 0) setSelectOption(options[selectedIndex]);
39+
}, [selectedIndex]);
40+
41+
return (
42+
<S.SelectBoxWrap
43+
onClick={() => setIsOpen(!isOpen)}
44+
verticalAlign="center"
45+
horizonAlign="center"
46+
>
47+
<S.SelectedDiv verticalAlign="center" horizonAlign="center">
48+
{isRequired ? <S.RequireNotice>*</S.RequireNotice> : <></>}
49+
<label>{selectedName}</label>
50+
<span></span>
51+
</S.SelectedDiv>
52+
<S.OptionUl isShow={isOpen}>
53+
{options.map((option, idx) => {
54+
return (
55+
<S.OptionListItem
56+
key={idx}
57+
id={String(idx)}
58+
onClick={handleOnChangeOption}
59+
>
60+
{option.name}
61+
</S.OptionListItem>
62+
);
63+
})}
64+
</S.OptionUl>
65+
</S.SelectBoxWrap>
66+
);
67+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Created on Sat June 15 2024
3+
*
4+
* Copyright (c) 2023 Your Company
5+
*/
6+
7+
import { Column, Row } from "@/components/Layouts";
8+
import styled from "@emotion/styled";
9+
10+
export const SelectBoxWrap = styled(Column.div)`
11+
border: 1px solid red;
12+
position: relative;
13+
cursor: pointer;
14+
15+
padding: 10px;
16+
width: 200px;
17+
border-radius: 10px;
18+
`;
19+
20+
export const OptionUl = styled.ul<{ isShow: boolean }>`
21+
max-height: ${(props) => (props.isShow ? "none" : "0")};
22+
position: absolute;
23+
list-style: none;
24+
top: 18px;
25+
left: 0;
26+
overflow: hidden;
27+
padding: 0;
28+
border: 1px solid blue;
29+
border-top: none;
30+
width: 100%;
31+
margin-top: 22px;
32+
`;
33+
34+
export const RequireNotice = styled.span`
35+
color: red;
36+
`;
37+
38+
export const SelectedDiv = styled(Row.div)`
39+
width: 100%;
40+
justify-content: space-between;
41+
`;
42+
43+
export const OptionListItem = styled.li`
44+
background-color: white;
45+
&:hover {
46+
background-color: black;
47+
}
48+
`;
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Created on Fri Feb 16 2024
3+
*
4+
* Copyright (c) 2024 Your Company
5+
*/
6+
7+
export { default as SelectBox } from "./SelectBox";

0 commit comments

Comments
 (0)