Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cpathipa committed Dec 9, 2024
1 parent e523043 commit f0dd5c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof DatePicker>;

export const ControlledExample: Story = {
render: () => {
const ControlledDatePicker = () => {
const [selectedDate, setSelectedDate] = React.useState<DateTime | null>(
DateTime.now()
);
const [selectedDate, setSelectedDate] = React.useState<DateTime | null>();

const handleChange = (newDate: DateTime | null) => {
setSelectedDate(newDate);
Expand All @@ -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}
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof DateTimePicker>;

Expand All @@ -14,7 +14,7 @@ export const ControlledExample: Story = {
const [
selectedDateTime,
setSelectedDateTime,
] = React.useState<DateTime | null>(DateTime.now());
] = React.useState<DateTime | null>(null); // Start with null

const handleChange = (newDateTime: DateTime | null) => {
setSelectedDateTime(newDateTime);
Expand All @@ -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}
/>
Expand Down
77 changes: 29 additions & 48 deletions packages/manager/src/components/DatePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>) => {
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
Expand All @@ -130,21 +97,16 @@ export const DateTimePicker = ({
if (newTime) {
setSelectedDateTime((prev) => {
if (prev) {
// Ensure hour and minute are valid numbers
const newHour = newTime.hour;
const newMinute = newTime.minute;

if (typeof newHour === 'number' && typeof newMinute === 'number') {
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) => {
Expand All @@ -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 (
<LocalizationProvider dateAdapter={AdapterLuxon}>
<Box>
Expand All @@ -168,7 +153,7 @@ export const DateTimePicker = ({
InputProps={{ readOnly: true }}
errorText={errorText}
label={label}
onClick={handleOpen}
onClick={(event) => setAnchorEl(event.currentTarget)}
placeholder={placeholder}
/>
</Box>
Expand All @@ -181,6 +166,9 @@ export const DateTimePicker = ({
>
<Box padding={2}>
<DateCalendar
onChange={handleDateChange}
value={selectedDateTime || null}
{...dateCalendarProps}
sx={(theme: Theme) => ({
'& .MuiDayCalendar-weekContainer, & .MuiDayCalendar-header': {
justifyContent: 'space-between',
Expand All @@ -203,9 +191,6 @@ export const DateTimePicker = ({
borderRadius: `${theme.spacing(2)}`,
borderWidth: '0px',
})}
onChange={handleDateChange}
value={selectedDateTime}
{...dateCalendarProps}
/>
<Grid
container
Expand All @@ -216,16 +201,12 @@ export const DateTimePicker = ({
<Grid item xs={4}>
<TimePicker
slotProps={{
openPickerButton: {
sx: {
padding: 0,
},
},
openPickerButton: { sx: { padding: 0 } },
textField: TimePickerFieldProps,
}}
onChange={handleTimeChange}
slots={{ textField: TextField }}
value={selectedDateTime}
value={selectedDateTime || null}
/>
</Grid>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
Expand Down Expand Up @@ -94,6 +97,7 @@ export const DateTimeRangePicker = ({
format={format}
label={startLabel}
onChange={handleStartDateTimeChange}
showTimeZone={showStartTimeZone}
timeSelectProps={{ label: 'Start Time' }}
value={startDateTime}
/>
Expand Down

0 comments on commit f0dd5c0

Please sign in to comment.