개발자로 전향중

React 체크박스 여러개 체크할 때 체크 value 본문

React

React 체크박스 여러개 체크할 때 체크 value

hovinee 2022. 11. 2. 15:27
const BuyProductSection = styled.section`
  width: 100%;
  height: 250px;

  .buyProductSection--header {
    height: 27px;
    border: 1px solid #cecece;
    border-radius: 13px;
    font-family: "Roobert";
    font-style: normal;
    font-weight: 400;
    font-size: 18.3881px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #000000;
  }
`;

const BuyProductWrapper = styled.div`
  display: flex;
  justify-content: space-between;
  margin-top: 10px;

  .buyProductSection--product2 {
    width: 105px;
    height: 105px;
    background: #ffffff;
    border: 1px solid #cecece;
    border-radius: 13.1343px;
    margin-right: 2px;
  }

  .buyProductSection--product3 {
    width: 105px;
    height: 105px;
    background: #ffffff;
    border: 1px solid #cecece;
    border-radius: 13.1343px;
    margin-right: 2px;
  }

  .buyProductSection--product4 {
    width: 105px;
    height: 105px;
    background: #ffffff;
    border: 1px solid #cecece;
    border-radius: 13.1343px;
    margin-right: 2px;
  }
`;

const BuyProduct1 = styled.div`
  position: relative;
  width: 105px;
  height: 105px;
  background: #ffffff;
  border: 1px solid #cecece;
  border-radius: 13.1343px;
  margin-right: 2px;
  .buyProduct1--selectClothes {
    width: 100%;
  }

  & > input[type="checkbox"] {
    position: absolute;
    left: 8px;
    bottom: 8px;
    appearance: none;
    width: 16px;
    height: 16px;
    background: #ffffff;
    border: 1px solid #cecece;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s;
  }

  & > input[type="checkbox"]:checked {
    border-color: transparent;
    background-image: url("data:image/svg+xml,%3Csvg width='9' height='9' viewBox='0 0 9 9' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 2.75L3.33333 8L8 1' stroke='%230C8AFF' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E%0A");
    background-size: 100% 100%;
    // background-position: 50%;
    background-repeat: no-repeat;
    border: 3px solid #0c8aff;
  }

  .buyProduct1--name {
    font-family: "Roobert";
    font-weight: 700;
    font-size: 12px;
    text-decoration-line: underline;
    color: #000000;
    margin: 15px 0px 10px 8px;
  }

  .buyProduct1--price {
    width: 50px;
    height: 20px;
    border: 1px solid #cecece;
    border-radius: 13px;
    font-family: "Roobert";
    font-weight: 400;
    font-size: 12px;
    color: #000000;
    display: flex;
    justify-content: center;
    margin: 0px 0px 0px 8px;
  }
`;
       {findBrand?.Top.map((value, index) => {
          return (
            <BuyProductSection>
              <div className="buyProductSection--header">test</div>
              <BuyProductWrapper>
                <BuyProduct1>
                  <img
                    className="buyProduct1--selectClothes"
                    src={value}
                    alt="selectClothes"
                  />
                  <SelectProduct
                    index={index}
                    checkedItemHandler={checkedItemHandler}
                  />
                  <div className="buyProduct1--name">test</div>
                  <div className="buyProduct1--price">*200</div>
                </BuyProduct1>
              </BuyProductWrapper>
            </BuyProductSection>
          );
        })}
        
        
        import React, { useEffect, useState } from "react";

interface valueProps {
  index: number;
  checkedItemHandler: (index: number, isChecked: boolean) => void;
}
const SelectProduct = ({ index, checkedItemHandler }: valueProps) => {
  // Issue 컴포넌트

  const [bChecked, setChecked] = useState(false);

  const checkHandler = (index: number, { target }: any) => {
    setChecked(!bChecked);
    checkedItemHandler(index, target.checked);
  };

  return (
    <>
      <input
        type="checkbox"
        checked={bChecked}
        onChange={(e) => checkHandler(index, e)}
      />
    </>
  );
};

export default SelectProduct;