forked from react-dates/react-dates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleDatePickerInput.jsx
More file actions
315 lines (279 loc) · 7.98 KB
/
SingleDatePickerInput.jsx
File metadata and controls
315 lines (279 loc) · 7.98 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import React from 'react';
import PropTypes from 'prop-types';
import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types';
import { css, withStyles, withStylesPropTypes } from 'react-with-styles';
import { SingleDatePickerInputPhrases } from '../defaultPhrases';
import getPhrasePropTypes from '../utils/getPhrasePropTypes';
import noflip from '../utils/noflip';
import DateInput from './DateInput';
import IconPositionShape from '../shapes/IconPositionShape';
import CloseButton from './CloseButton';
import CalendarIcon from './CalendarIcon';
import openDirectionShape from '../shapes/OpenDirectionShape';
import { ICON_BEFORE_POSITION, ICON_AFTER_POSITION, OPEN_DOWN } from '../constants';
const propTypes = forbidExtraProps({
...withStylesPropTypes,
id: PropTypes.string.isRequired,
children: PropTypes.node,
placeholder: PropTypes.string, // also used as label
displayValue: PropTypes.string,
screenReaderMessage: PropTypes.string,
focused: PropTypes.bool,
isFocused: PropTypes.bool, // describes actual DOM focus
disabled: PropTypes.bool,
required: PropTypes.bool,
readOnly: PropTypes.bool,
openDirection: openDirectionShape,
showCaret: PropTypes.bool,
showClearDate: PropTypes.bool,
customCloseIcon: PropTypes.node,
showDefaultInputIcon: PropTypes.bool,
inputIconPosition: IconPositionShape,
customInputIcon: PropTypes.node,
isRTL: PropTypes.bool,
noBorder: PropTypes.bool,
block: PropTypes.bool,
small: PropTypes.bool,
regular: PropTypes.bool,
verticalSpacing: nonNegativeInteger,
onChange: PropTypes.func,
onClearDate: PropTypes.func,
onFocus: PropTypes.func,
onKeyDownShiftTab: PropTypes.func,
onKeyDownTab: PropTypes.func,
onKeyDownArrowDown: PropTypes.func,
onKeyDownQuestionMark: PropTypes.func,
// i18n
phrases: PropTypes.shape(getPhrasePropTypes(SingleDatePickerInputPhrases)),
});
const defaultProps = {
children: null,
placeholder: 'Select Date',
displayValue: '',
screenReaderMessage: '',
focused: false,
isFocused: false,
disabled: false,
required: false,
readOnly: false,
openDirection: OPEN_DOWN,
showCaret: false,
showClearDate: false,
showDefaultInputIcon: false,
inputIconPosition: ICON_BEFORE_POSITION,
customCloseIcon: null,
customInputIcon: null,
isRTL: false,
noBorder: false,
block: false,
small: false,
regular: false,
verticalSpacing: undefined,
onChange() {},
onClearDate() {},
onFocus() {},
onKeyDownShiftTab() {},
onKeyDownTab() {},
onKeyDownArrowDown() {},
onKeyDownQuestionMark() {},
// i18n
phrases: SingleDatePickerInputPhrases,
};
function SingleDatePickerInput({
id,
children,
placeholder,
displayValue,
focused,
isFocused,
disabled,
required,
readOnly,
showCaret,
showClearDate,
showDefaultInputIcon,
inputIconPosition,
phrases,
onClearDate,
onChange,
onFocus,
onKeyDownShiftTab,
onKeyDownTab,
onKeyDownArrowDown,
onKeyDownQuestionMark,
screenReaderMessage,
customCloseIcon,
customInputIcon,
openDirection,
isRTL,
noBorder,
block,
small,
regular,
verticalSpacing,
styles,
}) {
const calendarIcon = customInputIcon || (
<CalendarIcon {...css(styles.SingleDatePickerInput_calendarIcon_svg)} />
);
const closeIcon = customCloseIcon || (
<CloseButton
{...css(
styles.SingleDatePickerInput_clearDate_svg,
small && styles.SingleDatePickerInput_clearDate_svg__small,
)}
/>
);
const screenReaderText = screenReaderMessage || phrases.keyboardNavigationInstructions;
const inputIcon = (showDefaultInputIcon || customInputIcon !== null) && (
<button
{...css(styles.SingleDatePickerInput_calendarIcon)}
type="button"
disabled={disabled}
aria-label={phrases.focusStartDate}
onClick={onFocus}
>
{calendarIcon}
</button>
);
return (
<div
{...css(
styles.SingleDatePickerInput,
disabled && styles.SingleDatePickerInput__disabled,
isRTL && styles.SingleDatePickerInput__rtl,
!noBorder && styles.SingleDatePickerInput__withBorder,
block && styles.SingleDatePickerInput__block,
showClearDate && styles.SingleDatePickerInput__showClearDate,
)}
>
{inputIconPosition === ICON_BEFORE_POSITION && inputIcon}
<DateInput
id={id}
placeholder={placeholder} // also used as label
displayValue={displayValue}
screenReaderMessage={screenReaderText}
focused={focused}
isFocused={isFocused}
disabled={disabled}
required={required}
readOnly={readOnly}
showCaret={showCaret}
onChange={onChange}
onFocus={onFocus}
onKeyDownShiftTab={onKeyDownShiftTab}
onKeyDownTab={onKeyDownTab}
onKeyDownArrowDown={onKeyDownArrowDown}
onKeyDownQuestionMark={onKeyDownQuestionMark}
openDirection={openDirection}
verticalSpacing={verticalSpacing}
small={small}
regular={regular}
block={block}
/>
{children}
{showClearDate && (
<button
{...css(
styles.SingleDatePickerInput_clearDate,
small && styles.SingleDatePickerInput_clearDate__small,
!customCloseIcon && styles.SingleDatePickerInput_clearDate__default,
!displayValue && styles.SingleDatePickerInput_clearDate__hide,
)}
type="button"
aria-label={phrases.clearDate}
disabled={disabled}
onClick={onClearDate}
>
{closeIcon}
</button>
)}
{inputIconPosition === ICON_AFTER_POSITION && inputIcon}
</div>
);
}
SingleDatePickerInput.propTypes = propTypes;
SingleDatePickerInput.defaultProps = defaultProps;
export default withStyles(({ reactDates: { border, color } }) => ({
SingleDatePickerInput: {
display: 'inline-block',
backgroundColor: color.background,
},
SingleDatePickerInput__withBorder: {
borderColor: color.border,
borderWidth: border.pickerInput.borderWidth,
borderStyle: border.pickerInput.borderStyle,
borderRadius: border.pickerInput.borderRadius,
},
SingleDatePickerInput__rtl: {
direction: noflip('rtl'),
},
SingleDatePickerInput__disabled: {
backgroundColor: color.disabled,
},
SingleDatePickerInput__block: {
display: 'block',
},
SingleDatePickerInput__showClearDate: {
paddingRight: 30, // TODO: should be noflip wrapped and handled by an isRTL prop
},
SingleDatePickerInput_clearDate: {
background: 'none',
border: 0,
color: 'inherit',
font: 'inherit',
lineHeight: 'normal',
overflow: 'visible',
cursor: 'pointer',
padding: 10,
margin: '0 10px 0 5px', // TODO: should be noflip wrapped and handled by an isRTL prop
position: 'absolute',
right: 0, // TODO: should be noflip wrapped and handled by an isRTL prop
top: '50%',
transform: 'translateY(-50%)',
},
SingleDatePickerInput_clearDate__default: {
':focus': {
background: color.core.border,
borderRadius: '50%',
},
':hover': {
background: color.core.border,
borderRadius: '50%',
},
},
SingleDatePickerInput_clearDate__small: {
padding: 6,
},
SingleDatePickerInput_clearDate__hide: {
visibility: 'hidden',
},
SingleDatePickerInput_clearDate_svg: {
fill: color.core.grayLight,
height: 12,
width: 15,
verticalAlign: 'middle',
},
SingleDatePickerInput_clearDate_svg__small: {
height: 9,
},
SingleDatePickerInput_calendarIcon: {
background: 'none',
border: 0,
color: 'inherit',
font: 'inherit',
lineHeight: 'normal',
overflow: 'visible',
cursor: 'pointer',
display: 'inline-block',
verticalAlign: 'middle',
padding: 10,
margin: '0 5px 0 10px', // TODO: should be noflip wrapped and handled by an isRTL prop
},
SingleDatePickerInput_calendarIcon_svg: {
fill: color.core.grayLight,
height: 15,
width: 14,
verticalAlign: 'middle',
},
}), { pureComponent: typeof React.PureComponent !== 'undefined' })(SingleDatePickerInput);