@superdispatch/dates 0.21.6 → 0.21.13

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 (62) hide show
  1. package/.babelrc.js +5 -0
  2. package/.turbo/turbo-version.log +26 -0
  3. package/package.json +58 -32
  4. package/pkg/README.md +10 -0
  5. package/{dist-types → pkg/dist-types}/index.d.ts +237 -237
  6. package/pkg/package.json +32 -0
  7. package/playroom.ts +10 -0
  8. package/src/__tests__/index.spec.ts +48 -0
  9. package/src/base-date-picker/BaseDatePicker.tsx +145 -0
  10. package/src/calendar/Calendar.playroom.tsx +28 -0
  11. package/src/calendar/Calendar.spec.tsx +531 -0
  12. package/src/calendar/Calendar.stories.tsx +50 -0
  13. package/src/calendar/Calendar.tsx +534 -0
  14. package/src/calendar/CalendarQuickSelection.tsx +34 -0
  15. package/src/calendar/InternalCalendarComponents.tsx +79 -0
  16. package/src/date-config/DateConfig.spec.tsx +23 -0
  17. package/src/date-config/DateConfig.tsx +60 -0
  18. package/src/date-field/DateField.playroom.tsx +21 -0
  19. package/src/date-field/DateField.spec.tsx +350 -0
  20. package/src/date-field/DateField.stories.tsx +47 -0
  21. package/src/date-field/DateField.tsx +155 -0
  22. package/src/date-range-field/DateRangeField.playroom.tsx +24 -0
  23. package/src/date-range-field/DateRangeField.spec.tsx +318 -0
  24. package/src/date-range-field/DateRangeField.stories.tsx +51 -0
  25. package/src/date-range-field/DateRangeField.tsx +277 -0
  26. package/src/date-time-utils/DateTimeUtils.spec.ts +652 -0
  27. package/src/date-time-utils/DateTimeUtils.ts +339 -0
  28. package/src/date-utils/DateUtils.spec.ts +234 -0
  29. package/src/date-utils/DateUtils.ts +333 -0
  30. package/src/formatted-date/FormattedDate.spec.tsx +103 -0
  31. package/src/formatted-date/FormattedDate.ts +42 -0
  32. package/src/formatted-relative-time/FormattedRelativeTime.spec.tsx +93 -0
  33. package/src/formatted-relative-time/FormattedRelativeTime.ts +60 -0
  34. package/src/index.ts +12 -0
  35. package/src/time-field/TimeField.playroom.tsx +21 -0
  36. package/src/time-field/TimeField.stories.tsx +35 -0
  37. package/src/time-field/TimeField.tsx +221 -0
  38. package/src/use-date-time/useDateTime.spec.ts +45 -0
  39. package/src/use-date-time/useDateTime.ts +31 -0
  40. package/src/use-date-time-range/useDateTimeRange.spec.ts +53 -0
  41. package/src/use-date-time-range/useDateTimeRange.ts +24 -0
  42. package/tsconfig.json +19 -0
  43. package/LICENSE +0 -21
  44. /package/{dist-node → pkg/dist-node}/index.js +0 -0
  45. /package/{dist-node → pkg/dist-node}/index.js.map +0 -0
  46. /package/{dist-src → pkg/dist-src}/base-date-picker/BaseDatePicker.js +0 -0
  47. /package/{dist-src → pkg/dist-src}/calendar/Calendar.js +0 -0
  48. /package/{dist-src → pkg/dist-src}/calendar/CalendarQuickSelection.js +0 -0
  49. /package/{dist-src → pkg/dist-src}/calendar/InternalCalendarComponents.js +0 -0
  50. /package/{dist-src → pkg/dist-src}/date-config/DateConfig.js +0 -0
  51. /package/{dist-src → pkg/dist-src}/date-field/DateField.js +0 -0
  52. /package/{dist-src → pkg/dist-src}/date-range-field/DateRangeField.js +0 -0
  53. /package/{dist-src → pkg/dist-src}/date-time-utils/DateTimeUtils.js +0 -0
  54. /package/{dist-src → pkg/dist-src}/date-utils/DateUtils.js +0 -0
  55. /package/{dist-src → pkg/dist-src}/formatted-date/FormattedDate.js +0 -0
  56. /package/{dist-src → pkg/dist-src}/formatted-relative-time/FormattedRelativeTime.js +0 -0
  57. /package/{dist-src → pkg/dist-src}/index.js +0 -0
  58. /package/{dist-src → pkg/dist-src}/time-field/TimeField.js +0 -0
  59. /package/{dist-src → pkg/dist-src}/use-date-time/useDateTime.js +0 -0
  60. /package/{dist-src → pkg/dist-src}/use-date-time-range/useDateTimeRange.js +0 -0
  61. /package/{dist-web → pkg/dist-web}/index.js +0 -0
  62. /package/{dist-web → pkg/dist-web}/index.js.map +0 -0
