@primer/components 0.0.0-2021104155230 → 0.0.0-2021104181937

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 (158) hide show
  1. package/CHANGELOG.md +1 -3
  2. package/dist/browser.esm.js +474 -473
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +98 -97
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
  7. package/lib/Autocomplete/Autocomplete.d.ts +30 -28
  8. package/lib/Autocomplete/AutocompleteInput.d.ts +30 -28
  9. package/lib/Button/Button.d.ts +24 -24
  10. package/lib/Button/Button.js +1 -1
  11. package/lib/Button/ButtonClose.d.ts +43 -43
  12. package/lib/Button/ButtonDanger.d.ts +24 -24
  13. package/lib/Button/ButtonInvisible.d.ts +24 -24
  14. package/lib/Button/ButtonOutline.d.ts +24 -24
  15. package/lib/Button/ButtonPrimary.d.ts +24 -24
  16. package/lib/CircleOcticon.d.ts +40 -40
  17. package/lib/DatePicker/DatePicker.d.ts +52 -0
  18. package/lib/DatePicker/DatePicker.js +109 -0
  19. package/lib/DatePicker/DatePickerAnchor.d.ts +5 -0
  20. package/lib/DatePicker/DatePickerAnchor.js +202 -0
  21. package/lib/DatePicker/DatePickerOverlay.d.ts +3 -0
  22. package/lib/DatePicker/DatePickerOverlay.js +55 -0
  23. package/lib/DatePicker/DatePickerPanel.d.ts +2 -0
  24. package/lib/DatePicker/DatePickerPanel.js +363 -0
  25. package/lib/DatePicker/Day.d.ts +15 -0
  26. package/lib/DatePicker/Day.js +204 -0
  27. package/lib/DatePicker/Month.d.ts +8 -0
  28. package/lib/DatePicker/Month.js +122 -0
  29. package/lib/DatePicker/dateParser.d.ts +12 -0
  30. package/lib/DatePicker/dateParser.js +193 -0
  31. package/lib/DatePicker/index.d.ts +2 -0
  32. package/lib/DatePicker/index.js +13 -0
  33. package/lib/DatePicker/useDatePicker.d.ts +107 -0
  34. package/lib/DatePicker/useDatePicker.js +561 -0
  35. package/lib/Dialog.d.ts +43 -43
  36. package/lib/Dropdown.d.ts +168 -168
  37. package/lib/DropdownMenu/DropdownButton.d.ts +44 -44
  38. package/lib/FilterList.d.ts +40 -40
  39. package/lib/Position.d.ts +4 -4
  40. package/lib/SelectMenu/SelectMenu.d.ts +239 -237
  41. package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
  42. package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
  43. package/lib/TextInputWithTokens.d.ts +30 -28
  44. package/lib/Timeline.d.ts +41 -41
  45. package/lib/Token/AvatarToken.d.ts +1 -1
  46. package/lib/Token/IssueLabelToken.d.ts +1 -1
  47. package/lib/Token/Token.d.ts +1 -1
  48. package/lib/hooks/useDebounce.d.ts +2 -0
  49. package/lib/hooks/useDebounce.js +24 -0
  50. package/lib/hooks/useResizeObserver.d.ts +1 -1
  51. package/lib/hooks/useResizeObserver.js +1 -1
  52. package/lib/sx.d.ts +0 -2
  53. package/lib/sx.js +0 -8
  54. package/lib/theme-preval.js +0 -79
  55. package/lib/utils/testing.d.ts +0 -13
  56. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
  57. package/lib-esm/Autocomplete/Autocomplete.d.ts +30 -28
  58. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +30 -28
  59. package/lib-esm/Button/Button.d.ts +24 -24
  60. package/lib-esm/Button/Button.js +1 -1
  61. package/lib-esm/Button/ButtonClose.d.ts +43 -43
  62. package/lib-esm/Button/ButtonDanger.d.ts +24 -24
  63. package/lib-esm/Button/ButtonInvisible.d.ts +24 -24
  64. package/lib-esm/Button/ButtonOutline.d.ts +24 -24
  65. package/lib-esm/Button/ButtonPrimary.d.ts +24 -24
  66. package/lib-esm/CircleOcticon.d.ts +40 -40
  67. package/lib-esm/DatePicker/DatePicker.d.ts +52 -0
  68. package/lib-esm/DatePicker/DatePicker.js +92 -0
  69. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +5 -0
  70. package/lib-esm/DatePicker/DatePickerAnchor.js +174 -0
  71. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +3 -0
  72. package/lib-esm/DatePicker/DatePickerOverlay.js +40 -0
  73. package/lib-esm/DatePicker/DatePickerPanel.d.ts +2 -0
  74. package/lib-esm/DatePicker/DatePickerPanel.js +332 -0
  75. package/lib-esm/DatePicker/Day.d.ts +15 -0
  76. package/lib-esm/DatePicker/Day.js +180 -0
  77. package/lib-esm/DatePicker/Month.d.ts +8 -0
  78. package/lib-esm/DatePicker/Month.js +98 -0
  79. package/lib-esm/DatePicker/dateParser.d.ts +12 -0
  80. package/lib-esm/DatePicker/dateParser.js +179 -0
  81. package/lib-esm/DatePicker/index.d.ts +2 -0
  82. package/lib-esm/DatePicker/index.js +1 -0
  83. package/lib-esm/DatePicker/useDatePicker.d.ts +107 -0
  84. package/lib-esm/DatePicker/useDatePicker.js +526 -0
  85. package/lib-esm/Dialog.d.ts +43 -43
  86. package/lib-esm/Dropdown.d.ts +168 -168
  87. package/lib-esm/DropdownMenu/DropdownButton.d.ts +44 -44
  88. package/lib-esm/FilterList.d.ts +40 -40
  89. package/lib-esm/Position.d.ts +4 -4
  90. package/lib-esm/SelectMenu/SelectMenu.d.ts +239 -237
  91. package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
  92. package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
  93. package/lib-esm/TextInputWithTokens.d.ts +30 -28
  94. package/lib-esm/Timeline.d.ts +41 -41
  95. package/lib-esm/Token/AvatarToken.d.ts +1 -1
  96. package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
  97. package/lib-esm/Token/Token.d.ts +1 -1
  98. package/lib-esm/hooks/useDebounce.d.ts +2 -0
  99. package/lib-esm/hooks/useDebounce.js +16 -0
  100. package/lib-esm/hooks/useResizeObserver.d.ts +1 -1
  101. package/lib-esm/hooks/useResizeObserver.js +1 -1
  102. package/lib-esm/sx.d.ts +0 -2
  103. package/lib-esm/sx.js +1 -3
  104. package/lib-esm/theme-preval.js +0 -79
  105. package/lib-esm/utils/testing.d.ts +0 -13
  106. package/package.json +9 -10
  107. package/lib/ActionList2/Description.d.ts +0 -12
  108. package/lib/ActionList2/Description.js +0 -53
  109. package/lib/ActionList2/Divider.d.ts +0 -5
  110. package/lib/ActionList2/Divider.js +0 -35
  111. package/lib/ActionList2/Group.d.ts +0 -11
  112. package/lib/ActionList2/Group.js +0 -57
  113. package/lib/ActionList2/Header.d.ts +0 -26
  114. package/lib/ActionList2/Header.js +0 -55
  115. package/lib/ActionList2/Item.d.ts +0 -71
  116. package/lib/ActionList2/Item.js +0 -223
  117. package/lib/ActionList2/LinkItem.d.ts +0 -17
  118. package/lib/ActionList2/LinkItem.js +0 -57
  119. package/lib/ActionList2/List.d.ts +0 -26
  120. package/lib/ActionList2/List.js +0 -51
  121. package/lib/ActionList2/Selection.d.ts +0 -5
  122. package/lib/ActionList2/Selection.js +0 -70
  123. package/lib/ActionList2/Visuals.d.ts +0 -11
  124. package/lib/ActionList2/Visuals.js +0 -90
  125. package/lib/ActionList2/index.d.ts +0 -40
  126. package/lib/ActionList2/index.js +0 -47
  127. package/lib/experiments.d.ts +0 -1
  128. package/lib/experiments.js +0 -18
  129. package/lib/utils/create-slots.d.ts +0 -17
  130. package/lib/utils/create-slots.js +0 -105
  131. package/lib/utils/use-force-update.d.ts +0 -1
  132. package/lib/utils/use-force-update.js +0 -19
  133. package/lib-esm/ActionList2/Description.d.ts +0 -12
  134. package/lib-esm/ActionList2/Description.js +0 -37
  135. package/lib-esm/ActionList2/Divider.d.ts +0 -5
  136. package/lib-esm/ActionList2/Divider.js +0 -23
  137. package/lib-esm/ActionList2/Group.d.ts +0 -11
  138. package/lib-esm/ActionList2/Group.js +0 -40
  139. package/lib-esm/ActionList2/Header.d.ts +0 -26
  140. package/lib-esm/ActionList2/Header.js +0 -44
  141. package/lib-esm/ActionList2/Item.d.ts +0 -71
  142. package/lib-esm/ActionList2/Item.js +0 -197
  143. package/lib-esm/ActionList2/LinkItem.d.ts +0 -17
  144. package/lib-esm/ActionList2/LinkItem.js +0 -43
  145. package/lib-esm/ActionList2/List.d.ts +0 -26
  146. package/lib-esm/ActionList2/List.js +0 -34
  147. package/lib-esm/ActionList2/Selection.d.ts +0 -5
  148. package/lib-esm/ActionList2/Selection.js +0 -52
  149. package/lib-esm/ActionList2/Visuals.d.ts +0 -11
  150. package/lib-esm/ActionList2/Visuals.js +0 -68
  151. package/lib-esm/ActionList2/index.d.ts +0 -40
  152. package/lib-esm/ActionList2/index.js +0 -33
  153. package/lib-esm/experiments.d.ts +0 -1
  154. package/lib-esm/experiments.js +0 -2
  155. package/lib-esm/utils/create-slots.d.ts +0 -17
  156. package/lib-esm/utils/create-slots.js +0 -84
  157. package/lib-esm/utils/use-force-update.d.ts +0 -1
  158. package/lib-esm/utils/use-force-update.js +0 -6
