Skip to content

Commit

Permalink
feat: #2 라디오 버튼 컴포넌트
Browse files Browse the repository at this point in the history
  • Loading branch information
leeminhee119 committed Jul 7, 2024
1 parent cf50db2 commit 49a993d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { css } from "@emotion/react";
import { useContext } from "react";

import ListItemCard from "./Card/ListItemCard";

import ListItemCard from "@/component/common/Card/ListItemCard";
import { RadioContext } from "@/store/context/RadioContext";

type RadioProps = {
Expand Down
31 changes: 31 additions & 0 deletions src/component/common/RadioButton/RadioButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { css } from "@emotion/react";
import { useState } from "react";

import Radio from "./Radio";

import { RadioContext } from "@/store/context/RadioContext";

type RadioButtonGroupProps = {
items: { value: string; text: string }[];
};

const RadioButtonGroup = ({ items }: RadioButtonGroupProps) => {
const [selectedValue, setSelectedValue] = useState<string>();
return (
<div
css={css`
display: flex;
flex-direction: column;
gap: 1rem;
`}
>
<RadioContext.Provider value={{ selectedValue, onChange: (val: string) => setSelectedValue(val) }}>
{items.map(({ value, text }) => {
return <Radio key={crypto.randomUUID()} value={value} text={text} />;
})}
</RadioContext.Provider>
</div>
);
};

export default RadioButtonGroup;
8 changes: 8 additions & 0 deletions src/store/context/RadioContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createContext } from "react";

type RadioContextState = {
selectedValue?: string;
onChange?: (value: string) => void;
};

export const RadioContext = createContext<RadioContextState | undefined>(undefined);

0 comments on commit 49a993d

Please sign in to comment.