@@ -0,0 +1,221 @@
1
+ import {
2
+ BaseTextFieldProps,
3
+ InputBaseProps,
4
+ TextField,
5
+ } from '@material-ui/core';
6
+ import { Autocomplete, FilterOptionsState } from '@material-ui/lab';
7
+ import { DateTime } from 'luxon';
8
+ import { forwardRef, useEffect, useMemo, useState } from 'react';
9
+ import {
10
+ DateConfig,
11
+ DateFormat,
12
+ useDateConfig,
13
+ } from '../date-config/DateConfig';
14
+ import {
15
+ DatePayload,
16
+ formatDate,
17
+ NullableDateInput,
18
+ toDatePayload,
19
+ } from '../date-time-utils/DateTimeUtils';
20
+ import { useDateTime } from '../use-date-time/useDateTime';
21
+
22
+ const TIME_MATCH_FORMATS = [
23
+ 'h:mm a',
24
+ 'h:mma',
25
+ 'H:mm',
26
+ 'h:mm',
27
+ 'hmm',
28
+ 'Hmm',
29
+ 'h',
30
+ 'H',
31
+ ];
32
+
33
+ interface TimeFieldOption {
34
+ value: number;
35
+ label: string;
36
+ pattern: RegExp;
37
+ }
38
+
39
+ function toTimeFieldOption(
40
+ date: DateTime,
41
+ config: DateConfig,
42
+ ): TimeFieldOption {
43
+ return {
44
+ value: date.valueOf(),
45
+ label: formatDate(date, { variant: 'Time' }, config),
46
+ pattern: new RegExp(
47
+ `^(${TIME_MATCH_FORMATS.map((format) => date.toFormat(format)).join(
48
+ '|',
49
+ )})`,
50
+ 'i',
51
+ ),
52
+ };
53
+ }
54
+
55
+ function normalizeInputValue(inputValue: string): string {
56
+ return inputValue.replace(/[\s]/g, '').toLowerCase();
57
+ }
58
+
59
+ function makeOptions(
60
+ config: DateConfig,
61
+ ): [
62
+ options: TimeFieldOption[],
63
+ filterOptions: (
64
+ options: TimeFieldOption[],
65
+ state: FilterOptionsState<TimeFieldOption>,
66
+ ) => TimeFieldOption[],
67
+ ] {
68
+ const options: TimeFieldOption[] = [];
69
+ const now = DateTime.local().startOf('day');
70
+
71
+ for (let i = 0; i < 96; i++) {
72
+ options.push(toTimeFieldOption(now.set({ minute: i * 15 }), config));
73
+ }
74
+
75
+ const cache = new Map<string, TimeFieldOption[]>();
76
+
77
+ return [
78
+ options,
79
+ (_, { inputValue }) => {
80
+ const query = normalizeInputValue(inputValue);
81
+
82
+ if (!query) {
83
+ return options;
84
+ }
85
+
86
+ let filtered = cache.get(query);
87
+
88
+ if (!filtered) {
89
+ filtered = options.filter((option) => option.pattern.test(query));
90
+ cache.set(query, filtered);
91
+ }
92
+
93
+ return filtered;
94
+ },
95
+ ];
96
+ }
97
+
98
+ export interface TimeFieldProps
99
+ extends Pick<
100
+ BaseTextFieldProps,
101
+ | 'disabled'
102
+ | 'error'
103
+ | 'fullWidth'
104
+ | 'helperText'
105
+ | 'id'
106
+ | 'label'
107
+ | 'name'
108
+ | 'placeholder'
109
+ | 'required'
110
+ > {
111
+ format?: DateFormat;
112
+ value?: NullableDateInput;
113
+ onChange?: (value: DatePayload) => void;
114
+
115
+ InputProps?: Pick<InputBaseProps, 'startAdornment'>;
116
+ }
117
+
118
+ export const TimeField = forwardRef<HTMLDivElement, TimeFieldProps>(
119
+ (
120
+ {
121
+ disabled,
122
+ onChange,
123
+
124
+ value: valueProp,
125
+ format: formatProp,
126
+ ...props
127
+ },
128
+ ref,
129
+ ) => {
130
+ const config = useDateConfig({ format: formatProp });
131
+ const date = useDateTime(valueProp, config);
132
+ const selectedOption = useMemo(
133
+ () => (!date.isValid ? undefined : toTimeFieldOption(date, config)),
134
+ [date, config],
135
+ );
136
+ const [options, filterOptions] = useMemo(
137
+ () => makeOptions(config),
138
+ [config],
139
+ );
140
+
141
+ const [inputValue, setInputValue] = useState('');
142
+
143
+ function handleChange(nextValue: NullableDateInput): void {
144
+ if (onChange) {
145
+ onChange(toDatePayload(nextValue, config));
146
+ }
147
+ }
148
+
149
+ function handleType(text: string): void {
150
+ text = normalizeInputValue(text);
151
+
152
+ for (const timeFormat of TIME_MATCH_FORMATS) {
153
+ let nextDate = DateTime.fromFormat(text, timeFormat);
154
+
155
+ if (nextDate.isValid) {
156
+ if (onChange) {
157
+ if (date.isValid) {
158
+ nextDate = nextDate.set({
159
+ year: date.year,
160
+ month: date.month,
161
+ day: date.day,
162
+ });
163
+ }
164
+
165
+ onChange(toDatePayload(nextDate, config));
166
+ }
167
+
168
+ return;
169
+ }
170
+ }
171
+
172
+ setInputValue(selectedOption?.label || '');
173
+ }
174
+
175
+ useEffect(() => {
176
+ if (!date.isValid) {
177
+ setInputValue('');
178
+ } else {
179
+ setInputValue(formatDate(date, { variant: 'Time' }, config));
180
+ }
181
+ }, [date, config]);
182
+
183
+ return (
184
+ <Autocomplete
185
+ ref={ref}
186
+ disabled={disabled}
187
+ freeSolo={true}
188
+ autoComplete={true}
189
+ value={selectedOption}
190
+ inputValue={inputValue}
191
+ options={options}
192
+ includeInputInList={true}
193
+ filterOptions={filterOptions}
194
+ getOptionLabel={(option: string | TimeFieldOption) =>
195
+ typeof option === 'string' ? option : option.label
196
+ }
197
+ onBlur={(event: React.FocusEvent<HTMLInputElement>) => {
198
+ handleType(event.target.value);
199
+ }}
200
+ onChange={(_: unknown, nextValue: null | string | TimeFieldOption) => {
201
+ if (typeof nextValue === 'string') {
202
+ handleType(nextValue);
203
+ } else {
204
+ handleChange(nextValue?.value);
205
+ }
206
+ }}
207
+ onInputChange={(_, nextInputValue) => {
208
+ setInputValue(nextInputValue);
209
+ }}
210
+ renderInput={(params) => (
211
+ <TextField
212
+ variant="outlined"
213
+ {...props}
214
+ {...params}
215
+ InputProps={params.InputProps}
216
+ />
217
+ )}
218
+ />
219
+ );
220
+ },
221
+ );
@@ -0,0 +1,45 @@
1
+ import { mockDate, resetMockDate } from '@superdispatch/ui-testutils';
2
+ import { renderHook } from '@testing-library/react-hooks';
3
+ import { DateTime } from 'luxon';
4
+ import { DateConfig } from '../date-config/DateConfig';
5
+ import { NullableDateInput } from '../date-time-utils/DateTimeUtils';
6
+ import { useDateTime } from './useDateTime';
7
+
8
+ interface Props {
9
+ input: NullableDateInput;
10
+ options?: Partial<DateConfig>;
11
+ }
12
+
13
+ beforeEach(() => {
14
+ mockDate();
15
+ });
16
+
17
+ afterEach(() => {
18
+ resetMockDate();
19
+ });
20
+
21
+ test('basic', () => {
22
+ const { result } = renderHook<Props, DateTime>(
23
+ ({ input, options }) => useDateTime(input, options),
24
+ { initialProps: { input: '2020-05-24' } },
25
+ );
26
+
27
+ expect(result.current.toISO()).toBe('2020-05-24T00:00:00.000-05:00');
28
+ });
29
+
30
+ test('warnings', () => {
31
+ const error = jest.spyOn(console, 'error').mockImplementation();
32
+
33
+ const { result } = renderHook<Props, DateTime>(
34
+ ({ input, options }) => useDateTime(input, options),
35
+ { initialProps: { input: '2020-05-24', options: { format: 'JodaISO' } } },
36
+ );
37
+
38
+ expect(result.current.toISO()).toBeNull();
39
+ expect(error).toHaveBeenCalledTimes(1);
40
+ expect(error).toHaveBeenLastCalledWith(
41
+ '[useDateTime] Failed to parse "2020-05-24" string with "JodaISO" format.',
42
+ );
43
+
44
+ error.mockClear();
45
+ });
@@ -0,0 +1,31 @@
1
+ import { DateTime } from 'luxon';
2
+ import { useMemo } from 'react';
3
+ import { DateConfig, useDateConfig } from '../date-config/DateConfig';
4
+ import {
5
+ NullableDateInput,
6
+ parseDate,
7
+ toPrimitiveDateInput,
8
+ } from '../date-time-utils/DateTimeUtils';
9
+
10
+ export function useDateTime(
11
+ input: NullableDateInput,
12
+ options?: Partial<DateConfig>,
13
+ ): DateTime {
14
+ const config = useDateConfig(options);
15
+ const primitiveInput = toPrimitiveDateInput(input);
16
+
17
+ return useMemo(() => {
18
+ const date = parseDate(primitiveInput, config);
19
+
20
+ if (process.env.NODE_ENV !== 'production') {
21
+ if (!date.isValid && typeof primitiveInput === 'string') {
22
+ // eslint-disable-next-line no-console
23
+ console.error(
24
+ `[useDateTime] Failed to parse "${primitiveInput}" string with "${config.format}" format.`,
25
+ );
26
+ }
27
+ }
28
+
29
+ return date;
30
+ }, [config, primitiveInput]);
31
+ }
@@ -0,0 +1,53 @@
1
+ import { mockDate, resetMockDate } from '@superdispatch/ui-testutils';
2
+ import { renderHook } from '@testing-library/react-hooks';
3
+ import { DateConfig } from '../date-config/DateConfig';
4
+ import {
5
+ DateTimeRange,
6
+ NullableDateRangeInput,
7
+ } from '../date-time-utils/DateTimeUtils';
8
+ import { useDateTimeRange } from './useDateTimeRange';
9
+
10
+ interface Props {
11
+ input: NullableDateRangeInput;
12
+ options?: Partial<DateConfig>;
13
+ }
14
+
15
+ beforeEach(() => {
16
+ mockDate();
17
+ });
18
+
19
+ afterEach(() => {
20
+ resetMockDate();
21
+ });
22
+
23
+ test('basic', () => {
24
+ const { result } = renderHook<Props, DateTimeRange>(
25
+ ({ input, options }) => useDateTimeRange(input, options),
26
+ { initialProps: { input: ['2020-05-24'] } },
27
+ );
28
+
29
+ expect(result.current[0]?.toISO()).toBe('2020-05-24T00:00:00.000-05:00');
30
+ expect(result.current[1]).toBeNull();
31
+ });
32
+
33
+ test('warnings', () => {
34
+ const error = jest.spyOn(console, 'error').mockImplementation();
35
+
36
+ const { result } = renderHook<Props, DateTimeRange>(
37
+ ({ input, options }) => useDateTimeRange(input, options),
38
+ {
39
+ initialProps: {
40
+ input: [null, '2020-05-24'],
41
+ options: { format: 'JodaISO' },
42
+ },
43
+ },
44
+ );
45
+
46
+ expect(result.current).toEqual([null, null]);
47
+ expect(error).toHaveBeenCalledTimes(1);
48
+ expect(error).toHaveBeenLastCalledWith(
49
+ '[useDateTime] Failed to parse "2020-05-24" string with "JodaISO" format.',
50
+ );
51
+
52
+ error.mockClear();
53
+ });
@@ -0,0 +1,24 @@
1
+ import { useMemo } from 'react';
2
+ import { DateConfig, useDateConfig } from '../date-config/DateConfig';
3
+ import {
4
+ DateTimeRange,
5
+ NullableDateRangeInput,
6
+ parseDateRange,
7
+ toPrimitiveDateRangeInput,
8
+ } from '../date-time-utils/DateTimeUtils';
9
+ import { useDateTime } from '../use-date-time/useDateTime';
10
+
11
+ export function useDateTimeRange(
12
+ input: NullableDateRangeInput,
13
+ options?: Partial<DateConfig>,
14
+ ): DateTimeRange {
15
+ const config = useDateConfig(options);
16
+ const [startInput, finishInput] = toPrimitiveDateRangeInput(input);
17
+ const startDate = useDateTime(startInput, config);
18
+ const finishDate = useDateTime(finishInput, config);
19
+
20
+ return useMemo(
21
+ () => parseDateRange([startDate, finishDate], config),
22
+ [config, startDate, finishDate],
23
+ );
24
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "extends": "../../tsconfig.build",
3
+ "exclude": [
4
+ "pkg",
5
+ "playroom.ts",
6
+ "**/*.spec.*",
7
+ "**/*.stories.*",
8
+ "**/*.playroom.*",
9
+ "**/__tests__/**",
10
+ "**/__testutils__/**"
11
+ ],
12
+ "references": [{ "path": "../ui" }, { "path": "../__testutils__" }],
13
+ "compilerOptions": {
14
+ "composite": true,
15
+ "rootDir": "src",
16
+ "outDir": "pkg/dist-types",
17
+ "tsBuildInfoFile": "pkg/dist-types/.tsbuildinfo"
18
+ }
19
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 Super Dispatch
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes