ํฐ์คํ ๋ฆฌ ๋ทฐ
study/ts
๐ Webpack๊ณผ TypeScript๋ก ๋ง๋๋ ์น์ฑ ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ฐ๋ฐ ์๋ฆฌ์ฆ - ์ฌ์ฉ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ – ๋ ์ง ์ ํ๊ณผ ์ ์ด๋ ๊ธฐ๋ฅ ๊ตฌํ
octo54 2025. 3. 25. 11:19๋ฐ์ํ
๐ Webpack๊ณผ TypeScript๋ก ๋ง๋๋ ์น์ฑ ์บ๋ฆฐ๋ ํ๋ฌ๊ทธ์ธ ๊ฐ๋ฐ ์๋ฆฌ์ฆ - ์ฌ์ฉ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ – ๋ ์ง ์ ํ๊ณผ ์ ์ด๋ ๊ธฐ๋ฅ ๊ตฌํ
์ง๋ ํธ์์ ์ฐ๋ฆฌ๋ ๊ธฐ๋ณธ ๋ฌ๋ ฅ UI๋ฅผ ๊ตฌ์ฑํ๊ณ ํ์ฌ ์์ ๋ ์ง๋ฅผ ํ๋ฉด์ ํ์ํ๋ ๊ธฐ๋ฅ์ ๋ง๋ค์์ต๋๋ค.
์ด์ ๋ ์ค์ ๋ก ๋์ํ๋ ์บ๋ฆฐ๋, ์ฆ
โ
๋ ์ง๋ฅผ ํด๋ฆญํ๋ฉด ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ณ ,
โ
์ข์ฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ด์ /๋ค์ ๋ฌ๋ก ์ด๋ํ ์ ์๋
๊ธฐ๋ฅ๋ค์ ์ถ๊ฐํ๊ฒ ์ต๋๋ค.
๐ฏ ๋ชฉํ
- ๋ ์ง ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
- ์ด์ /๋ค์ ๋ฌ ์ด๋ ๋ฒํผ ์ถ๊ฐ ๋ฐ ์ํ ๊ด๋ฆฌ
- ํด๋ฆญ๋ ๋ ์ง์ ์๊ฐ์ ๊ฐ์กฐ
๐ง ์ฌ์ฉ์ ์ด๋ฒคํธ ์ค๊ณ ํ๋ฆ
โ ํ์ํ ๋์ ๋ชฉ๋ก
๋์ ์ค๋ช
๋ ์ง ํด๋ฆญ | ํด๋น ๋ ์ง๋ฅผ ๊ฐ์กฐํ๊ณ , ์ดํ ๊ธฐ๋ฅ๊ณผ ์ฐ๊ฒฐ |
์ด์ ๋ฌ ํด๋ฆญ | ํ์ฌ ์์์ -1 ์ด๋ |
๋ค์ ๋ฌ ํด๋ฆญ | ํ์ฌ ์์์ +1 ์ด๋ |
๐ ๏ธ 1. ์ํ๋ฅผ ๊ด๋ฆฌํ ์ ์๋ ๊ตฌ์กฐ ๋ง๋ค๊ธฐ
โ CalendarState ์ธํฐํ์ด์ค ์ถ๊ฐ
// src/types.ts
export interface CalendarState {
year: number;
month: number;
selectedDate?: Date;
}
๐ฆ 2. ์ ์ ํ ๋ฒํผ ์ถ๊ฐ
โ HTML ํ ํ๋ฆฟ ์์
// src/components/Calendar.ts (renderCalendar ๋ด๋ถ)
target.innerHTML = `
<div class="calendar-header">
<button class="prev-month">โ</button>
<h2>${year}๋
${month + 1}์</h2>
<button class="next-month">โถ</button>
</div>
<div class="calendar-grid">
${["์ผ", "์", "ํ", "์", "๋ชฉ", "๊ธ", "ํ "]
.map((d) => `<div class="calendar-cell header">${d}</div>`)
.join("")}
${matrix
.flat()
.map((date) => {
const day = date?.getDate();
const inMonth = date?.getMonth() === month;
const dateStr = date?.toISOString();
return `<div class="calendar-cell${inMonth ? "" : " out"}" data-date="${dateStr}">${day}</div>`;
})
.join("")}
</div>
`;
๐งฉ 3. ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
โ attachCalendarEvents ํจ์ ์ ์
๋ฐ์ํ
// src/components/Calendar.ts
export function attachCalendarEvents(
target: HTMLElement,
state: CalendarState,
rerender: () => void
) {
// ๋ ์ง ํด๋ฆญ
const cells = target.querySelectorAll(".calendar-cell:not(.header)");
cells.forEach((cell) => {
cell.addEventListener("click", () => {
const dateStr = cell.getAttribute("data-date");
if (dateStr) {
state.selectedDate = new Date(dateStr);
rerender();
}
});
});
// ์ ์ด๋ ๋ฒํผ
const prevBtn = target.querySelector(".prev-month");
const nextBtn = target.querySelector(".next-month");
prevBtn?.addEventListener("click", () => {
if (state.month === 0) {
state.month = 11;
state.year -= 1;
} else {
state.month -= 1;
}
rerender();
});
nextBtn?.addEventListener("click", () => {
if (state.month === 11) {
state.month = 0;
state.year += 1;
} else {
state.month += 1;
}
rerender();
});
}
๐ผ๏ธ 4. ์ ํ๋ ๋ ์ง ์คํ์ผ ๊ฐ์กฐ
โ ์ ํ๋ ๋ ์ง ๋น๊ต ์ถ๊ฐ
// ๋ ์ง ์
๋ ๋๋ง ๋ถ๋ถ (์ถ๊ฐ)
const isSelected = date?.toDateString() === state.selectedDate?.toDateString();
return `<div class="calendar-cell${inMonth ? "" : " out"}${isSelected ? " selected" : ""}" data-date="${dateStr}">${day}</div>`;
๐จ 5. CSS ์คํ์ผ ์ถ๊ฐ
.calendar-cell.selected {
background-color: #007bff;
color: #fff;
font-weight: bold;
}
.calendar-header button {
background: none;
border: none;
font-size: 1.2rem;
cursor: pointer;
padding: 0 1rem;
}
๐ 6. index.ts ์ ๋ฐ์ดํธ
import { renderCalendar, attachCalendarEvents } from "./components/Calendar";
import { CalendarState } from "./types";
const root = document.getElementById("calendar-root");
const state: CalendarState = {
year: new Date().getFullYear(),
month: new Date().getMonth(),
};
function render() {
if (root) {
renderCalendar(root, state.year, state.month, state);
attachCalendarEvents(root, state, render);
}
}
render();
โ ๊ฒฐ๊ณผ ํ์ธ
npm run dev
- ๋ ์ง ํด๋ฆญ ์ ์คํ์ผ์ด ๊ฐ์กฐ๋จ
- โ, โถ ๋ฒํผ์ผ๋ก ์ ์ด๋ ๊ฐ๋ฅ
- ์ ํ๋ ๋ ์ง๋ ์ ์ง๋๋ฉฐ ๊ฐ์กฐ ํ์๋จ
๐ ์ฐธ๊ณ ์๋ฃ
๋ค์ ํธ ์๊ณ :
๐ 6ํธ – ์ผ์ ๋ฐ์ดํฐ ๊ด๋ฆฌํ๊ธฐ: ์ด๋ฒคํธ ์ถ๊ฐ ๋ฐ ๋ฐ์ดํฐ ์ ์ฅ ๊ตฌ์กฐ ์ค๊ณ
์ฌ์ฉ์๊ฐ ์ ํํ ๋ ์ง์ ์ผ์ ์ ์ถ๊ฐํ๊ณ ๊ด๋ฆฌํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํด๋ด
๋๋ค! ๐๏ธ
๋๋์ด “์ง์ง ์ผ์ ํ๋ฌ๊ทธ์ธ”์ผ๋ก ์งํํ๋ ๋จ๊ณ์
๋๋ค!
'study > ts' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โป ์ด ํฌ์คํ
์ ์ฟ ํก ํํธ๋์ค ํ๋์ ์ผํ์ผ๋ก, ์ด์ ๋ฐ๋ฅธ ์ผ์ ์ก์ ์์๋ฃ๋ฅผ ์ ๊ณต๋ฐ์ต๋๋ค.
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- ๊ด๋ฆฌ์
- Webpack
- PostgreSQL
- llm
- ํ๋ก ํธ์๋
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- Docker
- github
- App Router
- Next.js
- ๋ฐฑ์๋
- nextJS
- SEO์ต์ ํ
- AI์ฑ๋ด
- ์น๊ฐ๋ฐ
- CI/CD
- seo ์ต์ ํ 10๊ฐ
- rag
- AI ์๋ํ
- Ktor
- fastapi
- LangChain
- Prisma
- ๋ฐฑ์๋๊ฐ๋ฐ
- REACT
- kotlin
- nodejs
- NestJS
- gatsbyjs
- ์ค๋งํธ ์ปจํธ๋ํธ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
๊ธ ๋ณด๊ดํจ
๋ฐ์ํ