diginet-core-ui 1.4.63-beta.1 → 1.4.64-beta.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.
@@ -41,8 +41,8 @@ export class DateField {
41
41
  }
42
42
  }
43
43
 
44
- /**
45
- * Pad a number with zeros to the left.
44
+ /**
45
+ * Pad a number with zeros to the left.
46
46
  */
47
47
  const padNumber = (number, length) => {
48
48
  let numberString = String(number);
@@ -158,7 +158,7 @@ export const useDateField = (format, localize, date) => {
158
158
  minute,
159
159
  second
160
160
  } = dateField;
161
- const date = new Date(year || 0, typeof month === 'number' ? month - 1 : 0,
161
+ const date = new Date(year || 0, typeof month === 'number' ? Math.max(0, month - 1) : 0,
162
162
  // The default day is 1 when the value is null, otherwise it becomes the last day of the month.
163
163
  day || 1, hour || 0, minute || 0, second || 0);
164
164
  if (typeof type === 'undefined' || typeof value === 'undefined') {
@@ -1,5 +1,6 @@
1
1
  /** @jsxRuntime classic */
2
2
  /** @jsx jsx */
3
+ import { useEffect } from 'react';
3
4
  import { css, jsx } from '@emotion/core';
4
5
  import { HelperText, Label } from "../..";
5
6
  import { getGlobal } from "../../../global";
@@ -8,12 +9,19 @@ import PropTypes from 'prop-types';
8
9
  import { forwardRef, memo, useImperativeHandle, useMemo, useRef, useState } from 'react';
9
10
  import { cursorNotAllowed, displayBlock, positionRelative } from "../../../styles/general";
10
11
  import useThemeProps from "../../../theme/utils/useThemeProps";
11
- import { classNames, refType as ref, useControlled } from "../../../utils";
12
+ import { classNames, refType as ref, useControlled, parseDateByFormat } from "../../../utils";
12
13
  import UncontrolledInputBase from "../input-base/UncontrolledInputBase";
13
14
  import useDateInputState from "./useDateInputState";
14
15
  import useIsFocused from "./useIsFocused";
15
16
  import useKeyboardInputEvent from "./useKeyboardInputEvent";
16
17
  import { getInputSelectedState, isFieldFullValue, useEventCallback, useInputSelection, validateDateTime } from "./utils";
18
+ const usePrevious = value => {
19
+ const ref = useRef();
20
+ useEffect(() => {
21
+ ref.current = value;
22
+ }, [value]);
23
+ return ref.current;
24
+ };
17
25
  const DateInput = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, reference) => {
18
26
  if (!reference) reference = useRef(null);
19
27
 
@@ -57,6 +65,7 @@ const DateInput = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, reference
57
65
  selectionStart: 0,
58
66
  selectionEnd: 0
59
67
  });
68
+ const selectedStateOld = usePrevious(selectedState) || {};
60
69
  const isError = !!error;
61
70
  const isEnabled = !readOnly && !disabled;
62
71
  useImperativeHandle(reference, () => {
@@ -124,15 +133,25 @@ const DateInput = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, reference
124
133
  setSelectionRange(state.selectionStart, state.selectionEnd);
125
134
  });
126
135
  const onSegmentValueChangeWithNumericKeys = useEventCallback(event => {
136
+ var _inputRef$current;
127
137
  const input = event.target;
128
138
  const key = event.key;
129
139
  const pattern = selectedState.selectedPattern;
130
140
  if (!pattern) {
131
141
  return;
132
142
  }
143
+ const displayDate = ((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.value) || '';
144
+ const dataDisplayVal = parseDateByFormat(displayDate, formatStr) || [];
145
+ const patternName = Object.keys(dataDisplayVal).find(name => name.includes(pattern));
146
+ const valDisplay = (dataDisplayVal === null || dataDisplayVal === void 0 ? void 0 : dataDisplayVal[patternName]) || '';
133
147
  const field = getDateField(pattern);
134
148
  const value = parseInt(key, 10);
135
- const padValue = parseInt(`${field.value || ''}${key}`, 10);
149
+ // * If Date or Month ['DD', 'MM'];
150
+ // * If current display value has two number and first num is 0
151
+ // * If current value !== 0
152
+ // * And Already finish text month or date
153
+ // ===> Not chain the previous num
154
+ const padValue = ['DD', 'MM'].includes(patternName) && valDisplay.length === 2 && valDisplay[0] === '0' && key !== 0 && (selectedStateOld.selectedPattern || '').toUpperCase() !== pattern ? Number(key) : parseInt(`${field.value || ''}${key}`, 10);
136
155
  let newValue = value;
137
156
 
138
157
  // Check if the value entered by the user is a valid date
@@ -1,9 +1,9 @@
1
1
  import { isValid, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setYear } from 'date-fns';
2
2
  import { useCallback, useEffect, useRef } from 'react';
3
3
 
4
- /**
5
- * https://github.com/facebook/react/issues/14099#issuecomment-440013892
6
- * @param {function} fn
4
+ /**
5
+ * https://github.com/facebook/react/issues/14099#issuecomment-440013892
6
+ * @param {function} fn
7
7
  */
8
8
  export const useEventCallback = fn => {
9
9
  const ref = useRef(fn);
@@ -217,7 +217,7 @@ export const modifyDate = (date, type, value) => {
217
217
  case 'year':
218
218
  return setYear(date, value);
219
219
  case 'month':
220
- return setMonth(date, value - 1);
220
+ return setMonth(date, Math.max(0, value - 1));
221
221
  case 'day':
222
222
  return setDate(date, value);
223
223
  case 'hour':
@@ -106,19 +106,24 @@ const DatePicker = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, referenc
106
106
  value: valueProp,
107
107
  viewType,
108
108
  onBlur,
109
+ delayOnChange: delayOnChangeProp,
109
110
  ...other
110
111
  } = props;
111
112
  const dateLocale = locale.get();
112
113
  const displayFormat = (_getDateFormats = getDateFormats(dateLocale, minZoom)) !== null && _getDateFormats !== void 0 ? _getDateFormats : displayFormatProp;
113
114
  const returnFormat = (_pickerReturnFormat$g = pickerReturnFormat.get(minZoom)) !== null && _pickerReturnFormat$g !== void 0 ? _pickerReturnFormat$g : returnFormatProp;
114
115
  const placeholder = placeholderProp !== null && placeholderProp !== void 0 ? placeholderProp : displayFormat;
116
+ const renderDateInputCondition = useDateInput && minZoom !== 'quarter' && !min && !max || true;
117
+ const delayOnChange = delayOnChangeProp || renderDateInputCondition ? typeof delayOnChangeProp === 'number' ? delayOnChangeProp : getGlobal('delayOnInput') : 0;
115
118
  const [value, setValue] = useControlled(parseValueToDate(valueProp), parseValueToDate(defaultValue));
116
119
  const [openPickerState, setOpenPickerState] = useState(false);
120
+ const defaultVal = useRef(valueProp);
117
121
  const ipRef = useRef(null);
118
122
  const ref = useRef(null);
119
123
  const focusedValue = useRef(null);
120
124
  const pickerRef = useRef(null);
121
125
  const buttonCarlendarRef = useRef(null);
126
+ const timer = useRef(null);
122
127
  const isError = !!error && (!value || !isValid(value));
123
128
  const _DatePickerRoot = DatePickerRoot();
124
129
  const _IconAreaCSS = IconAreaCSS();
@@ -140,9 +145,12 @@ const DatePicker = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, referenc
140
145
  const vl = (e === null || e === void 0 ? void 0 : e.value) || e;
141
146
  closePicker();
142
147
  setValue(vl);
143
- onChange === null || onChange === void 0 ? void 0 : onChange({
144
- value: isValid(vl) ? formatValue(vl, returnFormat) : String(vl) === 'Invalid Date' ? 'Invalid Date' : vl
145
- });
148
+ if (timer.current) clearTimeout(timer.current);
149
+ timer.current = setTimeout(() => {
150
+ onChange === null || onChange === void 0 ? void 0 : onChange({
151
+ value: isValid(vl) ? formatValue(vl, returnFormat) : String(vl) === 'Invalid Date' ? defaultVal.current : vl
152
+ });
153
+ }, delayOnChange);
146
154
  };
147
155
  const onCalendarClick = e => {
148
156
  !controls ? onChangeValue(e) : focusedValue.current = e === null || e === void 0 ? void 0 : e.value;
@@ -224,7 +232,6 @@ const DatePicker = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((inProps, referenc
224
232
  return null;
225
233
  } else return parse(value, lowerCaseDayYear(returnFormat), new Date());
226
234
  }, [value]);
227
- const renderDateInputCondition = useDateInput && minZoom !== 'quarter' && !min && !max || true;
228
235
  return jsx(Fragment, null, jsx("div", {
229
236
  ref: ref,
230
237
  css: _DatePickerRoot,
@@ -339,6 +346,8 @@ DatePicker.propTypes = {
339
346
  controls: PropTypes.bool,
340
347
  /** The default value of the component. */
341
348
  defaultValue: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.string, PropTypes.object]),
349
+ /** The number of milliseconds to wait before call onChange. */
350
+ delayOnChange: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
342
351
  /** If `true`, the form control will be disabled. */
343
352
  disabled: PropTypes.bool,
344
353
  /** Format to display value. */
package/icons/basic.js CHANGED
@@ -4679,6 +4679,34 @@ export const OneSquare = /*#__PURE__*/memo(({
4679
4679
  fill: fillColor(color)
4680
4680
  }));
4681
4681
  });
4682
+ export const OpenNew = /*#__PURE__*/memo(({
4683
+ width,
4684
+ height,
4685
+ color = 'system/rest',
4686
+ viewBox = false
4687
+ }) => {
4688
+ return viewBox ? /*#__PURE__*/React.createElement("svg", {
4689
+ width: width || 24,
4690
+ height: height || 24,
4691
+ viewBox: "0 0 24 24",
4692
+ fill: "none"
4693
+ }, /*#__PURE__*/React.createElement("path", {
4694
+ fillRule: "evenodd",
4695
+ clipRule: "evenodd",
4696
+ d: "M12 3V5H5V19H19V12H21V19C21 20.1 20.1 21 19 21H5C3.89 21 3 20.1 3 19V5C3 3.9 3.89 3 5 3H12ZM21 3V10H19V6.41016L9.16992 16.2402L7.75977 14.8301L17.5898 5H14V3H21Z",
4697
+ fill: fillColor(color)
4698
+ })) : /*#__PURE__*/React.createElement("svg", {
4699
+ width: width || 18,
4700
+ height: height || 18,
4701
+ viewBox: "0 0 18 18",
4702
+ fill: "none"
4703
+ }, /*#__PURE__*/React.createElement("path", {
4704
+ fillRule: "evenodd",
4705
+ clipRule: "evenodd",
4706
+ d: "M9 0V2H2V16H16V9H18V16C18 17.1 17.1 18 16 18H2C0.89 18 0 17.1 0 16V2C0 0.9 0.89 0 2 0H9ZM18 0V7H16V3.41016L6.16992 13.2402L4.75977 11.8301L14.5898 2H11V0H18Z",
4707
+ fill: fillColor(color)
4708
+ }));
4709
+ });
4682
4710
  export const Undo = /*#__PURE__*/memo(({
4683
4711
  width,
4684
4712
  height,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diginet-core-ui",
3
- "version": "1.4.63-beta.1",
3
+ "version": "1.4.64-beta.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "license": "UNLICENSED",
package/readme.md CHANGED
@@ -41,6 +41,9 @@ npm test
41
41
  ```
42
42
 
43
43
  ## Changelog
44
+ ## 1.4.64
45
+ - \[Added\]: Icon – Add Icon BloodMinus
46
+ - \[Changed\]: DatePicker - Allow manual input for the DatePicker (default value shows "Invalid Date" when typing) + add a "delayOnChange" prop.
44
47
 
45
48
  ## 1.4.63
46
49
  - \[Added\]: Icon – Add IconMenu MHRP39N0029
package/utils/date.js CHANGED
@@ -4,11 +4,11 @@ const language = locale.get();
4
4
  const localeName = language === 'en' ? 'en-US' : 'vi-VN';
5
5
  const units = ['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond'];
6
6
 
7
- /**
8
- *
9
- * @param {Date|String|Array} value is a date time
10
- * @param {String} format the input date format
11
- * @param {Boolean} setNow return current time if true
7
+ /**
8
+ *
9
+ * @param {Date|String|Array} value is a date time
10
+ * @param {String} format the input date format
11
+ * @param {Boolean} setNow return current time if true
12
12
  */
13
13
  const getDate = (value, format = 'MM/DD/YYYY', setNow = true) => {
14
14
  if (value) {
@@ -52,11 +52,11 @@ const getMethodOfUnit = unit => {
52
52
  }
53
53
  };
54
54
 
55
- /**
56
- * check 1 value is a date
57
- * @param {Date|String|Array|Number} value the value to check
58
- * @param {String} formatInput the format date to check valid date
59
- * @returns {Boolean} valid date
55
+ /**
56
+ * check 1 value is a date
57
+ * @param {Date|String|Array|Number} value the value to check
58
+ * @param {String} formatInput the format date to check valid date
59
+ * @returns {Boolean} valid date
60
60
  */
61
61
  export const isValidDate = (value, formatInput) => {
62
62
  if (typeof value === 'string' || Array.isArray(value)) {
@@ -69,11 +69,11 @@ export const isValidDate = (value, formatInput) => {
69
69
  return false;
70
70
  };
71
71
 
72
- /**
73
- * get the end of month from month and year or from a date
74
- * @param {Number|String|Date|Array} month the month of year (can also be validated as year) or a value with date type
75
- * @param {Number} year the year (can also be validated as month if arg 1 is year)
76
- * @returns {Number} end day
72
+ /**
73
+ * get the end of month from month and year or from a date
74
+ * @param {Number|String|Date|Array} month the month of year (can also be validated as year) or a value with date type
75
+ * @param {Number} year the year (can also be validated as month if arg 1 is year)
76
+ * @returns {Number} end day
77
77
  */
78
78
  export const getEndDayOfMonth = (month, year) => {
79
79
  if (!year) {
@@ -92,11 +92,11 @@ export const getEndDayOfMonth = (month, year) => {
92
92
  return 'Invalid Date';
93
93
  };
94
94
 
95
- /**
96
- * return a date time follow the concrete format
97
- * @param {Date|String|Array} value is a date time
98
- * @param {String} formatOutput format of date
99
- * @param {Boolean} utc is utc time
95
+ /**
96
+ * return a date time follow the concrete format
97
+ * @param {Date|String|Array} value is a date time
98
+ * @param {String} formatOutput format of date
99
+ * @param {Boolean} utc is utc time
100
100
  */
101
101
  export const formatDate = (value, formatOutput = 'DD/MM/YYYY', utc = false) => {
102
102
  if (!value) return formatOutput;
@@ -232,21 +232,21 @@ export const formatDate = (value, formatOutput = 'DD/MM/YYYY', utc = false) => {
232
232
  }
233
233
  };
234
234
 
235
- /**
236
- * return a date like moment
237
- * @param {Date|String|Array} value is date time
238
- * @param {String} formatInput format of input date value
239
- * @returns {Object} date
235
+ /**
236
+ * return a date like moment
237
+ * @param {Date|String|Array} value is date time
238
+ * @param {String} formatInput format of input date value
239
+ * @returns {Object} date
240
240
  */
241
241
  const date = (value, formatInput = 'MM/DD/YYYY') => {
242
242
  const originDate = getDate(value, formatInput);
243
243
 
244
- /**
245
- * compare two date time and return a period
246
- * @param {Date|String|Array} date is a date time
247
- * @param {String} unit one of milliseconds|seconds|minutes|hours|days|months|years
248
- * @param {Boolean|Number} floating allow odd numbers
249
- * @returns {Number} distance
244
+ /**
245
+ * compare two date time and return a period
246
+ * @param {Date|String|Array} date is a date time
247
+ * @param {String} unit one of milliseconds|seconds|minutes|hours|days|months|years
248
+ * @param {Boolean|Number} floating allow odd numbers
249
+ * @returns {Number} distance
250
250
  */
251
251
  const diff = (compareDate, unit = 'days', floating = false) => {
252
252
  compareDate = getDate(compareDate);
@@ -295,12 +295,12 @@ const date = (value, formatInput = 'MM/DD/YYYY') => {
295
295
  return result.toFixed(floating ? typeof floating === 'number' ? floating : 2 : 0);
296
296
  };
297
297
 
298
- /**
299
- * add or subtract date time
300
- * @param {Number} num the quantity to cal for unit
301
- * @param {String} unit the unit of date/time
302
- * @param {String} operator operator to cal (add/subtract)
303
- * @returns {Object} date
298
+ /**
299
+ * add or subtract date time
300
+ * @param {Number} num the quantity to cal for unit
301
+ * @param {String} unit the unit of date/time
302
+ * @param {String} operator operator to cal (add/subtract)
303
+ * @returns {Object} date
304
304
  */
305
305
  const calculation = (num, unit, operator) => {
306
306
  if (isNaN(num)) {
@@ -311,11 +311,11 @@ const date = (value, formatInput = 'MM/DD/YYYY') => {
311
311
  return date(originDate, formatInput);
312
312
  };
313
313
 
314
- /**
315
- * get the moment of time follow unit time
316
- * @param {String} unit the unit of time
317
- * @param {String} moment start|end to get start|end of the moment
318
- * @returns {Object} date
314
+ /**
315
+ * get the moment of time follow unit time
316
+ * @param {String} unit the unit of time
317
+ * @param {String} moment start|end to get start|end of the moment
318
+ * @returns {Object} date
319
319
  */
320
320
  const timeOf = (unit, moment) => {
321
321
  let temp = 1;
@@ -337,12 +337,12 @@ const date = (value, formatInput = 'MM/DD/YYYY') => {
337
337
  return date(originDate, formatInput);
338
338
  };
339
339
 
340
- /**
341
- *
342
- * @param {Date|String|Number|Array} compareDate date to calculate moment, undefined will compare with now
343
- * @param {String|Array} unit year|month|day|hour|minute|second|millisecond, allow undefined
344
- * @param {Boolean|Number} floating allow odd numbers
345
- * @returns {String} moments
340
+ /**
341
+ *
342
+ * @param {Date|String|Number|Array} compareDate date to calculate moment, undefined will compare with now
343
+ * @param {String|Array} unit year|month|day|hour|minute|second|millisecond, allow undefined
344
+ * @param {Boolean|Number} floating allow odd numbers
345
+ * @returns {String} moments
346
346
  */
347
347
  const relative = (compareDate, unit, floating) => {
348
348
  compareDate = getDate(compareDate);
@@ -378,17 +378,17 @@ const date = (value, formatInput = 'MM/DD/YYYY') => {
378
378
  };
379
379
  return {
380
380
  value: originDate,
381
- /**
382
- * return a date time follow the concrete format
383
- * @param {String} formatOutput format of date
384
- * @param {Boolean} utc is utc time
381
+ /**
382
+ * return a date time follow the concrete format
383
+ * @param {String} formatOutput format of date
384
+ * @param {Boolean} utc is utc time
385
385
  */
386
386
  format: (formatOutput, utc) => formatDate(originDate, formatOutput, utc),
387
387
  diff,
388
- /**
389
- * check 1 value is a date
390
- * @param {String} formatInput the format date to check valid date
391
- * @returns {Boolean} valid date
388
+ /**
389
+ * check 1 value is a date
390
+ * @param {String} formatInput the format date to check valid date
391
+ * @returns {Boolean} valid date
392
392
  */
393
393
  isValid: () => isValidDate(originDate, formatInput),
394
394
  add: (num, unit) => calculation(num, unit, 'add'),
@@ -398,4 +398,14 @@ const date = (value, formatInput = 'MM/DD/YYYY') => {
398
398
  relative
399
399
  };
400
400
  };
401
+ export const parseDateByFormat = (dateStr, format) => {
402
+ const separators = /[-/]/;
403
+ const dateParts = dateStr.split(separators);
404
+ const formatParts = format.split(separators);
405
+ const result = {};
406
+ formatParts.forEach((part, index) => {
407
+ result[part] = dateParts[index] === part ? '' : dateParts[index];
408
+ });
409
+ return result;
410
+ };
401
411
  export default date;