Showing
7 changed files
with
80 additions
and
42 deletions
... | @@ -2,14 +2,23 @@ import "../styles/Grid.css"; | ... | @@ -2,14 +2,23 @@ import "../styles/Grid.css"; |
2 | import GridHead from "./GridHead.js"; | 2 | import GridHead from "./GridHead.js"; |
3 | import GridRow from "./GridRow.js"; | 3 | import GridRow from "./GridRow.js"; |
4 | 4 | ||
5 | -import React from "react"; | 5 | +import React, { useContext } from "react"; |
6 | +import { CalendarStateContext } from "../pages/Calendar"; | ||
7 | +import { moveDate, toSunday, toYMD } from "../utils/Dates"; | ||
6 | 8 | ||
7 | const Grid = () => { | 9 | const Grid = () => { |
10 | + const [state] = useContext(CalendarStateContext); | ||
11 | + | ||
8 | const renderRows = () => { | 12 | const renderRows = () => { |
9 | const rows = []; | 13 | const rows = []; |
14 | + const ndate = new Date(state.year, state.month - 1, 1); | ||
15 | + toSunday(ndate); | ||
16 | + | ||
10 | for (let i = 0; i < 5; i++) { | 17 | for (let i = 0; i < 5; i++) { |
11 | - rows.push(<GridRow key={i} />); | 18 | + rows.push(<GridRow key={i} startDate={toYMD(ndate)} />); |
19 | + moveDate(ndate, "week", 1); | ||
12 | } | 20 | } |
21 | + | ||
13 | return rows; | 22 | return rows; |
14 | }; | 23 | }; |
15 | 24 | ... | ... |
1 | -const GridItem = () => { | 1 | +import { useContext } from "react"; |
2 | - return <div className="GridItem"></div>; | 2 | +import { CalendarStateContext } from "../pages/Calendar"; |
3 | + | ||
4 | +const GridItem = ({ targetDate }) => { | ||
5 | + const [state] = useContext(CalendarStateContext); | ||
6 | + const { month, date } = targetDate; | ||
7 | + | ||
8 | + const displayDate = () => { | ||
9 | + if (state.month !== month) return month + "/" + date; | ||
10 | + else return date; | ||
11 | + }; | ||
12 | + return ( | ||
13 | + <div className="GridItem" relative={month - state.month || null}> | ||
14 | + <span>{displayDate()}</span> | ||
15 | + </div> | ||
16 | + ); | ||
3 | }; | 17 | }; |
4 | 18 | ||
5 | export default GridItem; | 19 | export default GridItem; | ... | ... |
1 | +import { moveDate, toYMD } from "../utils/Dates"; | ||
1 | import GridItem from "./GridItem"; | 2 | import GridItem from "./GridItem"; |
2 | 3 | ||
3 | -const GridRow = () => { | 4 | +const GridRow = ({ startDate }) => { |
5 | + const { year, month, date } = startDate; | ||
6 | + | ||
4 | const renderItems = () => { | 7 | const renderItems = () => { |
5 | const items = []; | 8 | const items = []; |
9 | + const ndate = new Date(year, month - 1, date); | ||
10 | + | ||
6 | for (let i = 0; i < 7; i++) { | 11 | for (let i = 0; i < 7; i++) { |
7 | - items.push(<GridItem key={i} />); | 12 | + items.push(<GridItem key={i} targetDate={toYMD(ndate)} />); |
13 | + moveDate(ndate, "day", 1); | ||
8 | } | 14 | } |
15 | + | ||
9 | return items; | 16 | return items; |
10 | }; | 17 | }; |
11 | 18 | ... | ... |
... | @@ -2,6 +2,7 @@ import { useContext } from "react"; | ... | @@ -2,6 +2,7 @@ import { useContext } from "react"; |
2 | import { useNavigate } from "react-router-dom"; | 2 | import { useNavigate } from "react-router-dom"; |
3 | import { CalendarStateContext } from "../pages/Calendar"; | 3 | import { CalendarStateContext } from "../pages/Calendar"; |
4 | import "../styles/Header.css"; | 4 | import "../styles/Header.css"; |
5 | +import { moveDate, toYMD } from "../utils/Dates"; | ||
5 | 6 | ||
6 | const Header = () => { | 7 | const Header = () => { |
7 | const [state, setState] = useContext(CalendarStateContext); | 8 | const [state, setState] = useContext(CalendarStateContext); |
... | @@ -15,34 +16,10 @@ const Header = () => { | ... | @@ -15,34 +16,10 @@ const Header = () => { |
15 | navigate("/calendar/" + e.target.value); | 16 | navigate("/calendar/" + e.target.value); |
16 | }; | 17 | }; |
17 | 18 | ||
18 | - const gotoToday = () => { | ||
19 | - const scope = state.scope; | ||
20 | - const today = new Date(); | ||
21 | - const year = today.getFullYear(); | ||
22 | - const month = today.getMonth() + 1; | ||
23 | - const date = today.getDate(); | ||
24 | - setState({ scope, year, month, date }); | ||
25 | - }; | ||
26 | - | ||
27 | const move = (e) => { | 19 | const move = (e) => { |
28 | - const scope = state.scope; | ||
29 | const current = new Date(state.year, state.month - 1, state.date); | 20 | const current = new Date(state.year, state.month - 1, state.date); |
30 | - switch (scope) { | 21 | + moveDate(current, state.scope, Number(e.target.value)); |
31 | - case "month": | 22 | + setState({ scope: state.scope, ...toYMD(current) }); |
32 | - current.setMonth(current.getMonth() + Number(e.target.value)); | ||
33 | - break; | ||
34 | - case "week": | ||
35 | - current.setDate(current.getDate() + Number(e.target.value) * 7); | ||
36 | - break; | ||
37 | - case "day": | ||
38 | - current.setDate(current.getDate() + Number(e.target.value)); | ||
39 | - break; | ||
40 | - default: | ||
41 | - } | ||
42 | - const year = current.getFullYear(); | ||
43 | - const month = current.getMonth() + 1; | ||
44 | - const date = current.getDate(); | ||
45 | - setState({ scope, year, month, date }); | ||
46 | }; | 23 | }; |
47 | 24 | ||
48 | let headLabel; | 25 | let headLabel; |
... | @@ -64,7 +41,10 @@ const Header = () => { | ... | @@ -64,7 +41,10 @@ const Header = () => { |
64 | <span className="hls">확장 캘린더</span> | 41 | <span className="hls">확장 캘린더</span> |
65 | </div> | 42 | </div> |
66 | <div className="hc"> | 43 | <div className="hc"> |
67 | - <button className="hcb" onClick={gotoToday}> | 44 | + <button |
45 | + className="hcb" | ||
46 | + onClick={() => setState({ scope: state.scope, ...toYMD(new Date()) })} | ||
47 | + > | ||
68 | 오늘 | 48 | 오늘 |
69 | </button> | 49 | </button> |
70 | <div className="hcd"> | 50 | <div className="hcd"> | ... | ... |
1 | import React, { useState } from "react"; | 1 | import React, { useState } from "react"; |
2 | import { Route, Routes } from "react-router-dom"; | 2 | import { Route, Routes } from "react-router-dom"; |
3 | + | ||
3 | import Grid from "../components/Grid"; | 4 | import Grid from "../components/Grid"; |
4 | import Header from "../components/Header"; | 5 | import Header from "../components/Header"; |
5 | import "../styles/Home.css"; | 6 | import "../styles/Home.css"; |
7 | +import { toYMD } from "../utils/Dates"; | ||
6 | 8 | ||
7 | export const CalendarStateContext = React.createContext(); | 9 | export const CalendarStateContext = React.createContext(); |
8 | 10 | ||
9 | const Calendar = () => { | 11 | const Calendar = () => { |
10 | - const today = new Date(); | ||
11 | - const year = today.getFullYear(); | ||
12 | - const month = today.getMonth() + 1; | ||
13 | - const date = today.getDate(); | ||
14 | - //const day = today.getDay(); | ||
15 | - | ||
16 | //scope는 day, state는 date | 12 | //scope는 day, state는 date |
17 | const [state, setState] = useState({ | 13 | const [state, setState] = useState({ |
18 | scope: "month", | 14 | scope: "month", |
19 | - year: year, | 15 | + ...toYMD(new Date()), |
20 | - month: month, | ||
21 | - date: date, | ||
22 | }); | 16 | }); |
23 | 17 | ||
24 | return ( | 18 | return ( | ... | ... |
... | @@ -5,6 +5,7 @@ | ... | @@ -5,6 +5,7 @@ |
5 | height: 150px; | 5 | height: 150px; |
6 | flex-basis: 100px; | 6 | flex-basis: 100px; |
7 | flex-grow: 1; | 7 | flex-grow: 1; |
8 | + padding: 5px; | ||
8 | } | 9 | } |
9 | 10 | ||
10 | .GridHeadItem { | 11 | .GridHeadItem { |
... | @@ -23,3 +24,7 @@ | ... | @@ -23,3 +24,7 @@ |
23 | display: flex; | 24 | display: flex; |
24 | flex-direction: column; | 25 | flex-direction: column; |
25 | } | 26 | } |
27 | + | ||
28 | +.GridItem[relative] > span { | ||
29 | + color: gray; | ||
30 | +} | ... | ... |
src/utils/Dates.js
0 → 100644
1 | +function toYMD(dateObj) { | ||
2 | + const year = dateObj.getFullYear(); | ||
3 | + const month = dateObj.getMonth() + 1; | ||
4 | + const date = dateObj.getDate(); | ||
5 | + | ||
6 | + return { year, month, date }; | ||
7 | +} | ||
8 | + | ||
9 | +function toSunday(dateObj) { | ||
10 | + const day = dateObj.getDay(); | ||
11 | + moveDate(dateObj, "day", -day); | ||
12 | +} | ||
13 | + | ||
14 | +function moveDate(dateObj, scope, distance) { | ||
15 | + switch (scope) { | ||
16 | + case "month": | ||
17 | + dateObj.setMonth(dateObj.getMonth() + distance); | ||
18 | + break; | ||
19 | + case "week": | ||
20 | + dateObj.setDate(dateObj.getDate() + distance * 7); | ||
21 | + break; | ||
22 | + case "day": | ||
23 | + dateObj.setDate(dateObj.getDate() + distance); | ||
24 | + break; | ||
25 | + default: | ||
26 | + } | ||
27 | +} | ||
28 | + | ||
29 | +export { toYMD, toSunday, moveDate }; |
-
Please register or login to post a comment