fix(ui): preserve selected relative usage range

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
mrinal 2026-09-10 13:56:33 +00:00
parent 0274a77717
commit a217c324ef
2 changed files with 64 additions and 22 deletions

View file

@ -1,5 +1,6 @@
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import moment from "moment";
import { describe, it, expect, vi, beforeEach, beforeAll } from "vitest";
import AdvancedDatePicker from "./advanced_date_picker";
@ -96,8 +97,6 @@ describe("AdvancedDatePicker", () => {
openDropdown(container);
const presets = container.querySelectorAll('[data-slot="advanced-date-picker-preset"]');
expect(presets).toHaveLength(6);
expect(screen.getByText("Since last budget reset")).toBeInTheDocument();
await user.click(screen.getByText("Since last budget reset"));
@ -113,7 +112,46 @@ describe("AdvancedDatePicker", () => {
openDropdown(container);
expect(container.querySelectorAll('[data-slot="advanced-date-picker-preset"]')).toHaveLength(5);
expect(
screen.getAllByRole("button", {
name: /^(Todaytoday|Last 7 days7d|Last 30 days30d|Month to dateMTD|Year to dateYTD)$/,
}),
).toHaveLength(5);
});
it("keeps the chosen additional option selected when its range matches a built-in option", async () => {
const user = userEvent.setup();
const { container } = render(
<AdvancedDatePicker
value={defaultValue}
onValueChange={mockOnValueChange}
additionalOptions={[
{
label: "Since last budget reset",
shortLabel: "budget",
getValue: () => ({
from: moment().startOf("month").toDate(),
to: moment().endOf("day").toDate(),
}),
},
]}
/>,
);
openDropdown(container);
const budgetOption = screen.getByRole("button", { name: /Since last budget reset/ });
const monthToDateOption = screen.getByRole("button", { name: /^Month to date/ });
await user.click(budgetOption);
expect(budgetOption).toHaveAttribute("aria-pressed", "true");
expect(monthToDateOption).toHaveAttribute("aria-pressed", "false");
await user.click(screen.getByRole("button", { name: "Apply" }));
await user.click(screen.getByRole("button", { name: /1 Jan, 12:00 - 31 Jan, 12:00/ }));
expect(screen.getByRole("button", { name: /Since last budget reset/ })).toHaveAttribute("aria-pressed", "true");
expect(screen.getByRole("button", { name: /^Month to date/ })).toHaveAttribute("aria-pressed", "false");
});
it("anchors the panel to the trigger edge named by align", () => {
@ -225,7 +263,9 @@ describe("AdvancedDatePicker", () => {
await user.tab();
await user.keyboard("{Enter}");
const presets = Array.from(container.querySelectorAll('[data-slot="advanced-date-picker-preset"]'));
const presets = screen.getAllByRole("button", {
name: /^(Todaytoday|Last 7 days7d|Last 30 days30d|Month to dateMTD|Year to dateYTD)$/,
});
expect(presets).toHaveLength(5);
await user.tab();

View file

@ -90,29 +90,31 @@ const AdvancedDatePicker: React.FC<AdvancedDatePickerProps> = ({
const dropdownRef = useRef<HTMLDivElement>(null);
// Function to check if current value matches a relative time option
const getMatchingOption = useCallback((currentValue: DateRangePickerValue): string | null => {
if (!currentValue.from || !currentValue.to) return null;
const getMatchingOption = useCallback(
(currentValue: DateRangePickerValue, preferred: string | null): string | null => {
if (!currentValue.from || !currentValue.to) return null;
for (const option of options) {
const optionRange = option.getValue();
const matches = (option: RelativeTimeOption) => {
const optionRange = option.getValue();
const fromMatches = moment(currentValue.from).isSame(moment(optionRange.from), "day");
const toMatches = moment(currentValue.to).isSame(moment(optionRange.to), "day");
return fromMatches && toMatches;
};
// Compare dates with some tolerance (to account for time differences)
const fromMatches = moment(currentValue.from).isSame(moment(optionRange.from), "day");
const toMatches = moment(currentValue.to).isSame(moment(optionRange.to), "day");
if (fromMatches && toMatches) {
return option.shortLabel;
if (preferred && options.some((option) => option.shortLabel === preferred && matches(option))) {
return preferred;
}
}
return null;
}, [options]);
return options.find(matches)?.shortLabel ?? null;
},
[options],
);
// Update selected option when value changes
useEffect(() => {
const matchingOption = getMatchingOption(value);
const matchingOption = getMatchingOption(value, selectedOption);
setSelectedOption(matchingOption);
}, [value, getMatchingOption]);
}, [value, getMatchingOption, selectedOption]);
// Validation logic - simplified for dates only
const validateDateRange = useCallback(() => {
@ -231,14 +233,14 @@ const AdvancedDatePicker: React.FC<AdvancedDatePickerProps> = ({
setTempValue(newValue);
// Check if this matches any preset option
const matchingOption = getMatchingOption(newValue);
const matchingOption = getMatchingOption(newValue, selectedOption);
setSelectedOption(matchingOption);
}
}
} catch (error) {
console.warn("Invalid date format:", error);
}
}, [startDate, endDate, validation.isValid, getMatchingOption]);
}, [startDate, endDate, validation.isValid, getMatchingOption, selectedOption]);
// Update tempValue when inputs change
useEffect(() => {
@ -276,7 +278,7 @@ const AdvancedDatePicker: React.FC<AdvancedDatePickerProps> = ({
}
// Reset selected option
const matchingOption = getMatchingOption(value);
const matchingOption = getMatchingOption(value, selectedOption);
setSelectedOption(matchingOption);
setIsOpen(false);