์๊ธฐ๋ก ํ๋ํ๋ ์ด๋ ๊ฒ ๋ง๋ค์๋ ๋ชฉ๋ก์ด ์์๋ค.
์ด๊ฑธ tanstack table์ ์ฌ์ฉํ๋ฉด์, ๊ฐ์ ๋ฟ๋ ค์ฃผ๋ ํํ๋ก ์ฐ๊ณ ์ถ์ด์ ๋ง๋ค๊ฒ ๋จ
์ ๋ถ๋ถ์ column์ ์ด๋ฐ์์ผ๋ก ์์ฑ์ด ๋๋ค.
columnHelper.accessor("issueState", {
header: "์ํ",
cell({ row }) {
const { original } = row;
return original.issueState === 1110100
? "์์ฒญ"
: original.issueState === 1110101
? "์งํ"
: original.issueState === 1110102
? "์๋ฃ"
: original.issueState === 1110103
? "์ฌ์์ฒญ"
: "";
},
)
์๋๋ ๊ฐ์ ๋ฐ์์ค๋ฉด, ๊ทธ์ ๋ง๋ ์ ์ ์ ํ๊ธ์ ๋ฃ์ด์ฃผ๋ ๋ฐฉ์์ด์๋๋ฐ, ๋ญ๊ฐ ์ฌ์ฌํ ๋๋..
๊ทธ๋์ ๋ฐ๋ก ํ์ผ์ ํ๋ ์์ฑํด์ค
global.css
.state_css_1110100 {
@apply block w-1/12 text-sm font-semibold text-center mx-5 rounded-lg bg-point-color;
}
.state_css_1110101 {
@apply block w-1/12 text-sm font-semibold text-center mx-5 rounded-lg bg-green-300;
}
.state_css_1110102 {
@apply block w-1/12 text-sm font-semibold text-center mx-5 rounded-lg bg-red-300;
}
.state_css_1110103 {
@apply block w-1/12 text-sm font-semibold text-center mx-5 rounded-lg bg-blue-300;
}
IssueStateIcon.tsx
export default function IssueStateIcon({
key,
value,
list,
}: {
key: string;
value: number;
list: any;
}) {
{
list.map((e: any) => (
<div key={key} className={state_css_${value}}>
{e.name}
</div>
));
}
}
tableColumns.ts
columnHelper.accessor("issueState", {
header: "์ํ",
cell({ row }) {
const { original } = row;
const state = original.issueState;
const test: any = [
{ value: 1110100, name: "์์ฒญ" },
{ value: 1110101, name: "์งํ" },
{ value: 1110102, name: "์๋ฃ" },
{ value: 1110103, name: "์ฌ์์ฒญ" },
];
return IssueStateIcon({ key: "state" + original.id, value: state, list: test })
},
}),
๋๋ ๊ฐ๊ฐ์ value์ ๋ง๋ name์ด ๋ฌ๋ผ ์์ ์ ํํฐ ํ๋ ๊ฒ์ฒ๋ผ
map์ ๋์๋๋ฐ ์๊ฐํด๋ณด๋ map์ ๋์๋ ์๋ก์ ๊ฐ์ ๋น๊ตํด ์ค ์๊ฐ ์๋ ๋ ธ๋ฆ์ด์ด์, ์์ฉ์ด ์๋ ๊ฒ์ด์๋ค.
๊ทธ๋์ ์ฐ๊ธฐ๋ก ํ๊ฒ์ด Switch๋ฌธ!
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/switch
switch - JavaScript | MDN
The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.
developer.mozilla.org
var foo = 5;
switch (foo) {
case 2:
console.log(2);
break; // it encounters this break so will not continue into 'default:'
default:
console.log('default')
// fall-through
case 1:
console.log('1');
}
์์๋ก ์ด๋ฐ์์ผ๋ก ์ฐ๋ฉด ๋๋ค.
๊ทธ๋์ ํ๋ฒ ํด๋ณด๊ธฐ๋ก ํจ..
IssueStateIcon.tsx
export default function IssueStateIcon({ value }: { value: number }) {
switch (value) {
case 1110100:
return <div className={`issue_state_${value}`}>์์ฒญ</div>;
case 1110101:
return <div className={`issue_state_${value}`}>์งํ</div>;
case 1110102:
return <div className={`issue_state_${value}`}>์๋ฃ</div>;
case 1110103:
return <div className={`issue_state_${value}`}>์ฌ์์ฒญ</div>;
default:
return null;
}
}
tableColumns.ts
columnHelper.accessor("issueState", {
header: "์ํ",
cell({ row }) {
const { original } = row;
const state = original.issueState;
return IssueStateIcon({ value: state });
},
}),
๊ทผ๋ฐ ์ด๊ฒ ๋ ๋๋ ๊ฑฐ ๊ฐ๋๋ ๊ฐ์๊ธฐ ์๋๊ธฐ ์์..
switch (value) {
case 1110100:
return <div className="issue_state_1110100">์์ฒญ</div>;
case 1110101:
return <div className={issue_state_${value}}>์งํ</div>;
์์ ์ฒ๋ผ ํ๋ฉด ๋๊ณ ๋ฐ์ ์ฒ๋ผ ํ๋ฉด ์ ๋๋๋ผ..
class๋ ๋๊ฐ์ด ๋จนํ๋๋ฐ.. ์ ์๋๋์ง ๋ต๋ตํ ๋ฐ๋ฆ ใ _ใ
๊ทผ๋ฐ ์ฌ์ค ์ ๋ถ๋ถ์ ๋์ ์ฒ๋ฆฌํด์ค ์ด์ ๊ฐ ์๊ธฐ ๋๋ฌธ์, (๋ค ๊ฐ๊ธฐ ์กฐ๊ฑด๋ฌธ์ ๋ฌ์์)
๊ทธ๋ฅ ์ซ์๋ก ๋ฐ๊ฟ ์ฃผ์๋ค
๊ทผ๋ฐ ์ด์ ๊ฐ ๋๋ฌด ๊ถ๊ธํด ใ _ใ

'TIL > etc.' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] axios instance (0) | 2024.03.06 |
---|---|
[TIL] tailwind๋ฅผ ์ด์ฉํด ์คํ์ผ ์ฌ์ฌ์ฉํ๊ธฐ (feat. @layer, @apply) (0) | 2023.06.19 |
[TIL] Tailwind CSS (5) | 2023.05.25 |
[TIL] Chakra UI (4) | 2023.05.23 |
[TIL] MongoDB (0) | 2023.05.20 |