@primer/components 0.0.0-20219293614 → 0.0.0-2021931194230
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -7
- package/dist/browser.esm.js +439 -436
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +443 -440
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/Item.d.ts +0 -6
- package/lib/ActionList/Item.js +1 -5
- package/lib/ActionMenu.js +2 -2
- package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
- package/lib/Autocomplete/Autocomplete.d.ts +13 -11
- package/lib/Autocomplete/AutocompleteInput.d.ts +13 -11
- package/lib/Button/Button.d.ts +11 -11
- package/lib/Button/ButtonBase.d.ts +1 -1
- package/lib/Button/ButtonClose.d.ts +29 -29
- package/lib/Button/ButtonDanger.d.ts +11 -11
- package/lib/Button/ButtonInvisible.d.ts +11 -11
- package/lib/Button/ButtonOutline.d.ts +11 -11
- package/lib/Button/ButtonPrimary.d.ts +11 -11
- package/lib/Button/ButtonStyles.js +1 -1
- package/lib/CircleBadge.d.ts +2 -2
- package/lib/CircleOcticon.d.ts +33 -33
- package/lib/DatePicker/DatePicker.d.ts +48 -0
- package/lib/DatePicker/DatePicker.js +106 -0
- package/lib/DatePicker/DatePickerAnchor.d.ts +5 -0
- package/lib/DatePicker/DatePickerAnchor.js +223 -0
- package/lib/DatePicker/DatePickerOverlay.d.ts +3 -0
- package/lib/DatePicker/DatePickerOverlay.js +48 -0
- package/lib/DatePicker/DatePickerPanel.d.ts +2 -0
- package/lib/DatePicker/DatePickerPanel.js +143 -0
- package/lib/DatePicker/Day.d.ts +14 -0
- package/lib/DatePicker/Day.js +192 -0
- package/lib/DatePicker/Month.d.ts +9 -0
- package/lib/DatePicker/Month.js +122 -0
- package/lib/DatePicker/dateParser.d.ts +11 -0
- package/lib/DatePicker/dateParser.js +192 -0
- package/lib/DatePicker/index.d.ts +2 -0
- package/lib/DatePicker/index.js +13 -0
- package/lib/DatePicker/useDatePicker.d.ts +89 -0
- package/lib/DatePicker/useDatePicker.js +439 -0
- package/lib/Dialog/Dialog.d.ts +4 -4
- package/lib/Dialog/Dialog.js +22 -12
- package/lib/Dialog.d.ts +35 -35
- package/lib/Dropdown.d.ts +165 -85
- package/lib/Dropdown.js +5 -6
- package/lib/DropdownMenu/DropdownButton.d.ts +30 -30
- package/lib/FilterList.d.ts +26 -26
- package/lib/Label.d.ts +1 -1
- package/lib/Position.d.ts +4 -4
- package/lib/ProgressBar.d.ts +1 -1
- package/lib/SelectMenu/SelectMenu.d.ts +153 -151
- package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib/TextInputWithTokens.d.ts +13 -11
- package/lib/Timeline.d.ts +33 -33
- package/lib/Token/AvatarToken.d.ts +1 -1
- package/lib/Token/IssueLabelToken.d.ts +1 -1
- package/lib/Token/Token.d.ts +1 -1
- package/lib/_TextInputWrapper.d.ts +1 -1
- package/lib/constants.js +1 -3
- package/lib/hooks/useDebounce.d.ts +2 -0
- package/lib/hooks/useDebounce.js +24 -0
- package/lib/hooks/useResizeObserver.d.ts +1 -1
- package/lib/hooks/useResizeObserver.js +1 -1
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -8
- package/lib/theme-preval.js +2 -2
- package/lib/utils/testing.d.ts +1 -1
- package/lib-esm/ActionList/Item.d.ts +0 -6
- package/lib-esm/ActionList/Item.js +1 -5
- package/lib-esm/ActionMenu.js +2 -2
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
- package/lib-esm/Autocomplete/Autocomplete.d.ts +13 -11
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +13 -11
- package/lib-esm/Button/Button.d.ts +11 -11
- package/lib-esm/Button/ButtonBase.d.ts +1 -1
- package/lib-esm/Button/ButtonClose.d.ts +29 -29
- package/lib-esm/Button/ButtonDanger.d.ts +11 -11
- package/lib-esm/Button/ButtonInvisible.d.ts +11 -11
- package/lib-esm/Button/ButtonOutline.d.ts +11 -11
- package/lib-esm/Button/ButtonPrimary.d.ts +11 -11
- package/lib-esm/Button/ButtonStyles.js +1 -1
- package/lib-esm/CircleBadge.d.ts +2 -2
- package/lib-esm/CircleOcticon.d.ts +33 -33
- package/lib-esm/DatePicker/DatePicker.d.ts +48 -0
- package/lib-esm/DatePicker/DatePicker.js +89 -0
- package/lib-esm/DatePicker/DatePickerAnchor.d.ts +5 -0
- package/lib-esm/DatePicker/DatePickerAnchor.js +196 -0
- package/lib-esm/DatePicker/DatePickerOverlay.d.ts +3 -0
- package/lib-esm/DatePicker/DatePickerOverlay.js +29 -0
- package/lib-esm/DatePicker/DatePickerPanel.d.ts +2 -0
- package/lib-esm/DatePicker/DatePickerPanel.js +116 -0
- package/lib-esm/DatePicker/Day.d.ts +14 -0
- package/lib-esm/DatePicker/Day.js +169 -0
- package/lib-esm/DatePicker/Month.d.ts +9 -0
- package/lib-esm/DatePicker/Month.js +98 -0
- package/lib-esm/DatePicker/dateParser.d.ts +11 -0
- package/lib-esm/DatePicker/dateParser.js +178 -0
- package/lib-esm/DatePicker/index.d.ts +2 -0
- package/lib-esm/DatePicker/index.js +1 -0
- package/lib-esm/DatePicker/useDatePicker.d.ts +89 -0
- package/lib-esm/DatePicker/useDatePicker.js +408 -0
- package/lib-esm/Dialog/Dialog.d.ts +4 -4
- package/lib-esm/Dialog/Dialog.js +21 -12
- package/lib-esm/Dialog.d.ts +35 -35
- package/lib-esm/Dropdown.d.ts +165 -85
- package/lib-esm/Dropdown.js +3 -4
- package/lib-esm/DropdownMenu/DropdownButton.d.ts +30 -30
- package/lib-esm/FilterList.d.ts +26 -26
- package/lib-esm/Label.d.ts +1 -1
- package/lib-esm/Position.d.ts +4 -4
- package/lib-esm/ProgressBar.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenu.d.ts +153 -151
- package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
- package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
- package/lib-esm/TextInputWithTokens.d.ts +13 -11
- package/lib-esm/Timeline.d.ts +33 -33
- package/lib-esm/Token/AvatarToken.d.ts +1 -1
- package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
- package/lib-esm/Token/Token.d.ts +1 -1
- package/lib-esm/_TextInputWrapper.d.ts +1 -1
- package/lib-esm/constants.js +1 -3
- package/lib-esm/hooks/useDebounce.d.ts +2 -0
- package/lib-esm/hooks/useDebounce.js +16 -0
- package/lib-esm/hooks/useResizeObserver.d.ts +1 -1
- package/lib-esm/hooks/useResizeObserver.js +1 -1
- package/lib-esm/index.d.ts +0 -2
- package/lib-esm/index.js +0 -1
- package/lib-esm/theme-preval.js +2 -2
- package/lib-esm/utils/testing.d.ts +1 -1
- package/package.json +14 -13
- package/lib/NewButton/button.d.ts +0 -579
- package/lib/NewButton/button.js +0 -298
- package/lib/NewButton/counter.d.ts +0 -6
- package/lib/NewButton/counter.js +0 -33
- package/lib/NewButton/index.d.ts +0 -4
- package/lib/NewButton/index.js +0 -21
- package/lib-esm/NewButton/button.d.ts +0 -579
- package/lib-esm/NewButton/button.js +0 -274
- package/lib-esm/NewButton/counter.d.ts +0 -6
- package/lib-esm/NewButton/counter.js +0 -21
- package/lib-esm/NewButton/index.d.ts +0 -4
- package/lib-esm/NewButton/index.js +0 -3
@@ -0,0 +1,408 @@
|
|
1
|
+
import { CheckIcon, TrashIcon } from '@primer/octicons-react';
|
2
|
+
import { isEqual, isAfter, isBefore, addMonths, subMonths, isToday, isWeekend, differenceInDays, addDays, subDays } from 'date-fns';
|
3
|
+
import deepmerge from 'deepmerge';
|
4
|
+
import React, { createContext, useCallback, useContext, useMemo, useEffect, useState } from 'react';
|
5
|
+
import { Text, useConfirm } from '..';
|
6
|
+
import { formatDate } from './dateParser';
|
7
|
+
const DatePickerContext = /*#__PURE__*/createContext(null);
|
8
|
+
|
9
|
+
const useDatePicker = date => {
|
10
|
+
const value = useContext(DatePickerContext);
|
11
|
+
const [selected, setSelected] = useState(false);
|
12
|
+
const today = date ? isToday(date) : false;
|
13
|
+
|
14
|
+
if (!value) {
|
15
|
+
throw new Error('useDatePicker must be used inside a DatePickerProvider');
|
16
|
+
}
|
17
|
+
|
18
|
+
useEffect(() => {
|
19
|
+
if (date) {
|
20
|
+
if (value.hoverRange) {
|
21
|
+
if (isRangeSelection(value.hoverRange)) {
|
22
|
+
if (isEqual(date, value.hoverRange.from)) {
|
23
|
+
setSelected('start');
|
24
|
+
} else if (value.hoverRange.to && isEqual(date, value.hoverRange.to)) {
|
25
|
+
setSelected('end');
|
26
|
+
} else if (isAfter(date, value.hoverRange.from) && value.hoverRange.to && isBefore(date, value.hoverRange.to)) {
|
27
|
+
setSelected('middle');
|
28
|
+
} else {
|
29
|
+
setSelected(false);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
} else if (value.selection) {
|
33
|
+
if (isMultiSelection(value.selection)) {
|
34
|
+
setSelected(!!value.selection.find(d => isEqual(d, date)));
|
35
|
+
} else if (isRangeSelection(value.selection)) {
|
36
|
+
if (isEqual(date, value.selection.from)) {
|
37
|
+
setSelected('start');
|
38
|
+
} else if (value.selection.to && isEqual(date, value.selection.to)) {
|
39
|
+
setSelected('end');
|
40
|
+
} else if (isAfter(date, value.selection.from) && value.selection.to && isBefore(date, value.selection.to)) {
|
41
|
+
setSelected('middle');
|
42
|
+
} else {
|
43
|
+
setSelected(false);
|
44
|
+
}
|
45
|
+
} else {
|
46
|
+
setSelected(isEqual(date, value.selection));
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}, [date, value.hoverRange, value.selection, today]);
|
51
|
+
let blocked,
|
52
|
+
disabled = false;
|
53
|
+
|
54
|
+
if (date) {
|
55
|
+
// Determine if date is blocked out
|
56
|
+
if (value.configuration.blockedDates) {
|
57
|
+
blocked = !!value.configuration.blockedDates.find(d => isEqual(d, date));
|
58
|
+
} // Determine if date is disabled
|
59
|
+
|
60
|
+
|
61
|
+
if (value.configuration.minDate || value.configuration.maxDate || value.configuration.disableWeekends) {
|
62
|
+
disabled = (value.configuration.minDate ? isBefore(date, value.configuration.minDate) : false) || (value.configuration.maxDate ? isAfter(date, value.configuration.maxDate) : false) || (value.configuration.disableWeekends ? isWeekend(date) : false);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
return { ...value,
|
67
|
+
blocked,
|
68
|
+
disabled,
|
69
|
+
selected,
|
70
|
+
today
|
71
|
+
};
|
72
|
+
};
|
73
|
+
|
74
|
+
export default useDatePicker;
|
75
|
+
export function isSingleSelection(selection) {
|
76
|
+
return selection instanceof Date;
|
77
|
+
}
|
78
|
+
export function isMultiSelection(selection) {
|
79
|
+
return Array.isArray(selection);
|
80
|
+
}
|
81
|
+
export function isRangeSelection(selection) {
|
82
|
+
return !!selection.from;
|
83
|
+
}
|
84
|
+
export function isStringRangeSelection(selection) {
|
85
|
+
return !!selection.from;
|
86
|
+
}
|
87
|
+
|
88
|
+
function parseSelection(selection, variant) {
|
89
|
+
if (!selection) return;
|
90
|
+
|
91
|
+
if (variant === 'multi') {
|
92
|
+
if (isMultiSelection(selection)) {
|
93
|
+
const parsedSelection = [];
|
94
|
+
|
95
|
+
for (const d of selection) {
|
96
|
+
parsedSelection.push(new Date(new Date(d).toDateString()));
|
97
|
+
}
|
98
|
+
|
99
|
+
return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
|
100
|
+
} else if (selection instanceof Date) {
|
101
|
+
return [new Date(new Date(selection).toDateString())];
|
102
|
+
} else if (isRangeSelection(selection)) {
|
103
|
+
const parsedSelection = [];
|
104
|
+
parsedSelection.push(new Date(new Date(selection.from).toDateString()));
|
105
|
+
|
106
|
+
if (selection.to) {
|
107
|
+
parsedSelection.push(new Date(new Date(selection.to).toDateString()));
|
108
|
+
}
|
109
|
+
|
110
|
+
return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
|
111
|
+
}
|
112
|
+
} else if (variant === 'range') {
|
113
|
+
if (isRangeSelection(selection)) {
|
114
|
+
return {
|
115
|
+
from: new Date(new Date(selection.from).toDateString()),
|
116
|
+
to: selection.to ? new Date(new Date(selection.to).toDateString()) : null
|
117
|
+
};
|
118
|
+
} else if (isMultiSelection(selection)) {
|
119
|
+
return {
|
120
|
+
from: new Date(new Date(selection[0]).toDateString()),
|
121
|
+
to: selection[1] ? new Date(new Date(selection[1]).toDateString()) : null
|
122
|
+
};
|
123
|
+
} else if (selection instanceof Date) {
|
124
|
+
return {
|
125
|
+
from: new Date(new Date(selection).toDateString()),
|
126
|
+
to: null
|
127
|
+
};
|
128
|
+
}
|
129
|
+
} else {
|
130
|
+
if (selection instanceof Date) {
|
131
|
+
return new Date(new Date(selection).toDateString());
|
132
|
+
} else if (isMultiSelection(selection)) {
|
133
|
+
return new Date(new Date(selection[0]).toDateString());
|
134
|
+
} else if (isRangeSelection(selection)) {
|
135
|
+
return new Date(new Date(selection.from).toDateString());
|
136
|
+
} else {
|
137
|
+
return;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
const defaultConfiguration = {
|
143
|
+
anchorVariant: 'button',
|
144
|
+
confirmation: false,
|
145
|
+
confirmUnsavedClose: false,
|
146
|
+
disableWeekends: false,
|
147
|
+
iconPlacement: 'start',
|
148
|
+
placeholder: 'Select a Date...',
|
149
|
+
showInputPrompt: false,
|
150
|
+
variant: 'single',
|
151
|
+
view: '2-month',
|
152
|
+
weekStartsOn: 'Sunday'
|
153
|
+
};
|
154
|
+
export const DatePickerProvider = ({
|
155
|
+
configuration: externalConfig = {},
|
156
|
+
children,
|
157
|
+
closePicker,
|
158
|
+
value
|
159
|
+
}) => {
|
160
|
+
const [configuration, setConfiguration] = useState(deepmerge(defaultConfiguration, externalConfig));
|
161
|
+
const [previousSelection, setPreviousSelection] = useState(parseSelection(value, configuration.variant));
|
162
|
+
const [isDirty, setIsDirty] = useState(false);
|
163
|
+
const [selection, setSelection] = useState(parseSelection(value, configuration.variant));
|
164
|
+
const [hoverRange, setHoverRange] = useState(null);
|
165
|
+
const [currentViewingDate, setCurrentViewingDate] = useState(new Date());
|
166
|
+
const confirm = useConfirm();
|
167
|
+
useEffect(() => {
|
168
|
+
setConfiguration(deepmerge(defaultConfiguration, externalConfig));
|
169
|
+
setSelection(parseSelection(selection, configuration.variant)); // Don't want this to run every time selection gets updated
|
170
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
171
|
+
}, [configuration.variant, externalConfig]);
|
172
|
+
const goToMonth = useCallback(date => {
|
173
|
+
setCurrentViewingDate(new Date(new Date(date).toDateString()));
|
174
|
+
}, []);
|
175
|
+
const nextMonth = useCallback(() => {
|
176
|
+
setCurrentViewingDate(addMonths(currentViewingDate, 1));
|
177
|
+
}, [currentViewingDate]);
|
178
|
+
const previousMonth = useCallback(() => {
|
179
|
+
setCurrentViewingDate(subMonths(currentViewingDate, 1));
|
180
|
+
}, [currentViewingDate]);
|
181
|
+
const getFormattedDate = useMemo(() => {
|
182
|
+
const {
|
183
|
+
anchorVariant,
|
184
|
+
dateFormat,
|
185
|
+
placeholder,
|
186
|
+
variant
|
187
|
+
} = configuration;
|
188
|
+
return formatDate({
|
189
|
+
selection,
|
190
|
+
anchorVariant,
|
191
|
+
dateFormat,
|
192
|
+
placeholder,
|
193
|
+
variant
|
194
|
+
});
|
195
|
+
}, [configuration, selection]);
|
196
|
+
const saveValue = useCallback(updatedSelection => {
|
197
|
+
setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
|
198
|
+
setIsDirty(false);
|
199
|
+
closePicker === null || closePicker === void 0 ? void 0 : closePicker();
|
200
|
+
}, [closePicker, selection]);
|
201
|
+
const revertValue = useCallback(() => {
|
202
|
+
setSelection(previousSelection);
|
203
|
+
setIsDirty(false);
|
204
|
+
}, [previousSelection]);
|
205
|
+
const handleClose = useCallback(async () => {
|
206
|
+
if (configuration.confirmUnsavedClose) {
|
207
|
+
if (isDirty) {
|
208
|
+
const result = await confirm({
|
209
|
+
title: 'Save Changes?',
|
210
|
+
content: 'You have unsaved changes, would you like to save them?',
|
211
|
+
confirmButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CheckIcon, null), /*#__PURE__*/React.createElement(Text, {
|
212
|
+
sx: {
|
213
|
+
ml: 1
|
214
|
+
}
|
215
|
+
}, "Save")),
|
216
|
+
cancelButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TrashIcon, null), /*#__PURE__*/React.createElement(Text, {
|
217
|
+
sx: {
|
218
|
+
ml: 1
|
219
|
+
}
|
220
|
+
}, "Discard"))
|
221
|
+
});
|
222
|
+
|
223
|
+
if (result) {
|
224
|
+
saveValue();
|
225
|
+
} else {
|
226
|
+
revertValue();
|
227
|
+
}
|
228
|
+
}
|
229
|
+
} else if (isDirty) revertValue();
|
230
|
+
}, [configuration.confirmUnsavedClose, confirm, isDirty, revertValue, saveValue]);
|
231
|
+
const inputHandler = useCallback(updatedSelection => {
|
232
|
+
if (!updatedSelection) return;
|
233
|
+
const {
|
234
|
+
maxDate,
|
235
|
+
minDate,
|
236
|
+
variant,
|
237
|
+
maxSelections,
|
238
|
+
maxRangeSize
|
239
|
+
} = configuration;
|
240
|
+
|
241
|
+
switch (variant) {
|
242
|
+
case 'single':
|
243
|
+
{
|
244
|
+
if (updatedSelection instanceof Date) {
|
245
|
+
if (maxDate && isAfter(updatedSelection, maxDate)) {
|
246
|
+
setSelection(maxDate);
|
247
|
+
} else if (minDate && isBefore(minDate, updatedSelection)) {
|
248
|
+
setSelection(minDate);
|
249
|
+
} else {
|
250
|
+
setSelection(updatedSelection);
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
break;
|
255
|
+
}
|
256
|
+
|
257
|
+
case 'multi':
|
258
|
+
{
|
259
|
+
if (Array.isArray(updatedSelection)) {
|
260
|
+
let validSelections = updatedSelection.filter(d => (maxDate ? isBefore(d, maxDate) : true) && (minDate ? isAfter(d, minDate) : true));
|
261
|
+
|
262
|
+
if (maxSelections) {
|
263
|
+
validSelections = validSelections.slice(0, maxSelections);
|
264
|
+
}
|
265
|
+
|
266
|
+
setSelection(validSelections);
|
267
|
+
}
|
268
|
+
|
269
|
+
break;
|
270
|
+
}
|
271
|
+
|
272
|
+
case 'range':
|
273
|
+
{
|
274
|
+
if (isRangeSelection(updatedSelection)) {
|
275
|
+
const validRange = updatedSelection;
|
276
|
+
|
277
|
+
if (minDate) {
|
278
|
+
validRange.from = isAfter(updatedSelection.from, minDate) ? updatedSelection.from : minDate;
|
279
|
+
|
280
|
+
if (updatedSelection.to) {
|
281
|
+
validRange.to = isAfter(updatedSelection.to, minDate) ? updatedSelection.to : minDate;
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
if (maxDate) {
|
286
|
+
validRange.from = isBefore(updatedSelection.from, maxDate) ? updatedSelection.from : maxDate;
|
287
|
+
|
288
|
+
if (updatedSelection.to) {
|
289
|
+
validRange.to = isBefore(updatedSelection.to, maxDate) ? updatedSelection.to : maxDate;
|
290
|
+
}
|
291
|
+
}
|
292
|
+
|
293
|
+
if (maxRangeSize && validRange.to && Math.abs(differenceInDays(validRange.from, validRange.to)) >= maxRangeSize) {
|
294
|
+
validRange.to = addDays(validRange.from, maxRangeSize - 1);
|
295
|
+
}
|
296
|
+
|
297
|
+
setSelection(updatedSelection);
|
298
|
+
}
|
299
|
+
|
300
|
+
break;
|
301
|
+
}
|
302
|
+
}
|
303
|
+
}, [configuration]);
|
304
|
+
const selectionHandler = useCallback(date => {
|
305
|
+
setIsDirty(true);
|
306
|
+
|
307
|
+
if (configuration.variant === 'multi') {
|
308
|
+
const selections = [...selection];
|
309
|
+
const existingIndex = selections.findIndex(s => isEqual(s, date));
|
310
|
+
|
311
|
+
if (existingIndex > -1) {
|
312
|
+
selections.splice(existingIndex, 1);
|
313
|
+
setSelection(selections.sort((a, b) => a.getTime() - b.getTime()));
|
314
|
+
} else {
|
315
|
+
if (configuration.maxSelections && selections.length + 1 > configuration.maxSelections) return;
|
316
|
+
setSelection([...selections, date].sort((a, b) => a.getTime() - b.getTime()));
|
317
|
+
}
|
318
|
+
} else if (configuration.variant === 'range') {
|
319
|
+
if (selection && isRangeSelection(selection) && !selection.to) {
|
320
|
+
let toDate = date;
|
321
|
+
|
322
|
+
if (configuration.maxRangeSize && Math.abs(differenceInDays(selection.from, date)) >= configuration.maxRangeSize) {
|
323
|
+
toDate = isBefore(date, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
|
324
|
+
}
|
325
|
+
|
326
|
+
const updatedSelection = isBefore(toDate, selection.from) ? {
|
327
|
+
from: toDate,
|
328
|
+
to: selection.from
|
329
|
+
} : {
|
330
|
+
from: selection.from,
|
331
|
+
to: toDate
|
332
|
+
};
|
333
|
+
setSelection(updatedSelection);
|
334
|
+
setHoverRange(null);
|
335
|
+
|
336
|
+
if (!configuration.confirmation) {
|
337
|
+
saveValue(updatedSelection);
|
338
|
+
}
|
339
|
+
} else {
|
340
|
+
setHoverRange({
|
341
|
+
from: date,
|
342
|
+
to: date
|
343
|
+
});
|
344
|
+
setSelection({
|
345
|
+
from: date,
|
346
|
+
to: null
|
347
|
+
});
|
348
|
+
}
|
349
|
+
} else {
|
350
|
+
setSelection(date);
|
351
|
+
|
352
|
+
if (!configuration.confirmation) {
|
353
|
+
saveValue(date);
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}, [configuration.confirmation, configuration.maxRangeSize, configuration.maxSelections, configuration.variant, saveValue, selection]);
|
357
|
+
const focusHnadler = useCallback(date => {
|
358
|
+
if (!selection) return;
|
359
|
+
const {
|
360
|
+
minDate,
|
361
|
+
maxDate,
|
362
|
+
maxRangeSize,
|
363
|
+
variant
|
364
|
+
} = configuration;
|
365
|
+
|
366
|
+
if (variant === 'range' && isRangeSelection(selection) && hoverRange) {
|
367
|
+
let hoverDate = date;
|
368
|
+
if (minDate) hoverDate = isBefore(date, minDate) ? minDate : hoverDate;
|
369
|
+
if (maxDate) hoverDate = isAfter(date, maxDate) ? maxDate : hoverDate;
|
370
|
+
|
371
|
+
if (maxRangeSize && Math.abs(differenceInDays(selection.from, hoverDate)) >= maxRangeSize) {
|
372
|
+
hoverDate = isBefore(hoverDate, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
|
373
|
+
}
|
374
|
+
|
375
|
+
setHoverRange(isBefore(hoverDate, selection.from) ? {
|
376
|
+
from: hoverDate,
|
377
|
+
to: selection.from
|
378
|
+
} : {
|
379
|
+
from: selection.from,
|
380
|
+
to: hoverDate
|
381
|
+
});
|
382
|
+
}
|
383
|
+
}, [configuration, hoverRange, selection]);
|
384
|
+
const datePickerCtx = useMemo(() => {
|
385
|
+
return {
|
386
|
+
configuration,
|
387
|
+
currentViewingDate,
|
388
|
+
disabled: false,
|
389
|
+
formattedDate: getFormattedDate,
|
390
|
+
goToMonth,
|
391
|
+
hoverRange,
|
392
|
+
nextMonth,
|
393
|
+
onClose: handleClose,
|
394
|
+
onDateInput: inputHandler,
|
395
|
+
onDayFocus: focusHnadler,
|
396
|
+
onSelection: selectionHandler,
|
397
|
+
previousMonth,
|
398
|
+
revertValue,
|
399
|
+
saveValue,
|
400
|
+
selectionActive: false,
|
401
|
+
selection
|
402
|
+
};
|
403
|
+
}, [configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, handleClose, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
|
404
|
+
return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
|
405
|
+
value: datePickerCtx
|
406
|
+
}, children);
|
407
|
+
};
|
408
|
+
DatePickerProvider.displayName = "DatePickerProvider";
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import React from 'react';
|
2
|
-
import { ButtonProps } from '../
|
2
|
+
import { ButtonProps } from '../Button';
|
3
3
|
import { SxProp } from '../sx';
|
4
4
|
/**
|
5
5
|
* Props that characterize a button to be rendered into the footer of
|
@@ -9,7 +9,7 @@ export declare type DialogButtonProps = ButtonProps & {
|
|
9
9
|
/**
|
10
10
|
* The type of Button element to use
|
11
11
|
*/
|
12
|
-
buttonType?: '
|
12
|
+
buttonType?: 'normal' | 'primary' | 'danger';
|
13
13
|
/**
|
14
14
|
* The Button's inner text
|
15
15
|
*/
|
@@ -160,8 +160,8 @@ export declare const Dialog: React.ForwardRefExoticComponent<DialogProps & {
|
|
160
160
|
Buttons: React.FC<{
|
161
161
|
buttons: DialogButtonProps[];
|
162
162
|
}>;
|
163
|
-
CloseButton:
|
163
|
+
CloseButton: React.FC<{
|
164
164
|
onClose: () => void;
|
165
|
-
}
|
165
|
+
}>;
|
166
166
|
};
|
167
167
|
export {};
|
package/lib-esm/Dialog/Dialog.js
CHANGED
@@ -2,12 +2,13 @@ function _extends() { _extends = Object.assign || function (target) { for (var i
|
|
2
2
|
|
3
3
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
4
4
|
import styled from 'styled-components';
|
5
|
-
import Button from '../
|
5
|
+
import Button, { ButtonPrimary, ButtonDanger } from '../Button';
|
6
6
|
import Box from '../Box';
|
7
7
|
import { get, COMMON, POSITION } from '../constants';
|
8
8
|
import { useOnEscapePress, useProvidedRefOrCreate } from '../hooks';
|
9
9
|
import { useFocusTrap } from '../hooks/useFocusTrap';
|
10
10
|
import sx from '../sx';
|
11
|
+
import StyledOcticon from '../StyledOcticon';
|
11
12
|
import { XIcon } from '@primer/octicons-react';
|
12
13
|
import { useFocusZone } from '../hooks/useFocusZone';
|
13
14
|
import { FocusKeys } from '../behaviors/focusZone';
|
@@ -183,6 +184,11 @@ const Footer = styled(Box).attrs({
|
|
183
184
|
displayName: "Dialog__Footer",
|
184
185
|
componentId: "sc-11pkgky-6"
|
185
186
|
})(["box-shadow:0 -1px 0 ", ";padding:", ";display:flex;flex-flow:wrap;justify-content:flex-end;z-index:1;flex-shrink:0;button{margin-left:", ";&:first-child{margin-left:0;}}"], get('colors.border.default'), get('space.3'), get('space.1'));
|
187
|
+
const buttonTypes = {
|
188
|
+
normal: Button,
|
189
|
+
primary: ButtonPrimary,
|
190
|
+
danger: ButtonDanger
|
191
|
+
};
|
186
192
|
|
187
193
|
const Buttons = ({
|
188
194
|
buttons
|
@@ -205,30 +211,33 @@ const Buttons = ({
|
|
205
211
|
return /*#__PURE__*/React.createElement(React.Fragment, null, buttons.map((dialogButtonProps, index) => {
|
206
212
|
const {
|
207
213
|
content,
|
208
|
-
buttonType = '
|
214
|
+
buttonType = 'normal',
|
209
215
|
autoFocus = false,
|
210
216
|
...buttonProps
|
211
217
|
} = dialogButtonProps;
|
212
|
-
|
218
|
+
const ButtonElement = buttonTypes[buttonType];
|
219
|
+
return /*#__PURE__*/React.createElement(ButtonElement, _extends({
|
213
220
|
key: index
|
214
221
|
}, buttonProps, {
|
215
|
-
variant: buttonType,
|
216
222
|
ref: autoFocus && autoFocusCount === 0 ? (autoFocusCount++, autoFocusRef) : null
|
217
223
|
}), content);
|
218
224
|
}));
|
219
225
|
};
|
220
226
|
|
227
|
+
const DialogCloseButton = styled(Button).withConfig({
|
228
|
+
displayName: "Dialog__DialogCloseButton",
|
229
|
+
componentId: "sc-11pkgky-7"
|
230
|
+
})(["border-radius:4px;background:transparent;border:0;vertical-align:middle;color:", ";padding:", ";align-self:flex-start;line-height:normal;box-shadow:none;"], get('colors.fg.muted'), get('space.2'));
|
231
|
+
|
221
232
|
const CloseButton = ({
|
222
233
|
onClose
|
223
234
|
}) => {
|
224
|
-
return /*#__PURE__*/React.createElement(
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
icon: () => /*#__PURE__*/React.createElement(XIcon, null)
|
231
|
-
});
|
235
|
+
return /*#__PURE__*/React.createElement(DialogCloseButton, {
|
236
|
+
"aria-label": "Close",
|
237
|
+
onClick: onClose
|
238
|
+
}, /*#__PURE__*/React.createElement(StyledOcticon, {
|
239
|
+
icon: XIcon
|
240
|
+
}));
|
232
241
|
};
|
233
242
|
|
234
243
|
CloseButton.displayName = "CloseButton";
|