/
FieldDateRangeInput.js
192 lines (172 loc) · 6.02 KB
/
FieldDateRangeInput.js
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
/**
* Provides a date picker for Final Forms (using https://github.com/airbnb/react-dates)
*
* NOTE: If you are using this component inside BookingDatesForm,
* you should convert value.date to start date and end date before submitting it to API
*/
import React, { Component } from 'react';
import { bool, func, object, oneOf, string, arrayOf } from 'prop-types';
import { Field } from 'react-final-form';
import classNames from 'classnames';
import { START_DATE, END_DATE } from '../../util/dates';
import { propTypes } from '../../util/types';
import { ValidationError } from '../../components';
import DateRangeInput from './DateRangeInput';
import css from './FieldDateRangeInput.css';
const MAX_MOBILE_SCREEN_WIDTH = 768;
class FieldDateRangeInputComponent extends Component {
constructor(props) {
super(props);
this.state = { focusedInput: null };
this.handleBlur = this.handleBlur.bind(this);
this.handleFocus = this.handleFocus.bind(this);
}
componentDidUpdate(prevProps) {
// Update focusedInput in case a new value for it is
// passed in the props. This may occur if the focus
// is manually set to the date picker.
if (this.props.focusedInput && this.props.focusedInput !== prevProps.focusedInput) {
this.setState({ focusedInput: this.props.focusedInput });
}
}
handleBlur(focusedInput) {
this.setState({ focusedInput: null });
this.props.input.onBlur(focusedInput);
// Notify the containing component that the focused
// input has changed.
if (this.props.onFocusedInputChange) {
this.props.onFocusedInputChange(null);
}
}
handleFocus(focusedInput) {
this.setState({ focusedInput });
this.props.input.onFocus(focusedInput);
}
render() {
/* eslint-disable no-unused-vars */
const {
className,
rootClassName,
unitType,
startDateId,
startDateLabel,
endDateId,
endDateLabel,
input,
meta,
useMobileMargins,
// Extract focusedInput and onFocusedInputChange so that
// the same values will not be passed on to subcomponents.
focusedInput,
onFocusedInputChange,
...rest
} = this.props;
/* eslint-disable no-unused-vars */
if (startDateLabel && !startDateId) {
throw new Error('startDateId required when a startDateLabel is given');
}
if (endDateLabel && !endDateId) {
throw new Error('endDateId required when a endDateLabel is given');
}
const { touched, error } = meta;
const value = input.value;
// If startDate is valid label changes color and bottom border changes color too
const startDateIsValid = value && value.startDate instanceof Date;
const startDateLabelClasses = classNames(css.startDateLabel, {
[css.labelSuccess]: false, //startDateIsValid,
});
const startDateBorderClasses = classNames(css.input, {
[css.inputSuccess]: startDateIsValid,
[css.inputError]: touched && !startDateIsValid && typeof error === 'string',
[css.hover]: this.state.focusedInput === START_DATE,
});
// If endDate is valid label changes color and bottom border changes color too
const endDateIsValid = value && value.endDate instanceof Date;
const endDateLabelClasses = classNames(css.endDateLabel, {
[css.labelSuccess]: false, //endDateIsValid,
});
const endDateBorderClasses = classNames(css.input, {
[css.inputSuccess]: endDateIsValid,
[css.inputError]: touched && !endDateIsValid && typeof error === 'string',
[css.hover]: this.state.focusedInput === END_DATE,
});
const label =
startDateLabel && endDateLabel ? (
<div className={classNames(css.labels, { [css.mobileMargins]: useMobileMargins })}>
<label className={startDateLabelClasses} htmlFor={startDateId}>
{startDateLabel}
</label>
<label className={endDateLabelClasses} htmlFor={endDateId}>
{endDateLabel}
</label>
</div>
) : null;
// eslint-disable-next-line no-unused-vars
const { onBlur, onFocus, type, ...restOfInput } = input;
const inputProps = {
unitType,
onBlur: this.handleBlur,
onFocus: this.handleFocus,
useMobileMargins,
readOnly: typeof window !== 'undefined' && window.innerWidth < MAX_MOBILE_SCREEN_WIDTH,
...restOfInput,
...rest,
focusedInput: this.state.focusedInput,
startDateId,
endDateId,
};
const classes = classNames(rootClassName || css.fieldRoot, className);
const errorClasses = classNames({ [css.mobileMargins]: useMobileMargins });
return (
<div className={classes}>
{label}
<DateRangeInput {...inputProps} />
<div
className={classNames(css.inputBorders, {
[css.mobileMargins]: useMobileMargins && !this.state.focusedInput,
})}
>
<div className={startDateBorderClasses} />
<div className={endDateBorderClasses} />
</div>
<ValidationError className={errorClasses} fieldMeta={meta} />
</div>
);
}
}
FieldDateRangeInputComponent.defaultProps = {
className: null,
rootClassName: null,
useMobileMargins: false,
endDateId: null,
endDateLabel: null,
endDatePlaceholderText: null,
startDateId: null,
startDateLabel: null,
startDatePlaceholderText: null,
focusedInput: null,
onFocusedInputChange: null,
timeSlots: null,
};
FieldDateRangeInputComponent.propTypes = {
className: string,
rootClassName: string,
unitType: propTypes.bookingUnitType.isRequired,
useMobileMargins: bool,
endDateId: string,
endDateLabel: string,
endDatePlaceholderText: string,
startDateId: string,
startDateLabel: string,
startDatePlaceholderText: string,
timeSlots: arrayOf(propTypes.timeSlot),
input: object.isRequired,
meta: object.isRequired,
focusedInput: oneOf([START_DATE, END_DATE]),
onFocusedInputChange: func,
};
const FieldDateRangeInput = props => {
return <Field component={FieldDateRangeInputComponent} {...props} />;
};
export { DateRangeInput };
export default FieldDateRangeInput;