@semcore/date-picker 3.0.8 → 3.0.11

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 (39) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +72 -0
  3. package/lib/cjs/components/Calendar.js +17 -17
  4. package/lib/cjs/components/PickerAbstract.js +7 -7
  5. package/lib/cjs/components/RangePickerAbstract.js +7 -7
  6. package/lib/cjs/components/index.js +3 -3
  7. package/lib/es6/components/Calendar.js +17 -17
  8. package/lib/es6/components/PickerAbstract.js +7 -7
  9. package/lib/es6/components/RangePickerAbstract.js +7 -7
  10. package/lib/es6/components/index.js +3 -3
  11. package/package.json +2 -2
  12. package/src/DatePicker.jsx +0 -113
  13. package/src/DateRangePicker.jsx +0 -91
  14. package/src/MonthPicker.jsx +0 -71
  15. package/src/MonthRangePicker.jsx +0 -108
  16. package/src/components/ButtonTrigger.jsx +0 -23
  17. package/src/components/Calendar.jsx +0 -284
  18. package/src/components/PickerAbstract.jsx +0 -199
  19. package/src/components/RangePickerAbstract.jsx +0 -340
  20. package/src/components/index.jsx +0 -121
  21. package/src/index.d.ts +0 -355
  22. package/src/index.js +0 -8
  23. package/src/style/calendar.shadow.css +0 -190
  24. package/src/style/date-picker.shadow.css +0 -49
  25. package/src/translations/de.json +0 -13
  26. package/src/translations/en.json +0 -13
  27. package/src/translations/es.json +0 -13
  28. package/src/translations/fr.json +0 -13
  29. package/src/translations/it.json +0 -13
  30. package/src/translations/ja.json +0 -13
  31. package/src/translations/ko.json +0 -13
  32. package/src/translations/pt.json +0 -13
  33. package/src/translations/ru.json +0 -13
  34. package/src/translations/vi.json +0 -13
  35. package/src/translations/zh.json +0 -13
  36. package/src/utils/cronTabScheduler.js +0 -99
  37. package/src/utils/formatDate.js +0 -8
  38. package/src/utils/includesDate.js +0 -13
  39. package/src/utils/shortDateRangeFormat.js +0 -70