@@ -0,0 +1,179 @@
1
+ import { eachDayOfInterval, format, isBefore } from 'date-fns';
2
+ import { isRangeSelection } from './useDatePicker';
3
+ const INVALID_DATE = 'Invalid Date';
4
+ const DATE_REGEX = new RegExp(/(\d{1,2}[-/.]\d{1,2}[-/.](?:\d{2}){1,2})(?:\s?-\s?)?(\d{1,2}[-/.]\d{1,2}[-/.](?:\d{2}){1,2})?/g);
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 null;
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
+ const expandedParsedItems = [];
55
+
56
+ for (const item of parsedDateItems) {
57
+ if (isRangeSelection(item)) {
58
+ if (item.to) {
59
+ eachDayOfInterval({
60
+ start: item.from,
61
+ end: item.to
62
+ }).map(d => expandedParsedItems.push(d));
63
+ } else {
64
+ expandedParsedItems.push(item.from);
65
+ }
66
+ } else {
67
+ if (item) {
68
+ if (Array.isArray(item)) {
69
+ item.map(d => expandedParsedItems.push(d));
70
+ } else {
71
+ expandedParsedItems.push(item);
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ return expandedParsedItems;
78
+ }
79
+
80
+ case 'range':
81
+ {
82
+ return parsedDateItems.filter(d => isRangeSelection(d))[0];
83
+ }
84
+
85
+ default:
86
+ {
87
+ return parsedDateItems[0];
88
+ }
89
+ }
90
+ };
91
+ export const formatDate = ({
92
+ selection,
93
+ dateFormat = 'short',
94
+ placeholder = 'Choose Date',
95
+ rawFormat = false,
96
+ variant = 'single'
97
+ }) => {
98
+ if (!selection) {
99
+ if (rawFormat) return '';
100
+ return placeholder;
101
+ }
102
+
103
+ let template = 'MMM d';
104
+
105
+ if (!rawFormat && dateFormat) {
106
+ switch (dateFormat) {
107
+ case 'short':
108
+ template = 'MMM d';
109
+ break;
110
+
111
+ case 'long':
112
+ template = 'MMM d, yyyy';
113
+ break;
114
+
115
+ default:
116
+ template = dateFormat;
117
+ break;
118
+ }
119
+ } else {
120
+ template = 'MM/dd/yyyy';
121
+ }
122
+
123
+ switch (variant) {
124
+ case 'single':
125
+ {
126
+ if (selection instanceof Date) {
127
+ return format(selection, template);
128
+ } else if (Array.isArray(selection)) {
129
+ return format(selection[0], template);
130
+ } else if (isRangeSelection(selection)) {
131
+ return format(selection.from, template);
132
+ } else {
133
+ return 'Invalid Selection';
134
+ }
135
+ }
136
+
137
+ case 'multi':
138
+ {
139
+ if (Array.isArray(selection)) {
140
+ if (selection.length > 3 && !rawFormat) return `${selection.length} Selected`;
141
+ if (selection.length === 0 && !rawFormat) return placeholder;
142
+ const formatted = selection.map(d => format(d, template)).join(', ');
143
+ return formatted;
144
+ } else if (selection instanceof Date) {
145
+ return [selection].map(d => format(d, template)).join(', ');
146
+ } else if (isRangeSelection(selection)) {
147
+ return [selection.to, selection.from].map(d => d ? format(d, template) : '').join(', ');
148
+ } else {
149
+ return 'Invalid Selection';
150
+ }
151
+ }
152
+
153
+ case 'range':
154
+ {
155
+ if (isRangeSelection(selection)) {
156
+ return Object.entries(selection).map(([_, date]) => date ? format(date, template) : '').join(' - ');
157
+ } else if (selection instanceof Date) {
158
+ return Object.entries({
159
+ from: selection,
160
+ to: null
161
+ }).map(([_, date]) => date ? format(date, template) : '').join(' - ');
162
+ } else if (Array.isArray(selection)) {
163
+ return Object.entries({
164
+ from: selection[0],
165
+ to: selection[1]
166
+ }) // to date can still be null
167
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
168
+ .map(([_, date]) => date ? format(date, template) : '').join(' - ');
169
+ } else {
170
+ return 'Invalid Selection';
171
+ }
172
+ }
173
+
174
+ default:
175
+ {
176
+ return 'Invalid Configuration';
177
+ }
178
+ }
179
+ };
@@ -0,0 +1,2 @@
1
+ export { DatePicker } from './DatePicker';
2
+ export type { DatePickerProps } from './DatePicker';
@@ -0,0 +1 @@
1
+ export { DatePicker } from './DatePicker';
@@ -0,0 +1,107 @@
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
+ compressedHeader?: boolean;
11
+ dateFormat?: DateFormat;
12
+ disableWeekends?: boolean;
13
+ iconPlacement?: 'start' | 'end' | 'none';
14
+ maxDate?: Date | null;
15
+ maxSelections?: number;
16
+ maxRangeSize?: number;
17
+ minDate?: Date | null;
18
+ placeholder?: string;
19
+ rangeIncrement?: number;
20
+ showInputPrompt?: boolean;
21
+ variant?: SelectionVariant;
22
+ view?: '1-month' | '2-month';
23
+ weekStartsOn?: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
24
+ }
25
+ export declare type RangeSelection = {
26
+ from: Date;
27
+ to: Date | null;
28
+ };
29
+ export declare type StringRangeSelection = {
30
+ from: string;
31
+ to: string;
32
+ };
33
+ export interface DatePickerContext {
34
+ disabled?: boolean;
35
+ configuration: DatePickerConfiguration;
36
+ currentViewingDate: Date;
37
+ dialogOpen: boolean;
38
+ focusDate: Date;
39
+ formattedDate: string;
40
+ goToMonth: (date: Date) => void;
41
+ inputDate: string;
42
+ hoverRange?: RangeSelection | null;
43
+ nextMonth: () => void;
44
+ onClose: () => void;
45
+ onDateInput: (updatedSelection: Selection) => void;
46
+ onDayFocus: (date: Date) => void;
47
+ onSelection: (date: Date) => void;
48
+ previousMonth: () => void;
49
+ revertValue: () => void;
50
+ saveValue: (selection?: Selection) => void;
51
+ selection?: Selection;
52
+ selectionActive?: boolean;
53
+ setFocusDate: React.Dispatch<React.SetStateAction<Date>>;
54
+ setHoverRange: React.Dispatch<React.SetStateAction<RangeSelection | null>>;
55
+ softSelection?: Partial<RangeSelection> | null;
56
+ setDialogOpen: React.Dispatch<React.SetStateAction<boolean>>;
57
+ viewMode: '1-month' | '2-month';
58
+ }
59
+ export declare type Selection = Date | Array<Date> | RangeSelection | null;
60
+ export declare type StringSelection = string | Array<string> | {
61
+ to: string;
62
+ from: string;
63
+ } | null;
64
+ export declare type DaySelection = boolean | 'start' | 'middle' | 'end';
65
+ declare const useDatePicker: (date?: Date | undefined) => {
66
+ blocked: boolean | undefined;
67
+ disabled: boolean;
68
+ focused: boolean;
69
+ selected: DaySelection;
70
+ today: boolean;
71
+ configuration: DatePickerConfiguration;
72
+ currentViewingDate: Date;
73
+ dialogOpen: boolean;
74
+ focusDate: Date;
75
+ formattedDate: string;
76
+ goToMonth: (date: Date) => void;
77
+ inputDate: string;
78
+ hoverRange?: RangeSelection | null | undefined;
79
+ nextMonth: () => void;
80
+ onClose: () => void;
81
+ onDateInput: (updatedSelection: Selection) => void;
82
+ onDayFocus: (date: Date) => void;
83
+ onSelection: (date: Date) => void;
84
+ previousMonth: () => void;
85
+ revertValue: () => void;
86
+ saveValue: (selection?: Selection | undefined) => void;
87
+ selection?: Selection | undefined;
88
+ selectionActive?: boolean | undefined;
89
+ setFocusDate: React.Dispatch<React.SetStateAction<Date>>;
90
+ setHoverRange: React.Dispatch<React.SetStateAction<RangeSelection | null>>;
91
+ softSelection?: Partial<RangeSelection> | null | undefined;
92
+ setDialogOpen: React.Dispatch<React.SetStateAction<boolean>>;
93
+ viewMode: '1-month' | '2-month';
94
+ };
95
+ export default useDatePicker;
96
+ export interface DatePickerProviderProps {
97
+ closePicker?: () => void;
98
+ configuration?: DatePickerConfiguration;
99
+ isOpen?: boolean;
100
+ value?: Selection | StringSelection;
101
+ }
102
+ export declare function isSingleSelection(selection: Selection): selection is Date;
103
+ export declare function isMultiSelection(selection: Selection | StringSelection): selection is Array<Date> | Array<string>;
104
+ export declare function isRangeSelection(selection: Selection | StringSelection): selection is RangeSelection | StringRangeSelection;
105
+ export declare function isStringRangeSelection(selection: StringSelection): selection is StringRangeSelection;
106
+ export declare const normalizeDate: (date: Date | string) => Date;
107
+ export declare const DatePickerProvider: React.FC<DatePickerProviderProps>;