@primer/components 0.0.0-202192602912 → 0.0.0-202192633419

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @primer/components
2
2
 
3
- ## 0.0.0-202192602912
3
+ ## 0.0.0-202192633419
4
4
 
5
5
  ### Patch Changes
6
6
 
@@ -25,6 +25,8 @@ var _TextInput = _interopRequireDefault(require("../TextInput"));
25
25
 
26
26
  var _Box = _interopRequireDefault(require("../Box"));
27
27
 
28
+ var _dateFns = require("date-fns");
29
+
28
30
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
29
31
 
30
32
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -35,6 +37,7 @@ const DatePickerAnchorButton = (0, _styledComponents.default)(_Button.default).w
35
37
  displayName: "DatePickerAnchor__DatePickerAnchorButton",
36
38
  componentId: "sc-8gpb9d-0"
37
39
  })(["align-items:center;display:flex;flex-direction:row;justify-content:space-between;max-width:350px;overflow:hidden;& ", "{margin-left:", ";}"], _Text.default, (0, _constants.get)('space.2'));
40
+ const INVALID_DATE = 'Invalid Date';
38
41
 
39
42
  const DatePickerAnchor = /*#__PURE__*/_react.default.forwardRef(({
40
43
  onAction
@@ -46,8 +49,10 @@ const DatePickerAnchor = /*#__PURE__*/_react.default.forwardRef(({
46
49
  selection
47
50
  },
48
51
  disabled,
49
- formattedDate
52
+ formattedDate,
53
+ onDateInput
50
54
  } = (0, _useDatePicker.default)();
55
+ const [inputValue, setInputValue] = (0, _react.useState)(formattedDate);
51
56
  const keyPressHandler = (0, _react.useCallback)(event => {
52
57
  if (disabled) {
53
58
  return;
@@ -69,14 +74,21 @@ const DatePickerAnchor = /*#__PURE__*/_react.default.forwardRef(({
69
74
  if (!value) return;
70
75
 
71
76
  if (selection === 'range') {
72
- var _values$, _values$2;
77
+ var _values$, _values$2, _values$3, _values$4, _values$5, _values$6;
73
78
 
74
79
  const values = value.split(' - ');
75
- const dates = {
76
- from: new Date((_values$ = values[0]) === null || _values$ === void 0 ? void 0 : _values$.trim()),
77
- to: new Date((_values$2 = values[1]) === null || _values$2 === void 0 ? void 0 : _values$2.trim())
80
+ const dates = (0, _dateFns.isBefore)(new Date((_values$ = values[0]) === null || _values$ === void 0 ? void 0 : _values$.trim()), new Date((_values$2 = values[1]) === null || _values$2 === void 0 ? void 0 : _values$2.trim())) ? {
81
+ from: new Date((_values$3 = values[0]) === null || _values$3 === void 0 ? void 0 : _values$3.trim()),
82
+ to: new Date((_values$4 = values[1]) === null || _values$4 === void 0 ? void 0 : _values$4.trim())
83
+ } : {
84
+ from: new Date((_values$5 = values[1]) === null || _values$5 === void 0 ? void 0 : _values$5.trim()),
85
+ to: new Date((_values$6 = values[0]) === null || _values$6 === void 0 ? void 0 : _values$6.trim())
78
86
  };
79
- console.log(dates);
87
+ setInputValue(value);
88
+
89
+ if (dates.from.toString() !== INVALID_DATE && dates.to.toString() !== INVALID_DATE) {
90
+ onDateInput(dates);
91
+ }
80
92
  } else if (selection === 'multi') {
81
93
  const values = value.split(',');
82
94
  const dates = [];
@@ -85,12 +97,21 @@ const DatePickerAnchor = /*#__PURE__*/_react.default.forwardRef(({
85
97
  dates.push(new Date(date.trim()));
86
98
  }
87
99
 
88
- console.log(dates);
100
+ setInputValue(value);
101
+
102
+ if (dates.every(d => d.toString() !== INVALID_DATE)) {
103
+ onDateInput(dates);
104
+ }
89
105
  } else {
90
106
  const date = new Date(value);
91
- console.log(date);
107
+ setInputValue(value);
108
+ if (date.toString() !== INVALID_DATE) onDateInput(date);
92
109
  }
93
- }, [selection]);
110
+ }, [onDateInput, selection]);
111
+
112
+ const onBlurHandler = () => {
113
+ setInputValue(formattedDate);
114
+ };
94
115
 
95
116
  if (anchorVariant === 'input') {
96
117
  const calendarButton = side => /*#__PURE__*/_react.default.createElement(_Button.ButtonInvisible, {
@@ -130,9 +151,10 @@ const DatePickerAnchor = /*#__PURE__*/_react.default.forwardRef(({
130
151
  flex: 1
131
152
  }
132
153
  }, iconPlacement === 'start' && calendarButton('left'), /*#__PURE__*/_react.default.createElement(_TextInput.default, {
133
- defaultValue: formattedDate,
154
+ value: inputValue,
134
155
  onChange: onInputChangeHandler,
135
- sx: inputSx()
156
+ sx: inputSx(),
157
+ onBlur: onBlurHandler
136
158
  }), iconPlacement === 'end' && calendarButton('right'));
137
159
  }
138
160
 
@@ -27,6 +27,15 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
27
27
 
28
28
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
29
29
 
30
+ const weekdayEnum = {
31
+ Sunday: 0,
32
+ Monday: 1,
33
+ Tuesday: 2,
34
+ Wednesday: 3,
35
+ Thursday: 4,
36
+ Friday: 5,
37
+ Saturday: 6
38
+ };
30
39
  const MonthComponent = (0, _styledComponents.default)(_Box.default).withConfig({
31
40
  displayName: "Month__MonthComponent",
32
41
  componentId: "l6j7o0-0"
@@ -50,9 +59,11 @@ const Month = ({
50
59
  const [selectedDay, setSelectedDay] = (0, _react.useState)(null);
51
60
  const getTitle = (0, _react.useMemo)(() => `${(0, _dateFns.format)(new Date(year, month), 'MMMM yyyy')}`, [month, year]);
52
61
  const weekdayHeaders = (0, _react.useMemo)(() => {
62
+ var _configuration$weekSt;
63
+
53
64
  const now = new Date(year, month);
54
65
  const weekOptions = {
55
- weekStartsOn: configuration.weekStartsOn === 'Sunday' ? 0 : 1
66
+ weekStartsOn: weekdayEnum[(_configuration$weekSt = configuration.weekStartsOn) !== null && _configuration$weekSt !== void 0 ? _configuration$weekSt : 'Sunday']
56
67
  };
57
68
  return (0, _dateFns.eachDayOfInterval)({
58
69
  start: (0, _dateFns.startOfWeek)(now, weekOptions),
@@ -37,9 +37,10 @@ export interface DatePickerContext {
37
37
  selectionActive?: boolean;
38
38
  formattedDate: string;
39
39
  nextMonth: () => void;
40
- onSelection: (date: Date) => void;
40
+ onDateInput: (updatedSelection: Selection) => void;
41
41
  onDayFocus: (date: Date) => void;
42
42
  onDayBlur: (date: Date) => void;
43
+ onSelection: (date: Date) => void;
43
44
  previousMonth: () => void;
44
45
  revertValue: () => void;
45
46
  saveValue: (selection?: Selection) => void;
@@ -63,9 +64,10 @@ declare const useDatePicker: (date?: Date | undefined) => {
63
64
  selectionActive?: boolean | undefined;
64
65
  formattedDate: string;
65
66
  nextMonth: () => void;
66
- onSelection: (date: Date) => void;
67
+ onDateInput: (updatedSelection: Selection) => void;
67
68
  onDayFocus: (date: Date) => void;
68
69
  onDayBlur: (date: Date) => void;
70
+ onSelection: (date: Date) => void;
69
71
  previousMonth: () => void;
70
72
  revertValue: () => void;
71
73
  saveValue: (selection?: Selection | undefined) => void;
@@ -281,6 +281,10 @@ const DatePickerProvider = ({
281
281
  setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
282
282
  closePicker === null || closePicker === void 0 ? void 0 : closePicker();
283
283
  }, [closePicker, selection]);
284
+ const inputHandler = (0, _react.useCallback)(updatedSelection => {
285
+ // validate date falls within range
286
+ setSelection(updatedSelection);
287
+ }, []);
284
288
  const selectionHandler = (0, _react.useCallback)(date => {
285
289
  if (configuration.selection === 'multi') {
286
290
  const selections = [...selection];
@@ -356,6 +360,7 @@ const DatePickerProvider = ({
356
360
  goToMonth,
357
361
  hoverRange,
358
362
  nextMonth,
363
+ onDateInput: inputHandler,
359
364
  onDayBlur: blurHnadler,
360
365
  onDayFocus: focusHnadler,
361
366
  onSelection: selectionHandler,
@@ -365,7 +370,7 @@ const DatePickerProvider = ({
365
370
  selectionActive: false,
366
371
  selection
367
372
  };
368
- }, [blurHnadler, configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, hoverRange, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
373
+ }, [blurHnadler, configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
369
374
  return /*#__PURE__*/_react.default.createElement(DatePickerContext.Provider, {
370
375
  value: datePickerCtx
371
376
  }, children);
@@ -1,6 +1,6 @@
1
1
  import { CalendarIcon } from '@primer/octicons-react';
2
2
  import styled from 'styled-components';
3
- import React, { useCallback } from 'react';
3
+ import React, { useCallback, useState } from 'react';
4
4
  import Button, { ButtonInvisible } from '../Button';
5
5
  import Text from '../Text';
6
6
  import { get } from '../constants';
@@ -8,10 +8,12 @@ import StyledOcticon from '../StyledOcticon';
8
8
  import useDatePicker from './useDatePicker';
9
9
  import TextInput from '../TextInput';
10
10
  import Box from '../Box';
11
+ import { isBefore } from 'date-fns';
11
12
  const DatePickerAnchorButton = styled(Button).withConfig({
12
13
  displayName: "DatePickerAnchor__DatePickerAnchorButton",
13
14
  componentId: "sc-8gpb9d-0"
14
15
  })(["align-items:center;display:flex;flex-direction:row;justify-content:space-between;max-width:350px;overflow:hidden;& ", "{margin-left:", ";}"], Text, get('space.2'));
16
+ const INVALID_DATE = 'Invalid Date';
15
17
  export const DatePickerAnchor = /*#__PURE__*/React.forwardRef(({
16
18
  onAction
17
19
  }, ref) => {
@@ -22,8 +24,10 @@ export const DatePickerAnchor = /*#__PURE__*/React.forwardRef(({
22
24
  selection
23
25
  },
24
26
  disabled,
25
- formattedDate
27
+ formattedDate,
28
+ onDateInput
26
29
  } = useDatePicker();
30
+ const [inputValue, setInputValue] = useState(formattedDate);
27
31
  const keyPressHandler = useCallback(event => {
28
32
  if (disabled) {
29
33
  return;
@@ -45,14 +49,21 @@ export const DatePickerAnchor = /*#__PURE__*/React.forwardRef(({
45
49
  if (!value) return;
46
50
 
47
51
  if (selection === 'range') {
48
- var _values$, _values$2;
52
+ var _values$, _values$2, _values$3, _values$4, _values$5, _values$6;
49
53
 
50
54
  const values = value.split(' - ');
51
- const dates = {
52
- from: new Date((_values$ = values[0]) === null || _values$ === void 0 ? void 0 : _values$.trim()),
53
- to: new Date((_values$2 = values[1]) === null || _values$2 === void 0 ? void 0 : _values$2.trim())
55
+ const dates = isBefore(new Date((_values$ = values[0]) === null || _values$ === void 0 ? void 0 : _values$.trim()), new Date((_values$2 = values[1]) === null || _values$2 === void 0 ? void 0 : _values$2.trim())) ? {
56
+ from: new Date((_values$3 = values[0]) === null || _values$3 === void 0 ? void 0 : _values$3.trim()),
57
+ to: new Date((_values$4 = values[1]) === null || _values$4 === void 0 ? void 0 : _values$4.trim())
58
+ } : {
59
+ from: new Date((_values$5 = values[1]) === null || _values$5 === void 0 ? void 0 : _values$5.trim()),
60
+ to: new Date((_values$6 = values[0]) === null || _values$6 === void 0 ? void 0 : _values$6.trim())
54
61
  };
55
- console.log(dates);
62
+ setInputValue(value);
63
+
64
+ if (dates.from.toString() !== INVALID_DATE && dates.to.toString() !== INVALID_DATE) {
65
+ onDateInput(dates);
66
+ }
56
67
  } else if (selection === 'multi') {
57
68
  const values = value.split(',');
58
69
  const dates = [];
@@ -61,12 +72,21 @@ export const DatePickerAnchor = /*#__PURE__*/React.forwardRef(({
61
72
  dates.push(new Date(date.trim()));
62
73
  }
63
74
 
64
- console.log(dates);
75
+ setInputValue(value);
76
+
77
+ if (dates.every(d => d.toString() !== INVALID_DATE)) {
78
+ onDateInput(dates);
79
+ }
65
80
  } else {
66
81
  const date = new Date(value);
67
- console.log(date);
82
+ setInputValue(value);
83
+ if (date.toString() !== INVALID_DATE) onDateInput(date);
68
84
  }
69
- }, [selection]);
85
+ }, [onDateInput, selection]);
86
+
87
+ const onBlurHandler = () => {
88
+ setInputValue(formattedDate);
89
+ };
70
90
 
71
91
  if (anchorVariant === 'input') {
72
92
  const calendarButton = side => /*#__PURE__*/React.createElement(ButtonInvisible, {
@@ -106,9 +126,10 @@ export const DatePickerAnchor = /*#__PURE__*/React.forwardRef(({
106
126
  flex: 1
107
127
  }
108
128
  }, iconPlacement === 'start' && calendarButton('left'), /*#__PURE__*/React.createElement(TextInput, {
109
- defaultValue: formattedDate,
129
+ value: inputValue,
110
130
  onChange: onInputChangeHandler,
111
- sx: inputSx()
131
+ sx: inputSx(),
132
+ onBlur: onBlurHandler
112
133
  }), iconPlacement === 'end' && calendarButton('right'));
113
134
  }
114
135
 
@@ -6,6 +6,15 @@ import Text from '../Text';
6
6
  import { get } from '../constants';
7
7
  import { BlankDay, Day } from './Day';
8
8
  import useDatePicker from './useDatePicker';
9
+ const weekdayEnum = {
10
+ Sunday: 0,
11
+ Monday: 1,
12
+ Tuesday: 2,
13
+ Wednesday: 3,
14
+ Thursday: 4,
15
+ Friday: 5,
16
+ Saturday: 6
17
+ };
9
18
  const MonthComponent = styled(Box).withConfig({
10
19
  displayName: "Month__MonthComponent",
11
20
  componentId: "l6j7o0-0"
@@ -28,9 +37,11 @@ export const Month = ({
28
37
  const [selectedDay, setSelectedDay] = useState(null);
29
38
  const getTitle = useMemo(() => `${format(new Date(year, month), 'MMMM yyyy')}`, [month, year]);
30
39
  const weekdayHeaders = useMemo(() => {
40
+ var _configuration$weekSt;
41
+
31
42
  const now = new Date(year, month);
32
43
  const weekOptions = {
33
- weekStartsOn: configuration.weekStartsOn === 'Sunday' ? 0 : 1
44
+ weekStartsOn: weekdayEnum[(_configuration$weekSt = configuration.weekStartsOn) !== null && _configuration$weekSt !== void 0 ? _configuration$weekSt : 'Sunday']
34
45
  };
35
46
  return eachDayOfInterval({
36
47
  start: startOfWeek(now, weekOptions),
@@ -37,9 +37,10 @@ export interface DatePickerContext {
37
37
  selectionActive?: boolean;
38
38
  formattedDate: string;
39
39
  nextMonth: () => void;
40
- onSelection: (date: Date) => void;
40
+ onDateInput: (updatedSelection: Selection) => void;
41
41
  onDayFocus: (date: Date) => void;
42
42
  onDayBlur: (date: Date) => void;
43
+ onSelection: (date: Date) => void;
43
44
  previousMonth: () => void;
44
45
  revertValue: () => void;
45
46
  saveValue: (selection?: Selection) => void;
@@ -63,9 +64,10 @@ declare const useDatePicker: (date?: Date | undefined) => {
63
64
  selectionActive?: boolean | undefined;
64
65
  formattedDate: string;
65
66
  nextMonth: () => void;
66
- onSelection: (date: Date) => void;
67
+ onDateInput: (updatedSelection: Selection) => void;
67
68
  onDayFocus: (date: Date) => void;
68
69
  onDayBlur: (date: Date) => void;
70
+ onSelection: (date: Date) => void;
69
71
  previousMonth: () => void;
70
72
  revertValue: () => void;
71
73
  saveValue: (selection?: Selection | undefined) => void;
@@ -255,6 +255,10 @@ export const DatePickerProvider = ({
255
255
  setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
256
256
  closePicker === null || closePicker === void 0 ? void 0 : closePicker();
257
257
  }, [closePicker, selection]);
258
+ const inputHandler = useCallback(updatedSelection => {
259
+ // validate date falls within range
260
+ setSelection(updatedSelection);
261
+ }, []);
258
262
  const selectionHandler = useCallback(date => {
259
263
  if (configuration.selection === 'multi') {
260
264
  const selections = [...selection];
@@ -330,6 +334,7 @@ export const DatePickerProvider = ({
330
334
  goToMonth,
331
335
  hoverRange,
332
336
  nextMonth,
337
+ onDateInput: inputHandler,
333
338
  onDayBlur: blurHnadler,
334
339
  onDayFocus: focusHnadler,
335
340
  onSelection: selectionHandler,
@@ -339,7 +344,7 @@ export const DatePickerProvider = ({
339
344
  selectionActive: false,
340
345
  selection
341
346
  };
342
- }, [blurHnadler, configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, hoverRange, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
347
+ }, [blurHnadler, configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
343
348
  return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
344
349
  value: datePickerCtx
345
350
  }, children);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primer/components",
3
- "version": "0.0.0-202192602912",
3
+ "version": "0.0.0-202192633419",
4
4
  "description": "Primer react components",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib-esm/index.js",