@primer/components 0.0.0-202192719508 → 0.0.0-2021927202224

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 (74) hide show
  1. package/CHANGELOG.md +23 -1
  2. package/dist/browser.esm.js +54 -53
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +51 -50
  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/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  9. package/lib/Autocomplete/Autocomplete.d.ts +2 -4
  10. package/lib/Autocomplete/AutocompleteInput.d.ts +2 -4
  11. package/lib/Breadcrumbs.d.ts +8 -7
  12. package/lib/Breadcrumbs.js +7 -12
  13. package/lib/SelectMenu/SelectMenu.d.ts +2 -4
  14. package/lib/TextInputWithTokens.d.ts +6 -4
  15. package/lib/TextInputWithTokens.js +102 -29
  16. package/lib/Token/Token.js +13 -2
  17. package/lib/Token/TokenBase.js +0 -4
  18. package/lib/Token/_RemoveTokenButton.js +15 -2
  19. package/lib/_TextInputWrapper.js +1 -1
  20. package/lib/theme-preval.js +2 -2
  21. package/lib/utils/testing.d.ts +1 -1
  22. package/lib-esm/ActionList/Item.d.ts +6 -0
  23. package/lib-esm/ActionList/Item.js +5 -1
  24. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  25. package/lib-esm/Autocomplete/Autocomplete.d.ts +2 -4
  26. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +2 -4
  27. package/lib-esm/Breadcrumbs.d.ts +8 -7
  28. package/lib-esm/Breadcrumbs.js +8 -13
  29. package/lib-esm/SelectMenu/SelectMenu.d.ts +2 -4
  30. package/lib-esm/TextInputWithTokens.d.ts +6 -4
  31. package/lib-esm/TextInputWithTokens.js +101 -30
  32. package/lib-esm/Token/Token.js +13 -2
  33. package/lib-esm/Token/TokenBase.js +0 -4
  34. package/lib-esm/Token/_RemoveTokenButton.js +11 -2
  35. package/lib-esm/_TextInputWrapper.js +1 -1
  36. package/lib-esm/theme-preval.js +2 -2
  37. package/lib-esm/utils/testing.d.ts +1 -1
  38. package/package.json +13 -14
  39. package/lib/DatePicker/DatePicker.d.ts +0 -48
  40. package/lib/DatePicker/DatePicker.js +0 -104
  41. package/lib/DatePicker/DatePickerAnchor.d.ts +0 -5
  42. package/lib/DatePicker/DatePickerAnchor.js +0 -182
  43. package/lib/DatePicker/DatePickerOverlay.d.ts +0 -3
  44. package/lib/DatePicker/DatePickerOverlay.js +0 -48
  45. package/lib/DatePicker/DatePickerPanel.d.ts +0 -2
  46. package/lib/DatePicker/DatePickerPanel.js +0 -126
  47. package/lib/DatePicker/Day.d.ts +0 -14
  48. package/lib/DatePicker/Day.js +0 -190
  49. package/lib/DatePicker/Month.d.ts +0 -9
  50. package/lib/DatePicker/Month.js +0 -120
  51. package/lib/DatePicker/index.d.ts +0 -2
  52. package/lib/DatePicker/index.js +0 -13
  53. package/lib/DatePicker/useDatePicker.d.ts +0 -88
  54. package/lib/DatePicker/useDatePicker.js +0 -421
  55. package/lib/hooks/useDebounce.d.ts +0 -2
  56. package/lib/hooks/useDebounce.js +0 -24
  57. package/lib-esm/DatePicker/DatePicker.d.ts +0 -48
  58. package/lib-esm/DatePicker/DatePicker.js +0 -87
  59. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +0 -5
  60. package/lib-esm/DatePicker/DatePickerAnchor.js +0 -155
  61. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +0 -3
  62. package/lib-esm/DatePicker/DatePickerOverlay.js +0 -29
  63. package/lib-esm/DatePicker/DatePickerPanel.d.ts +0 -2
  64. package/lib-esm/DatePicker/DatePickerPanel.js +0 -100
  65. package/lib-esm/DatePicker/Day.d.ts +0 -14
  66. package/lib-esm/DatePicker/Day.js +0 -167
  67. package/lib-esm/DatePicker/Month.d.ts +0 -9
  68. package/lib-esm/DatePicker/Month.js +0 -96
  69. package/lib-esm/DatePicker/index.d.ts +0 -2
  70. package/lib-esm/DatePicker/index.js +0 -1
  71. package/lib-esm/DatePicker/useDatePicker.d.ts +0 -88
  72. package/lib-esm/DatePicker/useDatePicker.js +0 -391
  73. package/lib-esm/hooks/useDebounce.d.ts +0 -2
  74. package/lib-esm/hooks/useDebounce.js +0 -16
@@ -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,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;