@@ -1,108 +0,0 @@
1
- import React from 'react';
2
- import dayjs from 'dayjs';
3
- import createComponent from '@semcore/core';
4
- import shortDateRangeFormat from './utils/shortDateRangeFormat';
5
- import { Header, Next, Period, Popper, Prev, Title, Trigger } from './components';
6
- import { CalendarMonths as Calendar } from './components/Calendar';
7
- import RangePickerAbstract from './components/RangePickerAbstract';
8
-
9
- class MonthRangePickerRoot extends RangePickerAbstract {
10
- static displayName = 'MonthRangePicker';
11
- static defaultProps = (props) => {
12
- return {
13
- ...RangePickerAbstract.defaultProps(props),
14
- children: (
15
- <>
16
- <MonthRangePicker.Trigger />
17
- <MonthRangePicker.Popper />
18
- </>
19
- ),
20
- };
21
- };
22
-
23
- navigateStep = 'year';
24
- keyStep = 'month';
25
- keyDiff = {
26
- 37: -1,
27
- 38: -3,
28
- 39: 1,
29
- 40: 3,
30
- };
31
-
32
- getDefaultPeriods() {
33
- const { getI18nText } = this.asProps;
34
- const today = new Date(new Date().setHours(0, 0, 0, 0));
35
- return [
36
- {
37
- children: getI18nText('lastMonth'),
38
- value: [
39
- dayjs(today).subtract(1, 'month').startOf('month').toDate(),
40
- dayjs(today).startOf('month').toDate(),
41
- ],
42
- },
43
- {
44
- children: getI18nText('last3Months'),
45
- value: [
46
- dayjs(today).subtract(2, 'month').startOf('month').toDate(),
47
- dayjs(today).startOf('month').toDate(),
48
- ],
49
- },
50
- {
51
- children: getI18nText('last6Months'),
52
- value: [
53
- dayjs(today).subtract(5, 'month').startOf('month').toDate(),
54
- dayjs(today).startOf('month').toDate(),
55
- ],
56
- },
57
- {
58
- children: getI18nText('last12Months'),
59
- value: [
60
- dayjs(today).subtract(11, 'month').startOf('month').toDate(),
61
- dayjs(today).startOf('month').toDate(),
62
- ],
63
- },
64
- ];
65
- }
66
-
67
- getTriggerProps() {
68
- const { value, locale } = this.asProps;
69
- return {
70
- ...super.getTriggerProps(),
71
- placeholder: 'Select month period',
72
- children: shortDateRangeFormat(value, {
73
- locale,
74
- month: 'short',
75
- year: 'numeric',
76
- }),
77
- };
78
- }
79
-
80
- getTitleProps(props, index) {
81
- const { displayedPeriod, locale } = this.asProps;
82
- return {
83
- ...super.getTitleProps(props, index),
84
- children: new Intl.DateTimeFormat(locale, { year: 'numeric' }).format(
85
- dayjs(displayedPeriod).add(index, this.navigateStep).startOf(this.navigateStep).toDate(),
86
- ),
87
- };
88
- }
89
- }
90
-
91
- const MonthRangePicker = createComponent(
92
- MonthRangePickerRoot,
93
- {
94
- Trigger,
95
- Popper,
96
- Header,
97
- Title,
98
- Prev,
99
- Next,
100
- Calendar,
101
- Period,
102
- },
103
- {
104
- parent: Calendar,
105
- },
106
- );
107
-
108
- export default MonthRangePicker;
@@ -1,23 +0,0 @@
1
- import React from 'react';
2
- import BaseTrigger from '@semcore/base-trigger';
3
- import addonTextChildren from '@semcore/utils/lib/addonTextChildren';
4
- import Calendar from '@semcore/icon/Calendar/m';
5
- import createComponent, { Root } from '@semcore/core';
6
-
7
- const ButtonTriggerRoot = ({ Children }) => {
8
- return (
9
- <Root render={BaseTrigger}>
10
- <ButtonTrigger.Addon tag={Calendar} />
11
- {addonTextChildren(Children, ButtonTrigger.Text, ButtonTrigger.Addon)}
12
- </Root>
13
- );
14
- };
15
-
16
- ButtonTriggerRoot.displayName = 'ButtonTrigger';
17
-
18
- const ButtonTrigger = createComponent(ButtonTriggerRoot, {
19
- Text: BaseTrigger.Text,
20
- Addon: BaseTrigger.Addon,
21
- });
22
-
23
- export default ButtonTrigger;
@@ -1,284 +0,0 @@
1
- import React from 'react';
2
- import dayjs from 'dayjs';
3
- import isBetween from 'dayjs/plugin/isBetween';
4
- import createComponent, { Component, Root, sstyled } from '@semcore/core';
5
- import { Box } from '@semcore/flex-box';
6
- import fire from '@semcore/utils/lib/fire';
7
- import includesDate from '../utils/includesDate';
8
- import { getLocaleDate } from '../utils/formatDate';
9
-
10
- import style from '../style/calendar.shadow.css';
11
-
12
- dayjs.extend(isBetween);
13
-
14
- const range = (N, cb) => Array.from({ length: N }, (v, k) => cb(k));
15
-
16
- function getDayJSLocaleParams(locale) {
17
- if (locale.includes('en') || locale.includes('ja')) {
18
- return {
19
- weekStart: 0,
20
- ...dayjs.Ls[locale],
21
- };
22
- }
23
- return {
24
- weekStart: 1,
25
- ...dayjs.Ls[locale],
26
- };
27
- }
28
-
29
- class CalendarWeekDaysRoot extends Component {
30
- static displayName = 'CalendarWeekDays';
31
- static style = style;
32
- static defaultProps = {
33
- locale: 'en',
34
- children: ({ days }) => days.map((data, i) => <CalendarWeekDays.Unit key={i} {...data} />),
35
- };
36
-
37
- getDaysByWeek() {
38
- const { locale } = this.asProps;
39
-
40
- let date = dayjs().locale(locale, getDayJSLocaleParams(locale)).startOf('week');
41
- return range(7, () => {
42
- const weekday = new Intl.DateTimeFormat(locale, { weekday: 'short' }).format(date.valueOf());
43
- date = date.add(1, 'day');
44
- return {
45
- children: weekday,
46
- };
47
- });
48
- }
49
-
50
- setContext() {
51
- return {
52
- days: this.getDaysByWeek(),
53
- };
54
- }
55
-
56
- render() {
57
- const SWeekDays = Root;
58
- const { Children, styles } = this.asProps;
59
- return sstyled(styles)(
60
- <SWeekDays render={Box}>
61
- <Children />
62
- </SWeekDays>,
63
- );
64
- }
65
- }
66
-
67
- function CalendarWeekUnit(props) {
68
- const SWeekDay = Root;
69
- return sstyled(props.styles)(<SWeekDay render={Box} />);
70
- }
71
-
72
- class CalendarAbstract extends Component {
73
- static style = style;
74
-
75
- today = new Date(new Date().setHours(0, 0, 0, 0));
76
-
77
- createUnit({ date, ...other }, unit) {
78
- const self = this;
79
- const { disabled: _disabled, value, locale, highlighted: _highlighted } = this.asProps;
80
- let value0 = value[0] ? dayjs(value[0]) : null;
81
- let value1 = value[1] ? dayjs(value[1]) : null;
82
- let selected;
83
- if (value0 && value1) {
84
- if (value0 > value1) [value1, value0] = [value0, value1];
85
- selected = date.isBetween(value0, value1, unit, '()');
86
- }
87
- const startSelected = value0 && date.isSame(value0, unit);
88
- const endSelected = value1 && date.isSame(value1, unit);
89
-
90
- let highlighted0 = _highlighted[0] ? dayjs(_highlighted[0]) : null;
91
- let highlighted1 = _highlighted[1] ? dayjs(_highlighted[1]) : highlighted0;
92
- let startHighlighted, endHighlighted, highlighted;
93
- if (highlighted0 && highlighted1) {
94
- if (highlighted0 > highlighted1) [highlighted1, highlighted0] = [highlighted0, highlighted1];
95
- highlighted = date.isBetween(highlighted0, highlighted1, unit, '[]');
96
- startHighlighted = highlighted0 && date.isSame(highlighted0, 'date');
97
- endHighlighted = highlighted1 && date.isSame(highlighted1, 'date');
98
- }
99
-
100
- const disabled = _disabled.some(includesDate(date, unit));
101
-
102
- return {
103
- date: getLocaleDate(date, locale),
104
- children: '',
105
- startSelected,
106
- endSelected,
107
- selected,
108
- disabled,
109
- highlighted,
110
- startHighlighted,
111
- endHighlighted,
112
- today: date.isSame(self.today, unit),
113
- onClick: () => {
114
- const { highlighted: _highlighted } = this.asProps;
115
- let highlighted = [date.valueOf()];
116
-
117
- if (_highlighted.length === 1) {
118
- highlighted = [_highlighted[0], date.valueOf()];
119
- } else if (_highlighted.length >= 2) {
120
- highlighted = [];
121
- }
122
-
123
- fire(this, 'onHighlightedChange', highlighted);
124
- fire(this, 'onChange', date.toDate());
125
- },
126
- onMouseEnter: () => {
127
- const { value, highlighted: _highlighted } = this.asProps;
128
- if (_highlighted[0] || value.length === 1) {
129
- const startDate = _highlighted[0] || value[0];
130
- const highlighted = [startDate.valueOf(), date.valueOf()];
131
- fire(this, 'onHighlightedChange', highlighted);
132
- }
133
- },
134
- ...other,
135
- };
136
- }
137
-
138
- handleMouseLeave = () => {
139
- const { highlighted } = this.asProps;
140
-
141
- fire(this, 'onHighlightedChange', highlighted.length ? [highlighted[0]] : []);
142
- };
143
- }
144
-
145
- function CalendarUnit({ styles, date }) {
146
- const SCalendarUnit = Root;
147
- return sstyled(styles)(
148
- <SCalendarUnit render={Box} tag="button" aria-hidden={!date} aria-label={date} tabIndex={-1} />,
149
- );
150
- }
151
-
152
- class CalendarDaysRoot extends CalendarAbstract {
153
- static displayName = 'CalendarDays';
154
- static defaultProps = {
155
- displayedPeriod: new Date(new Date().setHours(0, 0, 0, 0)),
156
- disabled: [],
157
- value: [],
158
- locale: 'en',
159
- children: ({ days }) => days.map((data, i) => <CalendarDays.Unit key={i} {...data} />),
160
- };
161
-
162
- getDaysByMonth() {
163
- const { displayedPeriod, renderOutdated, locale } = this.asProps;
164
- let date = dayjs(displayedPeriod).startOf('month');
165
- let prevDate = date.subtract(1, 'month');
166
- let nextDate = date.add(1, 'month');
167
-
168
- /* 0 - 6 */
169
- const dateStartOfWeek = date
170
- .locale(locale, getDayJSLocaleParams(locale))
171
- .startOf('week')
172
- .get('d')
173
- ? (date.get('d') || 7) - 1
174
- : date.get('d');
175
-
176
- let prevMonthDays = dateStartOfWeek
177
- ? range(prevDate.daysInMonth(), () => {
178
- const day = this.createUnit({ date: prevDate, outdated: true }, 'date');
179
- day.children = String(prevDate.get('date'));
180
- prevDate = prevDate.add(1, 'day');
181
- return day;
182
- }).slice(-dateStartOfWeek)
183
- : [];
184
-
185
- const monthDays = range(date.daysInMonth(), () => {
186
- const day = this.createUnit({ date }, 'date');
187
- day.children = String(date.get('date'));
188
- date = date.add(1, 'day');
189
- return day;
190
- });
191
-
192
- let nextMonthDays = range(42 - prevMonthDays.length - monthDays.length, () => {
193
- const day = this.createUnit({ date: nextDate, outdated: true }, 'date');
194
- day.children = String(nextDate.get('date'));
195
- nextDate = nextDate.add(1, 'day');
196
- return day;
197
- });
198
-
199
- if (!renderOutdated) {
200
- prevMonthDays = prevMonthDays.map(() => ({
201
- disabled: true,
202
- children: '',
203
- }));
204
- nextMonthDays = [];
205
- }
206
-
207
- return [...prevMonthDays, ...monthDays, ...nextMonthDays];
208
- }
209
-
210
- setContext() {
211
- return {
212
- days: this.getDaysByMonth(),
213
- };
214
- }
215
-
216
- render() {
217
- const SCalendar = Root;
218
- const SGridDays = 'div';
219
- const { Children, styles, locale } = this.asProps;
220
-
221
- return sstyled(styles)(
222
- <SCalendar render={Box}>
223
- <CalendarWeekDays locale={locale} />
224
- <SGridDays onMouseLeave={this.handleMouseLeave}>
225
- <Children />
226
- </SGridDays>
227
- </SCalendar>,
228
- );
229
- }
230
- }
231
-
232
- class CalendarMonthsRoot extends CalendarAbstract {
233
- static displayName = 'CalendarMonths';
234
- static defaultProps = {
235
- children: ({ months }) => months.map((data, i) => <CalendarMonths.Unit key={i} {...data} />),
236
- };
237
-
238
- getMonthsByYear() {
239
- const { displayedPeriod, locale } = this.asProps;
240
-
241
- let date = dayjs(displayedPeriod).startOf('year');
242
-
243
- return range(12, () => {
244
- const month = this.createUnit({ date }, 'month');
245
- month.children = new Intl.DateTimeFormat(locale, { month: 'short' }).format(date.valueOf());
246
- date = date.add(1, 'month');
247
- return month;
248
- });
249
- }
250
-
251
- setContext() {
252
- return {
253
- months: this.getMonthsByYear(),
254
- };
255
- }
256
-
257
- render() {
258
- const SCalendar = Root;
259
- const SGridMonths = 'div';
260
- const { Children, styles } = this.asProps;
261
-
262
- return sstyled(styles)(
263
- <SCalendar render={Box}>
264
- <SGridMonths onMouseLeave={this.handleMouseLeave}>
265
- <Children />
266
- </SGridMonths>
267
- </SCalendar>,
268
- );
269
- }
270
- }
271
-
272
- const CalendarWeekDays = createComponent(CalendarWeekDaysRoot, {
273
- Unit: CalendarWeekUnit,
274
- });
275
-
276
- const CalendarDays = createComponent(CalendarDaysRoot, {
277
- Unit: CalendarUnit,
278
- });
279
-
280
- const CalendarMonths = createComponent(CalendarMonthsRoot, {
281
- Unit: CalendarUnit,
282
- });
283
-
284
- export { CalendarUnit, CalendarWeekDays, CalendarDays, CalendarMonths };
@@ -1,199 +0,0 @@
1
- import React from 'react';
2
- import dayjs from 'dayjs';
3
- import { Component, Root, CORE_INSTANCE, sstyled } from '@semcore/core';
4
- import Dropdown from '@semcore/dropdown';
5
- import i18nEnhance from '@semcore/utils/lib/enhances/i18nEnhance';
6
- import de from '../translations/de.json';
7
- import en from '../translations/en.json';
8
- import es from '../translations/es.json';
9
- import fr from '../translations/fr.json';
10
- import it from '../translations/it.json';
11
- import ja from '../translations/ja.json';
12
- import pt from '../translations/pt.json';
13
- import ru from '../translations/ru.json';
14
- import zh from '../translations/zh.json';
15
- import ko from '../translations/ko.json';
16
- import vi from '../translations/vi.json';
17
-
18
- import style from '../style/date-picker.shadow.css';
19
- import includesDate from '../utils/includesDate';
20
-
21
- const i18n = { de, en, es, fr, it, ja, ru, zh, pt, ko, vi };
22
-
23
- const INTERACTION_TAGS = ['INPUT'];
24
-
25
- const defaultDisplayedPeriod = new Date(new Date().setHours(0, 0, 0, 0));
26
-
27
- class PickerAbstract extends Component {
28
- static displayName = 'DatePicker';
29
- static style = style;
30
- static defaultProps({ value, defaultValue }) {
31
- return {
32
- i18n,
33
- locale: 'en',
34
- defaultDisplayedPeriod: value || defaultValue || defaultDisplayedPeriod,
35
- defaultValue: null,
36
- defaultVisible: false,
37
- defaultHighlighted: [],
38
- disabled: [],
39
- size: 'm',
40
- };
41
- }
42
- static enhance = [i18nEnhance()];
43
-
44
- static add = (date, amount, unit) => {
45
- return dayjs(date).add(amount, unit).toDate();
46
- };
47
-
48
- static subtract = (date, amount, unit) => {
49
- return dayjs(date).subtract(amount, unit).toDate();
50
- };
51
-
52
- navigateStep;
53
- keyDiff;
54
- keyStep;
55
-
56
- uncontrolledProps() {
57
- return {
58
- displayedPeriod: null,
59
- visible: [
60
- null,
61
- (visible) => {
62
- if (!visible) {
63
- this.handlers.highlighted([]);
64
- this.handlers.displayedPeriod(this.asProps.value || this.props.defaultDisplayedPeriod);
65
- }
66
- },
67
- ],
68
- highlighted: null,
69
- value: [
70
- null,
71
- (value) => {
72
- // TODO: работает только из-за new Date() !== new Date()
73
- this.handlers.visible(false);
74
- this.handlers.displayedPeriod(value);
75
- },
76
- ],
77
- };
78
- }
79
-
80
- navigateView = (direction) => {
81
- const { displayedPeriod } = this.asProps;
82
- const action = direction >= 1 ? 'add' : 'subtract';
83
- const date = dayjs(displayedPeriod)[action](1, this.navigateStep).toDate();
84
- this.handlers.displayedPeriod(date);
85
- };
86
-
87
- bindHandlerNavigateClick = (direction) => () => {
88
- this.navigateView(direction);
89
- };
90
-
91
- handlerKeyDown = (e) => {
92
- if (e.target !== e.currentTarget) return;
93
- const { value, displayedPeriod, highlighted, disabled: _disabled } = this.asProps;
94
- const day = this.keyDiff[e.keyCode];
95
-
96
- const getCurrentHighlightedDay = (day) => {
97
- const current_day = day.toDate();
98
- const isDisabledDay = _disabled.some(includesDate(dayjs(current_day), 'date'));
99
- return isDisabledDay ? null : current_day;
100
- };
101
-
102
- if (e.keyCode === 32 || (e.keyCode === 13 && highlighted.length)) {
103
- this.handlers.value(highlighted[0]);
104
- e.preventDefault();
105
- }
106
- if (day) {
107
- if (INTERACTION_TAGS.includes(e.target.tagName)) return;
108
- const current_highlighted =
109
- !highlighted[0] && !value
110
- ? dayjs(highlighted[0] || displayedPeriod)
111
- : dayjs(displayedPeriod).add(day, this.keyStep);
112
- const current_day =
113
- getCurrentHighlightedDay(current_highlighted) ||
114
- getCurrentHighlightedDay(dayjs(highlighted[0] || displayedPeriod));
115
-
116
- if (current_day) {
117
- this.handlers.highlighted([current_day]);
118
- this.handlers.displayedPeriod(current_day);
119
- e.preventDefault();
120
- }
121
- }
122
- };
123
-
124
- getTriggerProps() {
125
- const { value, size } = this.asProps;
126
- return {
127
- size,
128
- empty: !value,
129
- onKeyDown: this.handlerKeyDown,
130
- };
131
- }
132
-
133
- getPopperProps() {
134
- const Picker = this[CORE_INSTANCE];
135
- return {
136
- tabIndex: 0,
137
- onKeyDown: this.handlerKeyDown,
138
- children: (
139
- <>
140
- <Picker.Header />
141
- <Picker.Calendar />
142
- </>
143
- ),
144
- };
145
- }
146
-
147
- getHeaderProps() {
148
- const Picker = this[CORE_INSTANCE];
149
- return {
150
- children: (
151
- <>
152
- <Picker.Prev />
153
- <Picker.Title />
154
- <Picker.Next />
155
- </>
156
- ),
157
- };
158
- }
159
-
160
- getTitleProps() {
161
- return {};
162
- }
163
-
164
- getNextProps() {
165
- return {
166
- onClick: this.bindHandlerNavigateClick(1),
167
- };
168
- }
169
-
170
- getPrevProps() {
171
- return {
172
- onClick: this.bindHandlerNavigateClick(-1),
173
- };
174
- }
175
-
176
- getCalendarProps() {
177
- const { locale, displayedPeriod, disabled, value, onChange, highlighted } = this.asProps;
178
- return {
179
- locale,
180
- displayedPeriod,
181
- disabled,
182
- onChange,
183
- highlighted,
184
- value: [value, value],
185
- renderOutdated: true,
186
- };
187
- }
188
-
189
- render() {
190
- const { styles, Children } = this.asProps;
191
- return sstyled(styles)(
192
- <Root render={Dropdown}>
193
- <Children />
194
- </Root>,
195
- );
196
- }
197
- }
198
-
199
- export default PickerAbstract;