Skip to content

Commit c82d541

Browse files
committed
Finish month view
1 parent dfc7ac4 commit c82d541

File tree

11 files changed

+199
-6
lines changed

11 files changed

+199
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qwik-calendar",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Um componente de calendário para aplicações Qwik",
55
"main": "./lib/index.qwik.mjs",
66
"qwik": "./lib/index.qwik.mjs",

src/components/calendar/calendar.module.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121

2222
#qwikCalendar *{
2323
box-sizing: border-box;
24-
}
24+
}

src/components/calendar/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const Calendar = component$((props) => {
1212

1313
const onChangeCurrentDate$ = $((newCurrentDate) => {
1414
currentDate.value = newCurrentDate;
15-
console.log("aqui no calendar")
1615
});
1716

1817
return (
@@ -24,6 +23,7 @@ export const Calendar = component$((props) => {
2423
onChangeCurrentDate$={onChangeCurrentDate$}
2524
/>
2625
<MonthView
26+
locale={locale}
2727
dateObj={currentDate.value}
2828
/>
2929
</div>

src/components/days/days.module.css

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#days{
2+
display: flex;
3+
flex-wrap: wrap;
4+
}
5+
6+
.day, .priorDay{
7+
flex: 0 0 14.2857%;
8+
overflow: hidden;
9+
margin-inline-end: 0px;
10+
11+
max-width: 100%;
12+
padding: 10px 6.6667px;
13+
background: none;
14+
text-align: center;
15+
line-height: 16px;
16+
}
17+
18+
.priorDay{
19+
color: #757575;
20+
}
21+
22+
.weekendDayDay{
23+
color: #d10000;
24+
}

src/components/days/index.jsx

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { component$ } from "@builder.io/qwik";
2+
3+
import css from "./days.module.css";
4+
5+
const Day = component$((props) => {
6+
const thisDate = new Date(props.dateObj);
7+
const today = new Date();
8+
9+
let textColor = thisDate.getDay() === 0 || thisDate.getDay() === 6 ? "#d10000" : "#000000";
10+
11+
let bgColor = Math.round(props.dateObj / 8.64e7) === Math.round(today.valueOf() / 8.64e7) ? "#FFFF76" : "transparent"
12+
13+
return (
14+
<>
15+
<button class={css.day} style={{color: textColor, backgroundColor: bgColor}}>
16+
<abbr>{thisDate.getDate()}</abbr>
17+
</button>
18+
</>
19+
);
20+
});
21+
22+
const PriorDay = component$((props) => {
23+
const thisDate = new Date(props.dateObj);
24+
25+
return (
26+
<>
27+
<button disabled class={css.priorDay}>
28+
<abbr>{thisDate.getDate()}</abbr>
29+
</button>
30+
</>
31+
);
32+
})
33+
34+
function getDaysSinceLastSunday(dateObj){
35+
let days = [];
36+
37+
while (dateObj.getDay() !== 0){
38+
dateObj.setDate(dateObj.getDate() - 1);
39+
days.unshift(<PriorDay key={dateObj.valueOf()} dateObj={dateObj.valueOf()}/>);
40+
}
41+
42+
return days;
43+
}
44+
45+
function getMonthLength(dateObj){
46+
let year = dateObj.getFullYear();
47+
let month = dateObj.getMonth() + 1;
48+
49+
return new Date(year, month, 0).getDate();
50+
}
51+
52+
function getDaysOfTheMonth(dateObj){
53+
let days = [];
54+
55+
let lens = getMonthLength(dateObj)
56+
57+
for (let i = 0; i < lens; i++){
58+
days.push(<Day key={dateObj.valueOf()} dateObj={dateObj.valueOf()}/>);
59+
dateObj.setDate(dateObj.getDate() + 1);
60+
}
61+
62+
return days;
63+
}
64+
65+
function getDaysUntilNextSaturday(dateObj){
66+
let year = dateObj.getFullYear();
67+
let month = dateObj.getMonth();
68+
69+
let newDateObj = new Date(year, month, 0);
70+
71+
let days = [];
72+
73+
let delta = 6 - newDateObj.getDay();
74+
75+
for (let i = 0; i < delta; i++){
76+
newDateObj.setDate(newDateObj.getDate() + 1);
77+
days.push(<PriorDay key={newDateObj.valueOf()} dateObj={newDateObj.valueOf()}/>);
78+
}
79+
80+
return days;
81+
}
82+
83+
export const Days = component$((props) => {
84+
let newDate = new Date(props.dateObj);
85+
newDate.setDate(1);
86+
let daysSinceLastSunday = getDaysSinceLastSunday(newDate);
87+
88+
newDate = new Date(props.dateObj);
89+
newDate.setDate(1);
90+
let daysOfTheMonth = getDaysOfTheMonth(newDate);
91+
92+
let daysUntilNextSaturday = getDaysUntilNextSaturday(newDate);
93+
94+
let days = [...daysSinceLastSunday, ...daysOfTheMonth, ...daysUntilNextSaturday];
95+
96+
return (
97+
<>
98+
<div id={css.days}>
99+
{days}
100+
</div>
101+
</>
102+
);
103+
});

src/components/monthview/index.jsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { component$ } from "@builder.io/qwik";
22

3+
import css from "./monthview.module.css";
4+
import { WeekDays } from "../weekdays";
5+
import { Days } from "../days";
6+
37
export const MonthView = component$((props) => {
48
return (
59
<>
6-
<div>
7-
<p>{props.dateObj.toLocaleString(props.locale, {month: 'long'})}</p>
10+
<div id={css.monthview}>
11+
<WeekDays
12+
locale={props.locale}
13+
/>
14+
<Days
15+
dateObj={props.dateObj}
16+
/>
817
</div>
918
</>
1019
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#monthview{
2+
flex-grow: 1;
3+
text-align: center;
4+
text-transform: uppercase;
5+
font-weight: bold;
6+
font-size: 0.75em;
7+
}

src/components/navigation/index.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const Navigation = component$((props) => {
1818
useVisibleTask$(({ track }) => {
1919
track(() => dateObj.value);
2020
props.onChangeCurrentDate$(dateObj.value);
21-
console.log("executei")
2221
});
2322

2423
return (

src/components/weekdays/index.jsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { component$, Slot } from "@builder.io/qwik";
2+
3+
import css from "./weekdays.module.css";
4+
import { weekdays } from "../../locale";
5+
6+
const WeekDay = component$((props) => {
7+
return (
8+
<div class={css.weekday}>
9+
<abbr aria-label={props.label} title={props.label}>
10+
<Slot/>
11+
</abbr>
12+
</div>
13+
);
14+
});
15+
16+
export const WeekDays = component$((props) => {
17+
18+
let daysOfTheWeek = weekdays[props.locale];
19+
20+
return (
21+
<>
22+
<div id={css.weekdays}>
23+
<WeekDay label={daysOfTheWeek[0]}>{daysOfTheWeek[0].slice(0, 3)}</WeekDay>
24+
<WeekDay label={daysOfTheWeek[1]}>{daysOfTheWeek[1].slice(0, 3)}</WeekDay>
25+
<WeekDay label={daysOfTheWeek[2]}>{daysOfTheWeek[2].slice(0, 3)}</WeekDay>
26+
<WeekDay label={daysOfTheWeek[3]}>{daysOfTheWeek[3].slice(0, 3)}</WeekDay>
27+
<WeekDay label={daysOfTheWeek[4]}>{daysOfTheWeek[4].slice(0, 3)}</WeekDay>
28+
<WeekDay label={daysOfTheWeek[5]}>{daysOfTheWeek[5].slice(0, 3)}</WeekDay>
29+
<WeekDay label={daysOfTheWeek[6]}>{daysOfTheWeek[6].slice(0, 3)}</WeekDay>
30+
</div>
31+
</>
32+
);
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#weekdays{
2+
display: flex;
3+
flex-wrap: nowrap;
4+
}
5+
6+
.weekday{
7+
flex: 0 0 14.2857%;
8+
overflow: hidden;
9+
margin-inline-end: 0px;
10+
padding: 0.5em;
11+
}

src/locale.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const weekdays = {
2+
"pt-br": [
3+
"Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"
4+
]
5+
}
6+
7+
export {weekdays}

0 commit comments

Comments
 (0)