-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathDateNavigation.tsx
158 lines (146 loc) · 3.74 KB
/
DateNavigation.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
/**
* SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/
import Button from '@clayui/button';
import {Option, Picker} from '@clayui/core';
import Icon from '@clayui/icon';
import {Keys} from '@clayui/shared';
import React from 'react';
import {setMonth} from './Helpers';
import Select, {ISelectOption} from './Select';
import {IAriaLabels} from './types';
type Props = {
ariaLabels: IAriaLabels;
currentMonth: Date;
disabled?: boolean;
months: Array<ISelectOption>;
onDotClicked: () => void;
onMonthChange: (date: Date) => void;
/**
* Path to the location of the spritemap resource.
*/
spritemap?: string;
years: Array<ISelectOption>;
};
const ClayDatePickerDateNavigation = ({
ariaLabels,
currentMonth,
disabled,
months,
onDotClicked,
onMonthChange,
spritemap,
years,
}: Props) => {
/**
* Handles the change of the month from the available
* years in the range
*/
function handleChangeMonth(month: number) {
const date = setMonth(years, month, currentMonth);
if (date) {
onMonthChange(date);
}
}
/**
* Handles the previous month button
*/
const handlePreviousMonthClicked = () => handleChangeMonth(-1);
/**
* Handles the next month button
*/
const handleNextMonthClicked = () => handleChangeMonth(1);
return (
<div className="date-picker-calendar-header">
<div className="date-picker-nav">
<div className="date-picker-nav-item input-date-picker-month">
<Select
disabled={disabled}
name="month"
onChange={(event) =>
onMonthChange(
new Date(
currentMonth.getFullYear(),
Number(event.target.value)
)
)
}
options={months}
testId="month-select"
value={currentMonth.getMonth()}
/>
</div>
<div className="date-picker-nav-item input-date-picker-year">
<Picker
UNSAFE_behavior="secondary"
className="form-control-sm"
data-testid="year-select"
disabled={disabled}
items={years}
native
onKeyDown={(
event: React.KeyboardEvent<HTMLButtonElement>
) => {
if (
event.shiftKey &&
(event.key === Keys.Up ||
event.key === Keys.Down)
) {
event.key =
event.key === Keys.Up
? 'PageUp'
: 'PageDown';
}
}}
onSelectionChange={(key: React.Key) =>
onMonthChange(
new Date(Number(key), currentMonth.getMonth())
)
}
selectedKey={String(currentMonth.getFullYear())}
>
{(item) => (
<Option key={item.value}>
{String(item.label)}
</Option>
)}
</Picker>
</div>
<div className="date-picker-nav-controls date-picker-nav-item date-picker-nav-item-expand">
<Button
aria-label={ariaLabels.buttonPreviousMonth}
className="nav-btn nav-btn-monospaced"
disabled={disabled}
displayType={null}
onClick={handlePreviousMonthClicked}
title={ariaLabels.buttonPreviousMonth}
>
<Icon spritemap={spritemap} symbol="angle-left" />
</Button>
<Button
aria-label={ariaLabels.buttonDot}
className="nav-btn nav-btn-monospaced"
disabled={disabled}
displayType={null}
onClick={onDotClicked}
title={ariaLabels.buttonDot}
>
<Icon spritemap={spritemap} symbol="simple-circle" />
</Button>
<Button
aria-label={ariaLabels.buttonNextMonth}
className="nav-btn nav-btn-monospaced"
disabled={disabled}
displayType={null}
onClick={handleNextMonthClicked}
title={ariaLabels.buttonNextMonth}
>
<Icon spritemap={spritemap} symbol="angle-right" />
</Button>
</div>
</div>
</div>
);
};
export default ClayDatePickerDateNavigation;