@primer/components 0.0.0-202192719508 → 0.0.0-2021927232856
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 +19 -1
- package/dist/browser.esm.js +53 -49
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +50 -46
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/Item.d.ts +6 -0
- package/lib/ActionList/Item.js +5 -1
- package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
- package/lib/Autocomplete/Autocomplete.d.ts +2 -4
- package/lib/Autocomplete/AutocompleteInput.d.ts +2 -4
- package/lib/SelectMenu/SelectMenu.d.ts +2 -4
- package/lib/TextInputWithTokens.d.ts +6 -4
- package/lib/TextInputWithTokens.js +102 -29
- package/lib/Token/Token.js +13 -2
- package/lib/Token/TokenBase.js +0 -4
- package/lib/Token/_RemoveTokenButton.js +15 -2
- package/lib/_TextInputWrapper.js +1 -1
- package/lib/theme-preval.js +2 -2
- package/lib/utils/testing.d.ts +1 -1
- package/lib-esm/ActionList/Item.d.ts +6 -0
- package/lib-esm/ActionList/Item.js +5 -1
- package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
- package/lib-esm/Autocomplete/Autocomplete.d.ts +2 -4
- package/lib-esm/Autocomplete/AutocompleteInput.d.ts +2 -4
- package/lib-esm/SelectMenu/SelectMenu.d.ts +2 -4
- package/lib-esm/TextInputWithTokens.d.ts +6 -4
- package/lib-esm/TextInputWithTokens.js +101 -30
- package/lib-esm/Token/Token.js +13 -2
- package/lib-esm/Token/TokenBase.js +0 -4
- package/lib-esm/Token/_RemoveTokenButton.js +11 -2
- package/lib-esm/_TextInputWrapper.js +1 -1
- package/lib-esm/theme-preval.js +2 -2
- package/lib-esm/utils/testing.d.ts +1 -1
- package/package.json +14 -14
- package/lib/DatePicker/DatePicker.d.ts +0 -48
- package/lib/DatePicker/DatePicker.js +0 -104
- package/lib/DatePicker/DatePickerAnchor.d.ts +0 -5
- package/lib/DatePicker/DatePickerAnchor.js +0 -182
- package/lib/DatePicker/DatePickerOverlay.d.ts +0 -3
- package/lib/DatePicker/DatePickerOverlay.js +0 -48
- package/lib/DatePicker/DatePickerPanel.d.ts +0 -2
- package/lib/DatePicker/DatePickerPanel.js +0 -126
- package/lib/DatePicker/Day.d.ts +0 -14
- package/lib/DatePicker/Day.js +0 -190
- package/lib/DatePicker/Month.d.ts +0 -9
- package/lib/DatePicker/Month.js +0 -120
- package/lib/DatePicker/index.d.ts +0 -2
- package/lib/DatePicker/index.js +0 -13
- package/lib/DatePicker/useDatePicker.d.ts +0 -88
- package/lib/DatePicker/useDatePicker.js +0 -421
- package/lib/hooks/useDebounce.d.ts +0 -2
- package/lib/hooks/useDebounce.js +0 -24
- package/lib-esm/DatePicker/DatePicker.d.ts +0 -48
- package/lib-esm/DatePicker/DatePicker.js +0 -87
- package/lib-esm/DatePicker/DatePickerAnchor.d.ts +0 -5
- package/lib-esm/DatePicker/DatePickerAnchor.js +0 -155
- package/lib-esm/DatePicker/DatePickerOverlay.d.ts +0 -3
- package/lib-esm/DatePicker/DatePickerOverlay.js +0 -29
- package/lib-esm/DatePicker/DatePickerPanel.d.ts +0 -2
- package/lib-esm/DatePicker/DatePickerPanel.js +0 -100
- package/lib-esm/DatePicker/Day.d.ts +0 -14
- package/lib-esm/DatePicker/Day.js +0 -167
- package/lib-esm/DatePicker/Month.d.ts +0 -9
- package/lib-esm/DatePicker/Month.js +0 -96
- package/lib-esm/DatePicker/index.d.ts +0 -2
- package/lib-esm/DatePicker/index.js +0 -1
- package/lib-esm/DatePicker/useDatePicker.d.ts +0 -88
- package/lib-esm/DatePicker/useDatePicker.js +0 -391
- package/lib-esm/hooks/useDebounce.d.ts +0 -2
- package/lib-esm/hooks/useDebounce.js +0 -16
@@ -1,96 +0,0 @@
|
|
1
|
-
import { format, isEqual, lastDayOfMonth, getDaysInMonth, eachDayOfInterval, startOfWeek, endOfWeek } from 'date-fns';
|
2
|
-
import React, { useMemo, useState } from 'react';
|
3
|
-
import styled from 'styled-components';
|
4
|
-
import Box from '../Box';
|
5
|
-
import Text from '../Text';
|
6
|
-
import { get } from '../constants';
|
7
|
-
import { BlankDay, Day } from './Day';
|
8
|
-
import useDatePicker from './useDatePicker';
|
9
|
-
const weekdayEnum = {
|
10
|
-
Sunday: 0,
|
11
|
-
Monday: 1,
|
12
|
-
Tuesday: 2,
|
13
|
-
Wednesday: 3,
|
14
|
-
Thursday: 4,
|
15
|
-
Friday: 5,
|
16
|
-
Saturday: 6
|
17
|
-
};
|
18
|
-
const MonthComponent = styled(Box).withConfig({
|
19
|
-
displayName: "Month__MonthComponent",
|
20
|
-
componentId: "l6j7o0-0"
|
21
|
-
})(["display:grid;grid-auto-rows:min-content;grid-template-columns:repeat(1fr,7);grid-template-rows:1fr;gap:0px 0px;grid-template-areas:'month month month month month month month' 'sunday monday tuesday wednesday thursday friday saturday';justify-items:stretch;align-items:stretch;"]);
|
22
|
-
const MonthTitle = styled(Text).withConfig({
|
23
|
-
displayName: "Month__MonthTitle",
|
24
|
-
componentId: "l6j7o0-1"
|
25
|
-
})(["font-size:", ";font-weight:", ";grid-area:month;justify-self:center;"], get('fontSizes.1'), get('fontWeights.bold'));
|
26
|
-
const WeekdayHeader = styled(Text).withConfig({
|
27
|
-
displayName: "Month__WeekdayHeader",
|
28
|
-
componentId: "l6j7o0-2"
|
29
|
-
})(["color:", ";font-family:", ";font-size:", ";justify-self:center;padding:", " 0 ", ";"], get('colors.fg.subtle'), get('fonts.mono'), get('fontSizes.0'), get('space.3'), get('space.2'));
|
30
|
-
export const Month = ({
|
31
|
-
month,
|
32
|
-
year
|
33
|
-
}) => {
|
34
|
-
const {
|
35
|
-
configuration
|
36
|
-
} = useDatePicker();
|
37
|
-
const [selectedDay, setSelectedDay] = useState(null);
|
38
|
-
const getTitle = useMemo(() => `${format(new Date(year, month), 'MMMM yyyy')}`, [month, year]);
|
39
|
-
const weekdayHeaders = useMemo(() => {
|
40
|
-
var _configuration$weekSt;
|
41
|
-
|
42
|
-
const now = new Date(year, month);
|
43
|
-
const weekOptions = {
|
44
|
-
weekStartsOn: weekdayEnum[(_configuration$weekSt = configuration.weekStartsOn) !== null && _configuration$weekSt !== void 0 ? _configuration$weekSt : 'Sunday']
|
45
|
-
};
|
46
|
-
return eachDayOfInterval({
|
47
|
-
start: startOfWeek(now, weekOptions),
|
48
|
-
end: endOfWeek(now, weekOptions)
|
49
|
-
}).map(d => /*#__PURE__*/React.createElement(WeekdayHeader, {
|
50
|
-
key: `weekday-${d}-header`
|
51
|
-
}, format(d, 'EEEEEE')));
|
52
|
-
}, [configuration.weekStartsOn, month, year]);
|
53
|
-
|
54
|
-
const dayAction = date => {
|
55
|
-
setSelectedDay(date);
|
56
|
-
};
|
57
|
-
|
58
|
-
const dayComponents = useMemo(() => {
|
59
|
-
var _configuration$weekSt2, _configuration$weekSt3;
|
60
|
-
|
61
|
-
const components = [];
|
62
|
-
const firstDay = new Date(year, month, 1);
|
63
|
-
const preBlanks = (firstDay.getDay() + (7 - weekdayEnum[(_configuration$weekSt2 = configuration.weekStartsOn) !== null && _configuration$weekSt2 !== void 0 ? _configuration$weekSt2 : 'Sunday'])) % 7;
|
64
|
-
|
65
|
-
for (let i = 0; i < preBlanks; i++) {
|
66
|
-
components.push( /*#__PURE__*/React.createElement(BlankDay, {
|
67
|
-
key: `month-pre-blank-${i}`
|
68
|
-
}));
|
69
|
-
}
|
70
|
-
|
71
|
-
for (let i = 1; i <= getDaysInMonth(firstDay); i++) {
|
72
|
-
const date = new Date(year, month, i);
|
73
|
-
components.push( /*#__PURE__*/React.createElement(Day, {
|
74
|
-
key: `day-component-${date.toString()}`,
|
75
|
-
date: date,
|
76
|
-
selected: selectedDay ? isEqual(date, selectedDay) : false,
|
77
|
-
onAction: dayAction
|
78
|
-
}));
|
79
|
-
}
|
80
|
-
|
81
|
-
const lastDay = lastDayOfMonth(firstDay);
|
82
|
-
const postBlanks = (lastDay.getDay() + (7 - weekdayEnum[(_configuration$weekSt3 = configuration.weekStartsOn) !== null && _configuration$weekSt3 !== void 0 ? _configuration$weekSt3 : 'Sunday'])) % 7;
|
83
|
-
|
84
|
-
for (let i = 6; i > postBlanks; i--) {
|
85
|
-
components.push( /*#__PURE__*/React.createElement(BlankDay, {
|
86
|
-
key: `month-post-blank-${i}`
|
87
|
-
}));
|
88
|
-
}
|
89
|
-
|
90
|
-
return components;
|
91
|
-
}, [configuration.weekStartsOn, month, selectedDay, year]);
|
92
|
-
return /*#__PURE__*/React.createElement(MonthComponent, {
|
93
|
-
role: "grid"
|
94
|
-
}, /*#__PURE__*/React.createElement(MonthTitle, null, getTitle), weekdayHeaders, dayComponents);
|
95
|
-
};
|
96
|
-
Month.displayName = "Month";
|
@@ -1 +0,0 @@
|
|
1
|
-
export { DatePicker } from './DatePicker';
|
@@ -1,88 +0,0 @@
|
|
1
|
-
import React from 'react';
|
2
|
-
export declare type AnchorVariant = 'input' | 'button' | 'icon-only';
|
3
|
-
export declare type DateFormat = 'short' | 'long' | string;
|
4
|
-
export declare type SelectionVariant = 'single' | 'multi' | 'range';
|
5
|
-
export interface DatePickerConfiguration {
|
6
|
-
anchorVariant?: AnchorVariant;
|
7
|
-
blockedDates?: Array<Date>;
|
8
|
-
confirmation?: boolean;
|
9
|
-
confirmUnsavedClose?: boolean;
|
10
|
-
dateFormat?: DateFormat;
|
11
|
-
disableWeekends?: boolean;
|
12
|
-
iconPlacement?: 'start' | 'end' | 'none';
|
13
|
-
maxDate?: Date | null;
|
14
|
-
maxSelections?: number;
|
15
|
-
maxRange?: number;
|
16
|
-
minDate?: Date | null;
|
17
|
-
placeholder?: string;
|
18
|
-
rangeIncrement?: number;
|
19
|
-
selection?: SelectionVariant;
|
20
|
-
view?: '1-month' | '2-month';
|
21
|
-
weekStartsOn?: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
|
22
|
-
}
|
23
|
-
export declare type RangeSelection = {
|
24
|
-
from: Date;
|
25
|
-
to: Date | null;
|
26
|
-
};
|
27
|
-
export declare type StringRangeSelection = {
|
28
|
-
from: string;
|
29
|
-
to: string;
|
30
|
-
};
|
31
|
-
export interface DatePickerContext {
|
32
|
-
disabled?: boolean;
|
33
|
-
configuration: DatePickerConfiguration;
|
34
|
-
currentViewingDate: Date;
|
35
|
-
goToMonth: (date: Date) => void;
|
36
|
-
hoverRange?: RangeSelection | null;
|
37
|
-
selection?: Selection;
|
38
|
-
softSelection?: Partial<RangeSelection> | null;
|
39
|
-
selectionActive?: boolean;
|
40
|
-
formattedDate: string;
|
41
|
-
nextMonth: () => void;
|
42
|
-
onClose: () => void;
|
43
|
-
onDateInput: (updatedSelection: Selection) => void;
|
44
|
-
onDayFocus: (date: Date) => void;
|
45
|
-
onSelection: (date: Date) => void;
|
46
|
-
previousMonth: () => void;
|
47
|
-
revertValue: () => void;
|
48
|
-
saveValue: (selection?: Selection) => void;
|
49
|
-
}
|
50
|
-
export declare type Selection = Date | Array<Date> | RangeSelection | null;
|
51
|
-
export declare type StringSelection = string | Array<string> | {
|
52
|
-
to: string;
|
53
|
-
from: string;
|
54
|
-
} | null;
|
55
|
-
export declare type DaySelection = boolean | 'start' | 'middle' | 'end';
|
56
|
-
declare const useDatePicker: (date?: Date | undefined) => {
|
57
|
-
blocked: boolean | undefined;
|
58
|
-
disabled: boolean;
|
59
|
-
selected: DaySelection;
|
60
|
-
today: boolean;
|
61
|
-
configuration: DatePickerConfiguration;
|
62
|
-
currentViewingDate: Date;
|
63
|
-
goToMonth: (date: Date) => void;
|
64
|
-
hoverRange?: RangeSelection | null | undefined;
|
65
|
-
selection?: Selection | undefined;
|
66
|
-
softSelection?: Partial<RangeSelection> | null | undefined;
|
67
|
-
selectionActive?: boolean | undefined;
|
68
|
-
formattedDate: string;
|
69
|
-
nextMonth: () => void;
|
70
|
-
onClose: () => void;
|
71
|
-
onDateInput: (updatedSelection: Selection) => void;
|
72
|
-
onDayFocus: (date: Date) => void;
|
73
|
-
onSelection: (date: Date) => void;
|
74
|
-
previousMonth: () => void;
|
75
|
-
revertValue: () => void;
|
76
|
-
saveValue: (selection?: Selection | undefined) => void;
|
77
|
-
};
|
78
|
-
export default useDatePicker;
|
79
|
-
export interface DatePickerProviderProps {
|
80
|
-
closePicker?: () => void;
|
81
|
-
configuration?: DatePickerConfiguration;
|
82
|
-
value?: Selection | StringSelection;
|
83
|
-
}
|
84
|
-
export declare function isSingleSelection(selection: Selection): selection is Date;
|
85
|
-
export declare function isMultiSelection(selection: Selection | StringSelection): selection is Array<Date> | Array<string>;
|
86
|
-
export declare function isRangeSelection(selection: Selection | StringSelection): selection is RangeSelection | StringRangeSelection;
|
87
|
-
export declare function isStringRangeSelection(selection: StringSelection): selection is StringRangeSelection;
|
88
|
-
export declare const DatePickerProvider: React.FC<DatePickerProviderProps>;
|
@@ -1,391 +0,0 @@
|
|
1
|
-
import { CheckIcon, TrashIcon } from '@primer/octicons-react';
|
2
|
-
import { format, isEqual, isAfter, isBefore, addMonths, subMonths, isToday, isWeekend } 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
|
-
const DatePickerContext = /*#__PURE__*/createContext(null);
|
7
|
-
|
8
|
-
const useDatePicker = date => {
|
9
|
-
const value = useContext(DatePickerContext);
|
10
|
-
const [selected, setSelected] = useState(false);
|
11
|
-
const today = date ? isToday(date) : false;
|
12
|
-
|
13
|
-
if (!value) {
|
14
|
-
throw new Error('useDatePicker must be used inside a DatePickerProvider');
|
15
|
-
}
|
16
|
-
|
17
|
-
useEffect(() => {
|
18
|
-
if (date) {
|
19
|
-
if (value.hoverRange) {
|
20
|
-
if (isRangeSelection(value.hoverRange)) {
|
21
|
-
if (isEqual(date, value.hoverRange.from)) {
|
22
|
-
setSelected('start');
|
23
|
-
} else if (value.hoverRange.to && isEqual(date, value.hoverRange.to)) {
|
24
|
-
setSelected('end');
|
25
|
-
} else if (isAfter(date, value.hoverRange.from) && value.hoverRange.to && isBefore(date, value.hoverRange.to)) {
|
26
|
-
setSelected('middle');
|
27
|
-
} else {
|
28
|
-
setSelected(false);
|
29
|
-
}
|
30
|
-
}
|
31
|
-
} else if (value.selection) {
|
32
|
-
if (isMultiSelection(value.selection)) {
|
33
|
-
setSelected(!!value.selection.find(d => isEqual(d, date)));
|
34
|
-
} else if (isRangeSelection(value.selection)) {
|
35
|
-
if (isEqual(date, value.selection.from)) {
|
36
|
-
setSelected('start');
|
37
|
-
} else if (value.selection.to && isEqual(date, value.selection.to)) {
|
38
|
-
setSelected('end');
|
39
|
-
} else if (isAfter(date, value.selection.from) && value.selection.to && isBefore(date, value.selection.to)) {
|
40
|
-
setSelected('middle');
|
41
|
-
} else {
|
42
|
-
setSelected(false);
|
43
|
-
}
|
44
|
-
} else {
|
45
|
-
setSelected(isEqual(date, value.selection));
|
46
|
-
}
|
47
|
-
}
|
48
|
-
}
|
49
|
-
}, [date, value.hoverRange, value.selection, today]);
|
50
|
-
let blocked,
|
51
|
-
disabled = false;
|
52
|
-
|
53
|
-
if (date) {
|
54
|
-
// Determine if date is blocked out
|
55
|
-
if (value.configuration.blockedDates) {
|
56
|
-
blocked = !!value.configuration.blockedDates.find(d => isEqual(d, date));
|
57
|
-
} // Determine if date is disabled
|
58
|
-
|
59
|
-
|
60
|
-
if (value.configuration.minDate || value.configuration.maxDate || value.configuration.disableWeekends) {
|
61
|
-
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);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
return { ...value,
|
66
|
-
blocked,
|
67
|
-
disabled,
|
68
|
-
selected,
|
69
|
-
today
|
70
|
-
};
|
71
|
-
};
|
72
|
-
|
73
|
-
export default useDatePicker;
|
74
|
-
export function isSingleSelection(selection) {
|
75
|
-
return selection instanceof Date;
|
76
|
-
}
|
77
|
-
export function isMultiSelection(selection) {
|
78
|
-
return Array.isArray(selection);
|
79
|
-
}
|
80
|
-
export function isRangeSelection(selection) {
|
81
|
-
return !!selection.from;
|
82
|
-
}
|
83
|
-
export function isStringRangeSelection(selection) {
|
84
|
-
return !!selection.from;
|
85
|
-
}
|
86
|
-
|
87
|
-
function parseSelection(selection, variant) {
|
88
|
-
if (!selection) return;
|
89
|
-
|
90
|
-
if (variant === 'multi') {
|
91
|
-
if (isMultiSelection(selection)) {
|
92
|
-
const parsedSelection = [];
|
93
|
-
|
94
|
-
for (const d of selection) {
|
95
|
-
parsedSelection.push(new Date(new Date(d).toDateString()));
|
96
|
-
}
|
97
|
-
|
98
|
-
return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
|
99
|
-
} else if (selection instanceof Date) {
|
100
|
-
return [new Date(new Date(selection).toDateString())];
|
101
|
-
} else if (isRangeSelection(selection)) {
|
102
|
-
const parsedSelection = [];
|
103
|
-
parsedSelection.push(new Date(new Date(selection.from).toDateString()));
|
104
|
-
|
105
|
-
if (selection.to) {
|
106
|
-
parsedSelection.push(new Date(new Date(selection.to).toDateString()));
|
107
|
-
}
|
108
|
-
|
109
|
-
return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
|
110
|
-
}
|
111
|
-
} else if (variant === 'range') {
|
112
|
-
if (isRangeSelection(selection)) {
|
113
|
-
return {
|
114
|
-
from: new Date(new Date(selection.from).toDateString()),
|
115
|
-
to: selection.to ? new Date(new Date(selection.to).toDateString()) : null
|
116
|
-
};
|
117
|
-
} else if (isMultiSelection(selection)) {
|
118
|
-
return {
|
119
|
-
from: new Date(new Date(selection[0]).toDateString()),
|
120
|
-
to: selection[1] ? new Date(new Date(selection[1]).toDateString()) : null
|
121
|
-
};
|
122
|
-
} else if (selection instanceof Date) {
|
123
|
-
return {
|
124
|
-
from: new Date(new Date(selection).toDateString()),
|
125
|
-
to: null
|
126
|
-
};
|
127
|
-
}
|
128
|
-
} else {
|
129
|
-
if (selection instanceof Date) {
|
130
|
-
return new Date(new Date(selection).toDateString());
|
131
|
-
} else if (isMultiSelection(selection)) {
|
132
|
-
return new Date(new Date(selection[0]).toDateString());
|
133
|
-
} else if (isRangeSelection(selection)) {
|
134
|
-
return new Date(new Date(selection.from).toDateString());
|
135
|
-
} else {
|
136
|
-
return;
|
137
|
-
}
|
138
|
-
}
|
139
|
-
}
|
140
|
-
|
141
|
-
const defaultConfiguration = {
|
142
|
-
anchorVariant: 'button',
|
143
|
-
confirmation: false,
|
144
|
-
confirmUnsavedClose: false,
|
145
|
-
disableWeekends: false,
|
146
|
-
iconPlacement: 'start',
|
147
|
-
placeholder: 'Select a Date...',
|
148
|
-
selection: 'single',
|
149
|
-
view: '2-month',
|
150
|
-
weekStartsOn: 'Sunday'
|
151
|
-
};
|
152
|
-
export const DatePickerProvider = ({
|
153
|
-
configuration: externalConfig = {},
|
154
|
-
children,
|
155
|
-
closePicker,
|
156
|
-
value
|
157
|
-
}) => {
|
158
|
-
const [configuration, setConfiguration] = useState(deepmerge(defaultConfiguration, externalConfig));
|
159
|
-
const [previousSelection, setPreviousSelection] = useState(parseSelection(value, configuration.selection));
|
160
|
-
const [isDirty, setIsDirty] = useState(false);
|
161
|
-
const [selection, setSelection] = useState(parseSelection(value, configuration.selection));
|
162
|
-
const [hoverRange, setHoverRange] = useState(null);
|
163
|
-
const [currentViewingDate, setCurrentViewingDate] = useState(new Date());
|
164
|
-
const confirm = useConfirm();
|
165
|
-
useEffect(() => {
|
166
|
-
setConfiguration(deepmerge(defaultConfiguration, externalConfig));
|
167
|
-
setSelection(parseSelection(selection, configuration.selection)); // Don't want this to run every time selection gets updated
|
168
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
169
|
-
}, [configuration.selection, externalConfig]);
|
170
|
-
const goToMonth = useCallback(date => {
|
171
|
-
setCurrentViewingDate(new Date(new Date(date).toDateString()));
|
172
|
-
}, []);
|
173
|
-
const nextMonth = useCallback(() => {
|
174
|
-
setCurrentViewingDate(addMonths(currentViewingDate, 1));
|
175
|
-
}, [currentViewingDate]);
|
176
|
-
const previousMonth = useCallback(() => {
|
177
|
-
setCurrentViewingDate(subMonths(currentViewingDate, 1));
|
178
|
-
}, [currentViewingDate]);
|
179
|
-
const getFormattedDate = useMemo(() => {
|
180
|
-
if (!selection) {
|
181
|
-
return configuration.placeholder;
|
182
|
-
}
|
183
|
-
|
184
|
-
let template = 'MMM d';
|
185
|
-
|
186
|
-
if (configuration.anchorVariant !== 'input' && configuration.dateFormat) {
|
187
|
-
switch (configuration.dateFormat) {
|
188
|
-
case 'short':
|
189
|
-
template = 'MMM d';
|
190
|
-
break;
|
191
|
-
|
192
|
-
case 'long':
|
193
|
-
template = 'MMM d, yyyy';
|
194
|
-
break;
|
195
|
-
|
196
|
-
default:
|
197
|
-
template = configuration.dateFormat;
|
198
|
-
break;
|
199
|
-
}
|
200
|
-
} else {
|
201
|
-
template = 'MM/dd/yyyy';
|
202
|
-
}
|
203
|
-
|
204
|
-
switch (configuration.selection) {
|
205
|
-
case 'single':
|
206
|
-
{
|
207
|
-
if (selection instanceof Date) {
|
208
|
-
return format(selection, template);
|
209
|
-
} else if (Array.isArray(selection)) {
|
210
|
-
return format(selection[0], template);
|
211
|
-
} else if (isRangeSelection(selection)) {
|
212
|
-
return format(selection.from, template);
|
213
|
-
} else {
|
214
|
-
return 'Invalid Selection';
|
215
|
-
}
|
216
|
-
}
|
217
|
-
|
218
|
-
case 'multi':
|
219
|
-
{
|
220
|
-
if (Array.isArray(selection)) {
|
221
|
-
if (selection.length > 3) return `${selection.length} Selected`;
|
222
|
-
const formatted = selection.map(d => format(d, template)).join(', ');
|
223
|
-
return formatted;
|
224
|
-
} else if (selection instanceof Date) {
|
225
|
-
return [selection].map(d => format(d, template)).join(', ');
|
226
|
-
} else if (isRangeSelection(selection)) {
|
227
|
-
return [selection.to, selection.from].map(d => d ? format(d, template) : '').join(', ');
|
228
|
-
} else {
|
229
|
-
return 'Invalid Selection';
|
230
|
-
}
|
231
|
-
}
|
232
|
-
|
233
|
-
case 'range':
|
234
|
-
{
|
235
|
-
if (isRangeSelection(selection)) {
|
236
|
-
return Object.entries(selection).map(([_, date]) => date ? format(date, template) : '').join(' - ');
|
237
|
-
} else if (selection instanceof Date) {
|
238
|
-
return Object.entries({
|
239
|
-
from: selection,
|
240
|
-
to: null
|
241
|
-
}).map(([_, date]) => date ? format(date, template) : '').join(' - ');
|
242
|
-
} else if (Array.isArray(selection)) {
|
243
|
-
return Object.entries({
|
244
|
-
from: selection[0],
|
245
|
-
to: selection[1]
|
246
|
-
}) // to date can still be null
|
247
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
248
|
-
.map(([_, date]) => date ? format(date, template) : '').join(' - ');
|
249
|
-
} else {
|
250
|
-
return 'Invalid Selection';
|
251
|
-
}
|
252
|
-
}
|
253
|
-
|
254
|
-
default:
|
255
|
-
{
|
256
|
-
return 'Invalid Configuration';
|
257
|
-
}
|
258
|
-
}
|
259
|
-
}, [configuration.anchorVariant, configuration.dateFormat, configuration.placeholder, configuration.selection, selection]);
|
260
|
-
const saveValue = useCallback(updatedSelection => {
|
261
|
-
setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
|
262
|
-
setIsDirty(false);
|
263
|
-
closePicker === null || closePicker === void 0 ? void 0 : closePicker();
|
264
|
-
}, [closePicker, selection]);
|
265
|
-
const revertValue = useCallback(() => {
|
266
|
-
setSelection(previousSelection);
|
267
|
-
setIsDirty(false);
|
268
|
-
}, [previousSelection]);
|
269
|
-
const handleClose = useCallback(async () => {
|
270
|
-
if (configuration.confirmUnsavedClose) {
|
271
|
-
if (isDirty) {
|
272
|
-
const result = await confirm({
|
273
|
-
title: 'Save Changes?',
|
274
|
-
content: 'You have unsaved changes, would you like to save them?',
|
275
|
-
confirmButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CheckIcon, null), /*#__PURE__*/React.createElement(Text, {
|
276
|
-
sx: {
|
277
|
-
ml: 1
|
278
|
-
}
|
279
|
-
}, "Save")),
|
280
|
-
cancelButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TrashIcon, null), /*#__PURE__*/React.createElement(Text, {
|
281
|
-
sx: {
|
282
|
-
ml: 1
|
283
|
-
}
|
284
|
-
}, "Discard"))
|
285
|
-
});
|
286
|
-
|
287
|
-
if (result) {
|
288
|
-
saveValue();
|
289
|
-
} else {
|
290
|
-
revertValue();
|
291
|
-
}
|
292
|
-
}
|
293
|
-
} else if (isDirty) revertValue();
|
294
|
-
}, [configuration.confirmUnsavedClose, confirm, isDirty, revertValue, saveValue]);
|
295
|
-
const inputHandler = useCallback(updatedSelection => {
|
296
|
-
// validate date falls within range
|
297
|
-
setSelection(updatedSelection);
|
298
|
-
}, []);
|
299
|
-
const selectionHandler = useCallback(date => {
|
300
|
-
setIsDirty(true);
|
301
|
-
|
302
|
-
if (configuration.selection === 'multi') {
|
303
|
-
const selections = [...selection];
|
304
|
-
const existingIndex = selections.findIndex(s => isEqual(s, date));
|
305
|
-
|
306
|
-
if (existingIndex > -1) {
|
307
|
-
selections.splice(existingIndex, 1);
|
308
|
-
setSelection(selections.sort((a, b) => a.getTime() - b.getTime()));
|
309
|
-
} else {
|
310
|
-
if (configuration.maxSelections && selections.length + 1 > configuration.maxSelections) return;
|
311
|
-
setSelection([...selections, date].sort((a, b) => a.getTime() - b.getTime()));
|
312
|
-
}
|
313
|
-
} else if (configuration.selection === 'range') {
|
314
|
-
if (selection && isRangeSelection(selection) && !selection.to) {
|
315
|
-
const updatedSelection = isBefore(date, selection.from) ? {
|
316
|
-
from: date,
|
317
|
-
to: selection.from
|
318
|
-
} : {
|
319
|
-
from: selection.from,
|
320
|
-
to: date
|
321
|
-
};
|
322
|
-
setSelection(updatedSelection);
|
323
|
-
setHoverRange(null);
|
324
|
-
|
325
|
-
if (!configuration.confirmation) {
|
326
|
-
saveValue(updatedSelection);
|
327
|
-
}
|
328
|
-
} else {
|
329
|
-
setHoverRange({
|
330
|
-
from: date,
|
331
|
-
to: date
|
332
|
-
});
|
333
|
-
setSelection({
|
334
|
-
from: date,
|
335
|
-
to: null
|
336
|
-
});
|
337
|
-
}
|
338
|
-
} else {
|
339
|
-
setSelection(date);
|
340
|
-
|
341
|
-
if (!configuration.confirmation) {
|
342
|
-
saveValue(date);
|
343
|
-
}
|
344
|
-
}
|
345
|
-
}, [configuration.confirmation, configuration.maxSelections, configuration.selection, saveValue, selection]);
|
346
|
-
const focusHnadler = useCallback(date => {
|
347
|
-
if (!selection) return;
|
348
|
-
const {
|
349
|
-
minDate,
|
350
|
-
maxDate,
|
351
|
-
selection: configSelection
|
352
|
-
} = configuration;
|
353
|
-
|
354
|
-
if (configSelection === 'range' && isRangeSelection(selection) && hoverRange) {
|
355
|
-
let hoverDate = date;
|
356
|
-
if (minDate) hoverDate = isBefore(date, minDate) ? minDate : hoverDate;
|
357
|
-
if (maxDate) hoverDate = isAfter(date, maxDate) ? maxDate : hoverDate;
|
358
|
-
setHoverRange(isBefore(hoverDate, selection.from) ? {
|
359
|
-
from: hoverDate,
|
360
|
-
to: selection.from
|
361
|
-
} : {
|
362
|
-
from: selection.from,
|
363
|
-
to: hoverDate
|
364
|
-
});
|
365
|
-
}
|
366
|
-
}, [configuration, hoverRange, selection]);
|
367
|
-
const datePickerCtx = useMemo(() => {
|
368
|
-
return {
|
369
|
-
configuration,
|
370
|
-
currentViewingDate,
|
371
|
-
disabled: false,
|
372
|
-
formattedDate: getFormattedDate,
|
373
|
-
goToMonth,
|
374
|
-
hoverRange,
|
375
|
-
nextMonth,
|
376
|
-
onClose: handleClose,
|
377
|
-
onDateInput: inputHandler,
|
378
|
-
onDayFocus: focusHnadler,
|
379
|
-
onSelection: selectionHandler,
|
380
|
-
previousMonth,
|
381
|
-
revertValue,
|
382
|
-
saveValue,
|
383
|
-
selectionActive: false,
|
384
|
-
selection
|
385
|
-
};
|
386
|
-
}, [configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, handleClose, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
|
387
|
-
return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
|
388
|
-
value: datePickerCtx
|
389
|
-
}, children);
|
390
|
-
};
|
391
|
-
DatePickerProvider.displayName = "DatePickerProvider";
|
@@ -1,16 +0,0 @@
|
|
1
|
-
import { useEffect, useState } from 'react';
|
2
|
-
|
3
|
-
function useDebounce(value, delay) {
|
4
|
-
const [debouncedValue, setDebouncedValue] = useState(value);
|
5
|
-
useEffect(() => {
|
6
|
-
const handler = setTimeout(() => {
|
7
|
-
setDebouncedValue(value);
|
8
|
-
}, delay);
|
9
|
-
return () => {
|
10
|
-
clearTimeout(handler);
|
11
|
-
};
|
12
|
-
}, [value, delay]);
|
13
|
-
return debouncedValue;
|
14
|
-
}
|
15
|
-
|
16
|
-
export default useDebounce;
|