ํ์ ์คํฌ๋ฆฝํธ๋ก ์ปดํฌ๋ํธ ํ์ ์ ์ค์ ํ๋ ๋ฐ ์์ด ๊ณ ๋ฏผ์ด ์๊ฒผ๋ค.
<aside> ๐ก ๋ง์ฝ n๋ฒ์งธ ๋ฐฐ์ด์ ๊ฐ๊ณผ m๋ฒ์งธ ๋ฐฐ์ด์ ๊ฐ์ ํ์ ์ ์ง์ ํด์ฃผ๊ณ ์ถ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
</aside>
๊ฐ์ฒด์ ๊ฒฝ์ฐ ๊ฐ๋จํ๊ฒ ์ธํฐํ์ด์ค๋์ง, ํ์ ์ด๋์ง ๊ฐ์ ์ค์ ํด์ฃผ๋ฉด ๊ทธ๋ง์ธ๋ฐ, ๋ฐฐ์ด์ ์ข ๋ค๋ฅธ ๋๋์ด์๋ค.
์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์์๊น?
๋ค์๊ณผ ๊ฐ์ด ๋ฐฐ์ด์ ๋ค์ด๊ฐ ์ ์๋ ํ์ ๊ฐ์ ์ง์ ํด์ฃผ์๋ค.
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>
ํ์ฌ ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ์ ํด์ ธ ์๋ค. ๋ฐ๋ผ์ ํ์ ์คํฌ๋ฆฝํธ ํธ๋๋ถ์ ์ ํ ํํ์ ์ฌ์ฉํด๋ณด๋๋ก ํ์.
ํํ์ด๋, ์ฝ๊ฒ ๋งํด์, ๊ธธ์ด๊ฐ ์ ํด์ ธ ์๋ ๋ฐฐ์ด์ด๋ผ ๋ณด๋ฉด ๋๊ฒ ๋ค.
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;