Skip to content

Commit

Permalink
Allow dates to be unselected
Browse files Browse the repository at this point in the history
  • Loading branch information
Heenawter committed Dec 13, 2023
1 parent 97dc0d3 commit 2840238
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,15 @@
* Side Public License, v 1.
*/

import { EuiDatePicker, EuiDatePickerRange, EuiFormControlLayoutDelimited } from '@elastic/eui';
import { EuiDatePicker, EuiDatePickerRange } from '@elastic/eui';
import moment, { Moment } from 'moment-timezone';
import React, { forwardRef, useMemo } from 'react';
import React, { useMemo } from 'react';
import { pluginServices } from '../../services';
import { getMomentTimezone } from '../../time_slider/time_utils';
import { useTimePicker } from '../embeddable/time_picker_embeddable';

import './time_picker.scss';

const ExampleCustomInput = forwardRef(({ onClick, min, max }, ref) => {
return (
// <EuiButton className="example-custom-input" onClick={onClick}>
// {min?.format('MM/DD/YYYY')} {'->'} {max?.format('MM/DD/YYYY')}
// </EuiButton>
<EuiFormControlLayoutDelimited
onClick={onClick}
startControl={
<input type={'text'} placeholder={min?.format('MM/DD/YYYY')} className="euiFieldNumber" />
}
endControl={
<input type={'text'} placeholder={max?.format('MM/DD/YYYY')} className="euiFieldNumber" />
}
fullWidth
/>
);
});

export const TimePickerControl = () => {
const timePicker = useTimePicker();

Expand Down Expand Up @@ -84,15 +66,19 @@ export const TimePickerControl = () => {
isLoading={loading}
showTimeSelect={false}
showIcon={false}
popoverPlacement={'downCenter'}
selected={startDate}
minDate={minDate}
maxDate={maxDate}
allowSameDay={true}
adjustDateOnChange={false}
onChange={(date) => {
if (!date) return;
timePicker.dispatch.setSingleDate(date.valueOf());
// timePicker.dispatch.setStartDate(date.startOf('day').valueOf());
// timePicker.dispatch.setEndDate(date.endOf('day').valueOf());
if (date.isSame(startDate)) {
timePicker.dispatch.setSingleDate(undefined);
} else {
timePicker.dispatch.setSingleDate(date.valueOf());
}
}}
/>
) : (
Expand All @@ -104,12 +90,17 @@ export const TimePickerControl = () => {
isInvalid={isInvalid}
startDateControl={
<EuiDatePicker
allowSameDay={true}
placeholder={minDate?.format('MM/DD/YYYY')}
adjustDateOnChange={false}
selected={startDate}
selected={startDate === minDate ? undefined : startDate}
onChange={(date) => {
if (!date) return;
timePicker.dispatch.setStartDate(date.startOf('day').valueOf());
if (date.isSame(startDate)) {
timePicker.dispatch.setStartDate(undefined);
} else {
timePicker.dispatch.setStartDate(date.startOf('day').valueOf());
}
}}
startDate={startDate ?? minDate}
endDate={endDate ?? maxDate}
Expand All @@ -121,12 +112,17 @@ export const TimePickerControl = () => {
}
endDateControl={
<EuiDatePicker
allowSameDay={true}
placeholder={maxDate?.format('MM/DD/YYYY')}
adjustDateOnChange={false}
selected={endDate}
selected={endDate === maxDate ? undefined : endDate}
onChange={(date) => {
if (!date) return;
timePicker.dispatch.setEndDate(date.endOf('day').valueOf());
if (date.isSame(endDate)) {
timePicker.dispatch.setEndDate(undefined);
} else {
timePicker.dispatch.setEndDate(date.endOf('day').valueOf());
}
}}
startDate={startDate ?? minDate}
endDate={endDate ?? maxDate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export const getDefaultComponentState = (): TimePickerReduxState['componentState
});

export const timePickerReducers = {
setSingleDate: (state: WritableDraft<TimePickerReduxState>, action: PayloadAction<number>) => {
setSingleDate: (
state: WritableDraft<TimePickerReduxState>,
action: PayloadAction<number | undefined>
) => {
state.explicitInput.startDate = action.payload;
state.explicitInput.endDate = action.payload;
},
Expand Down

0 comments on commit 2840238

Please sign in to comment.