From fb6fa21d98b4b926cb83a567378a56c0382e356c Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:16:40 -0400 Subject: [PATCH] fix: Improves user experience around custom years --- .../dates/years/YearContainer.svelte | 20 +++++++++++++++---- .../Utilities/DateWithValidation.svelte | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/settings/creator/Containers/dates/years/YearContainer.svelte b/src/settings/creator/Containers/dates/years/YearContainer.svelte index 7816d83..6157aed 100644 --- a/src/settings/creator/Containers/dates/years/YearContainer.svelte +++ b/src/settings/creator/Containers/dates/years/YearContainer.svelte @@ -20,7 +20,7 @@ const calendar = getContext("store"); const plugin = getContext("plugin"); - const { yearStore, staticStore } = calendar; + const { yearStore, staticStore, currentStore } = calendar; const { customYears } = yearStore; const grip = (node: HTMLElement) => { @@ -28,9 +28,18 @@ }; const trash = (node: HTMLElement, item: Year) => { - new ExtraButtonComponent(node) - .setIcon(TRASH) - .onClick(() => yearStore.delete(item.id)); + new ExtraButtonComponent(node).setIcon(TRASH).onClick(() => { + const index = + $yearStore?.findIndex((year) => year.id === item.id) ?? 0; + yearStore.delete(item.id); + if ($yearStore?.length) { + if (index > 0) { + $currentStore.year = index; + } else { + $currentStore.year = 1; + } + } + }); }; const name = (node: HTMLElement, item: Year) => { @@ -150,6 +159,9 @@ { yearStore.add(evt.detail); + if ($yearStore?.length === 1) { + $currentStore.year = 1; + } }} /> {/if} diff --git a/src/settings/creator/Utilities/DateWithValidation.svelte b/src/settings/creator/Utilities/DateWithValidation.svelte index b3d4937..b3af460 100644 --- a/src/settings/creator/Utilities/DateWithValidation.svelte +++ b/src/settings/creator/Utilities/DateWithValidation.svelte @@ -75,7 +75,7 @@ bind:value={$currentStore.year} class:invalid={!$validYear} > - {#each $yearStore?.filter((m) => m && m.name) ?? [] as year, index} + {#each $yearStore?.filter((m) => m && m.name) ?? [] as year, index (year.id)} {/each}