[React] input type = 'radio', 'checkbox'์˜ ์ƒํƒœ๊ด€๋ฆฌ
728x90

Today, What I learned?

๋ผ๋””์˜ค ๋ฒ„ํŠผ๊ณผ ์ฒดํฌ๋ฐ•์Šค์˜ ์ƒํƒœ ์œ ์ง€๋ฅผ ์œ„ํ•œ ๊ณ ๋ฏผ์˜ ๊ณผ์ •์„ ๊ธฐ๋กํ•ด ๋ณธ๋‹ค!..

๋‹จ์ˆœํžˆ ๊ฐ’์„ ๋„ฃ์–ด state๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด์ „ ์ƒํƒœ ์Šค๋ƒ…์ƒท์„ ์ด์šฉํ•ด์„œ state๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋กœ์ง์„ ์จ๋ณด๊ฒŒ ๋˜์–ด ์ข‹์•˜๋‹ค.

 

์ฒซ ๋ฒˆ์งธ ์‹œ๋„

๋ผ๋””์˜ค ๋ฒ„ํŠผ

  const [isJuniorChecked, setIsJuniorChecked] = useState<boolean>(
    user?.isJunior,
  );
  const [checkedPositions, setCheckedPositions] = useState<string[]>(
    user?.positions,
  );

 

๋ผ๋””์˜ค ๋ฒ„ํŠผ์— ๋“ค์–ด๊ฐˆ onChange ์ด๋ฒคํŠธ.

const handleCheckedRadioChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      setIsJuniorChecked(e.target.id === 'isJunior' ? true : false);
    },
    [],
  );

 

๊ธฐ์กด ๋ผ๋””์˜ค ๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค state๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค.

  <>
      {juniorFlag ? (
        <InfoRadioBoxInput
          type="radio"
          id={id}
          name="isJunior"
          defaultValue={value}
          defaultChecked
          onChange={onChange}
        />
      ) : (
        <InfoRadioBoxInput
          type="radio"
          id={id}
          defaultValue={value}
          name="isJunior"
          onChange={onChange}
        />
      )}

      <InfoRadioBoxLabel htmlFor={id}>
        <InfocCheckBoxSpan>{value}</InfocCheckBoxSpan>
      </InfoRadioBoxLabel>
    </>

 

 

์ฒดํฌ๋ฐ•์Šค

์ฒดํฌ๋ฐ•์Šค ํ•ธ๋“ค๋Ÿฌ๋Š” ํด๋ฆญ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ธฐ์กด ์ƒํƒœ์— ํฌํ•จ๋˜์–ด ์žˆ์—ˆ๋‹ค๋ฉด ๋นผ์ค˜์•ผ ํ•˜๊ณ  ์—†์œผ๋ฉด ๋„ฃ์–ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค.
์ด์ „ state์˜ ์Šค๋ƒ…์ƒท์„ ๊ฐ€์ ธ์™€์„œ ํ•„ํ„ฐ๋งํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ธํ’‹ ํƒœ๊ทธ์— onChange ์ด๋ฒคํŠธ ๊ฑธ์–ด์ฃผ๋Š” ๋ฐฉ์‹์€ ๋™์ผํ•˜๋‹ค.

  const handleCheckedPositionsChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const { value, checked } = e.target;

      if (checked) {
        setCheckedPositions((prev) => [...prev, value]);
      } else {
        // ์ฒดํฌ ํ•ด์ œ๋œ ๊ฐ’ ํ•„ํ„ฐ๋งํ•˜๊ธฐ
        setCheckedPositions((prev) =>
          prev.filter((position) => position !== value),
        );
      }
    },
    [],
  );

 

์ผ๋‹จ ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ๋Š” ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด๋Ÿฌ๊ณ  ๋ณด๋‹ˆ ... ๐Ÿ‘€
๋งˆ์ดํŽ˜์ด์ง€ ์•ˆ์—์„œ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋Š” state ๊ฐœ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋Š˜์–ด๋‚œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๋‹ค.
ํ˜„์žฌ ๋งˆ์ดํŽ˜์ด์ง€์—์„œ๋Š” ๋‹‰๋„ค์ž„, ํ”„๋กœํ•„, ๊ฒฝ๋ ฅ ๋“ฑ๋“ฑ 5๊ฐœ ์ด์ƒ์˜ ์œ ์ € ์ •๋ณด State๊ฐ€ ์žˆ๋Š”๋ฐ!...

๊ทธ๋ž˜์„œ ์ƒํƒœ๋ฅผ ํ•˜๋‚˜๋กœ ๊ด€๋ฆฌํ•ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

 

๋‘ ๋ฒˆ์งธ

userInfo ๋ผ๋Š” ์ „์ฒด state๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

 const [userInfo, setUserInfo] = useState<UserInfo>(initialUserInfo);

 

์ด state๋Š” ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ’๋“ค์„ ๊ฐ€์ง„๋‹ค.

const initialUserInfo = {
  displayName: '',
  photoURL: '',
  isJunior: false,
  positions: [],
  plannerStack: [],
  designerStack: [],
  developerStack: [],
};

 

๋ผ๋””์˜ค ๋ฒ„ํŠผ

๋ผ๋””์˜ค ๋ฒ„ํŠผ ์ƒํƒœ ํ•ธ๋“ค๋Ÿฌ๋Š” ์ด์ „๊ณผ ๋น„์Šทํ•˜๋‹ค.
์ด ์ „์˜ state ์Šค๋ƒ…์ƒท์„ ๊ฐ€์ ธ์˜ค๊ณ , ๋ผ๋””์˜ค ๋ฒ„ํŠผ์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค๊ฐ’๋งŒ ๋”ฐ๋กœ ์ˆ˜์ •ํ•ด ์ค€๋‹ค.

const handleCheckedRadioChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const { id } = e.currentTarget;

      setUserInfo((prevState) => {
        return {
          ...prevState,
          isJunior: id === 'junior' ? true : false,
        };
      });

      handleButtonActive();
    },
    [],
  );

 

 

์ฒดํฌ ๋ฐ•์Šค

์ฒดํฌ๋ฐ•์Šค๋„ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ, ์ด์ „ ์ƒํƒœ ์Šค๋ƒ…์ƒท์„ ๊ฐ€์ ธ์˜ค๊ณ  ํ•ด๋‹น ํ‚ค๊ฐ’์„ ์ฝ• ์ง‘์–ด์„œ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.

 const handleCheckedPositionsChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      handleButtonActive();
      const { value, checked } = e.target;

      if (checked) {
        setUserInfo((prevState) => {
          return {
            ...prevState,
            positions: [...prevState.positions, value],
          };
        });
      } else {
        // ์ฒดํฌ ํ•ด์ œ๋œ ๊ฐ’ ํ•„ํ„ฐ๋งํ•˜๊ธฐ
        setUserInfo((prevState) => {
          return {
            ...prevState,
            positions: prevState.positions.filter(
              (position) => position !== value,
            ),
          };
        });
      }
    },
    [],
  );

 

 

 

์ด ์™ธ์— ๊ธฐ์ˆ  ์Šคํƒ ์„ ํƒ ๋ถ€๋ถ„์— ์žˆ์–ด์„œ ๋‚œํ•ญ์„ ๊ฒช๊ณ  ์žˆ๊ธด ํ•˜์ง€๋งŒ.. ๐Ÿ˜‚
์ด๊ฑด ์ถ”ํ›„์— ๊ธ€์„ ์ž‘์„ฑํ•ด ๋ณด์•„์•ผ๊ฒ ๋‹ค.

728x90