@dhis2-ui/calendar 9.12.0-alpha.1 → 9.12.0-alpha.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,9 +12,9 @@ var _input = require("@dhis2-ui/input");
12
12
  var _layer = require("@dhis2-ui/layer");
13
13
  var _popper = require("@dhis2-ui/popper");
14
14
  var _classnames = _interopRequireDefault(require("classnames"));
15
+ var _propTypes = _interopRequireDefault(require("prop-types"));
15
16
  var _react = _interopRequireWildcard(require("react"));
16
17
  var _calendarContainer = require("../calendar/calendar-container.js");
17
- var _calendar = require("../calendar/calendar.js");
18
18
  var _index = _interopRequireDefault(require("../locales/index.js"));
19
19
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
20
20
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
@@ -35,7 +35,6 @@ const CalendarInput = function () {
35
35
  locale,
36
36
  numberingSystem,
37
37
  weekDayFormat,
38
- timeZone,
39
38
  width,
40
39
  cellSize,
41
40
  clearable,
@@ -54,11 +53,9 @@ const CalendarInput = function () {
54
53
  const useDatePickerOptions = (0, _react.useMemo)(() => ({
55
54
  calendar,
56
55
  locale,
57
- timeZone,
58
- // todo: we probably shouldn't have had timezone here in the first place
59
56
  numberingSystem,
60
57
  weekDayFormat
61
- }), [calendar, locale, numberingSystem, timeZone, weekDayFormat]);
58
+ }), [calendar, locale, numberingSystem, weekDayFormat]);
62
59
  const pickerResults = (0, _multiCalendarDates.useDatePicker)({
63
60
  onDateSelect: result => {
64
61
  setOpen(false);
@@ -72,11 +69,20 @@ const CalendarInput = function () {
72
69
  options: useDatePickerOptions
73
70
  });
74
71
  const handleChange = e => {
72
+ setOpen(false);
75
73
  setPartialDate(e.value);
76
74
  };
77
75
  const handleBlur = (_, e) => {
76
+ const validated = (0, _multiCalendarDates.validateDateString)(partialDate, {
77
+ calendar,
78
+ format,
79
+ minDateString: minDate,
80
+ maxDateString: maxDate,
81
+ strictValidation
82
+ });
78
83
  parentOnDateSelect === null || parentOnDateSelect === void 0 ? void 0 : parentOnDateSelect({
79
- calendarDateString: partialDate
84
+ calendarDateString: partialDate,
85
+ ...validated
80
86
  });
81
87
  if (excludeRef.current && !excludeRef.current.contains(e.relatedTarget)) {
82
88
  setOpen(false);
@@ -147,8 +153,40 @@ const CalendarInput = function () {
147
153
  };
148
154
  exports.CalendarInput = CalendarInput;
149
155
  CalendarInput.defaultProps = {
150
- dataTest: 'dhis2-uiwidgets-calendar-inputfield'
156
+ dataTest: 'dhis2-uiwidgets-calendar-inputfield',
157
+ cellSize: '32px',
158
+ width: '240px',
159
+ weekDayFormat: 'narrow'
151
160
  };
152
161
  CalendarInput.propTypes = {
153
- ..._calendar.CalendarProps
162
+ /** the calendar to use such gregory, ethiopic, nepali - full supported list here: https://github.com/dhis2/multi-calendar-dates/blob/main/src/constants/calendars.ts */
163
+ calendar: _propTypes.default.any.isRequired,
164
+ /** Called with signature `(null)` \|\| `({ dateCalendarString: string, dateCalendar: Temporal.ZonedDateTime })` with `dateCalendarString` being the stringified date in the specified calendar in the format `yyyy-MM-dd` */
165
+ onDateSelect: _propTypes.default.func.isRequired,
166
+ /** the size of a single cell in the table forming the calendar */
167
+ cellSize: _propTypes.default.string,
168
+ /** Whether the clear button is displayed */
169
+ clearable: _propTypes.default.bool,
170
+ /** 'data-test' attribute of `InputField` component */
171
+ dataTest: _propTypes.default.string,
172
+ /** the currently selected date using an iso-like format YYYY-MM-DD, in the calendar system provided (not iso8601) */
173
+ date: _propTypes.default.string,
174
+ /** the direction of the library - internally the library will use rtl for rtl-languages but this can be overridden here for more control */
175
+ dir: _propTypes.default.oneOf(['ltr', 'rtl']),
176
+ /** The date format to use either `YYYY-MM-DD` or `DD-MM-YYYY` */
177
+ format: _propTypes.default.oneOf(['YYYY-MM-DD', 'DD-MM-YYYY']),
178
+ /** any valid locale - if none provided, the internal library will fallback to the user locale (more info here: https://github.com/dhis2/multi-calendar-dates/blob/main/src/hooks/internal/useResolvedLocaleOptions.ts#L15) */
179
+ locale: _propTypes.default.string,
180
+ /** The maximum selectable date */
181
+ maxDate: _propTypes.default.string,
182
+ /** The minimum selectable date */
183
+ minDate: _propTypes.default.string,
184
+ /** numbering system to use - full list here https://github.com/dhis2/multi-calendar-dates/blob/main/src/constants/numberingSystems.ts */
185
+ numberingSystem: _propTypes.default.string,
186
+ /** Whether to use strict validation by showing errors for out-of-range dates when enabled (default), and warnings when disabled */
187
+ strictValidation: _propTypes.default.bool,
188
+ /** the format to display for the week day, i.e. Monday (long), Mon (short), M (narrow) */
189
+ weekDayFormat: _propTypes.default.oneOf(['narrow', 'short', 'long']),
190
+ /** the width of the calendar component */
191
+ width: _propTypes.default.string
154
192
  };
@@ -5,12 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.CalendarWithClearButton = void 0;
7
7
  exports.CalendarWithEditiableInput = CalendarWithEditiableInput;
8
+ exports.CalendarWithEditiableInputReactForm = CalendarWithEditiableInputReactForm;
8
9
  exports.default = exports.NepaliWithNepali = exports.NepaliWithEnglish = exports.IslamicWithArabic = exports.GregorianWithEnglish = exports.GregorianWithArabic = exports.EthiopicWithEnglish = exports.EthiopicWithAmharic = void 0;
9
10
  var _react = _interopRequireWildcard(require("react"));
11
+ var _reactFinalForm = require("react-final-form");
10
12
  var _calendarInput = require("../calendar-input/calendar-input.js");
11
13
  var _calendarStoryWrapper = require("./calendar-story-wrapper.js");
12
14
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
13
15
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
16
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
14
17
  const subtitle = `[Experimental] Calendar Input is a wrapper around Calendar displaying an input that triggers the calendar`;
15
18
  const description = `
16
19
  Use a CalendarInput where there is a need to let the user input a date.
@@ -129,8 +132,55 @@ function CalendarWithEditiableInput() {
129
132
  },
130
133
  width: '700px',
131
134
  inputWidth: "900px",
132
- timeZone: 'UTC',
133
135
  minDate: '2020-07-01',
134
136
  maxDate: '2020-07-09'
135
137
  })));
138
+ }
139
+ function CalendarWithEditiableInputReactForm() {
140
+ const [calendarError, setCalendarError] = (0, _react.useState)(undefined);
141
+ const errored = () => {
142
+ return {
143
+ calendar: calendarError
144
+ };
145
+ };
146
+ return /*#__PURE__*/_react.default.createElement(_reactFinalForm.Form, {
147
+ onSubmit: (values, meta) => {
148
+ console.log('SUBMITTING', {
149
+ values,
150
+ meta
151
+ });
152
+ },
153
+ initialValues: {
154
+ calendar: '2022-10-12'
155
+ },
156
+ validate: errored
157
+ }, _ref3 => {
158
+ let {
159
+ handleSubmit
160
+ } = _ref3;
161
+ return /*#__PURE__*/_react.default.createElement("form", {
162
+ onSubmit: handleSubmit
163
+ }, /*#__PURE__*/_react.default.createElement(_reactFinalForm.Field, {
164
+ name: "calendar"
165
+ }, props => /*#__PURE__*/_react.default.createElement(_calendarInput.CalendarInput, _extends({}, props, {
166
+ input: props.input,
167
+ meta: props.meta,
168
+ editable: true,
169
+ date: props.input.value,
170
+ calendar: "gregory",
171
+ onDateSelect: date => {
172
+ if (!date.isValid) {
173
+ setCalendarError(date.errorMessage);
174
+ } else {
175
+ setCalendarError(undefined);
176
+ }
177
+ props.input.onChange(date ? date === null || date === void 0 ? void 0 : date.calendarDateString : '');
178
+ },
179
+ timeZone: 'UTC'
180
+ }))), /*#__PURE__*/_react.default.createElement("button", {
181
+ type: "submit",
182
+ disabled: false,
183
+ onClick: handleSubmit
184
+ }, "Submit"));
185
+ });
136
186
  }
@@ -1,15 +1,15 @@
1
1
  import _JSXStyle from "styled-jsx/style";
2
2
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
3
- import { useDatePicker, useResolvedDirection } from '@dhis2/multi-calendar-dates';
3
+ import { useDatePicker, useResolvedDirection, validateDateString } from '@dhis2/multi-calendar-dates';
4
4
  import { Button } from '@dhis2-ui/button';
5
5
  import { Card } from '@dhis2-ui/card';
6
6
  import { InputField } from '@dhis2-ui/input';
7
7
  import { Layer } from '@dhis2-ui/layer';
8
8
  import { Popper } from '@dhis2-ui/popper';
9
9
  import cx from 'classnames';
10
+ import PropTypes from 'prop-types';
10
11
  import React, { useRef, useState, useMemo, useEffect } from 'react';
11
12
  import { CalendarContainer } from '../calendar/calendar-container.js';
12
- import { CalendarProps } from '../calendar/calendar.js';
13
13
  import i18n from '../locales/index.js';
14
14
  const offsetModifier = {
15
15
  name: 'offset',
@@ -26,7 +26,6 @@ export const CalendarInput = function () {
26
26
  locale,
27
27
  numberingSystem,
28
28
  weekDayFormat,
29
- timeZone,
30
29
  width,
31
30
  cellSize,
32
31
  clearable,
@@ -45,11 +44,9 @@ export const CalendarInput = function () {
45
44
  const useDatePickerOptions = useMemo(() => ({
46
45
  calendar,
47
46
  locale,
48
- timeZone,
49
- // todo: we probably shouldn't have had timezone here in the first place
50
47
  numberingSystem,
51
48
  weekDayFormat
52
- }), [calendar, locale, numberingSystem, timeZone, weekDayFormat]);
49
+ }), [calendar, locale, numberingSystem, weekDayFormat]);
53
50
  const pickerResults = useDatePicker({
54
51
  onDateSelect: result => {
55
52
  setOpen(false);
@@ -63,11 +60,20 @@ export const CalendarInput = function () {
63
60
  options: useDatePickerOptions
64
61
  });
65
62
  const handleChange = e => {
63
+ setOpen(false);
66
64
  setPartialDate(e.value);
67
65
  };
68
66
  const handleBlur = (_, e) => {
67
+ const validated = validateDateString(partialDate, {
68
+ calendar,
69
+ format,
70
+ minDateString: minDate,
71
+ maxDateString: maxDate,
72
+ strictValidation
73
+ });
69
74
  parentOnDateSelect === null || parentOnDateSelect === void 0 ? void 0 : parentOnDateSelect({
70
- calendarDateString: partialDate
75
+ calendarDateString: partialDate,
76
+ ...validated
71
77
  });
72
78
  if (excludeRef.current && !excludeRef.current.contains(e.relatedTarget)) {
73
79
  setOpen(false);
@@ -137,8 +143,40 @@ export const CalendarInput = function () {
137
143
  }, [".calendar-input-wrapper.jsx-633677374{position:relative;}", ".calendar-clear-button.jsx-633677374{position:absolute;inset-inline-end:6px;-webkit-inset-block-start:27px;-ms-intb-rlock-start:27px;inset-block-start:27px;}", ".calendar-clear-button.with-icon.jsx-633677374{inset-inline-end:36px;}", ".calendar-clear-button.with-dense-wrapper.jsx-633677374{-webkit-inset-block-start:23px;-ms-intb-rlock-start:23px;inset-block-start:23px;}"]));
138
144
  };
139
145
  CalendarInput.defaultProps = {
140
- dataTest: 'dhis2-uiwidgets-calendar-inputfield'
146
+ dataTest: 'dhis2-uiwidgets-calendar-inputfield',
147
+ cellSize: '32px',
148
+ width: '240px',
149
+ weekDayFormat: 'narrow'
141
150
  };
142
151
  CalendarInput.propTypes = {
143
- ...CalendarProps
152
+ /** the calendar to use such gregory, ethiopic, nepali - full supported list here: https://github.com/dhis2/multi-calendar-dates/blob/main/src/constants/calendars.ts */
153
+ calendar: PropTypes.any.isRequired,
154
+ /** Called with signature `(null)` \|\| `({ dateCalendarString: string, dateCalendar: Temporal.ZonedDateTime })` with `dateCalendarString` being the stringified date in the specified calendar in the format `yyyy-MM-dd` */
155
+ onDateSelect: PropTypes.func.isRequired,
156
+ /** the size of a single cell in the table forming the calendar */
157
+ cellSize: PropTypes.string,
158
+ /** Whether the clear button is displayed */
159
+ clearable: PropTypes.bool,
160
+ /** 'data-test' attribute of `InputField` component */
161
+ dataTest: PropTypes.string,
162
+ /** the currently selected date using an iso-like format YYYY-MM-DD, in the calendar system provided (not iso8601) */
163
+ date: PropTypes.string,
164
+ /** the direction of the library - internally the library will use rtl for rtl-languages but this can be overridden here for more control */
165
+ dir: PropTypes.oneOf(['ltr', 'rtl']),
166
+ /** The date format to use either `YYYY-MM-DD` or `DD-MM-YYYY` */
167
+ format: PropTypes.oneOf(['YYYY-MM-DD', 'DD-MM-YYYY']),
168
+ /** any valid locale - if none provided, the internal library will fallback to the user locale (more info here: https://github.com/dhis2/multi-calendar-dates/blob/main/src/hooks/internal/useResolvedLocaleOptions.ts#L15) */
169
+ locale: PropTypes.string,
170
+ /** The maximum selectable date */
171
+ maxDate: PropTypes.string,
172
+ /** The minimum selectable date */
173
+ minDate: PropTypes.string,
174
+ /** numbering system to use - full list here https://github.com/dhis2/multi-calendar-dates/blob/main/src/constants/numberingSystems.ts */
175
+ numberingSystem: PropTypes.string,
176
+ /** Whether to use strict validation by showing errors for out-of-range dates when enabled (default), and warnings when disabled */
177
+ strictValidation: PropTypes.bool,
178
+ /** the format to display for the week day, i.e. Monday (long), Mon (short), M (narrow) */
179
+ weekDayFormat: PropTypes.oneOf(['narrow', 'short', 'long']),
180
+ /** the width of the calendar component */
181
+ width: PropTypes.string
144
182
  };
@@ -1,4 +1,6 @@
1
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
1
2
  import React, { useState } from 'react';
3
+ import { Field, Form } from 'react-final-form';
2
4
  import { CalendarInput } from '../calendar-input/calendar-input.js';
3
5
  import { CalendarStoryWrapper } from './calendar-story-wrapper.js';
4
6
  const subtitle = `[Experimental] Calendar Input is a wrapper around Calendar displaying an input that triggers the calendar`;
@@ -117,8 +119,55 @@ export function CalendarWithEditiableInput() {
117
119
  },
118
120
  width: '700px',
119
121
  inputWidth: "900px",
120
- timeZone: 'UTC',
121
122
  minDate: '2020-07-01',
122
123
  maxDate: '2020-07-09'
123
124
  })));
125
+ }
126
+ export function CalendarWithEditiableInputReactForm() {
127
+ const [calendarError, setCalendarError] = useState(undefined);
128
+ const errored = () => {
129
+ return {
130
+ calendar: calendarError
131
+ };
132
+ };
133
+ return /*#__PURE__*/React.createElement(Form, {
134
+ onSubmit: (values, meta) => {
135
+ console.log('SUBMITTING', {
136
+ values,
137
+ meta
138
+ });
139
+ },
140
+ initialValues: {
141
+ calendar: '2022-10-12'
142
+ },
143
+ validate: errored
144
+ }, _ref3 => {
145
+ let {
146
+ handleSubmit
147
+ } = _ref3;
148
+ return /*#__PURE__*/React.createElement("form", {
149
+ onSubmit: handleSubmit
150
+ }, /*#__PURE__*/React.createElement(Field, {
151
+ name: "calendar"
152
+ }, props => /*#__PURE__*/React.createElement(CalendarInput, _extends({}, props, {
153
+ input: props.input,
154
+ meta: props.meta,
155
+ editable: true,
156
+ date: props.input.value,
157
+ calendar: "gregory",
158
+ onDateSelect: date => {
159
+ if (!date.isValid) {
160
+ setCalendarError(date.errorMessage);
161
+ } else {
162
+ setCalendarError(undefined);
163
+ }
164
+ props.input.onChange(date ? date === null || date === void 0 ? void 0 : date.calendarDateString : '');
165
+ },
166
+ timeZone: 'UTC'
167
+ }))), /*#__PURE__*/React.createElement("button", {
168
+ type: "submit",
169
+ disabled: false,
170
+ onClick: handleSubmit
171
+ }, "Submit"));
172
+ });
124
173
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2-ui/calendar",
3
- "version": "9.12.0-alpha.1",
3
+ "version": "9.12.0-alpha.3",
4
4
  "description": "UI Calendar",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,15 +33,15 @@
33
33
  "styled-jsx": "^4"
34
34
  },
35
35
  "dependencies": {
36
- "@dhis2-ui/button": "9.12.0-alpha.1",
37
- "@dhis2-ui/card": "9.12.0-alpha.1",
38
- "@dhis2-ui/input": "9.12.0-alpha.1",
39
- "@dhis2-ui/layer": "9.12.0-alpha.1",
40
- "@dhis2-ui/popper": "9.12.0-alpha.1",
41
- "@dhis2/multi-calendar-dates": "1.0.0-alpha.29",
36
+ "@dhis2-ui/button": "9.12.0-alpha.3",
37
+ "@dhis2-ui/card": "9.12.0-alpha.3",
38
+ "@dhis2-ui/input": "9.12.0-alpha.3",
39
+ "@dhis2-ui/layer": "9.12.0-alpha.3",
40
+ "@dhis2-ui/popper": "9.12.0-alpha.3",
41
+ "@dhis2/multi-calendar-dates": "1.3.0",
42
42
  "@dhis2/prop-types": "^3.1.2",
43
- "@dhis2/ui-constants": "9.12.0-alpha.1",
44
- "@dhis2/ui-icons": "9.12.0-alpha.1",
43
+ "@dhis2/ui-constants": "9.12.0-alpha.3",
44
+ "@dhis2/ui-icons": "9.12.0-alpha.3",
45
45
  "classnames": "^2.3.1",
46
46
  "prop-types": "^15.7.2"
47
47
  },