๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hi-๋žŒ๐Ÿ‘‹ High-๋žŒโ˜€๏ธ

[React] RadioButton ์ปค์Šคํ…€ํ•˜๊ธฐ with styled-components ๋ณธ๋ฌธ

React

[React] RadioButton ์ปค์Šคํ…€ํ•˜๊ธฐ with styled-components

ํ•˜์ด๋žŒ 2023. 10. 27. 17:40

 

๊ธฐ์กด Input์—์„œ ์ œ๊ณตํ•˜๋Š” RadioButton์—์„œ ์ƒ‰๋งŒ ๋ฐ”๊พธ๊ณ  ์‹ถ์—ˆ์„ ๋ฟ์ธ๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ์ž๋ฃŒ๋“ค์ด ๋งŽ์ง€ ์•Š์•˜๋‹ค. ์ปค์Šคํ…€ํ–ˆ๋‹ค๋Š” ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด radio ํŠน์œ ์˜ ๋™๊ทธ๋ผ๋ฏธ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์–‘๋ณด๋‹ค, ์•„์˜ˆ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋‹ค์ˆ˜์˜€๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜์ฒ˜๋Ÿผ ๊ณ ๋ฏผํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด RadioButton ์ปค์Šคํ…€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค!

 

๋จผ์ €, ๋‚ด๊ฐ€ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ๋ชจ์–‘์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

 

๊ณ ๋ คํ•ด์•ผ ํ•  ์ ์€ ์„ ํƒ๋˜์—ˆ์„ ๋•Œ

1. ๋‚ด๋ถ€์— ๋™๊ทธ๋ผ๋ฏธ๊ฐ€ ์ƒ๊ฒจ์•ผ ํ•˜๋ฉฐ
2. icon์˜ ์ƒ‰๊ณผ label์˜ ์ƒ‰์ƒ์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•œ๋‹ค

 

๋Š” ๊ฒƒ์ด๋‹ค.

 

๋‹ค๋ฅธ ๊ฒƒ๋ณด๋‹ค ๋‚˜๋Š” css์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๊ณ  ์žˆ์–ด์„œ, 1๋ฒˆ์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ง‰๋ง‰ํ–ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ css์— ๋Œ€ํ•ด ์•„๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

๊ฐ€์ƒ ์„ ํƒ์ž :after
์„ ํƒํ•œ ์š”์†Œ์˜ ๋‚ด์šฉ ๋’ค์— ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์ƒ ์„ ํƒ์ž
์กฐํ•ฉ์ž +
์ธ์ ‘ ํ˜•์ œ ์š”์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•ด๋‹น ์š”์†Œ์— ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
๊ฐ€์ƒ ์„ ํƒ์ž :checked
input ์š”์†Œ ์ค‘์—์„œ ์ฒดํฌ๋œ ์ƒํƒœ์ธ ์š”์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์ƒ ์„ ํƒ์ž

 

์œ„์˜ CSS ์„ ํƒ์ž๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

 

 

์ฝ”๋“œ


RadioButton.js

๋จผ์ €, ์ปดํฌ๋„ŒํŠธ์˜ ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import React from 'react';
import { RadioIcon, RadioInput, RadioWrapper } from './style';

const RadioButton = ({ name, value, checked, onChange, children }) => {
  return (
    <RadioWrapper checked={checked}>
      <RadioInput
        type="radio"
        name={name}
        value={value}
        checked={checked}
        onChange={onChange}
      />
      <RadioIcon checked={checked}/>
      {children}
    </RadioWrapper>
  );
};

export default RadioButton;

 

style.js

CSS๋Š” ๋”ฐ๋กœ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

import styled from 'styled-components';

export const RadioWrapper = styled.label`
  display: inline-block;
  margin-right: 10px;
  cursor: pointer;
  position: relative;
  padding-left: 25px;
  font-weight: 700;
  color: ${(props) => (props.checked ? '#1C1C1CD9' : '#1C1C1C99')};
`;

export const RadioInput = styled.input`
  position: absolute;
  opacity: 0;
  cursor: pointer;

  &:checked + span:after {
    display: block;
  }
`;

export const RadioIcon = styled.span`
  position: absolute;
  top: 0;
  left: 0;
  height: 16px;
  width: 16px;
  border-radius: 50%;
  border: 1.5px solid ${props => props.checked ? '#3D4371' : '#3D437199'};
  background-color: #fff;

  &:after {
    content: '';
    position: absolute;
    display: none;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #3D4371;
  }
`;

 

 

์‚ฌ์šฉ ๋ฐฉ๋ฒ•


์œ„์˜ RadioButton์„ ํ†ตํ•ด ๋ฉด์ ‘์„ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์ง๋ฌด๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

GenerateInterview.js

const GenerateInterview = () => {
  const [selectedJobType, setSelectedJobType] = useState("");

  const handleJobTypeChange = (event) => {
    setSelectedJobType(event.target.value);
  };
  
  return (
    <>
      <RadioButton
        name="jobType"
        value="ํ”„๋ก ํŠธ์—”๋“œ"
        checked={selectedJobType === "ํ”„๋ก ํŠธ์—”๋“œ"}
        onChange={handleJobTypeChange}
      >
        ํ”„๋ก ํŠธ์—”๋“œ
      </RadioButton>
      <RadioButton
        name="jobType"
        value="์„œ๋ฒ„/๋ฐฑ์—”๋“œ"
        checked={selectedJobType === "์„œ๋ฒ„/๋ฐฑ์—”๋“œ"}
        onChange={handleJobTypeChange}
      >
        ์„œ๋ฒ„/๋ฐฑ์—”๋“œ
      </RadioButton>
      <RadioButton
        name="jobType"
        value="์›น ํ’€์Šคํƒ"
        checked={selectedJobType === "์›น ํ’€์Šคํƒ"}
        onChange={handleJobTypeChange}
      >
        ์›น ํ’€์Šคํƒ
      </RadioButton>
      <RadioButton
        name="jobType"
        value="iOS"
        checked={selectedJobType === "iOS"}
        onChange={handleJobTypeChange}
      >
        iOS
      </RadioButton>
    </>
  );
};

export default GenerateInterview;

 

์‹คํ–‰ ๊ฒฐ๊ณผ