@primer/components 0.0.0-202192822657 → 0.0.0-202192952423

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.
Files changed (140) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/dist/browser.esm.js +436 -439
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +440 -443
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/ActionList/Item.d.ts +6 -0
  7. package/lib/ActionList/Item.js +5 -1
  8. package/lib/ActionMenu.js +2 -2
  9. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  10. package/lib/Autocomplete/Autocomplete.d.ts +11 -13
  11. package/lib/Autocomplete/AutocompleteInput.d.ts +11 -13
  12. package/lib/Button/Button.d.ts +11 -11
  13. package/lib/Button/ButtonBase.d.ts +1 -1
  14. package/lib/Button/ButtonClose.d.ts +29 -29
  15. package/lib/Button/ButtonDanger.d.ts +11 -11
  16. package/lib/Button/ButtonInvisible.d.ts +11 -11
  17. package/lib/Button/ButtonOutline.d.ts +11 -11
  18. package/lib/Button/ButtonPrimary.d.ts +11 -11
  19. package/lib/CircleBadge.d.ts +2 -2
  20. package/lib/CircleOcticon.d.ts +33 -33
  21. package/lib/Dialog/Dialog.d.ts +4 -4
  22. package/lib/Dialog/Dialog.js +12 -22
  23. package/lib/Dialog.d.ts +35 -35
  24. package/lib/Dropdown.d.ts +87 -165
  25. package/lib/Dropdown.js +6 -5
  26. package/lib/DropdownMenu/DropdownButton.d.ts +30 -30
  27. package/lib/FilterList.d.ts +26 -26
  28. package/lib/Label.d.ts +1 -1
  29. package/lib/NewButton/button.d.ts +581 -0
  30. package/lib/NewButton/button.js +332 -0
  31. package/lib/NewButton/buttonStyles.d.ts +2 -0
  32. package/lib/NewButton/buttonStyles.js +14 -0
  33. package/lib/NewButton/counter.d.ts +6 -0
  34. package/lib/NewButton/counter.js +33 -0
  35. package/lib/NewButton/index.d.ts +4 -0
  36. package/lib/NewButton/index.js +21 -0
  37. package/lib/Position.d.ts +4 -4
  38. package/lib/ProgressBar.d.ts +1 -1
  39. package/lib/SelectMenu/SelectMenu.d.ts +151 -153
  40. package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
  41. package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
  42. package/lib/TextInputWithTokens.d.ts +11 -13
  43. package/lib/Timeline.d.ts +33 -33
  44. package/lib/Token/AvatarToken.d.ts +1 -1
  45. package/lib/Token/IssueLabelToken.d.ts +1 -1
  46. package/lib/Token/Token.d.ts +1 -1
  47. package/lib/_TextInputWrapper.d.ts +1 -1
  48. package/lib/constants.js +3 -1
  49. package/lib/index.d.ts +2 -0
  50. package/lib/index.js +8 -0
  51. package/lib/theme-preval.js +2 -2
  52. package/lib/utils/testing.d.ts +1 -1
  53. package/lib-esm/ActionList/Item.d.ts +6 -0
  54. package/lib-esm/ActionList/Item.js +5 -1
  55. package/lib-esm/ActionMenu.js +2 -2
  56. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  57. package/lib-esm/Autocomplete/Autocomplete.d.ts +11 -13
  58. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +11 -13
  59. package/lib-esm/Button/Button.d.ts +11 -11
  60. package/lib-esm/Button/ButtonBase.d.ts +1 -1
  61. package/lib-esm/Button/ButtonClose.d.ts +29 -29
  62. package/lib-esm/Button/ButtonDanger.d.ts +11 -11
  63. package/lib-esm/Button/ButtonInvisible.d.ts +11 -11
  64. package/lib-esm/Button/ButtonOutline.d.ts +11 -11
  65. package/lib-esm/Button/ButtonPrimary.d.ts +11 -11
  66. package/lib-esm/CircleBadge.d.ts +2 -2
  67. package/lib-esm/CircleOcticon.d.ts +33 -33
  68. package/lib-esm/Dialog/Dialog.d.ts +4 -4
  69. package/lib-esm/Dialog/Dialog.js +12 -21
  70. package/lib-esm/Dialog.d.ts +35 -35
  71. package/lib-esm/Dropdown.d.ts +87 -165
  72. package/lib-esm/Dropdown.js +4 -3
  73. package/lib-esm/DropdownMenu/DropdownButton.d.ts +30 -30
  74. package/lib-esm/FilterList.d.ts +26 -26
  75. package/lib-esm/Label.d.ts +1 -1
  76. package/lib-esm/NewButton/button.d.ts +581 -0
  77. package/lib-esm/NewButton/button.js +308 -0
  78. package/lib-esm/NewButton/buttonStyles.d.ts +2 -0
  79. package/lib-esm/NewButton/buttonStyles.js +3 -0
  80. package/lib-esm/NewButton/counter.d.ts +6 -0
  81. package/lib-esm/NewButton/counter.js +21 -0
  82. package/lib-esm/NewButton/index.d.ts +4 -0
  83. package/lib-esm/NewButton/index.js +3 -0
  84. package/lib-esm/Position.d.ts +4 -4
  85. package/lib-esm/ProgressBar.d.ts +1 -1
  86. package/lib-esm/SelectMenu/SelectMenu.d.ts +151 -153
  87. package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
  88. package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
  89. package/lib-esm/TextInputWithTokens.d.ts +11 -13
  90. package/lib-esm/Timeline.d.ts +33 -33
  91. package/lib-esm/Token/AvatarToken.d.ts +1 -1
  92. package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
  93. package/lib-esm/Token/Token.d.ts +1 -1
  94. package/lib-esm/_TextInputWrapper.d.ts +1 -1
  95. package/lib-esm/constants.js +3 -1
  96. package/lib-esm/index.d.ts +2 -0
  97. package/lib-esm/index.js +1 -0
  98. package/lib-esm/theme-preval.js +2 -2
  99. package/lib-esm/utils/testing.d.ts +1 -1
  100. package/package.json +13 -14
  101. package/lib/DatePicker/DatePicker.d.ts +0 -48
  102. package/lib/DatePicker/DatePicker.js +0 -106
  103. package/lib/DatePicker/DatePickerAnchor.d.ts +0 -5
  104. package/lib/DatePicker/DatePickerAnchor.js +0 -194
  105. package/lib/DatePicker/DatePickerOverlay.d.ts +0 -3
  106. package/lib/DatePicker/DatePickerOverlay.js +0 -48
  107. package/lib/DatePicker/DatePickerPanel.d.ts +0 -2
  108. package/lib/DatePicker/DatePickerPanel.js +0 -126
  109. package/lib/DatePicker/Day.d.ts +0 -14
  110. package/lib/DatePicker/Day.js +0 -190
  111. package/lib/DatePicker/Month.d.ts +0 -9
  112. package/lib/DatePicker/Month.js +0 -120
  113. package/lib/DatePicker/dateParser.d.ts +0 -11
  114. package/lib/DatePicker/dateParser.js +0 -188
  115. package/lib/DatePicker/index.d.ts +0 -2
  116. package/lib/DatePicker/index.js +0 -13
  117. package/lib/DatePicker/useDatePicker.d.ts +0 -89
  118. package/lib/DatePicker/useDatePicker.js +0 -370
  119. package/lib/hooks/useDebounce.d.ts +0 -2
  120. package/lib/hooks/useDebounce.js +0 -24
  121. package/lib-esm/DatePicker/DatePicker.d.ts +0 -48
  122. package/lib-esm/DatePicker/DatePicker.js +0 -89
  123. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +0 -5
  124. package/lib-esm/DatePicker/DatePickerAnchor.js +0 -167
  125. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +0 -3
  126. package/lib-esm/DatePicker/DatePickerOverlay.js +0 -29
  127. package/lib-esm/DatePicker/DatePickerPanel.d.ts +0 -2
  128. package/lib-esm/DatePicker/DatePickerPanel.js +0 -100
  129. package/lib-esm/DatePicker/Day.d.ts +0 -14
  130. package/lib-esm/DatePicker/Day.js +0 -167
  131. package/lib-esm/DatePicker/Month.d.ts +0 -9
  132. package/lib-esm/DatePicker/Month.js +0 -96
  133. package/lib-esm/DatePicker/dateParser.d.ts +0 -11
  134. package/lib-esm/DatePicker/dateParser.js +0 -174
  135. package/lib-esm/DatePicker/index.d.ts +0 -2
  136. package/lib-esm/DatePicker/index.js +0 -1
  137. package/lib-esm/DatePicker/useDatePicker.d.ts +0 -89
  138. package/lib-esm/DatePicker/useDatePicker.js +0 -339
  139. package/lib-esm/hooks/useDebounce.d.ts +0 -2
  140. package/lib-esm/hooks/useDebounce.js +0 -16
