@startlibs/form 1.0.12 → 1.1.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.
Files changed (92) hide show
  1. package/lib/AutoComplete.mjs +550 -0
  2. package/lib/AutoComplete.mjs.map +1 -0
  3. package/lib/Checkbox.mjs +76 -0
  4. package/lib/Checkbox.mjs.map +1 -0
  5. package/lib/Combobox.mjs +544 -0
  6. package/lib/Combobox.mjs.map +1 -0
  7. package/lib/ComboboxWithCustom.mjs +77 -0
  8. package/lib/ComboboxWithCustom.mjs.map +1 -0
  9. package/lib/DatePicker.mjs +625 -0
  10. package/lib/DatePicker.mjs.map +1 -0
  11. package/lib/EditableBox.mjs +496 -0
  12. package/lib/EditableBox.mjs.map +1 -0
  13. package/lib/Errors.mjs +176 -0
  14. package/lib/Errors.mjs.map +1 -0
  15. package/lib/ExpandableBox.mjs +209 -0
  16. package/lib/ExpandableBox.mjs.map +1 -0
  17. package/lib/Field.mjs +65 -0
  18. package/lib/Field.mjs.map +1 -0
  19. package/lib/FieldBox.mjs +80 -0
  20. package/lib/FieldBox.mjs.map +1 -0
  21. package/lib/Form.mjs +322 -0
  22. package/lib/Form.mjs.map +1 -0
  23. package/lib/FormValue.mjs +111 -0
  24. package/lib/FormValue.mjs.map +1 -0
  25. package/lib/InputboxGroup.mjs +104 -0
  26. package/lib/InputboxGroup.mjs.map +1 -0
  27. package/lib/Radiobox.mjs +67 -0
  28. package/lib/Radiobox.mjs.map +1 -0
  29. package/lib/RichText.mjs +634 -0
  30. package/lib/RichText.mjs.map +1 -0
  31. package/lib/TextInput.mjs +359 -0
  32. package/lib/TextInput.mjs.map +1 -0
  33. package/lib/alt/checkbox/SimpleCheckbox.mjs +82 -0
  34. package/lib/alt/checkbox/SimpleCheckbox.mjs.map +1 -0
  35. package/lib/alt/checkbox/ToggleCheckbox.mjs +68 -0
  36. package/lib/alt/checkbox/ToggleCheckbox.mjs.map +1 -0
  37. package/lib/alt/radiobox/CleanTabRadiobox.mjs +44 -0
  38. package/lib/alt/radiobox/CleanTabRadiobox.mjs.map +1 -0
  39. package/lib/alt/radiobox/IconRadioBox.mjs +52 -0
  40. package/lib/alt/radiobox/IconRadioBox.mjs.map +1 -0
  41. package/lib/alt/radiobox/SimpleRadiobox.mjs +61 -0
  42. package/lib/alt/radiobox/SimpleRadiobox.mjs.map +1 -0
  43. package/lib/alt/radiobox/TabRadiobox.mjs +54 -0
  44. package/lib/alt/radiobox/TabRadiobox.mjs.map +1 -0
  45. package/lib/enhanceInput.mjs +131 -0
  46. package/lib/enhanceInput.mjs.map +1 -0
  47. package/lib/index.cjs +5345 -0
  48. package/lib/index.cjs.map +1 -0
  49. package/lib/index.mjs +28 -0
  50. package/lib/index.mjs.map +1 -0
  51. package/lib/useConfirmDialog.mjs +117 -0
  52. package/lib/useConfirmDialog.mjs.map +1 -0
  53. package/lib/useDraftConfirmation.mjs +56 -0
  54. package/lib/useDraftConfirmation.mjs.map +1 -0
  55. package/lib/validation.mjs +91 -0
  56. package/lib/validation.mjs.map +1 -0
  57. package/lib/withFormImageInput.mjs +281 -0
  58. package/lib/withFormImageInput.mjs.map +1 -0
  59. package/package.json +46 -26
  60. package/src/AutoComplete.jsx +642 -0
  61. package/src/Checkbox.jsx +45 -0
  62. package/src/Combobox.jsx +606 -0
  63. package/src/ComboboxWithCustom.jsx +68 -0
  64. package/src/DatePicker.jsx +733 -0
  65. package/src/EditableBox.jsx +502 -0
  66. package/src/Errors.jsx +174 -0
  67. package/src/ExpandableBox.jsx +207 -0
  68. package/src/Field.jsx +76 -0
  69. package/src/FieldBox.jsx +136 -0
  70. package/src/Form.jsx +291 -0
  71. package/src/FormValue.jsx +67 -0
  72. package/src/IconRadioBoxOldHubt.jsx +93 -0
  73. package/src/InputboxGroup.jsx +121 -0
  74. package/src/Radiobox.jsx +33 -0
  75. package/src/RichText.jsx +812 -0
  76. package/src/TextInput.jsx +519 -0
  77. package/src/alt/checkbox/SimpleCheckbox.jsx +162 -0
  78. package/src/alt/checkbox/ToggleCheckbox.jsx +143 -0
  79. package/src/alt/radiobox/CleanTabRadiobox.jsx +46 -0
  80. package/src/alt/radiobox/IconRadioBox.jsx +93 -0
  81. package/src/alt/radiobox/SimpleRadiobox.jsx +153 -0
  82. package/src/alt/radiobox/TabRadiobox.jsx +73 -0
  83. package/src/enhanceInput.jsx +107 -0
  84. package/src/index.js +27 -0
  85. package/src/useConfirmDialog.jsx +105 -0
  86. package/src/useDraftConfirmation.jsx +60 -0
  87. package/src/validation.js +92 -0
  88. package/src/withFormImageInput.jsx +297 -0
  89. package/.babelrc +0 -28
  90. package/lib/index.js +0 -6258
  91. package/rollup.config.js +0 -43
  92. package/yarn-error.log +0 -1607
