/
useInput.ts
183 lines (163 loc) · 5.07 KB
/
useInput.ts
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
import {
ChangeEventHandler,
FocusEventHandler,
InputHTMLAttributes,
useState
} from 'react';
import { differenceInCalendarDays, format as _format, parse } from 'date-fns';
import { enUS } from 'date-fns/locale';
import { parseFromToProps } from 'contexts/DayPicker/utils';
import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerSingleProps } from 'types/DayPickerSingle';
import {
DayClickEventHandler,
MonthChangeEventHandler
} from 'types/EventHandlers';
import { isValidDate } from './utils/isValidDate';
/** The props to attach to the input field when using {@link useInput}. */
export type InputProps = Pick<
InputHTMLAttributes<HTMLInputElement>,
'onBlur' | 'onChange' | 'onFocus' | 'value' | 'placeholder'
>;
/** The props to attach to the DayPicker component when using {@link useInput}. */
export type InputDayPickerProps = Pick<
DayPickerSingleProps,
| 'fromDate'
| 'toDate'
| 'locale'
| 'month'
| 'onDayClick'
| 'onMonthChange'
| 'selected'
| 'today'
>;
export interface UseInputOptions
extends Pick<
DayPickerBase,
| 'locale'
| 'fromDate'
| 'toDate'
| 'fromMonth'
| 'toMonth'
| 'fromYear'
| 'toYear'
| 'today'
> {
/** The initially selected date */
defaultSelected?: Date;
/**
* The format string for formatting the input field. See
* https://date-fns.org/docs/format for a list of format strings.
*
* @defaultValue PP
*/
format?: string;
/** Make the selection required. */
required?: boolean;
}
/** Represent the value returned by {@link useInput}. */
export interface UseInputValue {
/** The props to pass to a DayPicker component. */
dayPickerProps: InputDayPickerProps;
/** The props to pass to an input field. */
inputProps: InputProps;
/** A function to reset to the initial state. */
reset: () => void;
/** A function to set the selected day. */
setSelected: (day: Date | undefined) => void;
}
/** Return props and setters for binding an input field to DayPicker. */
export function useInput(options: UseInputOptions = {}): UseInputValue {
const {
locale = enUS,
required,
format = 'PP',
defaultSelected,
today = new Date()
} = options;
const { fromDate, toDate } = parseFromToProps(options);
// Shortcut to the DateFns functions
const parseValue = (value: string) => parse(value, format, today, { locale });
// Initialize states
const [month, setMonth] = useState(defaultSelected ?? today);
const [selectedDay, setSelectedDay] = useState(defaultSelected);
const defaultInputValue = defaultSelected
? _format(defaultSelected, format, { locale })
: '';
const [inputValue, setInputValue] = useState(defaultInputValue);
const reset = () => {
setSelectedDay(defaultSelected);
setMonth(defaultSelected ?? today);
setInputValue(defaultInputValue ?? '');
};
const setSelected = (date: Date | undefined) => {
setSelectedDay(date);
setMonth(date ?? today);
setInputValue(date ? _format(date, format, { locale }) : '');
};
const handleDayClick: DayClickEventHandler = (day, { selected }) => {
if (!required && selected) {
setSelectedDay(undefined);
setInputValue('');
return;
}
setSelectedDay(day);
setInputValue(day ? _format(day, format, { locale }) : '');
};
const handleMonthChange: MonthChangeEventHandler = (month) => {
setMonth(month);
};
// When changing the input field, save its value in state and check if the
// string is a valid date. If it is a valid day, set it as selected and update
// the calendar’s month.
const handleChange: ChangeEventHandler<HTMLInputElement> = (e) => {
setInputValue(e.target.value);
const day = parseValue(e.target.value);
const isBefore = fromDate && differenceInCalendarDays(fromDate, day) > 0;
const isAfter = toDate && differenceInCalendarDays(day, toDate) > 0;
if (!isValidDate(day) || isBefore || isAfter) {
setSelectedDay(undefined);
return;
}
setSelectedDay(day);
setMonth(day);
};
// Special case for _required_ fields: on blur, if the value of the input is not
// a valid date, reset the calendar and the input value.
const handleBlur: FocusEventHandler<HTMLInputElement> = (e) => {
const day = parseValue(e.target.value);
if (!isValidDate(day)) {
reset();
}
};
// When focusing, make sure DayPicker visualizes the month of the date in the
// input field.
const handleFocus: FocusEventHandler<HTMLInputElement> = (e) => {
if (!e.target.value) {
reset();
return;
}
const day = parseValue(e.target.value);
if (isValidDate(day)) {
setMonth(day);
}
};
const dayPickerProps: InputDayPickerProps = {
month: month,
onDayClick: handleDayClick,
onMonthChange: handleMonthChange,
selected: selectedDay,
locale,
fromDate,
toDate,
today
};
const inputProps: InputProps = {
onBlur: handleBlur,
onChange: handleChange,
onFocus: handleFocus,
value: inputValue,
placeholder: _format(new Date(), format, { locale })
};
return { dayPickerProps, inputProps, reset, setSelected };
}