@superdispatch/dates 0.16.0-alpha.0 → 0.16.0-alpha.1

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.
@@ -1,274 +0,0 @@
1
- import { DateTime } from 'luxon';
2
- import { useMemo } from 'react';
3
- /** @deprecated */
4
-
5
- /** @deprecated */
6
- function toDateTime(value) {
7
- return typeof value === 'number' ? DateTime.fromMillis(value) : DateTime.fromJSDate(value);
8
- }
9
- /** @deprecated */
10
-
11
-
12
- export function isDate(value) {
13
- return value != null && value instanceof Date;
14
- }
15
- /** @deprecated */
16
-
17
- export function isDateLike(value) {
18
- return isDate(value) || typeof value === 'number' && Number.isInteger(value);
19
- }
20
- /** @deprecated */
21
-
22
- export function isValidDate(value) {
23
- return isDate(value) && Number.isFinite(value.getTime());
24
- }
25
- /** @deprecated */
26
-
27
- function checkRange(range, validator) {
28
- if (!Array.isArray(range) || range.length > 2) {
29
- return false;
30
- }
31
-
32
- var [start, finish] = range;
33
- return (start == null || validator(start)) && (finish == null || validator(finish));
34
- }
35
- /** @deprecated */
36
-
37
-
38
- export function isDateRange(range) {
39
- return checkRange(range, isDate);
40
- }
41
- /** @deprecated */
42
-
43
- export function isDateRangeLike(range) {
44
- return checkRange(range, isDateLike);
45
- }
46
- /** @deprecated */
47
-
48
- export function isValidDateRange(range) {
49
- return checkRange(range, isValidDate);
50
- }
51
- /** @deprecated */
52
-
53
- export function toDate(value) {
54
- return !isDateLike(value) ? new Date(NaN) : new Date(value);
55
- }
56
- /** @deprecated */
57
-
58
- export function toDateRange(range) {
59
- if (range == null || !isDateRangeLike(range)) {
60
- return [];
61
- }
62
-
63
- return range.filter(x => x != null).map(x => x == null ? undefined : toDate(x)).sort((a, b) => !isValidDate(a) ? -1 : !isValidDate(b) ? 1 : a.valueOf() - b.valueOf());
64
- }
65
- /** @deprecated */
66
-
67
- /** @deprecated */
68
- export class DateUtils {
69
- /** @deprecated */
70
- toObject(value) {
71
- var {
72
- year = NaN,
73
- month = NaN,
74
- day = NaN,
75
- hour = NaN,
76
- minute = NaN,
77
- second = NaN,
78
- millisecond = NaN
79
- } = toDateTime(value).toObject({
80
- includeConfig: false
81
- });
82
- return {
83
- year,
84
- month,
85
- day,
86
- hour,
87
- minute,
88
- second,
89
- millisecond
90
- };
91
- }
92
- /** @deprecated */
93
-
94
-
95
- fromObject(_ref) {
96
- var {
97
- year = 0,
98
- month = 1,
99
- day = 1,
100
- hour = 0,
101
- minute = 0,
102
- second = 0,
103
- millisecond = 0
104
- } = _ref;
105
-
106
- if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day) || Number.isNaN(hour) || Number.isNaN(minute) || Number.isNaN(second) || Number.isNaN(millisecond)) {
107
- return new Date(NaN);
108
- }
109
-
110
- return DateTime.fromObject({
111
- year,
112
- month,
113
- day,
114
- hour,
115
- minute,
116
- second,
117
- millisecond
118
- }).toJSDate();
119
- }
120
- /** @deprecated */
121
-
122
-
123
- update(value, values) {
124
- return toDateTime(value).set(values).toJSDate();
125
- }
126
- /** @deprecated */
127
-
128
-
129
- mergeDateAndTime(date, time) {
130
- var {
131
- hour,
132
- minute,
133
- second,
134
- millisecond
135
- } = this.toObject(time);
136
-
137
- if (Number.isNaN(hour) || Number.isNaN(minute) || Number.isNaN(second) || Number.isNaN(millisecond)) {
138
- return new Date(NaN);
139
- }
140
-
141
- return this.update(date, {
142
- hour,
143
- minute,
144
- second,
145
- millisecond
146
- });
147
- }
148
- /** @deprecated */
149
-
150
-
151
- startOf(value, unit) {
152
- return toDateTime(value).startOf(unit).toJSDate();
153
- }
154
- /** @deprecated */
155
-
156
-
157
- endOf(value, unit) {
158
- return toDateTime(value).endOf(unit).toJSDate();
159
- }
160
- /** @deprecated */
161
-
162
-
163
- plus(value, values) {
164
- return toDateTime(value).plus(values).toJSDate();
165
- }
166
- /** @deprecated */
167
-
168
-
169
- minus(value, values) {
170
- return toDateTime(value).minus(values).toJSDate();
171
- }
172
- /** @deprecated */
173
-
174
-
175
- isSameDate(value, compare) {
176
- var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'millisecond';
177
-
178
- if (value == null && compare == null) {
179
- return true;
180
- }
181
-
182
- if (value == null || compare == null) {
183
- return false;
184
- }
185
-
186
- var dateTimeValue = toDateTime(value);
187
- var dateTimeCompare = toDateTime(compare);
188
- return dateTimeValue.isValid && dateTimeCompare.isValid && dateTimeValue.startOf(unit).equals(dateTimeCompare.startOf(unit));
189
- }
190
- /** @deprecated */
191
-
192
-
193
- isSameDateRange(value, compare, unit) {
194
- var range1 = toDateRange(value);
195
- var range2 = toDateRange(compare);
196
- return !range1.some((date, idx) => !this.isSameDate(date, range2[idx], unit));
197
- }
198
- /** @deprecated */
199
-
200
-
201
- diff(value, compare, unit) {
202
- var valueDateTime = toDateTime(value);
203
- var compareDateTime = toDateTime(compare);
204
- return valueDateTime.diff(compareDateTime, unit).as(unit);
205
- }
206
- /** @deprecated */
207
-
208
-
209
- toLocaleString(value, options) {
210
- return toDateTime(value).toLocaleString(options);
211
- }
212
- /** @deprecated */
213
-
214
-
215
- format(value, variant) {
216
- return this.toLocaleString(value, variant === 'date' ? {
217
- day: '2-digit',
218
- month: 'short',
219
- year: 'numeric'
220
- } : variant === 'shortDate' ? {
221
- day: '2-digit',
222
- month: 'short'
223
- } : variant === 'time' ? {
224
- hour: 'numeric',
225
- minute: 'numeric'
226
- } : {
227
- day: '2-digit',
228
- month: 'short',
229
- year: 'numeric',
230
- hour: 'numeric',
231
- minute: 'numeric'
232
- });
233
- }
234
- /** @deprecated */
235
-
236
-
237
- formatRange(value) {
238
- var emptyText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
239
- var range = toDateRange(value);
240
-
241
- if (!isValidDateRange(range)) {
242
- return 'Invalid Date Range';
243
- }
244
-
245
- var [start, finish] = range;
246
-
247
- if (!start) {
248
- return emptyText;
249
- }
250
-
251
- var startText = this.format(start, !this.isSameDate(start, finish, 'year') ? 'date' : 'shortDate');
252
- var finishText = !finish ? '…' : this.format(finish, 'date');
253
- return "".concat(startText, " - ").concat(finishText);
254
- }
255
- /** @deprecated */
256
-
257
-
258
- formatRelativeTime(value) {
259
- var {
260
- style = 'long',
261
- compare = Date.now()
262
- } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
263
- var valueDateTime = toDateTime(value);
264
- var compareDateTime = toDateTime(compare);
265
- return valueDateTime.toRelative({
266
- style,
267
- base: compareDateTime
268
- });
269
- }
270
-
271
- }
272
- export function useDateUtils() {
273
- return useMemo(() => new DateUtils(), []);
274
- }
@@ -1,35 +0,0 @@
1
- import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["variant", "fallback"],
4
- _excluded2 = ["date", "fallback"];
5
- import { renderChildren } from '@superdispatch/ui';
6
- import { useMemo } from 'react';
7
- import { useDateConfig } from "../date-config/DateConfig.js";
8
- import { formatDate } from "../date-time-utils/DateTimeUtils.js";
9
- import { useDateTime } from "../use-date-time/useDateTime.js";
10
- export function useFormattedDate(input, _ref) {
11
- var {
12
- variant,
13
- fallback
14
- } = _ref,
15
- dateConfig = _objectWithoutProperties(_ref, _excluded);
16
-
17
- var config = useDateConfig(dateConfig);
18
- var date = useDateTime(input, config);
19
- return useMemo(() => formatDate(date, {
20
- variant,
21
- fallback
22
- }, config), [date, config, variant, fallback]);
23
- }
24
- export function FormattedDate(_ref2) {
25
- var {
26
- date,
27
- fallback = 'Invalid Date'
28
- } = _ref2,
29
- options = _objectWithoutProperties(_ref2, _excluded2);
30
-
31
- var formatted = useFormattedDate(date, _objectSpread(_objectSpread({}, options), {}, {
32
- fallback: ''
33
- }));
34
- return renderChildren(formatted || fallback);
35
- }
@@ -1,44 +0,0 @@
1
- import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["unit", "round", "padding", "fallback", "base"],
4
- _excluded2 = ["date", "fallback"];
5
- import { renderChildren } from '@superdispatch/ui';
6
- import { useMemo } from 'react';
7
- import { useDateConfig } from "../date-config/DateConfig.js";
8
- import { formatRelativeTime } from "../date-time-utils/DateTimeUtils.js";
9
- import { useDateTime } from "../use-date-time/useDateTime.js";
10
- export function useFormattedRelativeTime(input) {
11
- var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
12
- {
13
- unit,
14
- round,
15
- padding,
16
- fallback,
17
- base: baseOption
18
- } = _ref,
19
- dateConfig = _objectWithoutProperties(_ref, _excluded);
20
-
21
- var config = useDateConfig(dateConfig);
22
- var date = useDateTime(input, config);
23
- var baseOptionDate = useDateTime(baseOption, config);
24
- var base = baseOption == null ? undefined : baseOptionDate;
25
- return useMemo(() => formatRelativeTime(date, {
26
- base,
27
- unit,
28
- round,
29
- padding,
30
- fallback
31
- }, config), [base, date, unit, round, config, padding, fallback]);
32
- }
33
- export function FormattedRelativeTime(_ref2) {
34
- var {
35
- date,
36
- fallback = 'Invalid Date'
37
- } = _ref2,
38
- options = _objectWithoutProperties(_ref2, _excluded2);
39
-
40
- var formatted = useFormattedRelativeTime(date, _objectSpread(_objectSpread({}, options), {}, {
41
- fallback: ''
42
- }));
43
- return renderChildren(formatted || fallback);
44
- }
@@ -1,12 +0,0 @@
1
- export * from "./calendar/Calendar.js";
2
- export * from "./calendar/CalendarQuickSelection.js";
3
- export * from "./date-config/DateConfig.js";
4
- export * from "./date-field/DateField.js";
5
- export * from "./date-range-field/DateRangeField.js";
6
- export * from "./date-time-utils/DateTimeUtils.js";
7
- export * from "./date-utils/DateUtils.js";
8
- export * from "./formatted-date/FormattedDate.js";
9
- export * from "./formatted-relative-time/FormattedRelativeTime.js";
10
- export * from "./time-field/TimeField.js";
11
- export * from "./use-date-time-range/useDateTimeRange.js";
12
- export * from "./use-date-time/useDateTime.js";
@@ -1,148 +0,0 @@
1
- import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
- import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["disabled", "onChange", "value", "format"];
4
- import { Autocomplete, TextField } from '@mui/material';
5
- import { DateTime } from 'luxon';
6
- import { forwardRef, useEffect, useMemo, useState } from 'react';
7
- import { useDateConfig } from "../date-config/DateConfig.js";
8
- import { formatDate, toDatePayload } from "../date-time-utils/DateTimeUtils.js";
9
- import { useDateTime } from "../use-date-time/useDateTime.js";
10
- import { jsx as _jsx } from "react/jsx-runtime";
11
- var TIME_MATCH_FORMATS = ['h:mm a', 'h:mma', 'H:mm', 'h:mm', 'hmm', 'Hmm', 'h', 'H'];
12
-
13
- function toTimeFieldOption(date, config) {
14
- return {
15
- value: date.valueOf(),
16
- label: formatDate(date, {
17
- variant: 'Time'
18
- }, config),
19
- pattern: new RegExp("^(".concat(TIME_MATCH_FORMATS.map(format => date.toFormat(format)).join('|'), ")"), 'i')
20
- };
21
- }
22
-
23
- function normalizeInputValue(inputValue) {
24
- return inputValue.replace(/[\s]/g, '').toLowerCase();
25
- }
26
-
27
- function makeOptions(config) {
28
- var options = [];
29
- var now = DateTime.local().startOf('day');
30
-
31
- for (var i = 0; i < 96; i++) {
32
- options.push(toTimeFieldOption(now.set({
33
- minute: i * 15
34
- }), config));
35
- }
36
-
37
- var cache = new Map();
38
- return [options, (_, _ref) => {
39
- var {
40
- inputValue
41
- } = _ref;
42
- var query = normalizeInputValue(inputValue);
43
-
44
- if (!query) {
45
- return options;
46
- }
47
-
48
- var filtered = cache.get(query);
49
-
50
- if (!filtered) {
51
- filtered = options.filter(option => option.pattern.test(query));
52
- cache.set(query, filtered);
53
- }
54
-
55
- return filtered;
56
- }];
57
- }
58
-
59
- export var TimeField = /*#__PURE__*/forwardRef((_ref2, ref) => {
60
- var {
61
- disabled,
62
- onChange,
63
- value: valueProp,
64
- format: formatProp
65
- } = _ref2,
66
- props = _objectWithoutProperties(_ref2, _excluded);
67
-
68
- var config = useDateConfig({
69
- format: formatProp
70
- });
71
- var date = useDateTime(valueProp, config);
72
- var selectedOption = useMemo(() => !date.isValid ? undefined : toTimeFieldOption(date, config), [date, config]);
73
- var [options, filterOptions] = useMemo(() => makeOptions(config), [config]);
74
- var [inputValue, setInputValue] = useState('');
75
-
76
- function handleChange(nextValue) {
77
- if (onChange) {
78
- onChange(toDatePayload(nextValue, config));
79
- }
80
- }
81
-
82
- function handleType(text) {
83
- text = normalizeInputValue(text);
84
-
85
- for (var timeFormat of TIME_MATCH_FORMATS) {
86
- var nextDate = DateTime.fromFormat(text, timeFormat);
87
-
88
- if (nextDate.isValid) {
89
- if (onChange) {
90
- if (date.isValid) {
91
- nextDate = nextDate.set({
92
- year: date.year,
93
- month: date.month,
94
- day: date.day
95
- });
96
- }
97
-
98
- onChange(toDatePayload(nextDate, config));
99
- }
100
-
101
- return;
102
- }
103
- }
104
-
105
- setInputValue((selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.label) || '');
106
- }
107
-
108
- useEffect(() => {
109
- if (!date.isValid) {
110
- setInputValue('');
111
- } else {
112
- setInputValue(formatDate(date, {
113
- variant: 'Time'
114
- }, config));
115
- }
116
- }, [date, config]);
117
- return /*#__PURE__*/_jsx(Autocomplete, {
118
- ref: ref,
119
- disabled: disabled,
120
- freeSolo: true,
121
- autoComplete: true,
122
- value: selectedOption,
123
- inputValue: inputValue,
124
- options: options,
125
- includeInputInList: true,
126
- filterOptions: filterOptions,
127
- getOptionLabel: option => typeof option === 'string' ? option : option.label,
128
- onBlur: event => {
129
- handleType(event.target.value);
130
- },
131
- onChange: (_, nextValue) => {
132
- if (typeof nextValue === 'string') {
133
- handleType(nextValue);
134
- } else {
135
- handleChange(nextValue === null || nextValue === void 0 ? void 0 : nextValue.value);
136
- }
137
- },
138
- onInputChange: (_, nextInputValue) => {
139
- setInputValue(nextInputValue);
140
- },
141
- renderInput: params => /*#__PURE__*/_jsx(TextField, _objectSpread(_objectSpread(_objectSpread({
142
- variant: "outlined"
143
- }, props), params), {}, {
144
- InputProps: params.InputProps
145
- }))
146
- });
147
- });
148
- if (process.env.NODE_ENV !== "production") TimeField.displayName = "TimeField";
@@ -1,19 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { useDateConfig } from "../date-config/DateConfig.js";
3
- import { parseDate, toPrimitiveDateInput } from "../date-time-utils/DateTimeUtils.js";
4
- export function useDateTime(input, options) {
5
- var config = useDateConfig(options);
6
- var primitiveInput = toPrimitiveDateInput(input);
7
- return useMemo(() => {
8
- var date = parseDate(primitiveInput, config);
9
-
10
- if (process.env.NODE_ENV !== 'production') {
11
- if (!date.isValid && typeof primitiveInput === 'string') {
12
- // eslint-disable-next-line no-console
13
- console.error("[useDateTime] Failed to parse \"".concat(primitiveInput, "\" string with \"").concat(config.format, "\" format."));
14
- }
15
- }
16
-
17
- return date;
18
- }, [config, primitiveInput]);
19
- }
@@ -1,11 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { useDateConfig } from "../date-config/DateConfig.js";
3
- import { parseDateRange, toPrimitiveDateRangeInput } from "../date-time-utils/DateTimeUtils.js";
4
- import { useDateTime } from "../use-date-time/useDateTime.js";
5
- export function useDateTimeRange(input, options) {
6
- var config = useDateConfig(options);
7
- var [startInput, finishInput] = toPrimitiveDateRangeInput(input);
8
- var startDate = useDateTime(startInput, config);
9
- var finishDate = useDateTime(finishInput, config);
10
- return useMemo(() => parseDateRange([startDate, finishDate], config), [config, startDate, finishDate]);
11
- }