@@ -0,0 +1,625 @@
1
+ import _extends from '@babel/runtime/helpers/extends';
2
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
+ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
4
+ import _taggedTemplateLiteral from '@babel/runtime/helpers/taggedTemplateLiteral';
5
+ import _isFinite from 'lodash/fp/isFinite';
6
+ import React, { forwardRef, useRef, useContext, useState, useEffect } from 'react';
7
+ import styled, { css } from 'styled-components';
8
+ import { useEnsureRef, usePopupToggle, StartlibsContext, usePrevious } from '@startlibs/core';
9
+ import { getColor, media, ifProp, formatDate, getNumberInputType, formatDateToPattern, parseDate, callIfFunction } from '@startlibs/utils';
10
+ import { Icon } from '@startlibs/components';
11
+ import { Field } from './Field.mjs';
12
+ import { TextInput } from './TextInput.mjs';
13
+ import { useInput, enhanceInput } from './enhanceInput.mjs';
14
+ import { darken } from 'polished';
15
+
16
+ const _excluded = ["format", "min", "max", "weekStartsMonday", "label", "mandatory", "tabIndex", "helpText", "path", "timezoneOffset", "placeholder", "onBlur", "raw", "disabled", "level", "daysFromNowShortcuts", "monthsFromNowShortcuts"];
17
+ var _templateObject, _templateObject2, _templateObject3;
18
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
19
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
20
+ const CALENDAR_LEVEL_DAY = 'day';
21
+ const CALENDAR_LEVEL_MONTH = 'month';
22
+ const CALENDAR_LEVEL_YEAR = 'year';
23
+ const LEVELS = [CALENDAR_LEVEL_DAY, CALENDAR_LEVEL_MONTH, CALENDAR_LEVEL_YEAR];
24
+ const DatePickerContainer = /*#__PURE__*/styled.div.withConfig({
25
+ displayName: "DatePicker__DatePickerContainer",
26
+ componentId: "sc-1wo8j4d-0"
27
+ })(["width:20rem;position:absolute;z-index:100;background-color:white;border-radius:5px;border:1px solid ", ";user-select:none;box-shadow:0 0 4px 0 rgba(0,0,0,0.2);font-size:13px;text-align:center;animation:0.2s datePickerFadeInDown ease-out;display:none;@supports (flex-wrap:wrap){display:block;}input{padding-right:2.25rem;", "}@keyframes datePickerFadeInDown{from{opacity:0;transform:translateY(-10px);pointer-events:none;}to{opacity:1;transform:none;pointer-events:none;}}"], getColor('gray210'), media.mobile(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 2.5rem;\n "]))));
28
+ const CalendarHeader = /*#__PURE__*/styled.div.withConfig({
29
+ displayName: "DatePicker__CalendarHeader",
30
+ componentId: "sc-1wo8j4d-1"
31
+ })(["display:flex;padding:0.5rem;align-items:center;"]);
32
+ const CalendarContentContainer = /*#__PURE__*/styled.div.withConfig({
33
+ displayName: "DatePicker__CalendarContentContainer",
34
+ componentId: "sc-1wo8j4d-2"
35
+ })(["padding:0 0.5rem 0.5rem 0.5rem;display:flex;flex-wrap:wrap;justify-content:space-around;"]);
36
+ const WeekHeader = /*#__PURE__*/styled(CalendarContentContainer).withConfig({
37
+ displayName: "DatePicker__WeekHeader",
38
+ componentId: "sc-1wo8j4d-3"
39
+ })(["font-size:12px;font-weight:600;cursor:default;span{width:2.5rem;}"]);
40
+ const CalendarClickable = /*#__PURE__*/styled.a.withConfig({
41
+ displayName: "DatePicker__CalendarClickable",
42
+ componentId: "sc-1wo8j4d-4"
43
+ })([":hover,:focus{background-color:rgba(0,0,0,0.1);cursor:pointer;}"]);
44
+ const TitleButton = /*#__PURE__*/styled(CalendarClickable).withConfig({
45
+ displayName: "DatePicker__TitleButton",
46
+ componentId: "sc-1wo8j4d-5"
47
+ })(["font-size:16px;font-weight:600;flex-grow:1;border-radius:4px;line-height:3rem;"]);
48
+ const NavigationButton = /*#__PURE__*/styled(CalendarClickable).withConfig({
49
+ displayName: "DatePicker__NavigationButton",
50
+ componentId: "sc-1wo8j4d-6"
51
+ })(["font-size:21px;width:3rem;text-align:center;border-radius:4px;height:3rem;", "{line-height:3rem;}"], Icon);
52
+ const CalendarContent = /*#__PURE__*/styled(CalendarClickable).withConfig({
53
+ displayName: "DatePicker__CalendarContent",
54
+ componentId: "sc-1wo8j4d-7"
55
+ })(["line-height:2.25rem;flex-grow:1;display:inline-block;border-radius:5px;margin-top:0.25rem;margin-left:0.25rem;", " ", " ", " ", " "], ifProp('isEdge', css(["color:rgba(0,0,0,0.4);"])), ifProp('isSelected', css(["box-shadow:inset 0 0 0 1px ", ";"], getColor('gray210'))), ifProp('isDisabled', "pointer-events: none;\n opacity: 0.5;"), ifProp('isCurrent', css(["background:", ";color:#FFF;:hover{background-color:", ";}", ""], getColor('main'), props => darken(0.075, getColor('main')(props)), ifProp('isSelected', 'box-shadow: none;'))));
56
+ const DayItem = /*#__PURE__*/styled(CalendarContent).withConfig({
57
+ displayName: "DatePicker__DayItem",
58
+ componentId: "sc-1wo8j4d-8"
59
+ })(["", " flex-basis:12%;"], props => props.index % 7 === 0 && 'margin-left:0;');
60
+ const MonthItem = /*#__PURE__*/styled(CalendarContent).withConfig({
61
+ displayName: "DatePicker__MonthItem",
62
+ componentId: "sc-1wo8j4d-9"
63
+ })(["", " flex-basis:30%;"], props => props.index % 3 === 0 && 'margin-left:0;');
64
+ const YearItem = /*#__PURE__*/styled(CalendarContent).withConfig({
65
+ displayName: "DatePicker__YearItem",
66
+ componentId: "sc-1wo8j4d-10"
67
+ })(["", " flex-basis:30%;"], props => props.index % 3 === 0 && 'margin-left:0;');
68
+ const DatePickerInputIcon = /*#__PURE__*/styled(Icon).withConfig({
69
+ displayName: "DatePicker__DatePickerInputIcon",
70
+ componentId: "sc-1wo8j4d-11"
71
+ })(["position:absolute;outline:none;bottom:0;font-size:21px;line-height:3rem;right:0;width:2rem;cursor:pointer;color:", ";:hover{color:", ";}display:none;@supports (flex-wrap:wrap){display:block;}", " ", ""], getColor('gray150'), getColor('main'), props => props.disabled && 'pointer-events: none;', media.mobile(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n line-height: 42px;\n width: 2.25rem;\n "]))));
72
+ /*#__PURE__*/styled(Icon).withConfig({
73
+ displayName: "DatePicker__ClearButton",
74
+ componentId: "sc-1wo8j4d-12"
75
+ })(["color:", ";font-size:12px;position:absolute;right:40px;bottom:0;transform:translateY(-50%);cursor:pointer;width:1.5rem;line-height:1.5rem;text-align:center;border-radius:50%;background:rgba(0,0,0,0.1);:hover{background:#f7d3d4;color:", ";}"], getColor('gray90'), getColor('alert'));
76
+ const InputAndIconWrapper = /*#__PURE__*/styled.div.withConfig({
77
+ displayName: "DatePicker__InputAndIconWrapper",
78
+ componentId: "sc-1wo8j4d-13"
79
+ })(["position:relative;"]);
80
+ const ShortcutsWrapper = /*#__PURE__*/styled.div.withConfig({
81
+ displayName: "DatePicker__ShortcutsWrapper",
82
+ componentId: "sc-1wo8j4d-14"
83
+ })(["display:flex;flex-wrap:wrap;gap:0.5rem;padding:0.5rem 0.25rem 0;color:", ";.link{cursor:pointer;font-weight:600;text-decoration:underline;", ":hover{color:", ";}", "}"], getColor('gray150'), media.mobile(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n font-size: 13px; \n "]))), getColor('main'), ifProp('disabled', css(["pointer-events:none;opacity:0.5;"])));
84
+ const KeyMapper = level => {
85
+ const upDownSpacing = level === CALENDAR_LEVEL_DAY ? 7 : 3;
86
+ return {
87
+ 'ArrowLeft': -1,
88
+ 'ArrowRight': 1,
89
+ 'ArrowUp': -upDownSpacing,
90
+ 'ArrowDown': upDownSpacing
91
+ };
92
+ };
93
+ const getUTCDate = function (year) {
94
+ let month = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
95
+ let date = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
96
+ let hour = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
97
+ let min = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
98
+ let sec = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
99
+ let ms = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
100
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms));
101
+ };
102
+ const getUTCDateBefore = function (year) {
103
+ let month = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
104
+ let date = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
105
+ let hour = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
106
+ let min = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
107
+ let sec = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
108
+ let ms = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : -1;
109
+ return new Date(Date.UTC(year, month, date, hour, min, sec, ms));
110
+ };
111
+ const DatePicker = /*#__PURE__*/forwardRef((_ref, maybeInputRef) => {
112
+ let {
113
+ format = 'dd/MM/yyyy',
114
+ min = new Date(now().setUTCFullYear(now().getUTCFullYear() - 100)),
115
+ max = new Date(now().setUTCFullYear(now().getUTCFullYear() + 100)),
116
+ weekStartsMonday = false,
117
+ label,
118
+ mandatory,
119
+ tabIndex,
120
+ helpText,
121
+ path,
122
+ timezoneOffset = 0,
123
+ placeholder,
124
+ onBlur,
125
+ raw,
126
+ disabled,
127
+ level,
128
+ daysFromNowShortcuts,
129
+ // Uma array de números representando atalhos de dias a partir de agora, e.g. [0, 1, 3]
130
+ monthsFromNowShortcuts // Uma array de números representando atalhos de meses a partir de agora, e.g. [0, 1, 3]
131
+ } = _ref,
132
+ rest = _objectWithoutProperties(_ref, _excluded);
133
+ const inputRef = useEnsureRef(maybeInputRef);
134
+ const [formValue, setUnoffsetedFormValue, {
135
+ hasErrors
136
+ }] = useInput(_objectSpread({
137
+ path
138
+ }, rest), raw);
139
+ const setFormValue = newV => setUnoffsetedFormValue(_isFinite(newV) && newV !== -1 ? newV - timezoneOffset * 36e5 : newV);
140
+ const popup = usePopupToggle();
141
+ const calendarElement = useRef();
142
+ const messages = useContext(StartlibsContext).dateMessages;
143
+ const time = Number(formValue);
144
+ const offsetedTime = time + timezoneOffset * 36e5;
145
+ const [textValue, setRawTextValue] = useState(!isNaN(time) ? formatDate(new Date(offsetedTime), format, messages) : '');
146
+ const prevValue = usePrevious(textValue);
147
+ const [hasFormatErrors, setHasFormatErrors] = useState(false);
148
+ useEffect(() => {
149
+ if (textValue && prevValue && textValue.length > prevValue.length + 1) {
150
+ setTimeout(() => inputRef.current.setSelectionRange(inputRef.current.value.length + 1, inputRef.current.value.length + 1), 1);
151
+ }
152
+ }, [textValue]);
153
+ const isShortcutsEnabled = !!daysFromNowShortcuts && daysFromNowShortcuts.length > 0 || !!monthsFromNowShortcuts && monthsFromNowShortcuts.length > 0;
154
+ const setDateToNDaysFromNow = daysFromNow => {
155
+ const date = new Date(now());
156
+ date.setUTCDate(date.getUTCDate() + daysFromNow);
157
+ setFormValue(date.getTime());
158
+ updateText(date);
159
+ };
160
+ const setDateToNMonthsFromNow = monthsFromNow => {
161
+ const date = new Date(now());
162
+ date.setUTCMonth(date.getUTCMonth() + monthsFromNow);
163
+ setFormValue(date.getTime());
164
+ updateText(date);
165
+ };
166
+ const setTextValue = textValue => {
167
+ const value = formValue === -1 || isNaN(formValue) ? now() : new Date(Number(formValue));
168
+ const date = parseDate(textValue, value, format, messages);
169
+ if (isNaN(date.getTime())) {
170
+ const nextMonth = new Date(value);
171
+ const closestLeapYear = new Date(value);
172
+ nextMonth.setUTCMonth(value.getUTCMonth() + 1);
173
+ closestLeapYear.setUTCFullYear(getClosestNextLeapYear(value.getUTCFullYear()));
174
+ const nextMonthDate = parseDate(textValue, nextMonth, format, messages);
175
+ const closestLeapYearDate = parseDate(textValue, closestLeapYear, format, messages);
176
+ setRawTextValue(textValue);
177
+ setHasFormatErrors(isNaN(nextMonthDate.getTime()) && isNaN(closestLeapYearDate));
178
+ } else if (!inRange({
179
+ min,
180
+ max
181
+ }, date)) {
182
+ setRawTextValue(textValue);
183
+ setHasFormatErrors(true);
184
+ setFormValue(-1);
185
+ } else {
186
+ setRawTextValue(textValue);
187
+ setHasFormatErrors(false);
188
+ setFormValue(date.getTime());
189
+ }
190
+ };
191
+ const updateText = date => {
192
+ setTextValue(formatDate(date, format, messages));
193
+ setHasFormatErrors(false);
194
+ };
195
+ const setValue = value => {
196
+ setFormValue(value);
197
+ updateText(new Date(value));
198
+ };
199
+ const confirmValue = () => {
200
+ popup.close();
201
+ if (!textValue) {
202
+ setFormValue(undefined);
203
+ setHasFormatErrors(false);
204
+ } else {
205
+ const date = parseDate(textValue, new Date(formValue), format, messages, true);
206
+ if (isNaN(date.getTime())) {
207
+ setHasFormatErrors(true);
208
+ setFormValue(-1);
209
+ } else {
210
+ updateText(date);
211
+ }
212
+ }
213
+ };
214
+ const _onBlur = e => {
215
+ const currentTarget = e.currentTarget;
216
+ setTimeout(() => {
217
+ if (!currentTarget.contains(document.activeElement)) {
218
+ confirmValue();
219
+ }
220
+ callIfFunction(onBlur);
221
+ }, 1);
222
+ };
223
+ const onKeyDown = e => {
224
+ const isConfirm = e.key === 'Enter';
225
+ if (Object.keys(KeyMapper()).indexOf(e.key) >= 0 || isConfirm) {
226
+ if (popup.isOpen) {
227
+ e.preventDefault();
228
+ calendarElement.current.keyDown(e);
229
+ }
230
+ }
231
+ if (e.key === 'Escape') {
232
+ e.stopPropagation();
233
+ popup.close();
234
+ const date = new Date(formValue);
235
+ if (!isNaN(date.getTime())) updateText(date);
236
+ }
237
+ };
238
+ const refocus = e => inputRef.current.focus();
239
+ const onClick = e => {
240
+ if (disabled) return;
241
+ e.stopPropagation();
242
+ refocus();
243
+ };
244
+ const iconClick = e => {
245
+ e.stopPropagation();
246
+ if (popup.isOpen) {
247
+ refocus();
248
+ popup.close();
249
+ } else {
250
+ refocus();
251
+ }
252
+ };
253
+ const stopFocusPropagation = e => e.stopPropagation();
254
+
255
+ // const calendarValue = new Date(!isNaN(time) && formValue !== -1 ? offsetedTime : now())
256
+ const calendarValue = new Date(!isNaN(time) && formValue !== -1 ? offsetedTime : nowTimeZoneAdjusted(timezoneOffset));
257
+ calendarValue.setUTCHours(0, 0, 0);
258
+ return /*#__PURE__*/React.createElement(Field, {
259
+ helpText: helpText,
260
+ label: label,
261
+ mandatory: mandatory,
262
+ onClick: onClick,
263
+ onFocus: popup.open,
264
+ onBlur: _onBlur
265
+ }, /*#__PURE__*/React.createElement(InputAndIconWrapper, null, /*#__PURE__*/React.createElement(TextInput, {
266
+ raw: true,
267
+ disabled: disabled,
268
+ type: getNumberInputType(),
269
+ tabIndex: tabIndex,
270
+ ref: inputRef,
271
+ constraint: formatDateToPattern(format),
272
+ value: textValue,
273
+ setValue: setTextValue,
274
+ placeholder: placeholder,
275
+ hasErrors: hasErrors || hasFormatErrors,
276
+ onKeyDown: onKeyDown
277
+ }), /*#__PURE__*/React.createElement(DatePickerInputIcon, {
278
+ tabIndex: -1,
279
+ onFocus: stopFocusPropagation,
280
+ disabled: disabled,
281
+ onClick: iconClick,
282
+ icon: "calendar",
283
+ className: isShortcutsEnabled ? "hasShortcuts" : ""
284
+ })), (daysFromNowShortcuts === null || daysFromNowShortcuts === void 0 ? void 0 : daysFromNowShortcuts.length) > 0 || (monthsFromNowShortcuts === null || monthsFromNowShortcuts === void 0 ? void 0 : monthsFromNowShortcuts.length) > 0 ? /*#__PURE__*/React.createElement(ShortcutsWrapper, null, /*#__PURE__*/React.createElement("span", null, "Em:"), daysFromNowShortcuts === null || daysFromNowShortcuts === void 0 ? void 0 : daysFromNowShortcuts.map(daysFromNow => /*#__PURE__*/React.createElement("span", {
285
+ class: "link",
286
+ key: "days-from-now-".concat(daysFromNow),
287
+ onClick: () => setDateToNDaysFromNow(daysFromNow),
288
+ disabled: disabled
289
+ }, daysFromNow, " dia", daysFromNow > 1 ? 's' : '')), monthsFromNowShortcuts === null || monthsFromNowShortcuts === void 0 ? void 0 : monthsFromNowShortcuts.map(monthsFromNow => /*#__PURE__*/React.createElement("span", {
290
+ class: "link",
291
+ key: "months-from-now".concat(monthsFromNow),
292
+ onClick: () => setDateToNMonthsFromNow(monthsFromNow),
293
+ disabled: disabled
294
+ }, monthsFromNow, " ", monthsFromNow > 1 ? 'meses' : 'mês'))) : null, popup.isOpen && /*#__PURE__*/React.createElement(DatePickerCalendar, {
295
+ raw: true,
296
+ min: min,
297
+ max: max,
298
+ level: level,
299
+ weekStartsMonday: weekStartsMonday,
300
+ messages: messages,
301
+ ref: calendarElement,
302
+ updateTransientValue: updateText,
303
+ refocus: refocus,
304
+ onSelectDay: popup.toggle,
305
+ setValue: setValue,
306
+ value: calendarValue.getTime()
307
+ }));
308
+ });
309
+ const defaultProps = {
310
+ format: 'dd/MM/yyyy',
311
+ min: new Date(now().setUTCFullYear(now().getUTCFullYear() - 100)),
312
+ max: new Date(now().setUTCFullYear(now().getUTCFullYear() + 100)),
313
+ weekStartsMonday: false
314
+ };
315
+ class RawDatePickerCalendar extends React.Component {
316
+ constructor(props) {
317
+ super(props);
318
+ this.element = void 0;
319
+ this.setCurrentDatePlus = (n, type) => () => {
320
+ const date = new Date(this.state.currentDate.getTime());
321
+ switch (type) {
322
+ case CALENDAR_LEVEL_DAY:
323
+ date.setUTCDate(date.getUTCDate() + n);
324
+ break;
325
+ case CALENDAR_LEVEL_MONTH:
326
+ date.setUTCMonth(date.getUTCMonth() + n);
327
+ break;
328
+ case CALENDAR_LEVEL_YEAR:
329
+ date.setUTCFullYear(date.getUTCFullYear() + n);
330
+ break;
331
+ }
332
+ this.props.updateTransientValue && this.props.updateTransientValue(date);
333
+ this.setState({
334
+ currentDate: date
335
+ });
336
+ };
337
+ this.up = () => {
338
+ this.setState({
339
+ level: LEVELS[LEVELS.indexOf(this.state.level) + 1]
340
+ });
341
+ };
342
+ this.keyDown = e => {
343
+ const {
344
+ level,
345
+ currentDate
346
+ } = this.state;
347
+ const keyMapper = KeyMapper(level);
348
+ if (keyMapper[e.key]) {
349
+ e.preventDefault();
350
+ this.setCurrentDatePlus(keyMapper[e.key], level)();
351
+ }
352
+ if (e.key === 'Enter') {
353
+ e.stopPropagation();
354
+ e.preventDefault();
355
+ this.handleChange(currentDate);
356
+ }
357
+ };
358
+ this.handleChange = date => {
359
+ this.props.setValue(date.getTime());
360
+ if (this.state.level === CALENDAR_LEVEL_DAY && this.props.onSelectDay) {
361
+ this.props.onSelectDay();
362
+ }
363
+ this.setState({
364
+ level: LEVELS[Math.max(LEVELS.indexOf(this.state.level) - 1, 0)]
365
+ });
366
+ };
367
+ this.getView = () => {
368
+ switch (this.state.level) {
369
+ case CALENDAR_LEVEL_DAY:
370
+ return /*#__PURE__*/React.createElement(Calendar, _extends({}, this.props, this.state, {
371
+ handleChange: this.handleChange,
372
+ changeCurrentDate: this.setCurrentDatePlus,
373
+ up: this.up
374
+ }));
375
+ case CALENDAR_LEVEL_MONTH:
376
+ return /*#__PURE__*/React.createElement(Months, _extends({}, this.props, this.state, {
377
+ handleChange: this.handleChange,
378
+ changeCurrentDate: this.setCurrentDatePlus,
379
+ up: this.up
380
+ }));
381
+ case CALENDAR_LEVEL_YEAR:
382
+ return /*#__PURE__*/React.createElement(Years, _extends({}, this.props, this.state, {
383
+ handleChange: this.handleChange,
384
+ changeCurrentDate: this.setCurrentDatePlus
385
+ }));
386
+ default:
387
+ return null;
388
+ }
389
+ };
390
+ this.setRef = e => {
391
+ this.element = e;
392
+ };
393
+ this.state = {
394
+ level: this.props.level || CALENDAR_LEVEL_DAY,
395
+ currentDate: new Date(props.value || now())
396
+ };
397
+ }
398
+ componentDidUpdate(prevProps) {
399
+ if (prevProps.value !== this.props.value) {
400
+ this.setState({
401
+ currentDate: new Date(this.props.value)
402
+ });
403
+ }
404
+ }
405
+ render() {
406
+ return /*#__PURE__*/React.createElement("div", {
407
+ ref: this.setRef,
408
+ tabIndex: -1,
409
+ onFocus: this.props.refocus,
410
+ onKeyDown: this.keyDown
411
+ }, this.getView());
412
+ }
413
+ }
414
+ RawDatePickerCalendar.defaultProps = defaultProps;
415
+ const DatePickerCalendar = enhanceInput(RawDatePickerCalendar);
416
+ function now() {
417
+ const dt = new Date();
418
+ dt.setUTCHours(0, 0, 0, 0);
419
+ return dt;
420
+ }
421
+ function nowTimeZoneAdjusted(timezoneOffset) {
422
+ const timezoneOffsetMilliseconds = timezoneOffset * 3600000; // 3600000 milliseconds in an hour
423
+ const dt = new Date();
424
+ dt.setTime(dt.getTime() + timezoneOffsetMilliseconds);
425
+ dt.setUTCHours(0, 0, 0, 0);
426
+ return dt;
427
+ }
428
+ function inRange(context, date) {
429
+ return context.min <= date && context.max >= date;
430
+ }
431
+ const mapDays = (currentDate, dayOffset, fn) => {
432
+ const month = [];
433
+ const iter = new Date(currentDate);
434
+ iter.setUTCDate(1);
435
+ let startDay = dayOffset + iter.getUTCDate() - iter.getUTCDay();
436
+ if (startDay > 1 && startDay < 10) {
437
+ startDay = startDay - 7;
438
+ }
439
+ iter.setUTCDate(startDay);
440
+ for (let week = 0; week < 6; week++) {
441
+ for (let day = week * 7; day < week * 7 + 7; day++) {
442
+ month.push(fn(iter, startDay + day, day));
443
+ iter.setUTCDate(iter.getUTCDate() + 1);
444
+ }
445
+ }
446
+ return month;
447
+ };
448
+ const Calendar = _ref2 => {
449
+ let {
450
+ messages,
451
+ max,
452
+ min,
453
+ value,
454
+ currentDate,
455
+ weekStartsMonday,
456
+ changeCurrentDate,
457
+ handleChange,
458
+ up
459
+ } = _ref2;
460
+ const currentMonth = currentDate.getUTCMonth();
461
+ const dayOffset = weekStartsMonday ? 1 : 0;
462
+ const today = now().getTime();
463
+ const handleDateChange = e => {
464
+ const date = new Date(currentDate);
465
+ handleChange(new Date(date.setUTCDate(e.currentTarget.getAttribute('data-day'))));
466
+ };
467
+ const previousMonth = getUTCDateBefore(currentDate.getUTCFullYear(), currentDate.getUTCMonth());
468
+ const nextMonth = getUTCDate(currentDate.getUTCFullYear(), currentDate.getUTCMonth() + 1);
469
+ return /*#__PURE__*/React.createElement(DatePickerContainer, null, /*#__PURE__*/React.createElement(CalendarHeader, null, /*#__PURE__*/React.createElement(NavigationButton, {
470
+ disabled: !inRange({
471
+ min,
472
+ max
473
+ }, previousMonth),
474
+ onClick: changeCurrentDate(-1, CALENDAR_LEVEL_MONTH)
475
+ }, /*#__PURE__*/React.createElement(Icon, {
476
+ icon: "arrow-left-line"
477
+ })), /*#__PURE__*/React.createElement(TitleButton, {
478
+ onClick: up
479
+ }, messages('month', currentMonth), " ", currentDate.getUTCFullYear()), /*#__PURE__*/React.createElement(NavigationButton, {
480
+ disabled: !inRange({
481
+ min,
482
+ max
483
+ }, nextMonth),
484
+ onClick: changeCurrentDate(1, CALENDAR_LEVEL_MONTH)
485
+ }, /*#__PURE__*/React.createElement(Icon, {
486
+ icon: "arrow-right-line"
487
+ }))), /*#__PURE__*/React.createElement(WeekHeader, null, [0, 1, 2, 3, 4, 5, 6].map(day => /*#__PURE__*/React.createElement("span", {
488
+ key: day
489
+ }, messages('day', (day + dayOffset) % 7).slice(0, 3)))), /*#__PURE__*/React.createElement(CalendarContentContainer, null, mapDays(currentDate, dayOffset, (date, day, index) => /*#__PURE__*/React.createElement(DayItem, {
490
+ index: index,
491
+ key: date.getTime(),
492
+ onClick: handleDateChange,
493
+ "data-day": day,
494
+ isEdge: date.getUTCMonth() !== currentMonth,
495
+ isCurrent: date.getTime() === currentDate.getTime(),
496
+ isSelected: date.getTime() === value,
497
+ isDisabled: !inRange({
498
+ max,
499
+ min
500
+ }, date),
501
+ isToday: date.getTime() === today
502
+ }, date.getUTCDate()))));
503
+ };
504
+ const Months = _ref3 => {
505
+ let {
506
+ currentDate,
507
+ messages,
508
+ value,
509
+ changeCurrentDate,
510
+ handleChange,
511
+ up,
512
+ min,
513
+ max
514
+ } = _ref3;
515
+ const currentMonth = currentDate.getUTCMonth();
516
+ const sameYear = currentDate.getUTCFullYear() === new Date(value).getUTCFullYear();
517
+ const selectedMonth = sameYear ? currentDate.getUTCMonth() : undefined;
518
+ const handleMonthChange = e => {
519
+ const date = new Date(currentDate);
520
+ handleChange(new Date(date.setUTCMonth(e.currentTarget.getAttribute('data-month'))));
521
+ };
522
+ return /*#__PURE__*/React.createElement(DatePickerContainer, null, /*#__PURE__*/React.createElement(CalendarHeader, null, /*#__PURE__*/React.createElement(NavigationButton, {
523
+ disabled: !inRange({
524
+ min,
525
+ max
526
+ }, getUTCDateBefore(currentDate.getUTCFullYear())),
527
+ onClick: changeCurrentDate(-1, CALENDAR_LEVEL_YEAR)
528
+ }, /*#__PURE__*/React.createElement(Icon, {
529
+ icon: "arrow-left-line"
530
+ })), /*#__PURE__*/React.createElement(TitleButton, {
531
+ onClick: up
532
+ }, currentDate.getUTCFullYear()), /*#__PURE__*/React.createElement(NavigationButton, {
533
+ disabled: !inRange({
534
+ min,
535
+ max
536
+ }, getUTCDate(currentDate.getUTCFullYear() + 1)),
537
+ onClick: changeCurrentDate(1, CALENDAR_LEVEL_YEAR)
538
+ }, /*#__PURE__*/React.createElement(Icon, {
539
+ icon: "arrow-right-line"
540
+ }))), /*#__PURE__*/React.createElement(CalendarContentContainer, null, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(month => /*#__PURE__*/React.createElement(MonthItem, {
541
+ disabled: !inRange({
542
+ min,
543
+ max
544
+ }, getUTCDate(currentDate.getUTCFullYear(), month)) && !inRange({
545
+ min,
546
+ max
547
+ }, getUTCDateBefore(currentDate.getUTCFullYear(), month + 1)),
548
+ onClick: handleMonthChange,
549
+ key: month,
550
+ "data-month": month,
551
+ index: month,
552
+ isCurrent: currentMonth === month,
553
+ isSelected: selectedMonth === month
554
+ }, messages('month', month).slice(0, 3)))));
555
+ };
556
+
557
+ // Given the specified context, produces an HTML string
558
+ // representing months of the year.
559
+
560
+ function mapYears(currentDate, fn) {
561
+ const result = [];
562
+ const year = currentDate.getUTCFullYear();
563
+ for (let i = 0; i < 4 * 3; i++) {
564
+ result.push(fn(year - year % 10 - 1 + i, i < 1 || i > 10, i));
565
+ }
566
+ return result;
567
+ }
568
+ const Years = _ref4 => {
569
+ let {
570
+ value,
571
+ currentDate,
572
+ changeCurrentDate,
573
+ handleChange,
574
+ min,
575
+ max
576
+ } = _ref4;
577
+ const currentYear = currentDate.getUTCFullYear();
578
+ const selectedYear = new Date(value).getUTCFullYear();
579
+ const handleYearChange = e => {
580
+ const date = new Date(currentDate);
581
+ handleChange(new Date(date.setUTCFullYear(e.currentTarget.getAttribute('data-year'))));
582
+ };
583
+ return /*#__PURE__*/React.createElement(DatePickerContainer, null, /*#__PURE__*/React.createElement(CalendarHeader, null, /*#__PURE__*/React.createElement(NavigationButton, {
584
+ disabled: !inRange({
585
+ min,
586
+ max
587
+ }, getUTCDateBefore(currentDate.getUTCFullYear())),
588
+ onClick: changeCurrentDate(-10, CALENDAR_LEVEL_YEAR)
589
+ }, /*#__PURE__*/React.createElement(Icon, {
590
+ icon: "arrow-left-line"
591
+ })), /*#__PURE__*/React.createElement(TitleButton, null, currentYear - currentYear % 10, "-", currentYear - currentYear % 10 + 9), /*#__PURE__*/React.createElement(NavigationButton, {
592
+ disabled: !inRange({
593
+ min,
594
+ max
595
+ }, getUTCDate(currentDate.getUTCFullYear() + 10)),
596
+ onClick: changeCurrentDate(10, CALENDAR_LEVEL_YEAR)
597
+ }, /*#__PURE__*/React.createElement(Icon, {
598
+ icon: "arrow-right-line"
599
+ }))), /*#__PURE__*/React.createElement(CalendarContentContainer, null, mapYears(currentDate, (year, outside, i) => /*#__PURE__*/React.createElement(YearItem, {
600
+ key: year,
601
+ onClick: handleYearChange,
602
+ disabled: !inRange({
603
+ min,
604
+ max
605
+ }, getUTCDate(year)) && !inRange({
606
+ min,
607
+ max
608
+ }, getUTCDateBefore(year + 1)),
609
+ "data-year": year,
610
+ index: i,
611
+ isCurrent: year === currentYear,
612
+ isSelected: year === selectedYear,
613
+ isEdge: outside
614
+ }, year))));
615
+ };
616
+ function getClosestNextLeapYear(year) {
617
+ let y = isNaN(year) ? now().getUTCFullYear() : year;
618
+ while (!(y % 100 === 0 ? y % 400 === 0 : y % 4 === 0)) {
619
+ y++;
620
+ }
621
+ return y;
622
+ }
623
+
624
+ export { CALENDAR_LEVEL_DAY, CALENDAR_LEVEL_MONTH, CALENDAR_LEVEL_YEAR, DatePicker };
625
+ //# sourceMappingURL=DatePicker.mjs.map