Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateRange last day selection fix #2981

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-buttons-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@evidence-dev/core-components': patch
---

fixed deselection issue for start and end date
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@

let currentDate = dateToYYYYMMDD(new Date());

let extraDayEndString;

$: if (endString && range) {
extraDayEndString = new Date(endString);
extraDayEndString.setDate(extraDayEndString.getDate() + 1);
extraDayEndString = formatDateString(extraDayEndString);
}

function onSelectedDateInputChange(selectedDateInput) {
if (selectedDateInput && (selectedDateInput.start || selectedDateInput.end) && range) {
$inputs[name] = {
Expand Down Expand Up @@ -101,6 +109,7 @@
{onSelectedDateInputChange}
start={startString}
end={endString}
{extraDayEndString}
loaded={loaded?.ready ?? true}
{presetRanges}
{defaultValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
export let range = false;
/** @type {string} */
export let title;
export let extraDayEndString = undefined;

/** @type { { label: string, group: string, range: import('bits-ui').DateRange }[] } */
$: presets = [
Expand Down Expand Up @@ -211,6 +212,11 @@
$: calendarStart = YYYYMMDDToCalendar(start);
$: calendarEnd = YYYYMMDDToCalendar(end);

let extraDayCalendarEnd = calendarEnd;
$: if (range) {
extraDayCalendarEnd = YYYYMMDDToCalendar(extraDayEndString);
}

function updateDateRange(start, end) {
if (selectedPreset) return;

Expand Down Expand Up @@ -296,7 +302,7 @@
selectedDateInput = value;
}}
minValue={calendarStart}
maxValue={calendarEnd}
maxValue={extraDayCalendarEnd}
/>
{:else}
<Calendar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,7 @@
</script>

<script>
import DateInput from '../date-input/_DateInput.svelte';
import { getLocalTimeZone } from '@internationalized/date';
import HiddenInPrint from '../shared/HiddenInPrint.svelte';
import { page } from '$app/stores';
import QueryLoad from '$lib/atoms/query-load/QueryLoad.svelte';
import { Skeleton } from '$lib/atoms/skeletons/index.js';
import { getInputContext } from '@evidence-dev/sdk/utils/svelte';
import { dateToYYYYMMDD, formatDateString } from '../date-input/helpers.js';
import { buildQuery } from '@evidence-dev/component-utilities/buildQuery';

const inputs = getInputContext();
/** @type {string} */
export let name;
/** @type {string | undefined} */
export let title;
/** @type {boolean} */
export let hideDuringPrint = true;

/** @type {string | Date | undefined} */
export let start;
/** @type {string | Date | undefined} */
export let end;

/** @type {Query | string | undefined} */
export let data;
/** @type {string | undefined} */
export let dates;
/** @type {[]string | undefined} */
export let presetRanges;
/** @type {string | undefined} */
export let defaultValue;

let query;
$: if (data && dates) {
const queryString = `SELECT min(${dates}) as start, max(${dates}) as end FROM ${typeof data === 'string' ? data : `(${data.text})`}`;
const id = `DateRange-${name}`;
const opts = {
initialData: $page?.data?.data[`DateRange-${name}_data`],
knownColumns: $page?.data?.data[`DateRange-${name}_columns`],
disableCache: true,
noResolve: false
};
query = buildQuery(queryString, id, opts.initialData, opts);
query.fetch();
}

$: startString = formatDateString(start || $query?.[0].start || new Date(0));
$: endString = formatDateString(end || $query?.[0].end || new Date());

function onSelectedDateInputChange(selectedDateInput) {
if (selectedDateInput && (selectedDateInput.start || selectedDateInput.end)) {
$inputs[name] = {
start: dateToYYYYMMDD(selectedDateInput.start?.toDate(getLocalTimeZone()) ?? new Date(0)),
end: dateToYYYYMMDD(selectedDateInput.end?.toDate(getLocalTimeZone()) ?? new Date())
};
}
}
import DateInput from '../date-input/DateInput.svelte';
</script>

<HiddenInPrint enabled={hideDuringPrint}>
<div class={`${title ? '-mt-0.5' : 'mt-2'} mb-4 ml-0 mr-2 inline-block`}>
{#if title}
<span class="text-xs font-medium text-base-content block mb-0.5">{title}</span>
{/if}

{#if $query?.error}
<span
class="group inline-flex items-center relative cursor-help cursor-helpfont-sans px-1 border border-negative py-[1px] bg-negative/10 rounded"
>
<span class="inline font-sans font-medium text-xs text-negative">error</span>
<span
class="hidden font-sans group-hover:inline absolute -top-1 left-[105%] text-sm z-10 px-2 py-1 bg-base-200 border border-base-300 leading-relaxed min-w-[150px] w-max max-w-[400px] rounded-md"
>
{$query.error}
</span>
</span>
{:else}
<QueryLoad data={query} let:loaded>
<svelte:fragment slot="skeleton">
<Skeleton class="h-8 w-72" />
</svelte:fragment>

<DateInput
{onSelectedDateInputChange}
start={startString}
end={endString}
loaded={loaded?.ready ?? true}
{presetRanges}
{defaultValue}
range
/>
</QueryLoad>
{/if}
</div>
</HiddenInPrint>
<DateInput {...$$props} range />
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
let className = undefined;

export { className as class };

export let maxValue = undefined;
export let minValue = undefined;

$: hideExtraDay =
(date.year >= maxValue.year && date.month >= maxValue.month && date.day >= maxValue.day) ||
(date.year <= minValue.year && date.month <= minValue.month && date.day < minValue.day)
? true
: false;
</script>

<RangeCalendarPrimitive.Day
on:click
{date}
data-disabled={hideExtraDay ? true : undefined}
{month}
class={cn(
buttonVariants({ variant: 'ghost' }),
Expand All @@ -34,9 +44,9 @@
'data-[disabled]:text-base-content-muted data-[disabled]:opacity-50',
// Unavailable
'data-[unavailable]:text-negative/20 data-[unavailable]:line-through',

className
)}
{...$$restProps}
let:disabled
let:unavailable
let:builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<RangeCalendar.GridRow class="w-full mt-2">
{#each weekDates as date}
<RangeCalendar.Cell {date}>
<RangeCalendar.Day {date} month={month.value} />
<RangeCalendar.Day {date} month={month.value} {maxValue} {minValue} />
</RangeCalendar.Cell>
{/each}
</RangeCalendar.GridRow>
Expand Down
Loading