@primer/components 0.0.0-20219253356 → 0.0.0-202192543046
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 +1 -1
- package/lib/DatePicker/DatePicker.js +8 -2
- package/lib/DatePicker/DatePickerPanel.js +34 -5
- package/lib/DatePicker/Month.js +16 -6
- package/lib/DatePicker/useDatePicker.d.ts +1 -0
- package/lib/DatePicker/useDatePicker.js +2 -1
- package/lib-esm/DatePicker/DatePicker.js +8 -2
- package/lib-esm/DatePicker/DatePickerPanel.js +33 -4
- package/lib-esm/DatePicker/Month.js +15 -6
- package/lib-esm/DatePicker/useDatePicker.d.ts +1 -0
- package/lib-esm/DatePicker/useDatePicker.js +2 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -23,6 +23,8 @@ const DatePicker = ({
|
|
23
23
|
confirmation,
|
24
24
|
focusTrapSettings,
|
25
25
|
focusZoneSettings,
|
26
|
+
maxDate,
|
27
|
+
minDate,
|
26
28
|
onOpen: onOpenExternal,
|
27
29
|
onClose: onCloseExternal,
|
28
30
|
open,
|
@@ -30,15 +32,19 @@ const DatePicker = ({
|
|
30
32
|
renderAnchor,
|
31
33
|
selection,
|
32
34
|
value,
|
33
|
-
view
|
35
|
+
view,
|
36
|
+
weekStartsOn
|
34
37
|
}) => {
|
35
38
|
const buttonRef = (0, _react.useRef)(null);
|
36
39
|
const [isOpen, setIsOpen] = (0, _react.useState)(false);
|
37
40
|
const datePickerConfiguration = {
|
38
41
|
anchorVariant,
|
39
42
|
confirmation,
|
43
|
+
maxDate,
|
44
|
+
minDate,
|
40
45
|
selection,
|
41
|
-
view
|
46
|
+
view,
|
47
|
+
weekStartsOn
|
42
48
|
};
|
43
49
|
|
44
50
|
const onOpen = gesture => {
|
@@ -7,7 +7,7 @@ exports.DatePickerPanel = void 0;
|
|
7
7
|
|
8
8
|
var _dateFns = require("date-fns");
|
9
9
|
|
10
|
-
var _react =
|
10
|
+
var _react = _interopRequireWildcard(require("react"));
|
11
11
|
|
12
12
|
var _Box = _interopRequireDefault(require("../Box"));
|
13
13
|
|
@@ -25,12 +25,12 @@ var _StyledOcticon = _interopRequireDefault(require("../StyledOcticon"));
|
|
25
25
|
|
26
26
|
var _Button = _interopRequireWildcard(require("../Button"));
|
27
27
|
|
28
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
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; }
|
31
33
|
|
32
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33
|
-
|
34
34
|
const DatePickerPanelContainer = (0, _styledComponents.default)(_Box.default).withConfig({
|
35
35
|
displayName: "DatePickerPanel__DatePickerPanelContainer",
|
36
36
|
componentId: "sc-19upxpo-0"
|
@@ -58,10 +58,38 @@ const DatePickerPanel = () => {
|
|
58
58
|
nextMonth,
|
59
59
|
previousMonth
|
60
60
|
} = (0, _useDatePicker.default)();
|
61
|
+
const previousDisabled = (0, _react.useMemo)(() => {
|
62
|
+
const {
|
63
|
+
minDate
|
64
|
+
} = configuration;
|
65
|
+
if (!minDate) return false;
|
66
|
+
const previous = (0, _dateFns.subMonths)(currentViewingDate, 1);
|
67
|
+
|
68
|
+
if (minDate.getFullYear() >= previous.getFullYear() && minDate.getMonth() > previous.getMonth()) {
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
|
72
|
+
return false;
|
73
|
+
}, [configuration, currentViewingDate]);
|
74
|
+
const nextDisabled = (0, _react.useMemo)(() => {
|
75
|
+
const {
|
76
|
+
maxDate,
|
77
|
+
view
|
78
|
+
} = configuration;
|
79
|
+
if (!maxDate) return false;
|
80
|
+
const next = (0, _dateFns.addMonths)(currentViewingDate, view === '2-month' ? 2 : 1);
|
81
|
+
|
82
|
+
if (maxDate.getFullYear() <= next.getFullYear() && maxDate.getMonth() < next.getMonth()) {
|
83
|
+
return true;
|
84
|
+
}
|
85
|
+
|
86
|
+
return false;
|
87
|
+
}, [configuration, currentViewingDate]);
|
61
88
|
return /*#__PURE__*/_react.default.createElement(DatePickerPanelContainer, null, /*#__PURE__*/_react.default.createElement(DatePickerPanelMonths, null, /*#__PURE__*/_react.default.createElement(ArrowButton, {
|
62
89
|
variant: "small",
|
63
90
|
side: "left",
|
64
|
-
onClick: previousMonth
|
91
|
+
onClick: previousMonth,
|
92
|
+
disabled: previousDisabled
|
65
93
|
}, /*#__PURE__*/_react.default.createElement(_StyledOcticon.default, {
|
66
94
|
icon: _octiconsReact.ChevronLeftIcon,
|
67
95
|
color: "fg.muted"
|
@@ -74,7 +102,8 @@ const DatePickerPanel = () => {
|
|
74
102
|
}), /*#__PURE__*/_react.default.createElement(ArrowButton, {
|
75
103
|
variant: "small",
|
76
104
|
side: "right",
|
77
|
-
onClick: nextMonth
|
105
|
+
onClick: nextMonth,
|
106
|
+
disabled: nextDisabled
|
78
107
|
}, /*#__PURE__*/_react.default.createElement(_StyledOcticon.default, {
|
79
108
|
icon: _octiconsReact.ChevronRightIcon,
|
80
109
|
color: "fg.muted"
|
package/lib/DatePicker/Month.js
CHANGED
@@ -19,6 +19,8 @@ var _constants = require("../constants");
|
|
19
19
|
|
20
20
|
var _Day = require("./Day");
|
21
21
|
|
22
|
+
var _useDatePicker = _interopRequireDefault(require("./useDatePicker"));
|
23
|
+
|
22
24
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23
25
|
|
24
26
|
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); }
|
@@ -42,17 +44,23 @@ const Month = ({
|
|
42
44
|
month,
|
43
45
|
year
|
44
46
|
}) => {
|
47
|
+
const {
|
48
|
+
configuration
|
49
|
+
} = (0, _useDatePicker.default)();
|
45
50
|
const [selectedDay, setSelectedDay] = (0, _react.useState)(null);
|
46
51
|
const getTitle = (0, _react.useMemo)(() => `${(0, _dateFns.format)(new Date(year, month), 'MMMM yyyy')}`, [month, year]);
|
47
52
|
const weekdayHeaders = (0, _react.useMemo)(() => {
|
48
53
|
const now = new Date(year, month);
|
54
|
+
const weekOptions = {
|
55
|
+
weekStartsOn: configuration.weekStartsOn === 'Sunday' ? 0 : 1
|
56
|
+
};
|
49
57
|
return (0, _dateFns.eachDayOfInterval)({
|
50
|
-
start: (0, _dateFns.startOfWeek)(now),
|
51
|
-
end: (0, _dateFns.endOfWeek)(now)
|
58
|
+
start: (0, _dateFns.startOfWeek)(now, weekOptions),
|
59
|
+
end: (0, _dateFns.endOfWeek)(now, weekOptions)
|
52
60
|
}).map(d => /*#__PURE__*/_react.default.createElement(WeekdayHeader, {
|
53
61
|
key: `weekday-${d}-header`
|
54
62
|
}, (0, _dateFns.format)(d, 'EEEEEE')));
|
55
|
-
}, [month, year]);
|
63
|
+
}, [configuration.weekStartsOn, month, year]);
|
56
64
|
|
57
65
|
const dayAction = date => {
|
58
66
|
setSelectedDay(date);
|
@@ -61,8 +69,9 @@ const Month = ({
|
|
61
69
|
const dayComponents = (0, _react.useMemo)(() => {
|
62
70
|
const components = [];
|
63
71
|
const firstDay = new Date(year, month, 1);
|
72
|
+
const preBlanks = configuration.weekStartsOn === 'Sunday' ? firstDay.getDay() : (firstDay.getDay() + 6) % 7;
|
64
73
|
|
65
|
-
for (let i = 0; i <
|
74
|
+
for (let i = 0; i < preBlanks; i++) {
|
66
75
|
components.push( /*#__PURE__*/_react.default.createElement(_Day.BlankDay, {
|
67
76
|
key: `month-pre-blank-${i}`
|
68
77
|
}));
|
@@ -79,15 +88,16 @@ const Month = ({
|
|
79
88
|
}
|
80
89
|
|
81
90
|
const lastDay = (0, _dateFns.lastDayOfMonth)(firstDay);
|
91
|
+
const postBlanks = configuration.weekStartsOn === 'Sunday' ? lastDay.getDay() : (lastDay.getDay() + 6) % 7;
|
82
92
|
|
83
|
-
for (let i = 6; i >
|
93
|
+
for (let i = 6; i > postBlanks; i--) {
|
84
94
|
components.push( /*#__PURE__*/_react.default.createElement(_Day.BlankDay, {
|
85
95
|
key: `month-post-blank-${i}`
|
86
96
|
}));
|
87
97
|
}
|
88
98
|
|
89
99
|
return components;
|
90
|
-
}, [month, selectedDay, year]);
|
100
|
+
}, [configuration.weekStartsOn, month, selectedDay, year]);
|
91
101
|
return /*#__PURE__*/_react.default.createElement(MonthComponent, {
|
92
102
|
role: "grid"
|
93
103
|
}, /*#__PURE__*/_react.default.createElement(MonthTitle, null, getTitle), weekdayHeaders, dayComponents);
|
@@ -8,6 +8,8 @@ export const DatePicker = ({
|
|
8
8
|
confirmation,
|
9
9
|
focusTrapSettings,
|
10
10
|
focusZoneSettings,
|
11
|
+
maxDate,
|
12
|
+
minDate,
|
11
13
|
onOpen: onOpenExternal,
|
12
14
|
onClose: onCloseExternal,
|
13
15
|
open,
|
@@ -15,15 +17,19 @@ export const DatePicker = ({
|
|
15
17
|
renderAnchor,
|
16
18
|
selection,
|
17
19
|
value,
|
18
|
-
view
|
20
|
+
view,
|
21
|
+
weekStartsOn
|
19
22
|
}) => {
|
20
23
|
const buttonRef = useRef(null);
|
21
24
|
const [isOpen, setIsOpen] = useState(false);
|
22
25
|
const datePickerConfiguration = {
|
23
26
|
anchorVariant,
|
24
27
|
confirmation,
|
28
|
+
maxDate,
|
29
|
+
minDate,
|
25
30
|
selection,
|
26
|
-
view
|
31
|
+
view,
|
32
|
+
weekStartsOn
|
27
33
|
};
|
28
34
|
|
29
35
|
const onOpen = gesture => {
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { addMonths } from 'date-fns';
|
2
|
-
import React from 'react';
|
1
|
+
import { addMonths, subMonths } from 'date-fns';
|
2
|
+
import React, { useMemo } from 'react';
|
3
3
|
import Box from '../Box';
|
4
4
|
import { Month } from './Month';
|
5
5
|
import styled from 'styled-components';
|
@@ -34,10 +34,38 @@ export const DatePickerPanel = () => {
|
|
34
34
|
nextMonth,
|
35
35
|
previousMonth
|
36
36
|
} = useDatePicker();
|
37
|
+
const previousDisabled = useMemo(() => {
|
38
|
+
const {
|
39
|
+
minDate
|
40
|
+
} = configuration;
|
41
|
+
if (!minDate) return false;
|
42
|
+
const previous = subMonths(currentViewingDate, 1);
|
43
|
+
|
44
|
+
if (minDate.getFullYear() >= previous.getFullYear() && minDate.getMonth() > previous.getMonth()) {
|
45
|
+
return true;
|
46
|
+
}
|
47
|
+
|
48
|
+
return false;
|
49
|
+
}, [configuration, currentViewingDate]);
|
50
|
+
const nextDisabled = useMemo(() => {
|
51
|
+
const {
|
52
|
+
maxDate,
|
53
|
+
view
|
54
|
+
} = configuration;
|
55
|
+
if (!maxDate) return false;
|
56
|
+
const next = addMonths(currentViewingDate, view === '2-month' ? 2 : 1);
|
57
|
+
|
58
|
+
if (maxDate.getFullYear() <= next.getFullYear() && maxDate.getMonth() < next.getMonth()) {
|
59
|
+
return true;
|
60
|
+
}
|
61
|
+
|
62
|
+
return false;
|
63
|
+
}, [configuration, currentViewingDate]);
|
37
64
|
return /*#__PURE__*/React.createElement(DatePickerPanelContainer, null, /*#__PURE__*/React.createElement(DatePickerPanelMonths, null, /*#__PURE__*/React.createElement(ArrowButton, {
|
38
65
|
variant: "small",
|
39
66
|
side: "left",
|
40
|
-
onClick: previousMonth
|
67
|
+
onClick: previousMonth,
|
68
|
+
disabled: previousDisabled
|
41
69
|
}, /*#__PURE__*/React.createElement(StyledOcticon, {
|
42
70
|
icon: ChevronLeftIcon,
|
43
71
|
color: "fg.muted"
|
@@ -50,7 +78,8 @@ export const DatePickerPanel = () => {
|
|
50
78
|
}), /*#__PURE__*/React.createElement(ArrowButton, {
|
51
79
|
variant: "small",
|
52
80
|
side: "right",
|
53
|
-
onClick: nextMonth
|
81
|
+
onClick: nextMonth,
|
82
|
+
disabled: nextDisabled
|
54
83
|
}, /*#__PURE__*/React.createElement(StyledOcticon, {
|
55
84
|
icon: ChevronRightIcon,
|
56
85
|
color: "fg.muted"
|
@@ -5,6 +5,7 @@ import Box from '../Box';
|
|
5
5
|
import Text from '../Text';
|
6
6
|
import { get } from '../constants';
|
7
7
|
import { BlankDay, Day } from './Day';
|
8
|
+
import useDatePicker from './useDatePicker';
|
8
9
|
const MonthComponent = styled(Box).withConfig({
|
9
10
|
displayName: "Month__MonthComponent",
|
10
11
|
componentId: "l6j7o0-0"
|
@@ -21,17 +22,23 @@ export const Month = ({
|
|
21
22
|
month,
|
22
23
|
year
|
23
24
|
}) => {
|
25
|
+
const {
|
26
|
+
configuration
|
27
|
+
} = useDatePicker();
|
24
28
|
const [selectedDay, setSelectedDay] = useState(null);
|
25
29
|
const getTitle = useMemo(() => `${format(new Date(year, month), 'MMMM yyyy')}`, [month, year]);
|
26
30
|
const weekdayHeaders = useMemo(() => {
|
27
31
|
const now = new Date(year, month);
|
32
|
+
const weekOptions = {
|
33
|
+
weekStartsOn: configuration.weekStartsOn === 'Sunday' ? 0 : 1
|
34
|
+
};
|
28
35
|
return eachDayOfInterval({
|
29
|
-
start: startOfWeek(now),
|
30
|
-
end: endOfWeek(now)
|
36
|
+
start: startOfWeek(now, weekOptions),
|
37
|
+
end: endOfWeek(now, weekOptions)
|
31
38
|
}).map(d => /*#__PURE__*/React.createElement(WeekdayHeader, {
|
32
39
|
key: `weekday-${d}-header`
|
33
40
|
}, format(d, 'EEEEEE')));
|
34
|
-
}, [month, year]);
|
41
|
+
}, [configuration.weekStartsOn, month, year]);
|
35
42
|
|
36
43
|
const dayAction = date => {
|
37
44
|
setSelectedDay(date);
|
@@ -40,8 +47,9 @@ export const Month = ({
|
|
40
47
|
const dayComponents = useMemo(() => {
|
41
48
|
const components = [];
|
42
49
|
const firstDay = new Date(year, month, 1);
|
50
|
+
const preBlanks = configuration.weekStartsOn === 'Sunday' ? firstDay.getDay() : (firstDay.getDay() + 6) % 7;
|
43
51
|
|
44
|
-
for (let i = 0; i <
|
52
|
+
for (let i = 0; i < preBlanks; i++) {
|
45
53
|
components.push( /*#__PURE__*/React.createElement(BlankDay, {
|
46
54
|
key: `month-pre-blank-${i}`
|
47
55
|
}));
|
@@ -58,15 +66,16 @@ export const Month = ({
|
|
58
66
|
}
|
59
67
|
|
60
68
|
const lastDay = lastDayOfMonth(firstDay);
|
69
|
+
const postBlanks = configuration.weekStartsOn === 'Sunday' ? lastDay.getDay() : (lastDay.getDay() + 6) % 7;
|
61
70
|
|
62
|
-
for (let i = 6; i >
|
71
|
+
for (let i = 6; i > postBlanks; i--) {
|
63
72
|
components.push( /*#__PURE__*/React.createElement(BlankDay, {
|
64
73
|
key: `month-post-blank-${i}`
|
65
74
|
}));
|
66
75
|
}
|
67
76
|
|
68
77
|
return components;
|
69
|
-
}, [month, selectedDay, year]);
|
78
|
+
}, [configuration.weekStartsOn, month, selectedDay, year]);
|
70
79
|
return /*#__PURE__*/React.createElement(MonthComponent, {
|
71
80
|
role: "grid"
|
72
81
|
}, /*#__PURE__*/React.createElement(MonthTitle, null, getTitle), weekdayHeaders, dayComponents);
|
@@ -141,7 +141,8 @@ const defaultConfiguration = {
|
|
141
141
|
dimWeekends: false,
|
142
142
|
placeholder: 'Select a Date...',
|
143
143
|
selection: 'single',
|
144
|
-
view: '2-month'
|
144
|
+
view: '2-month',
|
145
|
+
weekStartsOn: 'Sunday'
|
145
146
|
};
|
146
147
|
export const DatePickerProvider = ({
|
147
148
|
configuration: externalConfig = {},
|