-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDatePicker.tsx
89 lines (79 loc) · 2.62 KB
/
DatePicker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React from 'react';
import { Calendar as CalendarIcon } from '@osrd-project/ui-icons';
import cx from 'classnames';
import { type CalendarSlot } from '.';
import CalendarPicker, { type CalendarPickerPublicProps } from './CalendarPicker';
import useDatePicker from './useDatePicker';
import InputModal from '../../Modal';
import Input, { type InputProps } from '../Input';
type BaseDatePickerProps = {
selectableSlot?: CalendarSlot;
inputProps: InputProps;
calendarPickerProps?: CalendarPickerPublicProps;
errorMessages?: { invalidInput?: string; invalidDate?: string };
width?: number;
};
export type SingleDatePickerProps = BaseDatePickerProps & {
isRangeMode?: false;
onDateChange: (nextDate: Date) => void;
value?: Date;
};
export type RangeDatePickerProps = BaseDatePickerProps & {
isRangeMode: true;
onDateChange: (clickedDate: Date, nextSelectedSlot?: CalendarSlot) => void;
value?: CalendarSlot;
};
export type DatePickerProps = SingleDatePickerProps | RangeDatePickerProps;
export const DatePicker = (props: DatePickerProps) => {
const {
inputValue,
statusWithMessage,
selectedSlot,
showPicker,
modalPosition,
inputRef,
calendarPickerRef,
setShowPicker,
handleDayClick,
handleInputClick,
handleInputOnChange,
} = useDatePicker(props);
const { inputFieldWrapperClassname, ...otherInputProps } = props.inputProps;
const { selectableSlot } = props;
return (
<div className="date-picker">
<div>
<Input
{...otherInputProps}
ref={inputRef}
value={inputValue}
onClick={handleInputClick}
type="text"
trailingContent={{
content: <CalendarIcon />,
onClickCallback: () => setShowPicker(!showPicker),
}}
inputFieldWrapperClassname={cx('date-picker-input', inputFieldWrapperClassname, {
'range-mode': props.isRangeMode,
})}
autoComplete="off"
onChange={(e: React.ChangeEvent<HTMLInputElement>) => handleInputOnChange(e.target.value)}
statusWithMessage={statusWithMessage}
/>
</div>
<InputModal inputRef={inputRef} isOpen={showPicker} onClose={() => setShowPicker(false)}>
<div className="calendar-picker-wrapper">
<CalendarPicker
{...props.calendarPickerProps}
selectedSlot={selectedSlot}
onDayClick={handleDayClick}
modalPosition={modalPosition}
calendarPickerRef={calendarPickerRef}
selectableSlot={selectableSlot}
/>
</div>
</InputModal>
</div>
);
};
export default DatePicker;