Skip to content

Add timepicker inside datepicker component #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ export interface DatePickerProps extends AnalyticsProps {
showPresets?: boolean;
showPresetsWithDropdown?: boolean;
showInnerPresets?: boolean;
hasTimeInterval?: boolean;
}

interface DatePickerState {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/react": "^17.0.0",
"@types/react-dates": "^21.8.0",
"@types/react-dom": "^17.0.0",
"@types/react-time-picker": "^4.0.2",
"@types/react-toggle": "^4.0.2",
"@types/styled-components": "^5.1.4",
"@typescript-eslint/eslint-plugin": "4.3.0",
Expand Down Expand Up @@ -105,6 +106,7 @@
},
"dependencies": {
"formik": "^2.1.5",
"react-time-picker": "^4.5.0",
"yup": "^0.32.11"
}
}
94 changes: 90 additions & 4 deletions src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import styled, { css } from 'styled-components';
import { FocusedInputShape, DateRangePicker } from 'react-dates';
import { FaCalendarAlt } from 'react-icons/fa';
import { snakeCase } from 'lodash';
import TimePicker from 'react-time-picker';
// import StyledTimePicker from '../TimeHourPicker/TimeHourPicker';
import colors from '../../theme/colors';
import { getMonthOptions, getYearOptions } from '../../utils/helpers';
import {
addCustomTimeToDate,
getMonthOptions,
getYearOptions,
timeMinutesSplitter,
} from '../../utils/helpers';
import Dropdown from '../Dropdown';
import { Option } from '../Dropdown/Dropdown';
import DatePickerWrapper from './DatePickerWrapper';
Expand All @@ -19,6 +26,11 @@ import {
} from '../../analytics';
import 'react-dates/initialize';

const TimeIntervalContainer = styled.div`
display: flex;
justify-content: space-around;
`;

const ContentHolder = styled.div<{ isDropdown?: boolean }>`
margin: 0 auto;
padding: 15px 0 15px;
Expand Down Expand Up @@ -106,6 +118,7 @@ type Props = {
showInnerPresets?: boolean;
className?: string;
showPresetsWithDropdown?: boolean;
hasTimeInterval?: boolean;
} & AnalyticsProps;

const DatePicker = ({
Expand All @@ -124,10 +137,24 @@ const DatePicker = ({
className = '',
minimumNights = 0,
showPresetsWithDropdown = false,
hasTimeInterval = false,
tag = '',
}: Props) => {
const [activePeriod, setActivePeriod] = useState('');

const [startHour, setStartHour] = useState('00:00');
const [endHour, setEndHour] = useState('23:59');

const [startHours, startMinutes] = timeMinutesSplitter(startHour);
const [endHours, endMinutes] = timeMinutesSplitter(endHour);

const [startDateTime, setStartDateTime] = useState(
addCustomTimeToDate(startDateProp, startHours, startMinutes)
);
const [endDateTime, setEndDateTime] = useState(
addCustomTimeToDate(endDateProp, endHours, endMinutes)
);

useEffect(() => {
setActivePeriod(activePeriodPreset);
}, []);
Expand Down Expand Up @@ -195,7 +222,57 @@ const DatePicker = ({
onPeriodChange(period);
};

const renderTimeIntervals = () => {
const onTimePickerChange = (newValue: string, isStartTimePicker?: boolean) => {
const [hour, minutes] = timeMinutesSplitter(newValue);

let modifiedStartDate = startDateTime;
let modifiedEndDate = endDateTime;

if (isStartTimePicker) {
setStartHour(newValue);
modifiedStartDate = addCustomTimeToDate(startDateTime, hour, minutes);
} else {
setEndHour(newValue);
modifiedEndDate = addCustomTimeToDate(endDateTime, hour, minutes);
}

onDateChange({ startDate: modifiedStartDate, endDate: modifiedEndDate });
};
return (
<TimeIntervalContainer>
<TimePicker
disableClock
value={startHour}
onChange={(newValue: any) => onTimePickerChange(newValue, true)}
/>
<TimePicker
disableClock
value={endHour}
onChange={(newValue: any) => onTimePickerChange(newValue)}
minTime={startHour}
/>
</TimeIntervalContainer>
);
};

const renderDatePresets = () => {
if (hasTimeInterval)
return (
<TimeIntervalContainer>
<TimePicker
disableClock
value={startHour}
onChange={(newValue: any) => setStartHour(newValue)}
/>
<TimePicker
disableClock
value={endHour}
onChange={(newValue: any) => setEndHour(newValue)}
minTime={startHour}
/>
</TimeIntervalContainer>
);
if (!showInnerPresets) return '';

let presets: string[] = [];
Expand Down Expand Up @@ -261,7 +338,16 @@ const DatePicker = ({
);

const handleDateChange = ({ startDate, endDate }: DateChangeArgs) => {
onDateChange({ startDate, endDate });
setStartDateTime(startDate as Moment);
setEndDateTime(endDate as Moment);

const [hourStart, minutesStart] = timeMinutesSplitter(startHour);
const [hourEnd, minutesEnd] = timeMinutesSplitter(endHour);

const modifiedStartDate = addCustomTimeToDate(startDate, hourStart, minutesStart);
const modifiedEndDate = addCustomTimeToDate(endDate, hourEnd, minutesEnd);

onDateChange({ startDate: modifiedStartDate, endDate: modifiedEndDate });
setActivePeriod('');
};

Expand Down Expand Up @@ -513,11 +599,11 @@ const DatePicker = ({
endDate={endDateProp}
endDateId={endDateId}
customArrowIcon="to"
calendarInfoPosition="before"
calendarInfoPosition={hasTimeInterval ? 'bottom' : 'before'}
minimumNights={minimumNights}
enableOutsideDays
readOnly
renderCalendarInfo={renderDatePresets}
renderCalendarInfo={hasTimeInterval ? renderTimeIntervals : renderDatePresets}
onNextMonthClick={() => {
tracker.track({
event: AnalyticsEvents.CLICK,
Expand Down
10 changes: 9 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import moment from 'moment';
import moment, { Moment } from 'moment';

export const addCustomTimeToDate = (date: Moment | null, hour: any, minutes: any) =>
date?.set('hour', hour).set('minute', minutes).set('second', 0) || null;

export const timeMinutesSplitter = (date: string) => {
const [hour, minutes] = date.split(':');
return [hour, minutes];
};

export const getMonthOptions = (): Array<any> =>
moment.months().map((label, value) => ({ value, displayName: label }));
Expand Down