From 59e6747800133e091d20c2c167f600981abc5c0d Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Thu, 24 Oct 2024 17:28:03 +0200 Subject: [PATCH] feat(dashboard,ui): DateFilter should open correctly (#9775) --- .changeset/perfect-ravens-allow.md | 6 + .../data-table-filter/date-filter.tsx | 8 +- .../components/date-picker/date-picker.tsx | 228 ++++++++++-------- 3 files changed, 143 insertions(+), 99 deletions(-) create mode 100644 .changeset/perfect-ravens-allow.md diff --git a/.changeset/perfect-ravens-allow.md b/.changeset/perfect-ravens-allow.md new file mode 100644 index 0000000000000..9e22d99dfc555 --- /dev/null +++ b/.changeset/perfect-ravens-allow.md @@ -0,0 +1,6 @@ +--- +"@medusajs/dashboard": patch +"@medusajs/ui": patch +--- + +fix(dashboard,ui): DateFilter should remain open diff --git a/packages/admin/dashboard/src/components/table/data-table/data-table-filter/date-filter.tsx b/packages/admin/dashboard/src/components/table/data-table/data-table-filter/date-filter.tsx index 46a10873125fb..004c6dbb206ee 100644 --- a/packages/admin/dashboard/src/components/table/data-table/data-table-filter/date-filter.tsx +++ b/packages/admin/dashboard/src/components/table/data-table/data-table-filter/date-filter.tsx @@ -9,8 +9,8 @@ import { useTranslation } from "react-i18next" import { useDate } from "../../../../hooks/use-date" import { useSelectedParams } from "../hooks" import { useDataTableFilterContext } from "./context" -import { IFilter } from "./types" import FilterChip from "./filter-chip" +import { IFilter } from "./types" type DateFilterProps = IFilter @@ -97,7 +97,9 @@ export const DateFilter = ({ const displayValue = getDisplayValueFromPresets() || getCustomDisplayValue() - const [previousValue, setPreviousValue] = useState(displayValue) + const [previousValue, setPreviousValue] = useState( + displayValue + ) const handleRemove = () => { selectedParams.delete() @@ -210,6 +212,7 @@ export const DateFilter = ({
handleCustomDateChange(d, "start")} @@ -224,6 +227,7 @@ export const DateFilter = ({
{ diff --git a/packages/design-system/ui/src/components/date-picker/date-picker.tsx b/packages/design-system/ui/src/components/date-picker/date-picker.tsx index f21b3f004f128..a36e62b82d2d9 100644 --- a/packages/design-system/ui/src/components/date-picker/date-picker.tsx +++ b/packages/design-system/ui/src/components/date-picker/date-picker.tsx @@ -1,6 +1,10 @@ "use client" -import { CalendarDate, CalendarDateTime, getLocalTimeZone } from "@internationalized/date" +import { + CalendarDate, + CalendarDateTime, + getLocalTimeZone, +} from "@internationalized/date" import { CalendarMini, Clock, XMarkMini } from "@medusajs/icons" import { cva } from "cva" import * as React from "react" @@ -38,10 +42,14 @@ type DatePickerValueProps = { granularity?: Granularity size?: "base" | "small" className?: string + modal?: boolean } interface DatePickerProps - extends Omit, keyof DatePickerValueProps>, + extends Omit< + BaseDatePickerProps, + keyof DatePickerValueProps + >, DatePickerValueProps {} const datePickerStyles = ( @@ -72,103 +80,123 @@ const datePickerStyles = ( }, }) -const HAS_TIME = new Set(["hour","minute", "second"]) - -const DatePicker = React.forwardRef(({ - size = "base", - shouldCloseOnSelect = true, - className, - ...props -}, ref) => { - const [value, setValue] = React.useState( - getDefaultCalendarDateFromDate(props.value, props.defaultValue, props.granularity) - ) - - const innerRef = React.useRef(null) - React.useImperativeHandle(ref, () => innerRef.current as HTMLDivElement) - - const contentRef = React.useRef(null) - - const _props = convertProps(props, setValue) - - const state = useDatePickerState({ - ..._props, - shouldCloseOnSelect, - }) - - const { groupProps, fieldProps, buttonProps, dialogProps, calendarProps } = - useDatePicker(_props, state, innerRef) - - React.useEffect(() => { - setValue(props.value ? updateCalendarDateFromDate(value, props.value, props.granularity) : null) - state.setValue(props.value ? updateCalendarDateFromDate(value, props.value, props.granularity) : null) - }, [props.value]) - - function clear(e: React.MouseEvent) { - e.preventDefault() - e.stopPropagation() +const HAS_TIME = new Set(["hour", "minute", "second"]) - props.onChange?.(null) - state.setValue(null) - } - - useInteractOutside({ - ref: contentRef, - onInteractOutside: () => { - state.setOpen(false) +const DatePicker = React.forwardRef( + ( + { + size = "base", + shouldCloseOnSelect = true, + className, + modal = false, + ...props }, - }) - - const hasTime = props.granularity && HAS_TIME.has(props.granularity) - const Icon = hasTime ? Clock : CalendarMini - - return ( - - -
{ + const [value, setValue] = React.useState< + CalendarDateTime | CalendarDate | null | undefined + >( + getDefaultCalendarDateFromDate( + props.value, + props.defaultValue, + props.granularity + ) + ) + + const innerRef = React.useRef(null) + React.useImperativeHandle(ref, () => innerRef.current as HTMLDivElement) + + const contentRef = React.useRef(null) + + const _props = convertProps(props, setValue) + + const state = useDatePickerState({ + ..._props, + shouldCloseOnSelect, + }) + + const { groupProps, fieldProps, buttonProps, dialogProps, calendarProps } = + useDatePicker(_props, state, innerRef) + + React.useEffect(() => { + setValue( + props.value + ? updateCalendarDateFromDate(value, props.value, props.granularity) + : null + ) + state.setValue( + props.value + ? updateCalendarDateFromDate(value, props.value, props.granularity) + : null + ) + }, [props.value]) + + function clear(e: React.MouseEvent) { + e.preventDefault() + e.stopPropagation() + + props.onChange?.(null) + state.setValue(null) + } + + useInteractOutside({ + ref: contentRef, + onInteractOutside: () => { + state.setOpen(false) + }, + }) + + const hasTime = props.granularity && HAS_TIME.has(props.granularity) + const Icon = hasTime ? Clock : CalendarMini + + return ( + + +
+ + + + + {!!state.value && ( + + + + )} +
+
+ - - - - - {!!state.value && ( - - - - )} -
-
- -
- -
- {state.hasTime && (
- +
- )} -
-
- ) -}) + {state.hasTime && ( +
+ +
+ )} + + + ) + } +) DatePicker.displayName = "DatePicker" function convertProps( @@ -200,10 +228,16 @@ function convertProps( return { ...rest, - onChange: onChange as BaseDatePickerProps["onChange"], + onChange: onChange as BaseDatePickerProps< + CalendarDateTime | CalendarDate + >["onChange"], isDateUnavailable, - minValue: minValue ? createCalendarDateFromDate(minValue, props.granularity) : minValue, - maxValue: maxValue ? createCalendarDateFromDate(maxValue, props.granularity) : maxValue, + minValue: minValue + ? createCalendarDateFromDate(minValue, props.granularity) + : minValue, + maxValue: maxValue + ? createCalendarDateFromDate(maxValue, props.granularity) + : maxValue, } }