๐Ÿ”ฅ ๋ฌธ์ œ

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ์ปดํฌ๋„ŒํŠธ ํƒ€์ž…์„ ์„ค์ •ํ•˜๋Š” ๋ฐ ์žˆ์–ด ๊ณ ๋ฏผ์ด ์ƒ๊ฒผ๋‹ค.

<aside> ๐Ÿ’ก ๋งŒ์•ฝ n๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’๊ณผ m๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ํƒ€์ž…์„ ์ง€์ •ํ•ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

</aside>

๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค๋˜์ง€, ํƒ€์ž…์ด๋˜์ง€ ๊ฐ„์— ์„ค์ •ํ•ด์ฃผ๋ฉด ๊ทธ๋งŒ์ธ๋ฐ, ๋ฐฐ์—ด์€ ์ข€ ๋‹ค๋ฅธ ๋А๋‚Œ์ด์—ˆ๋‹ค.

์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ?


โญ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1. ๋ฐฐ์—ด + ํƒ€์ž… ๋‹จ์–ธ

๊ฐ€. ๋จผ์ € ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๊ฐ’ ์ง€์ •

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

export type callbackType = () => void;
export type buttonArrType = (string | callbackType)[];

๋‚˜. ํƒ€์ž… ์ถ”๋ก  ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์— ํƒ€์ž… ๋‹จ์–ธ

๊ฐœ๋ฐœ์ž๊ฐ€ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—๊ฒŒ ํƒ€์ž…์„ ๋‹จ์–ธํ•ด์คŒ์œผ๋กœ์จ, ๋ณด์ˆ˜์ ์ธ ํƒ€์ž… ์ถ”๋ก ์„ ํ•ด์†Œ์‹œ์ผœ์คŒ์œผ๋กœ์จ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

<StyledSortButtons width={width}>
  {buttonArr.map(([name, cb]: buttonArrType) => (
    <Button
      key="name"
      buttonType="primary"
      fontSize={14}
      onClick={cb as callbackType}
      reversal
      bold={false}
      backgroundColor="white"
      color="black"
    >
      {name}
    </Button>
  ))}
</StyledSortButtons>

2. ํŠœํ”Œ์„ ์‚ฌ์šฉ

ํ˜„์žฌ ๋ฐฐ์—ด์€ ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ•ธ๋“œ๋ถ์— ์ ํžŒ ํŠœํ”Œ์„ ์‚ฌ์šฉํ•ด๋ณด๋„๋ก ํ•˜์ž.

ํŠœํ”Œ์ด๋ž€, ์‰ฝ๊ฒŒ ๋งํ•ด์„œ, ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๋ฐฐ์—ด์ด๋ผ ๋ณด๋ฉด ๋˜๊ฒ ๋‹ค.

import Button from '@components/atoms/Button';
import styled from '@emotion/styled';
import React from 'react';

export type widthType = string | number;
export type callbackType = () => void;
export type buttonArrType = [string, callbackType];

export interface SortButtonsProps {
  width: widthType;
  [prop: string]: any;
}

const StyledSortButtons: React.FC<SortButtonsProps> = styled.section`
  display: flex;
  justify-content: space-between;
  width: ${({ width }: { width: widthType }) =>
    typeof width === 'string' ? width : `${width}px`};
`;

const SortButtons = ({ width, buttonArr }: SortButtonsProps) => {
  if (!buttonArr.length) return null;
  return (
    <StyledSortButtons width={width}>
      {buttonArr.map(([name, cb]: buttonArrType) => (
        <Button
          key="name"
          buttonType="primary"
          fontSize={14}
          onClick={cb}
          reversal
          bold={false}
          backgroundColor="white"
          color="black"
        >
          {name}
        </Button>
      ))}
    </StyledSortButtons>
  );
};

export default SortButtons;