-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
128 lines (112 loc) · 3.72 KB
/
index.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
import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { Icon, Popover, PopoverTrigger, PopoverBody, TextField } from 'nr1';
import {
formattedDateField,
formattedMonthYear,
firstDayOfMonth,
lastDateInMonth,
extractDateParts,
selectedDate,
daysOfWeek,
isSelectableDate,
} from './utils';
const DAYS_OF_WEEK = daysOfWeek();
import styles from './styles.scss';
const DatePicker = ({ date, validFrom, onChange }) => {
const [opened, setOpened] = useState(false);
const [current, setCurrent] = useState(extractDateParts(new Date()));
useEffect(() => {
if (!date || !(date instanceof Date)) return;
setCurrent(extractDateParts(date));
}, [date]);
const prevMonth = () => {
const prevMo = new Date(current.yr, current.mo - 1);
setCurrent(extractDateParts(prevMo));
};
const nextMonth = () => {
const nextMo = new Date(current.yr, current.mo + 1);
setCurrent(extractDateParts(nextMo));
};
const isDateInCurrentMonth = useCallback(
(d = new Date()) =>
d.getFullYear() === current.yr && d.getMonth() === current.mo,
[current]
);
const clickHandler = useCallback(
(e, dt) => {
e.stopPropagation();
if (!isSelectableDate(current, dt + 1, validFrom) || !onChange) return;
const d = date instanceof Date ? new Date(date.getTime()) : new Date();
d.setFullYear(current.yr);
d.setMonth(current.mo);
d.setDate(dt + 1);
onChange(d);
setOpened(false);
},
[current, date, validFrom]
);
const changeHandler = useCallback((_, o) => setOpened(o), []);
return (
<Popover opened={opened} onChange={changeHandler}>
<PopoverTrigger>
<TextField
className="date-picker-text-field"
value={formattedDateField(date)}
placeholder="Select a date"
readOnly
/>
</PopoverTrigger>
<PopoverBody>
<div className={`${styles.calendar}`}>
<div className={`${styles.cell} ${styles.prev}`} onClick={prevMonth}>
<Icon type={Icon.TYPE.INTERFACE__CHEVRON__CHEVRON_LEFT} />
</div>
<div className={`${styles.cell} ${styles['mo-yr']}`}>
{formattedMonthYear(new Date(current.yr, current.mo))}
</div>
<div
className={`${styles.cell} ${
isDateInCurrentMonth() ? styles.disabled : styles.next
}`}
onClick={isDateInCurrentMonth() ? null : nextMonth}
>
<Icon type={Icon.TYPE.INTERFACE__CHEVRON__CHEVRON_RIGHT} />
</div>
{DAYS_OF_WEEK.map(({ long, short }) => (
<div className={`${styles.cell} ${styles.day}`} key={long}>
<abbr title={long}>{short}</abbr>
</div>
))}
{Array.from({ length: firstDayOfMonth(current) }, (_, i) => (
<div
key={`${current.yr}${current.mo}blank${i}`}
className={`${styles.cell}`}
/>
))}
{Array.from({ length: lastDateInMonth(current) }, (_, i) => (
<div
key={`${current.yr}${current.mo}${i}`}
className={`${styles.cell} ${styles.date} ${
selectedDate(i, current, date) ? styles.selected : ''
} ${
!isSelectableDate(current, i + 1, validFrom)
? styles.disabled
: ''
}`}
onClick={(e) => clickHandler(e, i)}
>
{i + 1}
</div>
))}
</div>
</PopoverBody>
</Popover>
);
};
DatePicker.propTypes = {
date: PropTypes.instanceOf(Date),
validFrom: PropTypes.instanceOf(Date),
onChange: PropTypes.func,
};
export default DatePicker;