The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-3285: Use chips for Day/Week/Month/Quarter/Year selectors (#83954)
Will Fryd4714716d472
diff --git a/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current.png b/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current.pngindex 05db16442b2..f53a0445882 100644Binary files a/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current.png and b/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current.png differdiff --git a/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current_Dark_Theme.png b/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current_Dark_Theme.pngindex 4235355012b..951e9189d6b 100644Binary files a/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current_Dark_Theme.png and b/.loki/reference/chrome_laptop_Components_Parameters_DatePicker_Relative_Current_Dark_Theme.png differdiff --git a/frontend/src/metabase/querying/common/components/DatePicker/DatePicker.stories.tsx b/frontend/src/metabase/querying/common/components/DatePicker/DatePicker.stories.tsxindex 14f2dc0a736..d62e0fe0657 100644--- a/frontend/src/metabase/querying/common/components/DatePicker/DatePicker.stories.tsx+++ b/frontend/src/metabase/querying/common/components/DatePicker/DatePicker.stories.tsx@@ -153,10 +153,9 @@ export const RelativeCurrent = {},play: async ({ canvasElement }: { canvasElement: HTMLCanvasElement }) => {const canvas = within(canvasElement);- const week = canvas.getByRole("button", {- name: "Week",- });- week.classList.add("pseudo-hover");+ // Chip's hover styles live on its label, not the hidden radio input+ const week = canvas.getByText("Week").closest("label");+ week?.classList.add("pseudo-hover");const next = canvas.getByRole("tab", {name: "Next",});diff --git a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.tsx b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.tsxindex 79b49f51f82..488f42896e2 100644--- a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.tsx+++ b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.tsx@@ -1,3 +1,4 @@+import type { KeyboardEvent } from "react";import { t } from "ttag";import type {@@ -5,7 +6,7 @@ import type {DatePickerUnit,RelativeDatePickerValue,} from "metabase/querying/common/types";-import { Button, Group, Stack, Tooltip } from "metabase/ui";+import { Box, Chip, Group, Stack, Tooltip } from "metabase/ui";import * as Lib from "metabase-lib";import { useTimeConfig } from "../use-time-config";@@ -35,8 +36,21 @@ export function CurrentDatePicker({onChange(getCurrentValue(unit));};+ const handleKeyDown = (+ event: KeyboardEvent<HTMLInputElement>,+ unit: DatePickerTruncationUnit,+ ) => {+ const isActivationKey =+ event.key === "Enter" ||+ (event.key === " " && event.currentTarget.checked);+ if (isActivationKey) {+ event.preventDefault();+ handleClick(unit);+ }+ };+return (- <Stack>+ <Stack role="radiogroup" aria-label={t`Current`}>{unitGroups.map((group, groupIndex) => (<Group key={groupIndex}>{group.map((unit) => (@@ -44,13 +58,22 @@ export function CurrentDatePicker({key={unit}label={t`Right now, this is ${getTooltipLabel(unit)}`}>- <Button- variant={unit === value?.unit ? "filled" : "default"}- aria-selected={unit === value?.unit}- onClick={() => handleClick(unit)}- >- {Lib.describeTemporalUnit(unit)}- </Button>+ {/* Chip forwards refs and handlers to its hidden input, so the+ tooltip needs a visible element to attach to */}+ <Box>+ <Chip+ type="radio"+ variant="filled"+ icon={null}+ checked={unit === value?.unit}+ // onClick rather than onChange: re-selecting the current+ // unit must still submit, and radios don't fire change then+ onClick={() => handleClick(unit)}+ onKeyDown={(event) => handleKeyDown(event, unit)}+ >+ {Lib.describeTemporalUnit(unit)}+ </Chip>+ </Box></Tooltip>))}</Group>diff --git a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.unit.spec.tsx b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.unit.spec.tsxindex a79eff4e204..5fde67b9c42 100644--- a/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.unit.spec.tsx+++ b/frontend/src/metabase/querying/common/components/DatePicker/RelativeDatePicker/CurrentDatePicker/CurrentDatePicker.unit.spec.tsx@@ -17,6 +17,12 @@ const DEFAULT_VALUE: RelativeDatePickerValue = {unit: "hour",};+const CURRENT_MONTH_VALUE: RelativeDatePickerValue = {+ type: "relative",+ value: 0,+ unit: "month",+};+interface SetupOpts {value?: RelativeDatePickerValue;availableUnits?: DatePickerUnit[];@@ -75,6 +81,33 @@ describe("CurrentDatePicker", () => {});});+ it("should mark the selected interval as checked", () => {+ setup({ value: CURRENT_MONTH_VALUE });++ expect(+ screen.getByRole("radiogroup", { name: "Current" }),+ ).toBeInTheDocument();+ expect(screen.getByRole("radio", { name: "Month" })).toBeChecked();+ expect(screen.getByRole("radio", { name: "Week" })).not.toBeChecked();+ });++ it("should call onChange when the selected interval is clicked again", async () => {+ const { onChange } = setup({ value: CURRENT_MONTH_VALUE });++ await userEvent.click(screen.getByText("Month"));++ expect(onChange).toHaveBeenCalledWith(CURRENT_MONTH_VALUE);+ });++ it("should select the focused interval on Enter", async () => {+ const { onChange } = setup();++ screen.getByRole("radio", { name: "Month" }).focus();+ await userEvent.keyboard("{Enter}");++ expect(onChange).toHaveBeenCalledWith(CURRENT_MONTH_VALUE);+ });+it("should show the date range for the selected interval", async () => {setup({ startOfWeek: "monday" });diff --git a/frontend/src/metabase/querying/parameters/components/DateAllOptionsWidget/DateAllOptionsWidget.unit.spec.tsx b/frontend/src/metabase/querying/parameters/components/DateAllOptionsWidget/DateAllOptionsWidget.unit.spec.tsxindex e7dfc637598..f06e01aa2a4 100644--- a/frontend/src/metabase/querying/parameters/components/DateAllOptionsWidget/DateAllOptionsWidget.unit.spec.tsx+++ b/frontend/src/metabase/querying/parameters/components/DateAllOptionsWidget/DateAllOptionsWidget.unit.spec.tsx@@ -48,13 +48,10 @@ describe("DateAllOptionsWidget", () => {it('should accept a previously selected "relative" filter', async () => {setup({ value: "thisweek" });const panel = screen.getByRole("tabpanel", { name: "Current" });- expect(within(panel).getByRole("button", { name: "Week" })).toHaveAttribute(- "aria-selected",- "true",- );+ expect(within(panel).getByRole("radio", { name: "Week" })).toBeChecked();expect(- within(panel).getByRole("button", { name: "Year" }),- ).not.toHaveAttribute("aria-selected", "true");+ within(panel).getByRole("radio", { name: "Year" }),+ ).not.toBeChecked();});it('should allow to select an "exclude" filter', async () => {