Skip to content

Commit

Permalink
Merge pull request #21 from depromeet/feature/2/multi-select-button
Browse files Browse the repository at this point in the history
Feature/2/커스텀 라디오 버튼
  • Loading branch information
leeminhee119 authored Jul 8, 2024
2 parents e7ca080 + 148b0ae commit b4a68f2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/component/common/Card/ListItemCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { css } from "@emotion/react";

type ListItemCardProps = {
variant?: "default" | "theme";
height?: string;
borderRadius?: string;
children: React.ReactNode;
};

const ListItemCard = ({ variant = "default", height = "5rem", borderRadius = ".8rem", children }: ListItemCardProps) => {
return (
<div
css={css`
display: flex;
justify-content: center;
align-items: center;
background-color: ${variant === "default" ? "#F1F3F5" : "#608dff"};
height: ${height};
border-radius: ${borderRadius};
color: ${variant === "default" ? "#212529" : "#fff"};
transition: 0.4s all;
`}
>
{children}
</div>
);
};

export default ListItemCard;
45 changes: 45 additions & 0 deletions src/component/common/RadioButton/Radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { css } from "@emotion/react";
import { useContext } from "react";

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

type RadioProps = {
value: string;
children: React.ReactNode;
};

const Radio = ({ value, children }: RadioProps) => {
const radioContext = useContext(RadioContext);
return (
<ListItemCard variant={radioContext?.selectedValue === value ? "theme" : "default"}>
<label
htmlFor={value}
css={css`
font-weight: 600;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
`}
>
{children}
</label>
<input
type="radio"
name={radioContext?.radioName}
id={value}
value={value}
onChange={(e) => {
radioContext?.onChange && radioContext.onChange(e.target.value);
}}
css={css`
display: none;
`}
/>
</ListItemCard>
);
};

export default Radio;
26 changes: 26 additions & 0 deletions src/component/common/RadioButton/RadioButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { css } from "@emotion/react";

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

type RadioButtonGroupProps = {
children: React.ReactNode;
selectedValue: string | undefined;
onChange: React.Dispatch<React.SetStateAction<string | undefined>>;
radioName: string;
};

const RadioButtonGroup = ({ children, ...rest }: RadioButtonGroupProps) => {
return (
<div
css={css`
display: flex;
flex-direction: column;
gap: 1rem;
`}
>
<RadioContext.Provider value={rest}>{children}</RadioContext.Provider>
</div>
);
};

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

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

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

0 comments on commit b4a68f2

Please sign in to comment.