-
Notifications
You must be signed in to change notification settings - Fork 832
/
Copy pathDateRangePickerInput.tsx
161 lines (148 loc) · 4.38 KB
/
DateRangePickerInput.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as React from 'react';
import clsx from 'clsx';
import Typography from '@material-ui/core/Typography';
import KeyboardDateInput from '../_shared/KeyboardDateInput';
import { RangeInput, DateRange } from './RangeTypes';
import { useUtils } from '../_shared/hooks/useUtils';
import { makeStyles } from '@material-ui/core/styles';
import { MaterialUiPickersDate } from '../typings/date';
import { DateInputProps } from '../_shared/PureDateInput';
import { CurrentlySelectingRangeEndProps } from './RangeTypes';
import { mergeRefs, createDelegatedEventHandler, doNothing } from '../_helpers/utils';
export const useStyles = makeStyles(
theme => ({
rangeInputsContainer: {
display: 'flex',
alignItems: 'center',
[theme.breakpoints.down('xs')]: {
flexDirection: 'column',
},
},
toLabelDelimiter: {
margin: '8px 0',
[theme.breakpoints.up('sm')]: {
margin: '0 16px',
},
},
}),
{ name: 'MuiPickersDateRangePickerInput' }
);
export interface ExportedDateRangePickerInputProps {
toText?: React.ReactNode;
}
export interface DateRangeInputProps
extends ExportedDateRangePickerInputProps,
CurrentlySelectingRangeEndProps,
Omit<DateInputProps<RangeInput, DateRange>, 'forwardedRef'> {
startText: React.ReactNode;
endText: React.ReactNode;
forwardedRef?: React.Ref<HTMLDivElement>;
containerRef?: React.Ref<HTMLDivElement>;
}
export const DateRangePickerInput: React.FC<DateRangeInputProps> = ({
toText = 'to',
rawValue,
onChange,
onClick,
parsedDateValue: [start, end],
id,
open,
className,
containerRef,
forwardedRef,
currentlySelectingRangeEnd,
setCurrentlySelectingRangeEnd,
openPicker,
onFocus,
readOnly,
disableOpenPicker,
startText,
endText,
...other
}) => {
const utils = useUtils();
const classes = useStyles();
const startRef = React.useRef<HTMLInputElement>(null);
const endRef = React.useRef<HTMLInputElement>(null);
React.useEffect(() => {
if (!open) {
return;
}
if (currentlySelectingRangeEnd === 'start') {
startRef.current?.focus();
} else if (currentlySelectingRangeEnd === 'end') {
endRef.current?.focus();
}
}, [currentlySelectingRangeEnd, open]);
const handleStartChange = (date: MaterialUiPickersDate, inputString?: string) => {
if (date === null || utils.isValid(date)) {
onChange([date, end], inputString);
}
};
const handleEndChange = (date: MaterialUiPickersDate, inputString?: string) => {
if (date === null || utils.isValid(date)) {
onChange([start, date], inputString);
}
};
const openRangeStartSelection = () => {
if (setCurrentlySelectingRangeEnd) {
setCurrentlySelectingRangeEnd('start');
}
if (!disableOpenPicker) {
openPicker();
}
};
const openRangeEndSelection = () => {
if (setCurrentlySelectingRangeEnd) {
setCurrentlySelectingRangeEnd('end');
}
if (!disableOpenPicker) {
openPicker();
}
};
return (
<div
id={id}
className={clsx(classes.rangeInputsContainer, className)}
ref={mergeRefs([containerRef, forwardedRef])}
>
<KeyboardDateInput
{...other}
open={open}
forwardedRef={startRef}
rawValue={start}
parsedDateValue={start}
onChange={handleStartChange}
disableOpenPicker
openPicker={doNothing}
readOnly={readOnly}
label={startText}
focused={open && currentlySelectingRangeEnd === 'start'}
onClick={
readOnly ? createDelegatedEventHandler(openRangeStartSelection, onClick) : undefined
}
onFocus={
!readOnly ? createDelegatedEventHandler(openRangeStartSelection, onFocus) : undefined
}
/>
<Typography className={classes.toLabelDelimiter}>{toText}</Typography>
<KeyboardDateInput
{...other}
open={open}
forwardedRef={endRef}
rawValue={end}
parsedDateValue={end}
onChange={handleEndChange}
disableOpenPicker
openPicker={doNothing}
readOnly={readOnly}
label={endText}
focused={open && currentlySelectingRangeEnd === 'end'}
onClick={readOnly ? createDelegatedEventHandler(openRangeEndSelection, onClick) : undefined}
onFocus={
!readOnly ? createDelegatedEventHandler(openRangeEndSelection, onFocus) : undefined
}
/>
</div>
);
};