Skip to content

Commit c279499

Browse files
committed
feat(QYOG-162): 선택된값 받을 함수 props에 추가(추후 수정)
1 parent e0f9774 commit c279499

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/components/Design/SelectBox/SelectBox.stories.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ const meta = {
88

99
type Story = StoryObj<typeof meta>;
1010

11+
interface OptionInterface {
12+
name: string;
13+
value: string;
14+
}
15+
16+
function setSelectOption(result: OptionInterface) {
17+
console.log(result);
18+
return result;
19+
}
20+
1121
export const Primary: Story = {
1222
args: {
1323
options: [
@@ -17,6 +27,7 @@ export const Primary: Story = {
1727

1828
defaultName: "학년",
1929
isRequired: true,
30+
setSelectOption: setSelectOption,
2031
},
2132
};
2233

src/components/Design/SelectBox/SelectBox.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (c) 2024 Your Company
55
*/
66

7-
import { useState, useRef } from "react";
7+
import { useState, useRef, useEffect } from "react";
88

99
import * as S from "./emotion";
1010

@@ -16,12 +16,14 @@ interface SelectboxProps {
1616
options: OptionInterface[];
1717
defaultName: string;
1818
isRequired: boolean;
19+
setSelectOption: (result: OptionInterface) => OptionInterface; // setState or object
1920
}
2021

2122
export default function SelectBox({
2223
options,
2324
defaultName = "값을 선택하세요.",
2425
isRequired = false,
26+
setSelectOption,
2527
}: SelectboxProps) {
2628
const [isOpen, setIsOpen] = useState<boolean>(false);
2729
const [selectedName, setSelectedName] = useState(defaultName);
@@ -37,6 +39,10 @@ export default function SelectBox({
3739
setIsChanged(true);
3840
}
3941

42+
useEffect(() => {
43+
if (selectedIndex >= 0) setSelectOption(options[selectedIndex]);
44+
}, [selectedIndex]);
45+
4046
return (
4147
<S.SelectBoxWrap
4248
onClick={() => setIsOpen(!isOpen)}

0 commit comments

Comments
 (0)