Calendar selection event

Calendar style fixes
This commit is contained in:
Michał 2024-05-10 22:49:20 +01:00
parent cb6721ab96
commit 3bf9b7b0ad
3 changed files with 78 additions and 21 deletions

View file

@ -1,12 +1,39 @@
<script> <script lang="ts">
import { createEventDispatcher } from "svelte";
import { ArrowArcLeft, ArrowArcRight } from "phosphor-svelte"; import { ArrowArcLeft, ArrowArcRight } from "phosphor-svelte";
const weekLabels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const dispatcher = createEventDispatcher();
const monthLabels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"]; const weekLabels = [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
];
const monthLabels = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const today = new Date();
let date = new Date(); let date = new Date();
let year = date.getFullYear(); let year = date.getFullYear();
let month = date.getMonth(); let month = date.getMonth();
let selectedDate: Date;
$: firstDayOffset = new Date(year, month, 0).getDay(); $: firstDayOffset = new Date(year, month, 0).getDay();
$: monthLength = new Date(year, month + 1, 0).getDate(); $: monthLength = new Date(year, month + 1, 0).getDate();
@ -14,18 +41,20 @@
function last() { function last() {
month -= 1; month -= 1;
updateDate();
if (month < 0 || month > 11) {
date = new Date(year, month, new Date().getDate());
year = date.getFullYear();
month = date.getMonth();
} else {
date = new Date();
}
} }
function next() { function next() {
month += 1; month += 1;
updateDate();
}
function selected(day: number) {
selectedDate = new Date(year, month, day);
dispatcher("selected", { date: selectedDate });
}
function updateDate() {
if (month < 0 || month > 11) { if (month < 0 || month > 11) {
date = new Date(year, month, new Date().getDate()); date = new Date(year, month, new Date().getDate());
year = date.getFullYear(); year = date.getFullYear();
@ -42,28 +71,34 @@
<div class="calendar"> <div class="calendar">
<div class="calendar-header"> <div class="calendar-header">
<p>{monthLabels[month]}&nbsp;{year}</p> <p>{monthLabels[month]}&nbsp;<span>{year}</span></p>
<button on:click={last}><ArrowArcLeft weight="fill" /></button> <button on:click={last}><ArrowArcLeft weight="fill" /></button>
<button on:click={next}><ArrowArcRight weight="fill" /></button> <button on:click={next}><ArrowArcRight weight="fill" /></button>
</div> </div>
<div class="calendar-weeks"> <div class="calendar-weeks">
{#each weekLabels as weekname} {#each weekLabels as label}
<span>{weekname}</span> <span>{label}</span>
{/each} {/each}
</div> </div>
<div class="calendar-days"> <div class="calendar-days">
{#each {length:firstDayOffset} as _} {#each {length:firstDayOffset} as _}
<div /> <div />
{/each} {/each}
{#each {length:monthLength} as _, i} {#each {length:monthLength} as _, dayNum}
<div <div
class:today={ class:today={
i === date.getDate() dayNum+1 === today.getDate()
&& month === new Date().getMonth() && month === today.getMonth()
&& year === new Date().getFullYear() && year === today.getFullYear()
}
class:selected={
selectedDate
&& dayNum+1 === selectedDate.getDate()
&& month === selectedDate.getMonth()
&& year === selectedDate.getFullYear()
} }
> >
<button>{i+1}</button> <button on:click={() => { selected(dayNum+1) }}>{dayNum+1}</button>
</div> </div>
{/each} {/each}
{#each {length:lastDayOffset} as _} {#each {length:lastDayOffset} as _}

View file

@ -91,7 +91,11 @@
<div class="spacer" /> <div class="spacer" />
<Calendar /> <Calendar
on:selected={(event) => {
console.log(event.detail.date)
}}
/>
<div class="spacer" /> <div class="spacer" />

View file

@ -30,10 +30,17 @@
padding-left: $spacing-normal; padding-left: $spacing-normal;
flex-grow: 1; flex-grow: 1;
font-size: $font-size-h4; font-size: $font-size-h4;
> span {
font-weight: $font-weight-bolder;
}
} }
> button { > button {
padding: $spacing-normal; padding: 0 $spacing-normal;
height: 50px;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
@ -99,6 +106,17 @@
&.today { &.today {
> button { > button {
font-weight: $font-weight-black;
text-decoration: underline;
//background-color: $color-primary;
//color: $color-on-primary;
}
}
&.selected {
> button {
//background-color: $color-dark;
//color: $color-on-dark;
background-color: $color-primary; background-color: $color-primary;
color: $color-on-primary; color: $color-on-primary;
} }