@@ -1,174 +0,0 @@
1
- import { eachDayOfInterval, format, isBefore } from 'date-fns';
2
- import { isRangeSelection } from './useDatePicker';
3
- const INVALID_DATE = 'Invalid Date';
4
- const DATE_REGEX = /(\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4})(?:\s?-\s?)?(\d{1,2}[-/.]\d{1,2}[-/.](?:\d{2}){1,2})?/;
5
-
6
- const sanitizeDate = dateString => {
7
- return dateString.replaceAll('.', '/').replaceAll('-', '/');
8
- };
9
-
10
- export const parseDate = (dateString, variant = 'single') => {
11
- const dateItems = dateString.matchAll(DATE_REGEX);
12
- const parsedDateItems = []; // Determine Format
13
-
14
- for (const d of dateItems) {
15
- const tempD1 = new Date(sanitizeDate(d[1]));
16
- const tempD2 = d[2] ? new Date(sanitizeDate(d[2])) : null;
17
-
18
- if (tempD2) {
19
- // Range
20
- if (tempD1.toString() !== INVALID_DATE && tempD2.toString() !== INVALID_DATE) {
21
- parsedDateItems.push(isBefore(tempD1, tempD2) ? {
22
- from: tempD1,
23
- to: tempD2
24
- } : {
25
- from: tempD2,
26
- to: tempD1
27
- });
28
- } else if (tempD1.toString() !== INVALID_DATE) {
29
- parsedDateItems.push(tempD1);
30
- } else if (tempD2.toString() !== INVALID_DATE) {
31
- parsedDateItems.push(tempD2);
32
- }
33
- } else {
34
- if (tempD1.toString() !== INVALID_DATE) {
35
- parsedDateItems.push(tempD1);
36
- }
37
- }
38
- }
39
-
40
- if (parsedDateItems.length === 0) {
41
- // No Valid Dates
42
- return [];
43
- } // Cast Format based on variant
44
-
45
-
46
- switch (variant) {
47
- case 'single':
48
- {
49
- return parsedDateItems[0];
50
- }
51
-
52
- case 'multi':
53
- {
54
- // TODO: Cast ranges as individual dates
55
- const expandedParsedItems = [];
56
-
57
- for (const item of parsedDateItems) {
58
- if (isRangeSelection(item)) {
59
- if (item.to) {
60
- expandedParsedItems.push([...eachDayOfInterval({
61
- start: item.from,
62
- end: item.to
63
- })]);
64
- } else {
65
- expandedParsedItems.push(item.from);
66
- }
67
- } else {
68
- if (item) expandedParsedItems.push([...item]);
69
- }
70
- }
71
-
72
- return expandedParsedItems;
73
- }
74
-
75
- case 'range':
76
- {
77
- // TODO: Filter to only ranges
78
- return parsedDateItems.filter(d => isRangeSelection(d))[0];
79
- }
80
-
81
- default:
82
- {
83
- return parsedDateItems[0];
84
- }
85
- }
86
- };
87
- export const formatDate = ({
88
- selection,
89
- anchorVariant = 'button',
90
- dateFormat = 'short',
91
- placeholder = 'Select a Date',
92
- variant = 'single'
93
- }) => {
94
- if (!selection) {
95
- if (anchorVariant === 'input') return '';
96
- return placeholder;
97
- }
98
-
99
- let template = 'MMM d';
100
-
101
- if (anchorVariant !== 'input' && dateFormat) {
102
- switch (dateFormat) {
103
- case 'short':
104
- template = 'MMM d';
105
- break;
106
-
107
- case 'long':
108
- template = 'MMM d, yyyy';
109
- break;
110
-
111
- default:
112
- template = dateFormat;
113
- break;
114
- }
115
- } else {
116
- template = 'MM/dd/yyyy';
117
- }
118
-
119
- switch (variant) {
120
- case 'single':
121
- {
122
- if (selection instanceof Date) {
123
- return format(selection, template);
124
- } else if (Array.isArray(selection)) {
125
- return format(selection[0], template);
126
- } else if (isRangeSelection(selection)) {
127
- return format(selection.from, template);
128
- } else {
129
- return 'Invalid Selection';
130
- }
131
- }
132
-
133
- case 'multi':
134
- {
135
- if (Array.isArray(selection)) {
136
- if (selection.length > 3) return `${selection.length} Selected`;
137
- const formatted = selection.map(d => format(d, template)).join(', ');
138
- return formatted;
139
- } else if (selection instanceof Date) {
140
- return [selection].map(d => format(d, template)).join(', ');
141
- } else if (isRangeSelection(selection)) {
142
- return [selection.to, selection.from].map(d => d ? format(d, template) : '').join(', ');
143
- } else {
144
- return 'Invalid Selection';
145
- }
146
- }
147
-
148
- case 'range':
149
- {
150
- if (isRangeSelection(selection)) {
151
- return Object.entries(selection).map(([_, date]) => date ? format(date, template) : '').join(' - ');
152
- } else if (selection instanceof Date) {
153
- return Object.entries({
154
- from: selection,
155
- to: null
156
- }).map(([_, date]) => date ? format(date, template) : '').join(' - ');
157
- } else if (Array.isArray(selection)) {
158
- return Object.entries({
159
- from: selection[0],
160
- to: selection[1]
161
- }) // to date can still be null
162
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
163
- .map(([_, date]) => date ? format(date, template) : '').join(' - ');
164
- } else {
165
- return 'Invalid Selection';
166
- }
167
- }
168
-
169
- default:
170
- {
171
- return 'Invalid Configuration';
172
- }
173
- }
174
- };
@@ -1,2 +0,0 @@
1
- export { DatePicker } from './DatePicker';
2
- export type { DatePickerProps } from './DatePicker';
@@ -1 +0,0 @@
1
- export { DatePicker } from './DatePicker';
@@ -1,89 +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
- maxRangeSize?: number;
16
- minDate?: Date | null;
17
- placeholder?: string;
18
- rangeIncrement?: number;
19
- showInputPrompt?: boolean;
20
- variant?: SelectionVariant;
21
- view?: '1-month' | '2-month';
22
- weekStartsOn?: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
23
- }
24
- export declare type RangeSelection = {
25
- from: Date;
26
- to: Date | null;
27
- };
28
- export declare type StringRangeSelection = {
29
- from: string;
30
- to: string;
31
- };
32
- export interface DatePickerContext {
33
- disabled?: boolean;
34
- configuration: DatePickerConfiguration;
35
- currentViewingDate: Date;
36
- goToMonth: (date: Date) => void;
37
- hoverRange?: RangeSelection | null;
38
- selection?: Selection;
39
- softSelection?: Partial<RangeSelection> | null;
40
- selectionActive?: boolean;
41
- formattedDate: string;
42
- nextMonth: () => void;
43
- onClose: () => void;
44
- onDateInput: (updatedSelection: Selection) => void;
45
- onDayFocus: (date: Date) => void;
46
- onSelection: (date: Date) => void;
47
- previousMonth: () => void;
48
- revertValue: () => void;
49
- saveValue: (selection?: Selection) => void;
50
- }
51
- export declare type Selection = Date | Array<Date> | RangeSelection | null;
52
- export declare type StringSelection = string | Array<string> | {
53
- to: string;
54
- from: string;
55
- } | null;
56
- export declare type DaySelection = boolean | 'start' | 'middle' | 'end';
57
- declare const useDatePicker: (date?: Date | undefined) => {
58
- blocked: boolean | undefined;
59
- disabled: boolean;
60
- selected: DaySelection;
61
- today: boolean;
62
- configuration: DatePickerConfiguration;
63
- currentViewingDate: Date;
64
- goToMonth: (date: Date) => void;
65
- hoverRange?: RangeSelection | null | undefined;
66
- selection?: Selection | undefined;
67
- softSelection?: Partial<RangeSelection> | null | undefined;
68
- selectionActive?: boolean | undefined;
69
- formattedDate: string;
70
- nextMonth: () => void;
71
- onClose: () => void;
72
- onDateInput: (updatedSelection: Selection) => void;
73
- onDayFocus: (date: Date) => void;
74
- onSelection: (date: Date) => void;
75
- previousMonth: () => void;
76
- revertValue: () => void;
77
- saveValue: (selection?: Selection | undefined) => void;
78
- };
79
- export default useDatePicker;
80
- export interface DatePickerProviderProps {
81
- closePicker?: () => void;
82
- configuration?: DatePickerConfiguration;
83
- value?: Selection | StringSelection;
84
- }
85
- export declare function isSingleSelection(selection: Selection): selection is Date;
86
- export declare function isMultiSelection(selection: Selection | StringSelection): selection is Array<Date> | Array<string>;
87
- export declare function isRangeSelection(selection: Selection | StringSelection): selection is RangeSelection | StringRangeSelection;
88
- export declare function isStringRangeSelection(selection: StringSelection): selection is StringRangeSelection;
89
- export declare const DatePickerProvider: React.FC<DatePickerProviderProps>;
@@ -1,339 +0,0 @@
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
- // validate date falls within range
233
- setSelection(updatedSelection);
234
- }, []);
235
- const selectionHandler = useCallback(date => {
236
- setIsDirty(true);
237
-
238
- if (configuration.variant === 'multi') {
239
- const selections = [...selection];
240
- const existingIndex = selections.findIndex(s => isEqual(s, date));
241
-
242
- if (existingIndex > -1) {
243
- selections.splice(existingIndex, 1);
244
- setSelection(selections.sort((a, b) => a.getTime() - b.getTime()));
245
- } else {
246
- if (configuration.maxSelections && selections.length + 1 > configuration.maxSelections) return;
247
- setSelection([...selections, date].sort((a, b) => a.getTime() - b.getTime()));
248
- }
249
- } else if (configuration.variant === 'range') {
250
- if (selection && isRangeSelection(selection) && !selection.to) {
251
- let toDate = date;
252
-
253
- if (configuration.maxRangeSize && Math.abs(differenceInDays(selection.from, date)) >= configuration.maxRangeSize) {
254
- toDate = isBefore(date, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
255
- }
256
-
257
- const updatedSelection = isBefore(toDate, selection.from) ? {
258
- from: toDate,
259
- to: selection.from
260
- } : {
261
- from: selection.from,
262
- to: toDate
263
- };
264
- setSelection(updatedSelection);
265
- setHoverRange(null);
266
-
267
- if (!configuration.confirmation) {
268
- saveValue(updatedSelection);
269
- }
270
- } else {
271
- setHoverRange({
272
- from: date,
273
- to: date
274
- });
275
- setSelection({
276
- from: date,
277
- to: null
278
- });
279
- }
280
- } else {
281
- setSelection(date);
282
-
283
- if (!configuration.confirmation) {
284
- saveValue(date);
285
- }
286
- }
287
- }, [configuration.confirmation, configuration.maxRangeSize, configuration.maxSelections, configuration.variant, saveValue, selection]);
288
- const focusHnadler = useCallback(date => {
289
- if (!selection) return;
290
- const {
291
- minDate,
292
- maxDate,
293
- maxRangeSize,
294
- variant
295
- } = configuration;
296
-
297
- if (variant === 'range' && isRangeSelection(selection) && hoverRange) {
298
- let hoverDate = date;
299
- if (minDate) hoverDate = isBefore(date, minDate) ? minDate : hoverDate;
300
- if (maxDate) hoverDate = isAfter(date, maxDate) ? maxDate : hoverDate;
301
-
302
- if (maxRangeSize && Math.abs(differenceInDays(selection.from, hoverDate)) >= maxRangeSize) {
303
- hoverDate = isBefore(hoverDate, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
304
- }
305
-
306
- setHoverRange(isBefore(hoverDate, selection.from) ? {
307
- from: hoverDate,
308
- to: selection.from
309
- } : {
310
- from: selection.from,
311
- to: hoverDate
312
- });
313
- }
314
- }, [configuration, hoverRange, selection]);
315
- const datePickerCtx = useMemo(() => {
316
- return {
317
- configuration,
318
- currentViewingDate,
319
- disabled: false,
320
- formattedDate: getFormattedDate,
321
- goToMonth,
322
- hoverRange,
323
- nextMonth,
324
- onClose: handleClose,
325
- onDateInput: inputHandler,
326
- onDayFocus: focusHnadler,
327
- onSelection: selectionHandler,
328
- previousMonth,
329
- revertValue,
330
- saveValue,
331
- selectionActive: false,
332
- selection
333
- };
334
- }, [configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, handleClose, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
335
- return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
336
- value: datePickerCtx
337
- }, children);
338
- };
339
- DatePickerProvider.displayName = "DatePickerProvider";
@@ -1,2 +0,0 @@
1
- declare function useDebounce<T>(value: T, delay: number): T;
2
- export default useDebounce;
@@ -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;