[React] input radio, checkbox ์ปค์Šคํ…€ํ•˜๊ธฐ with Styled-components
728x90

Today, What I learned?

UI๋ฅผ ๋จผ์ € ์ž‘์—…ํ•˜๋ฉด์„œ input ํƒœ๊ทธ ์ค‘ ์ฒดํฌ๋ฐ•์Šค์™€ ๋ผ๋””์˜ค๋ฒ„ํŠผ์„ ์ปค์Šคํ…€ํ•ด์•ผ๊ฒ ๋‹ค ์ƒ๊ฐ์ด ๋“ค์—ˆ๋Š”๋ฐ,

๊ธฐ๋ณธ์ ์œผ๋กœ ์žกํ˜€์žˆ๋Š” UI๋ฅผ ๋‚ ๋ฆฌ๋ฉด์„œ ์–ด๋–ป๊ฒŒ ๋””์ž์ธ๋Œ€๋กœ ๊ตฌํ˜„ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์‚ฝ์งˆ ์‹œ๊ฐ„์ด ์ข€ ๊ธธ์—ˆ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ ์ปค์Šคํ…€์— ์„ฑ๊ณตํ–ˆ๊ณ  ์–ด๋–ป๊ฒŒ ํ•ด๋†“์•˜๋Š”์ง€ ๊ธฐ๋กํ•ด๋ณด๋ ค ํ•œ๋‹ค!..

์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ๋Š” ์ด๋ชจ์…˜์„ ์“ฐ๊ณ  ์žˆ์ง€๋งŒ Styled-components ํ˜•์‹๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ ์ค‘์ด๋ผ๋Š” ์ ..

 

 

input type='radio'

์™„์„ฑ๋œ ๋ชจ์Šต์€ ์ด๋ ‡๋‹ค.

์–ด๋–ป๊ฒŒ ์ปค์Šคํ…€์„ ํ•  ๊ฒƒ์ด๋ƒ? ๋ผ๋ฒจ์˜ htmlFor์™€ ์ธํ’‹์˜ name์ด ๋™์ผํ•  ๊ฒฝ์šฐ ๋ผ๋ฒจ์„ ๋ˆ„๋ฅด๊ธฐ๋งŒ ํ•ด๋„ ์ธํ’‹์ด ๋™์ž‘๋œ๋‹ค๋Š” ์ ์„ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค.

์™„์„ฑ๋œ ๋ชจ์Šต

 

 

 {career.map((career) => (
              <CareerRadioBox key={career.value}>
                <InfoRadioBoxInput
                  type="radio"
                  id={career.id}
                  name={career.value}
                />
                <InfoCheckBoxLabel htmlFor={career.id}>
                  <InfocCheckBoxSpan>{career.name}</InfocCheckBoxSpan>
                </InfoCheckBoxLabel>
              </CareerRadioBox>
))}

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์€ name ๊ฐ’์ด ๊ฐ™์•„์•ผ ํ•œ ๋ฌถ์Œ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ!

 

Styled-components์˜ CSS ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์ด๋ ‡๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์›๋ž˜์˜ ์ธํ’‹์„ ๋ณด์ด์ง€ ์•Š๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฉ€๋ฆฌ ๋ณด๋‚ด๋ฒ„๋ฆฌ๊ณ , ์ฒดํฌ๊ฐ€ ๋˜์—ˆ์„ ๊ฒฝ์šฐ ๋ผ๋ฒจ์˜ ๋ฐฐ๊ฒฝ์ƒ‰, ๊ธ€์ž์ƒ‰์„ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ  ์žˆ๋‹ค.

์ธํ’‹ํƒœ๊ทธ๋ฅผ display:none ์ฒ˜๋ฆฌ๋„ ํ•ด๋ณด์•˜์ง€๋งŒ ๋™์ž‘์ด ์•ˆ๋˜์–ด์„œ ์ง€๊ธˆ๊นŒ์ง„ ์ด ๋ฐฉ๋ฒ•์ด ์ตœ์„ ๊ฐ™๋‹ค.

const InfoRadioBoxInput = styled.input`
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;

  &:checked + label {
    background-color: ${COLORS.violetB400};
    color: ${COLORS.white};
  }
`;

 

 

 

input type='checkbox'

์™„์„ฑ๋œ ๋ชจ์Šต

 

์ฒดํฌ๋ฐ•์Šค๋„ ๋ผ๋””์˜ค๋ฒ„ํŠผ๊ณผ ๋น„์Šทํ•˜๋‹ค.
๋™์ผํ•˜๊ฒŒ ๋ผ๋ฒจ์„ ์ด์šฉํ•˜๊ณ , ์ธํ’‹์˜ id์™€ ๋ผ๋ฒจ์˜ htmlFor๋ฅผ ๊ฐ™์€ ๊ฐ’์„ ์ค€๋‹ค.

...
  <CheckBoxWrapper key={type}>
      <CheckBoxInput type="checkbox" id={type} name="skills" />
      <CheckboxLabel htmlFor={type}>
        <CheckBoxText>{name}</CheckBoxText>
      </CheckboxLabel>
 </CheckBoxWrapper>

 

 

CSS ์ค€ ๋ฐฉ์‹๋„ ์œ„์™€ ๋™์ผํ•จ..

const CheckBoxInput = styled.input`
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;

  &:checked + label {
    background-color: ${COLORS.violetB400};
    color: ${COLORS.white};
  }
`;

const CheckboxLabel = styled.label`
  padding: 0.5rem 1rem;
  height: 2.25rem;
  cursor: pointer;
  border-radius: 2rem;
  background-color: #f2f4f6;
  font-size: 0.75rem;
  color: #383838;
`;

 

 

์ปค์Šคํ…€ํ•˜๋Š๋ผ ์•ฝ๊ฐ„ ์‚ฝ์งˆํ–ˆ์ง€๋งŒ ๋‚˜์ค‘์—๋„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค!

728x90