diff --git a/packages/manager/src/components/DatePicker/DatePicker.stories.tsx b/packages/manager/src/components/DatePicker/DatePicker.stories.tsx index db3925b03eb..6e9d4408783 100644 --- a/packages/manager/src/components/DatePicker/DatePicker.stories.tsx +++ b/packages/manager/src/components/DatePicker/DatePicker.stories.tsx @@ -1,19 +1,17 @@ import { action } from '@storybook/addon-actions'; -import { DateTime } from 'luxon'; import * as React from 'react'; import { DatePicker } from './DatePicker'; import type { Meta, StoryObj } from '@storybook/react'; +import type { DateTime } from 'luxon'; type Story = StoryObj; export const ControlledExample: Story = { render: () => { const ControlledDatePicker = () => { - const [selectedDate, setSelectedDate] = React.useState( - DateTime.now() - ); + const [selectedDate, setSelectedDate] = React.useState(); const handleChange = (newDate: DateTime | null) => { setSelectedDate(newDate); @@ -26,6 +24,7 @@ export const ControlledExample: Story = { helperText="This is a controlled DatePicker" label="Controlled Date Picker" onChange={handleChange} + placeholder="yyyy-MM-dd" value={selectedDate} /> ); diff --git a/packages/manager/src/components/DatePicker/DateTimePicker.stories.tsx b/packages/manager/src/components/DatePicker/DateTimePicker.stories.tsx index 32d65dfa0b7..bb3f0c48233 100644 --- a/packages/manager/src/components/DatePicker/DateTimePicker.stories.tsx +++ b/packages/manager/src/components/DatePicker/DateTimePicker.stories.tsx @@ -1,10 +1,10 @@ import { action } from '@storybook/addon-actions'; -import { DateTime } from 'luxon'; import * as React from 'react'; import { DateTimePicker } from './DateTimePicker'; import type { Meta, StoryObj } from '@storybook/react'; +import type { DateTime } from 'luxon'; type Story = StoryObj; @@ -14,7 +14,7 @@ export const ControlledExample: Story = { const [ selectedDateTime, setSelectedDateTime, - ] = React.useState(DateTime.now()); + ] = React.useState(null); // Start with null const handleChange = (newDateTime: DateTime | null) => { setSelectedDateTime(newDateTime); @@ -32,6 +32,7 @@ export const ControlledExample: Story = { onCancel={action('Cancel clicked')} onChange={handleChange} placeholder="yyyy-MM-dd HH:mm" + showTimeZone={false} timeSelectProps={{ label: 'Select Time' }} value={selectedDateTime} /> diff --git a/packages/manager/src/components/DatePicker/DateTimePicker.tsx b/packages/manager/src/components/DatePicker/DateTimePicker.tsx index 027b25822d2..a67b5ccb155 100644 --- a/packages/manager/src/components/DatePicker/DateTimePicker.tsx +++ b/packages/manager/src/components/DatePicker/DateTimePicker.tsx @@ -82,39 +82,6 @@ export const DateTimePicker = ({ noMarginTop: true, }; - useEffect(() => { - if (value) { - setSelectedDateTime(value); - } - }, [value]); - - useEffect(() => { - if (timeZoneSelectProps.value) { - setSelectedTimeZone(timeZoneSelectProps.value); - } - }, [timeZoneSelectProps.value]); - - const handleOpen = (event: React.MouseEvent) => { - setAnchorEl(event.currentTarget); - }; - - const handleClose = () => { - setAnchorEl(null); - if (onCancel) { - onCancel(); - } - }; - - const handleApply = () => { - setAnchorEl(null); - if (onChange) { - onChange(selectedDateTime); - } - if (onApply) { - onApply(); - } - }; - const handleDateChange = (newDate: DateTime | null) => { setSelectedDateTime((prev) => newDate @@ -130,7 +97,6 @@ export const DateTimePicker = ({ if (newTime) { setSelectedDateTime((prev) => { if (prev) { - // Ensure hour and minute are valid numbers const newHour = newTime.hour; const newMinute = newTime.minute; @@ -138,13 +104,9 @@ export const DateTimePicker = ({ return prev.set({ hour: newHour, minute: newMinute }); } } - // Return the current `prev` value if newTime is invalid return prev; }); } - if (timeSelectProps.onChange && newTime) { - timeSelectProps.onChange(newTime.toISOTime()); - } }; const handleTimeZoneChange = (newTimeZone: string) => { @@ -154,6 +116,29 @@ export const DateTimePicker = ({ } }; + const handleApply = () => { + setAnchorEl(null); + if (onChange) { + onChange(selectedDateTime); + } + if (onApply) { + onApply(); + } + }; + + const handleClose = () => { + setAnchorEl(null); + if (onCancel) { + onCancel(); + } + }; + + useEffect(() => { + if (timeZoneSelectProps.value) { + setSelectedTimeZone(timeZoneSelectProps.value); + } + }, [timeZoneSelectProps.value]); + return ( @@ -168,7 +153,7 @@ export const DateTimePicker = ({ InputProps={{ readOnly: true }} errorText={errorText} label={label} - onClick={handleOpen} + onClick={(event) => setAnchorEl(event.currentTarget)} placeholder={placeholder} /> @@ -181,6 +166,9 @@ export const DateTimePicker = ({ > ({ '& .MuiDayCalendar-weekContainer, & .MuiDayCalendar-header': { justifyContent: 'space-between', @@ -203,9 +191,6 @@ export const DateTimePicker = ({ borderRadius: `${theme.spacing(2)}`, borderWidth: '0px', })} - onChange={handleDateChange} - value={selectedDateTime} - {...dateCalendarProps} /> )} diff --git a/packages/manager/src/components/DatePicker/DateTimeRangePicker.tsx b/packages/manager/src/components/DatePicker/DateTimeRangePicker.tsx index f4cb77b175a..0d973178715 100644 --- a/packages/manager/src/components/DatePicker/DateTimeRangePicker.tsx +++ b/packages/manager/src/components/DatePicker/DateTimeRangePicker.tsx @@ -17,8 +17,10 @@ interface DateTimeRangePickerProps { end: DateTime | null, startTimeZone?: null | string ) => void; - /** Whether to show the timezone field for the end date picker */ + /** Whether to show the end timezone field for the end date picker */ showEndTimeZone?: boolean; + /** Whether to show the start timezone field for the end date picker */ + showStartTimeZone?: boolean; /** Initial or controlled value for the start date-time */ startDateTimeValue?: DateTime | null; /** Custom labels for the start and end date/time fields */ @@ -33,6 +35,7 @@ export const DateTimeRangePicker = ({ format = 'yyyy-MM-dd HH:mm', onChange, showEndTimeZone = false, + showStartTimeZone = false, startDateTimeValue = null, startLabel = 'Start Date and Time', startTimeZoneValue = null, @@ -94,6 +97,7 @@ export const DateTimeRangePicker = ({ format={format} label={startLabel} onChange={handleStartDateTimeChange} + showTimeZone={showStartTimeZone} timeSelectProps={{ label: 'Start Time' }} value={startDateTime} />