LF-5300: Extend DateRangeSelector with filtered options and dynamic year entries#4181
Open
litefarm-pr-bot wants to merge 1 commit into
Conversation
…ear entries Adds two new props to the shared DateRangeSelector: `allowedOptions` to restrict which built-in options are rendered, and `dynamicOptions` for caller-supplied entries (such as per-year ranges derived from data). Selecting a dynamic entry persists its pre-baked startDate/endDate into the date-range state without involving the DateRange utility class. Also adds a `LAST_12_MONTHS` enum value with a corresponding `getLast12MonthsDateRange()` method on the DateRange utility. This option is intentionally excluded from the default option list — existing call sites see no change in dropdown contents — and is available only when a caller explicitly includes it via `allowedOptions`. Option-building and value-resolution logic was extracted to a `helpers.ts` module so it can be unit-tested without rendering react-select. The `useDateRange` hook now skips the on-mount date recompute when the persisted option is a caller-supplied dynamic value rather than a built-in enum value, preventing a runtime error if such an option is restored from Redux Persist. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The shared
DateRangeSelectorcurrently hard-codes its dropdown contents to a fixed list of nine static options. Any consumer that wants a different subset, or that needs to inject ranges derived from data (for example, a list of calendar years for which transactions exist), has no way to do so without either forking the component or hiding rows in CSS. This change adds two opt-in props that close that gap without affecting any existing call site.DateRangeInputnow accepts anallowedOptionsprop (an explicit allowlist of built-inDateRangeOptions, rendered in the order given) and adynamicOptionsprop (caller-supplied entries with pre-bakedstartDate/endDate, rendered between the static options and theCUSTOMrow). When both are omitted, the dropdown renders exactly the same nine entries it did before, so no other consumer needs to change. A newLAST_12_MONTHSenum value and matchinggetLast12MonthsDateRange()method onDateRangeare added at the same time — intentionally excluded from the default option set, so they only appear when a caller explicitly opts in viaallowedOptions. The option-building and dynamic-value lookup logic was lifted into ahelpers.tsmodule that can be unit-tested without rendering react-select, anduseDateRangewas hardened so that a persisted dynamic value (an arbitrary string that is not a known enum value) does not causegetDates(option)to throw when the hook recomputes on mount.The convention this PR establishes is: when a shared component needs to support per-consumer variation, prefer additive opt-in props with backward-compatible defaults over a forked component. Adding new entries to a shared enum is fine, but only wire them into the default option list when every existing consumer is intended to see them — otherwise gate visibility behind the explicit
allowedOptionsallowlist so the default behaviour stays frozen.Jira link: https://lite-farm.atlassian.net/browse/LF-5300
Type of change
How Has This Been Tested?
pnpm exec vitest run src/tests/dateRange.test.js src/tests/dateRangeSelectorHelpers.test.js— 204 tests pass (192 existingDateRangecases plus three newLAST_12_MONTHSexpectations, plus 12 new helpers tests coveringallowedOptionsfiltering,dynamicOptionsinsertion beforeCUSTOM, dynamic-option label pass-through,isStaticDateRangeOption, andfindDynamicDateRange). No local frontend was running; visual review of the rendered dropdown was not performed and should be confirmed when the dynamic-options consumer lands.Checklist:
pnpm i18nto help with this)