Skip to content

feat(DateRangePicker): add yesterday/quarter/year presets, expose helpers#231

Merged
wreiske merged 2 commits into
mainfrom
feat/date-range-picker-more-presets
May 14, 2026
Merged

feat(DateRangePicker): add yesterday/quarter/year presets, expose helpers#231
wreiske merged 2 commits into
mainfrom
feat/date-range-picker-more-presets

Conversation

@wreiske
Copy link
Copy Markdown
Member

@wreiske wreiske commented May 14, 2026

Summary

Extends DateRangePicker / DateRangeFilter with longer drill-down presets and exposes the range-math helpers so consumers can build their own preset menus on top of the same logic.

While integrating the date picker into an analytics dashboard, I found I couldn't render presets like "Yesterday" or "This Quarter" without re-implementing the date math, because calculateDateRange was a private function — any preset key not in its built-in switch returned { start: null, end: null }. This PR makes the helpers public and adds the obvious missing keys.

Screenshots

Screenshot 2026-05-14 at 2 17 18 PM Screenshot 2026-05-14 at 2 16 44 PM

New preset keys

Added to the DateRangePresetKey union and implemented in calculateDateRange:

  • yesterday
  • last-week
  • this-quarter
  • last-quarter
  • this-year
  • last-year
  • last-90-days
  • last-365-days

All bounded-day presets (today, yesterday, this-week, last-week, this-month, last-month, the quarter/year keys) now end at 23:59:59.999 of the last day. Previously some used midnight, which excluded same-day events. The trailing-window keys (last-N-days, last-24-hours) continue to end at now.

New exports

import {
  calculateDateRange,    // (key: string) => DateRange
  getDefaultPresets,     // unchanged default list
  getExtendedPresets,    // full list incl. yesterday/quarter/year/etc.
} from '@mieweb/ui';

This unlocks usage like:

<DateRangePicker
  presets={getExtendedPresets()}
  value={range}
  activePreset={presetKey}
  onChange={(r, key) => {
    setRange(r);
    setPresetKey(key);
  }}
/>

Or for custom UIs that just need the math:

const range = calculateDateRange('this-quarter');

Backward compatibility

  • The default preset list returned by DateRangePicker / DateRangeFilter when no presets prop is passed is unchanged. Existing consumers see no visual or behavioral diff.
  • All previously supported keys still resolve to the same ranges, with the only difference being that "this/last-month" and friends now extend through end-of-day (a strict improvement for inclusive range queries).
  • New labels?: { yesterday, lastWeek, thisQuarter, ... } keys are all optional with English defaults.

Tests

Added DateRangePicker.test.tsx with 15 unit tests using vi.useFakeTimers() to pin "now" to a stable date. Covers every preset key, including the Q1-rolls-back-across-year-boundary edge case for last-quarter.

✓ src/components/DateRangePicker/DateRangePicker.test.tsx (15 tests)
Test Files  1 passed (1)
     Tests  15 passed (15)

Checks

  • pnpm typecheck — clean
  • pnpm lint — clean
  • pnpm test src/components/DateRangePicker — 15/15 pass

…pers

Extend DateRangePresetKey with: yesterday, last-week, this-quarter,
last-quarter, this-year, last-year, last-90-days, last-365-days. Each
maps to a stable, end-of-day-inclusive range in calculateDateRange.

Export calculateDateRange, getDefaultPresets, and a new
getExtendedPresets helper so consumers can:
  - reuse the same range math when building custom UIs,
  - opt into the longer preset menu for analytics dashboards,
  - or compose their own list from the union of supported keys.

The default preset list shown by DateRangePicker and DateRangeFilter is
unchanged, so existing consumers see no behavior or visual diff.

Adds unit tests with frozen system time covering every preset key,
including the Q1 cross-year edge case.
Copilot AI review requested due to automatic review settings May 14, 2026 21:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds public DateRangePicker preset helpers and expands supported date-range preset keys for analytics-style ranges while keeping default preset lists unchanged.

Changes:

  • Exposes calculateDateRange, getDefaultPresets, and getExtendedPresets.
  • Adds yesterday, last-week, quarter, year, last-90-days, and last-365-days preset support.
  • Adds focused unit coverage for preset calculations and preset list helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/components/DateRangePicker/index.ts Re-exports the newly public helper functions.
src/components/DateRangePicker/DateRangePicker.tsx Extends preset keys, labels, preset helper APIs, and date range calculations.
src/components/DateRangePicker/DateRangePicker.test.tsx Adds unit tests covering preset math and helper lists.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 14, 2026

Deploying ui with  Cloudflare Pages  Cloudflare Pages

Latest commit: 112020d
Status: ✅  Deploy successful!
Preview URL: https://e4ca5cf9.ui-6d0.pages.dev
Branch Preview URL: https://feat-date-range-picker-more.ui-6d0.pages.dev

View logs

@wreiske wreiske merged commit ffd4ff3 into main May 14, 2026
10 checks passed
@wreiske wreiske deleted the feat/date-range-picker-more-presets branch May 14, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants