@os-design/core 1.0.163 → 1.0.165

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.
@@ -23,11 +23,6 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
23
23
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
24
24
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
25
25
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
26
- var pad = function pad(n) {
27
- return n.toString().padStart(2, '0');
28
- };
29
- var HOURS_12 = 12 * 60 * 60 * 1000;
30
-
31
26
  /**
32
27
  * The component to choose a time.
33
28
  */
@@ -39,7 +34,7 @@ var TimePicker = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
39
34
  _ref$onChange = _ref.onChange,
40
35
  onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
41
36
  _ref$onSelect = _ref.onSelect,
42
- _onSelect = _ref$onSelect === void 0 ? function () {} : _ref$onSelect,
37
+ onSelect = _ref$onSelect === void 0 ? function () {} : _ref$onSelect,
43
38
  _ref$onKeyDown = _ref.onKeyDown,
44
39
  onKeyDown = _ref$onKeyDown === void 0 ? function () {} : _ref$onKeyDown,
45
40
  _ref$disabled = _ref.disabled,
@@ -65,66 +60,49 @@ var TimePicker = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
65
60
  _useState2 = _slicedToArray(_useState, 2),
66
61
  selection = _useState2[0],
67
62
  setSelection = _useState2[1];
68
- var isPm = (0, _react.useMemo)(function () {
69
- return !!forwardedValue && forwardedValue.getHours() >= 12;
70
- }, [forwardedValue]);
71
- var setTime = (0, _react.useCallback)(function (time) {
72
- var _parseTime = (0, _timePickerUtils.parseTime)(time, notation, isPm),
73
- _parseTime2 = _slicedToArray(_parseTime, 2),
74
- h = _parseTime2[0],
75
- m = _parseTime2[1];
76
- var nextValue = new Date(forwardedValue ? forwardedValue.getTime() : 0);
77
- nextValue.setHours(h);
78
- nextValue.setMinutes(m);
79
- setForwardedValue(nextValue);
80
- }, [forwardedValue, isPm, notation, setForwardedValue]);
81
- var setTimeRef = (0, _react.useRef)(setTime);
82
- (0, _react.useEffect)(function () {
83
- setTimeRef.current = setTime;
84
- }, [setTime]);
85
- var time = (0, _react.useMemo)(function () {
86
- if (!forwardedValue) return notation === '12-hour' ? '12:00' : '00:00';
87
- var hour = notation === '12-hour' ? forwardedValue.getHours() % 12 || 12 : forwardedValue.getHours();
88
- var minute = forwardedValue.getMinutes();
89
- return "".concat(pad(hour), ":").concat(pad(minute));
90
- }, [forwardedValue, notation]);
91
- var changePeriod = (0, _react.useCallback)(function (nextIsPm) {
92
- if (!forwardedValue) return;
93
- var nextValue = new Date(forwardedValue.getTime() + HOURS_12 * (nextIsPm ? 1 : -1));
94
- setForwardedValue(nextValue);
95
- }, [forwardedValue, setForwardedValue]);
63
+ var _useTime = (0, _timePickerUtils.useTime)({
64
+ notation: notation,
65
+ selection: selection,
66
+ setSelection: setSelection,
67
+ forwardedValue: forwardedValue,
68
+ setForwardedValue: setForwardedValue
69
+ }),
70
+ time = _useTime.time,
71
+ isPm = _useTime.isPm,
72
+ changePeriod = _useTime.changePeriod,
73
+ keyHandler = _useTime.keyHandler;
74
+ var rightComponent = (0, _react.useMemo)(function () {
75
+ if (notation !== '12-hour' && !right) return null;
76
+ return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, notation === '12-hour' && /*#__PURE__*/_react["default"].createElement(_Button["default"], {
77
+ type: "ghost",
78
+ wide: "never",
79
+ size: "small",
80
+ disabled: disabled,
81
+ onClick: changePeriod
82
+ }, isPm ? 'PM' : 'AM'), right);
83
+ }, [changePeriod, disabled, isPm, notation, right]);
84
+ var keyDownHandler = (0, _react.useCallback)(function (e) {
85
+ keyHandler(e.key, e.metaKey);
86
+ onKeyDown(e);
87
+ e.preventDefault();
88
+ }, [keyHandler, onKeyDown]);
96
89
 
97
90
  // Update the selection
98
91
  (0, _react.useEffect)(function () {
99
92
  if (!inputRef.current) return;
100
93
  inputRef.current.setSelectionRange(selection.start, selection.end);
101
94
  }, [inputRef, selection]);
102
- var keyDownHandler = (0, _react.useCallback)(function (e) {
103
- var nextCaret = selection.start;
104
- if (e.key === 'ArrowUp' || e.key === 'ArrowLeft' && e.metaKey) {
105
- nextCaret = 0;
106
- } else if (e.key === 'ArrowDown' || e.key === 'ArrowRight' && e.metaKey) {
107
- nextCaret = 5;
108
- } else if (e.key === 'ArrowLeft') {
109
- nextCaret = selection.start > 0 ? selection.start - 1 : 0;
110
- } else if (e.key === 'ArrowRight') {
111
- nextCaret = selection.start < 5 ? selection.start + 1 : 5;
112
- } else if (e.key === 'Backspace') {
113
- setTimeRef.current('00:00');
114
- nextCaret = 0;
115
- } else if ((0, _timePickerUtils.isDigitKey)(e.key)) {
116
- var pos = selection.start === 2 ? 3 : selection.start;
117
- if (pos === 5) pos = 0;
118
- setTimeRef.current((0, _timePickerUtils.replaceChar)(time, pos, e.key));
119
- nextCaret = pos < 5 ? pos + 1 : 5;
120
- }
95
+ var selectHandler = (0, _react.useCallback)(function (e) {
96
+ // Update the selection state.
97
+ var _e$currentTarget = e.currentTarget,
98
+ selectionStart = _e$currentTarget.selectionStart,
99
+ selectionEnd = _e$currentTarget.selectionEnd;
121
100
  setSelection({
122
- start: nextCaret,
123
- end: nextCaret
101
+ start: selectionStart || 0,
102
+ end: selectionEnd || 0
124
103
  });
125
- onKeyDown(e);
126
- e.preventDefault();
127
- }, [onKeyDown, selection.start, time]);
104
+ onSelect(e);
105
+ }, [onSelect]);
128
106
  return /*#__PURE__*/_react["default"].createElement(_Input["default"], _extends({
129
107
  type: "text",
130
108
  inputMode: "decimal",
@@ -133,26 +111,8 @@ var TimePicker = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
133
111
  maxLength: 5,
134
112
  disabled: disabled,
135
113
  value: time,
136
- right: (notation === '12-hour' || right) && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, notation === '12-hour' && /*#__PURE__*/_react["default"].createElement(_Button["default"], {
137
- type: "ghost",
138
- wide: "never",
139
- size: "small",
140
- disabled: disabled,
141
- onClick: function onClick() {
142
- return changePeriod(!isPm);
143
- }
144
- }, isPm ? 'PM' : 'AM'), right),
145
- onSelect: function onSelect(e) {
146
- // Update the selection state.
147
- var _e$currentTarget = e.currentTarget,
148
- selectionStart = _e$currentTarget.selectionStart,
149
- selectionEnd = _e$currentTarget.selectionEnd;
150
- setSelection({
151
- start: selectionStart || 0,
152
- end: selectionEnd || 0
153
- });
154
- _onSelect(e);
155
- },
114
+ right: rightComponent,
115
+ onSelect: selectHandler,
156
116
  onKeyDown: keyDownHandler
157
117
  }, rest, {
158
118
  ref: mergedInputRef
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["pad","n","toString","padStart","HOURS_12","TimePicker","forwardRef","ref","notation","value","defaultValue","onChange","onSelect","onKeyDown","disabled","right","rest","useForwardedRef","inputRef","mergedInputRef","useForwardedState","forwardedValue","setForwardedValue","useState","start","end","selection","setSelection","isPm","useMemo","getHours","setTime","useCallback","time","parseTime","h","m","nextValue","Date","getTime","setHours","setMinutes","setTimeRef","useRef","useEffect","current","hour","minute","getMinutes","changePeriod","nextIsPm","setSelectionRange","keyDownHandler","e","nextCaret","key","metaKey","isDigitKey","pos","replaceChar","preventDefault","currentTarget","selectionStart","selectionEnd","displayName"],"sources":["../../../src/TimePicker/index.tsx"],"sourcesContent":["import {\n isDigitKey,\n replaceChar,\n parseTime,\n} from '@os-design/time-picker-utils';\nimport { useForwardedRef, useForwardedState } from '@os-design/utils';\nimport React, {\n forwardRef,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport Button from '../Button';\nimport Input, { InputProps } from '../Input';\n\nexport interface TimePickerProps\n extends Omit<InputProps, 'type' | 'value' | 'defaultValue' | 'onChange'> {\n /**\n * The time notation.\n * @default 12-hour\n */\n notation?: '12-hour' | '24-hour';\n /**\n * The selected date.\n * @default undefined\n */\n value?: Date;\n /**\n * The default value.\n * @default undefined\n */\n defaultValue?: Date;\n /**\n * The change event handler.\n * @default undefined\n */\n onChange?: (value: Date) => void;\n}\n\ninterface Selection {\n start: number;\n end: number;\n}\n\nconst pad = (n: number) => n.toString().padStart(2, '0');\nconst HOURS_12 = 12 * 60 * 60 * 1000;\n\n/**\n * The component to choose a time.\n */\nconst TimePicker = forwardRef<HTMLInputElement, TimePickerProps>(\n (\n {\n notation = '12-hour',\n value,\n defaultValue,\n onChange = () => {},\n onSelect = () => {},\n onKeyDown = () => {},\n disabled = false,\n right,\n ...rest\n },\n ref\n ) => {\n const [inputRef, mergedInputRef] = useForwardedRef(ref);\n const [forwardedValue, setForwardedValue] = useForwardedState({\n value,\n defaultValue,\n onChange,\n });\n const [selection, setSelection] = useState<Selection>({ start: 0, end: 0 });\n\n const isPm = useMemo(\n () => !!forwardedValue && forwardedValue.getHours() >= 12,\n [forwardedValue]\n );\n\n const setTime = useCallback(\n (time: string) => {\n const [h, m] = parseTime(time, notation, isPm);\n const nextValue = new Date(\n forwardedValue ? forwardedValue.getTime() : 0\n );\n nextValue.setHours(h);\n nextValue.setMinutes(m);\n setForwardedValue(nextValue);\n },\n [forwardedValue, isPm, notation, setForwardedValue]\n );\n\n const setTimeRef = useRef(setTime);\n useEffect(() => {\n setTimeRef.current = setTime;\n }, [setTime]);\n\n const time = useMemo(() => {\n if (!forwardedValue) return notation === '12-hour' ? '12:00' : '00:00';\n const hour =\n notation === '12-hour'\n ? forwardedValue.getHours() % 12 || 12\n : forwardedValue.getHours();\n const minute = forwardedValue.getMinutes();\n return `${pad(hour)}:${pad(minute)}`;\n }, [forwardedValue, notation]);\n\n const changePeriod = useCallback(\n (nextIsPm: boolean) => {\n if (!forwardedValue) return;\n const nextValue = new Date(\n forwardedValue.getTime() + HOURS_12 * (nextIsPm ? 1 : -1)\n );\n setForwardedValue(nextValue);\n },\n [forwardedValue, setForwardedValue]\n );\n\n // Update the selection\n useEffect(() => {\n if (!inputRef.current) return;\n inputRef.current.setSelectionRange(selection.start, selection.end);\n }, [inputRef, selection]);\n\n const keyDownHandler = useCallback(\n (e) => {\n let nextCaret = selection.start;\n if (e.key === 'ArrowUp' || (e.key === 'ArrowLeft' && e.metaKey)) {\n nextCaret = 0;\n } else if (\n e.key === 'ArrowDown' ||\n (e.key === 'ArrowRight' && e.metaKey)\n ) {\n nextCaret = 5;\n } else if (e.key === 'ArrowLeft') {\n nextCaret = selection.start > 0 ? selection.start - 1 : 0;\n } else if (e.key === 'ArrowRight') {\n nextCaret = selection.start < 5 ? selection.start + 1 : 5;\n } else if (e.key === 'Backspace') {\n setTimeRef.current('00:00');\n nextCaret = 0;\n } else if (isDigitKey(e.key)) {\n let pos = selection.start === 2 ? 3 : selection.start;\n if (pos === 5) pos = 0;\n setTimeRef.current(replaceChar(time, pos, e.key));\n nextCaret = pos < 5 ? pos + 1 : 5;\n }\n setSelection({ start: nextCaret, end: nextCaret });\n onKeyDown(e);\n e.preventDefault();\n },\n [onKeyDown, selection.start, time]\n );\n\n return (\n <Input\n type='text'\n inputMode='decimal'\n role='spinbutton'\n minLength={5}\n maxLength={5}\n disabled={disabled}\n value={time}\n right={\n (notation === '12-hour' || right) && (\n <>\n {notation === '12-hour' && (\n <Button\n type='ghost'\n wide='never'\n size='small'\n disabled={disabled}\n onClick={() => changePeriod(!isPm)}\n >\n {isPm ? 'PM' : 'AM'}\n </Button>\n )}\n {right}\n </>\n )\n }\n onSelect={(e) => {\n // Update the selection state.\n const { selectionStart, selectionEnd } = e.currentTarget;\n setSelection({ start: selectionStart || 0, end: selectionEnd || 0 });\n onSelect(e);\n }}\n onKeyDown={keyDownHandler}\n {...rest}\n ref={mergedInputRef}\n />\n );\n }\n);\n\nTimePicker.displayName = 'TimePicker';\n\nexport default TimePicker;\n"],"mappings":";;;;;;;AAAA;AAKA;AACA;AAQA;AACA;AAA6C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+B7C,IAAMA,GAAG,GAAG,SAANA,GAAG,CAAIC,CAAS;EAAA,OAAKA,CAAC,CAACC,QAAQ,EAAE,CAACC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC;AAAA;AACxD,IAAMC,QAAQ,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI;;AAEpC;AACA;AACA;AACA,IAAMC,UAAU,gBAAG,IAAAC,iBAAU,EAC3B,gBAYEC,GAAG,EACA;EAAA,yBAXDC,QAAQ;IAARA,QAAQ,8BAAG,SAAS;IACpBC,KAAK,QAALA,KAAK;IACLC,YAAY,QAAZA,YAAY;IAAA,qBACZC,QAAQ;IAARA,QAAQ,8BAAG,YAAM,CAAC,CAAC;IAAA,qBACnBC,QAAQ;IAARA,SAAQ,8BAAG,YAAM,CAAC,CAAC;IAAA,sBACnBC,SAAS;IAATA,SAAS,+BAAG,YAAM,CAAC,CAAC;IAAA,qBACpBC,QAAQ;IAARA,QAAQ,8BAAG,KAAK;IAChBC,KAAK,QAALA,KAAK;IACFC,IAAI;EAIT,uBAAmC,IAAAC,sBAAe,EAACV,GAAG,CAAC;IAAA;IAAhDW,QAAQ;IAAEC,cAAc;EAC/B,yBAA4C,IAAAC,wBAAiB,EAAC;MAC5DX,KAAK,EAALA,KAAK;MACLC,YAAY,EAAZA,YAAY;MACZC,QAAQ,EAARA;IACF,CAAC,CAAC;IAAA;IAJKU,c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IACnB,KAAK,EAAEmB,IAAK;IACZ,KAAK,EACH,CAACzB,QAAQ,KAAK,SAAS,IAAIO,KAAK,kBAC9B,kEACGP,QAAQ,KAAK,SAAS,iBACrB,gCAAC,kBAAM;MACL,IAAI,EAAC,OAAO;MACZ,IAAI,EAAC,OAAO;MACZ,IAAI,EAAC,OAAO;MACZ,QAAQ,EAAEM,QAAS;MACnB,OAAO,EAAE;QAAA,OAAMmC,YAAY,CAAC,CAACrB,IAAI,CAAC;MAAA;IAAC,GAElCA,IAAI,GAAG,IAAI,GAAG,IAAI,CAEtB,EACAb,KAAK,CAGX;IACD,QAAQ,EAAE,kBAACsC,CAAC,EAAK;MACf;MACA,uBAAyCA,CAAC,CAACQ,aAAa;QAAhDC,cAAc,oBAAdA,cAAc;QAAEC,YAAY,oBAAZA,YAAY;MACpCpC,YAAY,CAAC;QAAEH,KAAK,EAAEsC,cAAc,IAAI,CAAC;QAAErC,GAAG,EAAEsC,YAAY,IAAI;MAAE,CAAC,CAAC;MACpEnD,SAAQ,CAACyC,CAAC,CAAC;IACb,CAAE;IACF,SAAS,EAAED;EAAe,GACtBpC,IAAI;IACR,GAAG,EAAEG;EAAe,GACpB;AAEN,CAAC,CACF;AAEDd,UAAU,CAAC2D,WAAW,GAAG,YAAY;AAAC,eAEvB3D,UAAU;AAAA"}
1
+ {"version":3,"file":"index.js","names":["TimePicker","forwardRef","ref","notation","value","defaultValue","onChange","onSelect","onKeyDown","disabled","right","rest","useForwardedRef","inputRef","mergedInputRef","useForwardedState","forwardedValue","setForwardedValue","useState","start","end","selection","setSelection","useTime","time","isPm","changePeriod","keyHandler","rightComponent","useMemo","keyDownHandler","useCallback","e","key","metaKey","preventDefault","useEffect","current","setSelectionRange","selectHandler","currentTarget","selectionStart","selectionEnd","displayName"],"sources":["../../../src/TimePicker/index.tsx"],"sourcesContent":["import { useTime } from '@os-design/time-picker-utils';\nimport { useForwardedRef, useForwardedState } from '@os-design/utils';\nimport React, {\n forwardRef,\n useCallback,\n useEffect,\n useMemo,\n useState,\n} from 'react';\nimport Button from '../Button';\nimport Input, { InputProps } from '../Input';\n\nexport interface TimePickerProps\n extends Omit<InputProps, 'type' | 'value' | 'defaultValue' | 'onChange'> {\n /**\n * The time notation.\n * @default 12-hour\n */\n notation?: '12-hour' | '24-hour';\n /**\n * The selected date.\n * @default undefined\n */\n value?: Date;\n /**\n * The default value.\n * @default undefined\n */\n defaultValue?: Date;\n /**\n * The change event handler.\n * @default undefined\n */\n onChange?: (value: Date) => void;\n}\n\ninterface Selection {\n start: number;\n end: number;\n}\n\n/**\n * The component to choose a time.\n */\nconst TimePicker = forwardRef<HTMLInputElement, TimePickerProps>(\n (\n {\n notation = '12-hour',\n value,\n defaultValue,\n onChange = () => {},\n onSelect = () => {},\n onKeyDown = () => {},\n disabled = false,\n right,\n ...rest\n },\n ref\n ) => {\n const [inputRef, mergedInputRef] = useForwardedRef(ref);\n const [forwardedValue, setForwardedValue] = useForwardedState({\n value,\n defaultValue,\n onChange,\n });\n const [selection, setSelection] = useState<Selection>({ start: 0, end: 0 });\n\n const { time, isPm, changePeriod, keyHandler } = useTime({\n notation,\n selection,\n setSelection,\n forwardedValue,\n setForwardedValue,\n });\n\n const rightComponent = useMemo(() => {\n if (notation !== '12-hour' && !right) return null;\n return (\n <>\n {notation === '12-hour' && (\n <Button\n type='ghost'\n wide='never'\n size='small'\n disabled={disabled}\n onClick={changePeriod}\n >\n {isPm ? 'PM' : 'AM'}\n </Button>\n )}\n {right}\n </>\n );\n }, [changePeriod, disabled, isPm, notation, right]);\n\n const keyDownHandler = useCallback(\n (e) => {\n keyHandler(e.key, e.metaKey);\n onKeyDown(e);\n e.preventDefault();\n },\n [keyHandler, onKeyDown]\n );\n\n // Update the selection\n useEffect(() => {\n if (!inputRef.current) return;\n inputRef.current.setSelectionRange(selection.start, selection.end);\n }, [inputRef, selection]);\n\n const selectHandler = useCallback(\n (e) => {\n // Update the selection state.\n const { selectionStart, selectionEnd } = e.currentTarget;\n setSelection({ start: selectionStart || 0, end: selectionEnd || 0 });\n onSelect(e);\n },\n [onSelect]\n );\n\n return (\n <Input\n type='text'\n inputMode='decimal'\n role='spinbutton'\n minLength={5}\n maxLength={5}\n disabled={disabled}\n value={time}\n right={rightComponent}\n onSelect={selectHandler}\n onKeyDown={keyDownHandler}\n {...rest}\n ref={mergedInputRef}\n />\n );\n }\n);\n\nTimePicker.displayName = 'TimePicker';\n\nexport default TimePicker;\n"],"mappings":";;;;;;;AAAA;AACA;AACA;AAOA;AACA;AAA6C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+B7C;AACA;AACA;AACA,IAAMA,UAAU,gBAAG,IAAAC,iBAAU,EAC3B,gBAYEC,GAAG,EACA;EAAA,yBAXDC,QAAQ;IAARA,QAAQ,8BAAG,SAAS;IACpBC,KAAK,QAALA,KAAK;IACLC,YAAY,QAAZA,YAAY;IAAA,qBACZC,QAAQ;IAARA,QAAQ,8BAAG,YAAM,CAAC,CAAC;IAAA,qBACnBC,QAAQ;IAARA,QAAQ,8BAAG,YAAM,CAAC,CAAC;IAAA,sBACnBC,SAAS;IAATA,SAAS,+BAAG,YAAM,CAAC,CAAC;IAAA,qBACpBC,QAAQ;IAARA,QAAQ,8BAAG,KAAK;IAChBC,KAAK,QAALA,KAAK;IACFC,IAAI;EAIT,uBAAmC,IAAAC,sBAAe,EAACV,GAAG,CAAC;IAAA;IAAhDW,QAAQ;IAAEC,cAAc;EAC/B,yBAA4C,IAAAC,wBAAiB,EAAC;MAC5DX,KAAK,EAALA,KAAK;MACLC,YAAY,EAAZA,YAAY;MACZC,QAAQ,EAARA;IACF,CAAC,CAAC;IAAA;IAJKU,cAAc;IAAEC,iBAAiB;EAKxC,gBAAkC,IAAAC,eAAQ,EAAY;MAAEC,KAAK,EAAE,CAAC;MAAEC,GAAG,EAAE;IAAE,CAAC,CAAC;IAAA;IAApEC,SAAS;IAAEC,YAAY;EAE9B,eAAiD,IA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AEzB,IAAMkB,aAAa,GAAG,IAAAR,kBAAW,EAC/B,UAACC,CAAC,EAAK;IACL;IACA,uBAAyCA,CAAC,CAACQ,aAAa;MAAhDC,cAAc,oBAAdA,cAAc;MAAEC,YAAY,oBAAZA,YAAY;IACpCpB,YAAY,CAAC;MAAEH,KAAK,EAAEsB,cAAc,IAAI,CAAC;MAAErB,GAAG,EAAEsB,YAAY,IAAI;IAAE,CAAC,CAAC;IACpEnC,QAAQ,CAACyB,CAAC,CAAC;EACb,CAAC,EACD,CAACzB,QAAQ,CAAC,CACX;EAED,oBACE,gCAAC,iBAAK;IACJ,IAAI,EAAC,MAAM;IACX,SAAS,EAAC,SAAS;IACnB,IAAI,EAAC,YAAY;IACjB,SAAS,EAAE,CAAE;IACb,SAAS,EAAE,CAAE;IACb,QAAQ,EAAEE,QAAS;IACnB,KAAK,EAAEe,IAAK;IACZ,KAAK,EAAEI,cAAe;IACtB,QAAQ,EAAEW,aAAc;IACxB,SAAS,EAAET;EAAe,GACtBnB,IAAI;IACR,GAAG,EAAEG;EAAe,GACpB;AAEN,CAAC,CACF;AAEDd,UAAU,CAAC2C,WAAW,GAAG,YAAY;AAAC,eAEvB3C,UAAU;AAAA"}
@@ -72,46 +72,20 @@ const SelectedDay = styled(Day)`
72
72
  const DayInRange = styled(Day)`
73
73
  background-color: ${p => clr([...p.theme.colorText.slice(0, 3), (p.theme.colorText[3] || 1) * 0.05])};
74
74
  `;
75
- const createDate = (date, time) => {
76
- const d = new Date(0);
77
- d.setFullYear(date.getFullYear());
78
- d.setMonth(date.getMonth());
79
- d.setDate(date.getDate());
80
- d.setHours(0);
81
- d.setMinutes(0);
82
- if (time) {
83
- d.setHours(time.getHours());
84
- d.setMinutes(time.getMinutes());
85
- }
86
- return d;
87
- };
88
75
  const DatePickerCalendar = ({
89
76
  firstDayOfWeek,
90
77
  locale,
91
- range,
92
78
  value,
93
- onChange = () => {}
79
+ onSelect = () => {}
94
80
  }) => {
95
81
  const valueRef = useRef(value);
96
82
  useEffect(() => {
97
83
  valueRef.current = value;
98
84
  }, [value]);
99
- const rangeStartedRef = useRef(false);
100
- const changeHandler = useCallback(date => {
101
- if (range) {
102
- const dateWithoutTime = createDate(date);
103
- const dateIndex = rangeStartedRef.current ? 1 : 0;
104
- const nextValue = valueRef.current && Array.isArray(valueRef.current) && rangeStartedRef.current ? valueRef.current.map((item, index) => index === dateIndex ? createDate(date, item) : new Date(item.getTime())).sort((a, b) => a.getTime() - b.getTime()) : [dateWithoutTime, dateWithoutTime];
105
- rangeStartedRef.current = !rangeStartedRef.current;
106
- onChange(nextValue);
107
- } else if (!Array.isArray(valueRef.current)) {
108
- onChange(createDate(date, valueRef.current));
109
- }
110
- }, [onChange, range]);
111
- const changeHandlerRef = useRef(changeHandler);
85
+ const onSelectRef = useRef(onSelect);
112
86
  useEffect(() => {
113
- changeHandlerRef.current = changeHandler;
114
- }, [changeHandler]);
87
+ onSelectRef.current = onSelect;
88
+ }, [onSelect]);
115
89
  const {
116
90
  selectedMonth,
117
91
  updateMonth,
@@ -125,11 +99,11 @@ const DatePickerCalendar = ({
125
99
  role: 'button',
126
100
  'aria-label': getAccessibilityDateLabel(date, locale),
127
101
  onClick: () => {
128
- changeHandlerRef.current(date);
102
+ onSelectRef.current(date);
129
103
  },
130
104
  onKeyDown: e => {
131
105
  if (['Enter', ' '].includes(e.key)) {
132
- changeHandlerRef.current(date);
106
+ onSelectRef.current(date);
133
107
  e.preventDefault();
134
108
  }
135
109
  },
@@ -1 +1 @@
1
- {"version":3,"file":"DatePickerCalendar.js","names":["styled","getAccessibilityDateLabel","useDatePickerCalendar","Left","Right","ellipsisStyles","resetFocusStyles","transitionStyles","clr","React","useCallback","useEffect","useRef","Button","MonthContainer","div","Month","Calendar","p","theme","datePickerCellSize","DayOfWeek","datePickerDayOfWeekColorText","sizes","small","Day","datePickerDaySize","borderRadius","datePickerDayColorBgHover","datePickerDayColorTextHover","DayAnotherMonth","datePickerDayAnotherMonthColorText","Today","datePickerTodayColorBg","datePickerTodayColorText","datePickerTodayColorBorder","SelectedDay","datePickerSelectedDayColorBg","datePickerSelectedDayColorText","DayInRange","colorText","slice","createDate","date","time","d","Date","setFullYear","getFullYear","setMonth","getMonth","setDate","getDate","setHours","setMinutes","getHours","getMinutes","DatePickerCalendar","firstDayOfWeek","locale","range","value","onChange","valueRef","current","rangeStartedRef","changeHandler","dateWithoutTime","dateIndex","nextValue","Array","isArray","map","item","index","getTime","sort","a","b","changeHandlerRef","selectedMonth","updateMonth","days","getDayProps","tabIndex","role","onClick","onKeyDown","e","includes","key","preventDefault","onMouseDown","prevMonthLabel","months","month","year","nextMonthLabel","shortDaysOfWeek","type","toISOString"],"sources":["../../../src/DatePicker/DatePickerCalendar.tsx"],"sourcesContent":["import styled from '@emotion/styled';\nimport {\n DatePickerLocale,\n getAccessibilityDateLabel,\n useDatePickerCalendar,\n} from '@os-design/date-picker-utils';\nimport { Left, Right } from '@os-design/icons';\nimport {\n ellipsisStyles,\n resetFocusStyles,\n transitionStyles,\n} from '@os-design/styles';\nimport { clr, Color } from '@os-design/theming';\nimport React, { HTMLAttributes, useCallback, useEffect, useRef } from 'react';\nimport Button from '../Button';\n\ninterface DatePickerCalendarProps {\n firstDayOfWeek: 'sunday' | 'monday';\n locale: DatePickerLocale;\n range: boolean;\n value?: Date | [Date, Date] | null;\n onChange?: (value: Date | [Date, Date]) => void;\n}\n\nconst MonthContainer = styled.div`\n display: flex;\n align-items: center;\n margin-bottom: 0.5em;\n`;\n\nconst Month = styled.div`\n flex-grow: 1;\n font-weight: 500;\n text-align: center;\n padding: 0 0.4em;\n ${ellipsisStyles};\n`;\n\nconst Calendar = styled.div`\n display: grid;\n grid-template-columns: repeat(7, ${(p) => p.theme.datePickerCellSize}em);\n grid-auto-rows: ${(p) => p.theme.datePickerCellSize}em;\n justify-content: space-between;\n overflow-x: auto;\n`;\n\nconst DayOfWeek = styled.div`\n justify-self: center;\n align-self: center;\n color: ${(p) => clr(p.theme.datePickerDayOfWeekColorText)};\n font-size: ${(p) => p.theme.sizes.small}em;\n`;\n\nconst Day = styled.div`\n ${resetFocusStyles};\n justify-self: center;\n align-self: center;\n\n width: ${(p) => p.theme.datePickerDaySize}em;\n height: ${(p) => p.theme.datePickerDaySize}em;\n\n display: flex;\n justify-content: center;\n align-items: center;\n\n border-radius: ${(p) => p.theme.borderRadius}em;\n cursor: pointer;\n\n @media (hover: hover) {\n &:hover,\n &:focus {\n background-color: ${(p) => clr(p.theme.datePickerDayColorBgHover)};\n color: ${(p) => clr(p.theme.datePickerDayColorTextHover)};\n }\n }\n\n ${transitionStyles('background-color', 'color')};\n`;\n\nconst DayAnotherMonth = styled(Day)`\n color: ${(p) => clr(p.theme.datePickerDayAnotherMonthColorText)};\n`;\n\nconst Today = styled(Day)`\n background-color: ${(p) => clr(p.theme.datePickerTodayColorBg)};\n color: ${(p) => clr(p.theme.datePickerTodayColorText)};\n border: 1px solid ${(p) => clr(p.theme.datePickerTodayColorBorder)};\n box-sizing: border-box;\n`;\n\nconst SelectedDay = styled(Day)`\n background-color: ${(p) => clr(p.theme.datePickerSelectedDayColorBg)};\n color: ${(p) => clr(p.theme.datePickerSelectedDayColorText)};\n`;\n\nconst DayInRange = styled(Day)`\n background-color: ${(p) =>\n clr([\n ...p.theme.colorText.slice(0, 3),\n (p.theme.colorText[3] || 1) * 0.05,\n ] as Color)};\n`;\n\nconst createDate = (date: Date, time?: Date | null) => {\n const d = new Date(0);\n d.setFullYear(date.getFullYear());\n d.setMonth(date.getMonth());\n d.setDate(date.getDate());\n d.setHours(0);\n d.setMinutes(0);\n if (time) {\n d.setHours(time.getHours());\n d.setMinutes(time.getMinutes());\n }\n return d;\n};\n\nconst DatePickerCalendar: React.FC<DatePickerCalendarProps> = ({\n firstDayOfWeek,\n locale,\n range,\n value,\n onChange = () => {},\n}) => {\n const valueRef = useRef(value);\n useEffect(() => {\n valueRef.current = value;\n }, [value]);\n\n const rangeStartedRef = useRef(false);\n\n const changeHandler = useCallback(\n (date: Date) => {\n if (range) {\n const dateWithoutTime = createDate(date);\n const dateIndex = rangeStartedRef.current ? 1 : 0;\n const nextValue =\n valueRef.current &&\n Array.isArray(valueRef.current) &&\n rangeStartedRef.current\n ? valueRef.current\n .map((item, index) =>\n index === dateIndex\n ? createDate(date, item)\n : new Date(item.getTime())\n )\n .sort((a, b) => a.getTime() - b.getTime())\n : [dateWithoutTime, dateWithoutTime];\n rangeStartedRef.current = !rangeStartedRef.current;\n onChange(nextValue as [Date, Date]);\n } else if (!Array.isArray(valueRef.current)) {\n onChange(createDate(date, valueRef.current));\n }\n },\n [onChange, range]\n );\n\n const changeHandlerRef = useRef(changeHandler);\n useEffect(() => {\n changeHandlerRef.current = changeHandler;\n }, [changeHandler]);\n\n const { selectedMonth, updateMonth, days } = useDatePickerCalendar({\n value,\n firstDayOfWeek,\n });\n\n const getDayProps = useCallback<\n (date: Date) => HTMLAttributes<HTMLDivElement>\n >(\n (date: Date) => ({\n tabIndex: 0,\n role: 'button',\n 'aria-label': getAccessibilityDateLabel(date, locale),\n onClick: () => {\n changeHandlerRef.current(date);\n },\n onKeyDown: (e) => {\n if (['Enter', ' '].includes(e.key)) {\n changeHandlerRef.current(date);\n e.preventDefault();\n }\n },\n onMouseDown: (e) => e.preventDefault(),\n }),\n [locale]\n );\n\n return (\n <>\n <MonthContainer>\n <Button\n type='ghost'\n wide='never'\n size='small'\n onClick={() => updateMonth(-1)}\n aria-label={locale.prevMonthLabel}\n >\n <Left />\n </Button>\n <Month>\n {locale.months[selectedMonth.month]} {selectedMonth.year}\n </Month>\n <Button\n type='ghost'\n wide='never'\n size='small'\n onClick={() => updateMonth(1)}\n aria-label={locale.nextMonthLabel}\n >\n <Right />\n </Button>\n </MonthContainer>\n\n <Calendar>\n {(firstDayOfWeek === 'sunday'\n ? locale.shortDaysOfWeek\n : [...locale.shortDaysOfWeek.slice(1), locale.shortDaysOfWeek[0]]\n ).map((item) => (\n <DayOfWeek key={item}>{item}</DayOfWeek>\n ))}\n\n {days.map(({ type, date }) => {\n if (type === 'dayAnotherMonth') {\n return (\n <DayAnotherMonth key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </DayAnotherMonth>\n );\n }\n if (type === 'selectedDay') {\n return (\n <SelectedDay key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </SelectedDay>\n );\n }\n if (type === 'dayInRange') {\n return (\n <DayInRange key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </DayInRange>\n );\n }\n if (type === 'today') {\n return (\n <Today key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </Today>\n );\n }\n return (\n <Day key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </Day>\n );\n })}\n </Calendar>\n </>\n );\n};\n\nexport default DatePickerCalendar;\n"],"mappings":";AAAA,OAAOA,MAAM,MAAM,iBAAiB;AACpC,SAEEC,yBAAyB,EACzBC,qBAAqB,QAChB,8BAA8B;AACrC,SAASC,IAAI,EAAEC,KAAK,QAAQ,kBAAkB;AAC9C,SACEC,cAAc,EACdC,gBAAgB,EAChBC,gBAAgB,QACX,mBAAmB;AAC1B,SAASC,GAAG,QAAe,oBAAoB;AAC/C,OAAOC,KAAK,IAAoBC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,OAAO;AAC7E,OAAOC,MAAM,MAAM,WAAW;AAU9B,MAAMC,cAAc,GAAGd,MAAM,CAACe,GAAI;AAClC;AACA;AACA;AACA,CAAC;AAED,MAAMC,KAAK,GAAGhB,MAAM,CAACe,GAAI;AACzB;AACA;AACA;AACA;AACA,IAAIV,cAAe;AACnB,CAAC;AAED,MAAMY,QAAQ,GAAGjB,MAAM,CAACe,GAAI;AAC5B;AACA,qCAAsCG,CAAC,IAAKA,CAAC,CAACC,KAAK,CAACC,kBAAmB;AACvE,oBAAqBF,CAAC,IAAKA,CAAC,CAACC,KAAK,CAACC,kBAAmB;AACtD;AACA;AACA,CAAC;AAED,MAAMC,SAAS,GAAGrB,MAAM,CAACe,GA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;IACA,oBACE,oBAAC,GAAG;MAAC,GAAG,EAAET,IAAI,CAACyD,WAAW;IAAG,GAAKjB,WAAW,CAACxC,IAAI,CAAC,GAChDA,IAAI,CAACS,OAAO,EAAE,CACX;EAEV,CAAC,CAAC,CACO,CACV;AAEP,CAAC;AAED,eAAeK,kBAAkB"}
1
+ {"version":3,"file":"DatePickerCalendar.js","names":["styled","getAccessibilityDateLabel","useDatePickerCalendar","Left","Right","ellipsisStyles","resetFocusStyles","transitionStyles","clr","React","useCallback","useEffect","useRef","Button","MonthContainer","div","Month","Calendar","p","theme","datePickerCellSize","DayOfWeek","datePickerDayOfWeekColorText","sizes","small","Day","datePickerDaySize","borderRadius","datePickerDayColorBgHover","datePickerDayColorTextHover","DayAnotherMonth","datePickerDayAnotherMonthColorText","Today","datePickerTodayColorBg","datePickerTodayColorText","datePickerTodayColorBorder","SelectedDay","datePickerSelectedDayColorBg","datePickerSelectedDayColorText","DayInRange","colorText","slice","DatePickerCalendar","firstDayOfWeek","locale","value","onSelect","valueRef","current","onSelectRef","selectedMonth","updateMonth","days","getDayProps","date","tabIndex","role","onClick","onKeyDown","e","includes","key","preventDefault","onMouseDown","prevMonthLabel","months","month","year","nextMonthLabel","shortDaysOfWeek","map","item","type","toISOString","getDate"],"sources":["../../../src/DatePicker/DatePickerCalendar.tsx"],"sourcesContent":["import styled from '@emotion/styled';\nimport {\n DatePickerLocale,\n getAccessibilityDateLabel,\n useDatePickerCalendar,\n} from '@os-design/date-picker-utils';\nimport { Left, Right } from '@os-design/icons';\nimport {\n ellipsisStyles,\n resetFocusStyles,\n transitionStyles,\n} from '@os-design/styles';\nimport { clr, Color } from '@os-design/theming';\nimport React, { HTMLAttributes, useCallback, useEffect, useRef } from 'react';\nimport Button from '../Button';\n\ninterface DatePickerCalendarProps {\n firstDayOfWeek: 'sunday' | 'monday';\n locale: DatePickerLocale;\n value?: Date | [Date, Date] | null;\n onSelect?: (value: Date) => void;\n}\n\nconst MonthContainer = styled.div`\n display: flex;\n align-items: center;\n margin-bottom: 0.5em;\n`;\n\nconst Month = styled.div`\n flex-grow: 1;\n font-weight: 500;\n text-align: center;\n padding: 0 0.4em;\n ${ellipsisStyles};\n`;\n\nconst Calendar = styled.div`\n display: grid;\n grid-template-columns: repeat(7, ${(p) => p.theme.datePickerCellSize}em);\n grid-auto-rows: ${(p) => p.theme.datePickerCellSize}em;\n justify-content: space-between;\n overflow-x: auto;\n`;\n\nconst DayOfWeek = styled.div`\n justify-self: center;\n align-self: center;\n color: ${(p) => clr(p.theme.datePickerDayOfWeekColorText)};\n font-size: ${(p) => p.theme.sizes.small}em;\n`;\n\nconst Day = styled.div`\n ${resetFocusStyles};\n justify-self: center;\n align-self: center;\n\n width: ${(p) => p.theme.datePickerDaySize}em;\n height: ${(p) => p.theme.datePickerDaySize}em;\n\n display: flex;\n justify-content: center;\n align-items: center;\n\n border-radius: ${(p) => p.theme.borderRadius}em;\n cursor: pointer;\n\n @media (hover: hover) {\n &:hover,\n &:focus {\n background-color: ${(p) => clr(p.theme.datePickerDayColorBgHover)};\n color: ${(p) => clr(p.theme.datePickerDayColorTextHover)};\n }\n }\n\n ${transitionStyles('background-color', 'color')};\n`;\n\nconst DayAnotherMonth = styled(Day)`\n color: ${(p) => clr(p.theme.datePickerDayAnotherMonthColorText)};\n`;\n\nconst Today = styled(Day)`\n background-color: ${(p) => clr(p.theme.datePickerTodayColorBg)};\n color: ${(p) => clr(p.theme.datePickerTodayColorText)};\n border: 1px solid ${(p) => clr(p.theme.datePickerTodayColorBorder)};\n box-sizing: border-box;\n`;\n\nconst SelectedDay = styled(Day)`\n background-color: ${(p) => clr(p.theme.datePickerSelectedDayColorBg)};\n color: ${(p) => clr(p.theme.datePickerSelectedDayColorText)};\n`;\n\nconst DayInRange = styled(Day)`\n background-color: ${(p) =>\n clr([\n ...p.theme.colorText.slice(0, 3),\n (p.theme.colorText[3] || 1) * 0.05,\n ] as Color)};\n`;\n\nconst DatePickerCalendar: React.FC<DatePickerCalendarProps> = ({\n firstDayOfWeek,\n locale,\n value,\n onSelect = () => {},\n}) => {\n const valueRef = useRef(value);\n useEffect(() => {\n valueRef.current = value;\n }, [value]);\n\n const onSelectRef = useRef(onSelect);\n useEffect(() => {\n onSelectRef.current = onSelect;\n }, [onSelect]);\n\n const { selectedMonth, updateMonth, days } = useDatePickerCalendar({\n value,\n firstDayOfWeek,\n });\n\n const getDayProps = useCallback<\n (date: Date) => HTMLAttributes<HTMLDivElement>\n >(\n (date: Date) => ({\n tabIndex: 0,\n role: 'button',\n 'aria-label': getAccessibilityDateLabel(date, locale),\n onClick: () => {\n onSelectRef.current(date);\n },\n onKeyDown: (e) => {\n if (['Enter', ' '].includes(e.key)) {\n onSelectRef.current(date);\n e.preventDefault();\n }\n },\n onMouseDown: (e) => e.preventDefault(),\n }),\n [locale]\n );\n\n return (\n <>\n <MonthContainer>\n <Button\n type='ghost'\n wide='never'\n size='small'\n onClick={() => updateMonth(-1)}\n aria-label={locale.prevMonthLabel}\n >\n <Left />\n </Button>\n <Month>\n {locale.months[selectedMonth.month]} {selectedMonth.year}\n </Month>\n <Button\n type='ghost'\n wide='never'\n size='small'\n onClick={() => updateMonth(1)}\n aria-label={locale.nextMonthLabel}\n >\n <Right />\n </Button>\n </MonthContainer>\n\n <Calendar>\n {(firstDayOfWeek === 'sunday'\n ? locale.shortDaysOfWeek\n : [...locale.shortDaysOfWeek.slice(1), locale.shortDaysOfWeek[0]]\n ).map((item) => (\n <DayOfWeek key={item}>{item}</DayOfWeek>\n ))}\n\n {days.map(({ type, date }) => {\n if (type === 'dayAnotherMonth') {\n return (\n <DayAnotherMonth key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </DayAnotherMonth>\n );\n }\n if (type === 'selectedDay') {\n return (\n <SelectedDay key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </SelectedDay>\n );\n }\n if (type === 'dayInRange') {\n return (\n <DayInRange key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </DayInRange>\n );\n }\n if (type === 'today') {\n return (\n <Today key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </Today>\n );\n }\n return (\n <Day key={date.toISOString()} {...getDayProps(date)}>\n {date.getDate()}\n </Day>\n );\n })}\n </Calendar>\n </>\n );\n};\n\nexport default DatePickerCalendar;\n"],"mappings":";AAAA,OAAOA,MAAM,MAAM,iBAAiB;AACpC,SAEEC,yBAAyB,EACzBC,qBAAqB,QAChB,8BAA8B;AACrC,SAASC,IAAI,EAAEC,KAAK,QAAQ,kBAAkB;AAC9C,SACEC,cAAc,EACdC,gBAAgB,EAChBC,gBAAgB,QACX,mBAAmB;AAC1B,SAASC,GAAG,QAAe,oBAAoB;AAC/C,OAAOC,KAAK,IAAoBC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,OAAO;AAC7E,OAAOC,MAAM,MAAM,WAAW;AAS9B,MAAMC,cAAc,GAAGd,MAAM,CAACe,GAAI;AAClC;AACA;AACA;AACA,CAAC;AAED,MAAMC,KAAK,GAAGhB,MAAM,CAACe,GAAI;AACzB;AACA;AACA;AACA;AACA,IAAIV,cAAe;AACnB,CAAC;AAED,MAAMY,QAAQ,GAAGjB,MAAM,CAACe,GAAI;AAC5B;AACA,qCAAsCG,CAAC,IAAKA,CAAC,CAACC,KAAK,CAACC,kBAAmB;AACvE,oBAAqBF,CAAC,IAAKA,CAAC,CAACC,KAAK,CAACC,kBAAmB;AACtD;AACA;AACA,CAAC;AAED,MAAMC,SAAS,GAAGrB,MAAM,CAACe,GAAI;AAC7B;AACA;AACA,WAAYG,CAAC,IAAKV,GAAG,CAACU,CAAC,CAACC,KAAK,CAACG,4BAA4B,CAAE;AAC5D,eAAgBJ,CAAC,IAAKA,CAAC,CAACC,KAAK,CAACI,KAAK,CAACC,KAAM;AAC1C,CAAC;AAED,MAAMC,GAAG,GAAGzB,MAAM,CAACe,GAAI;AACvB,IAAIT,gBAAiB;AACrB;AACA;AACA;AACA,WAAYY,CAAC,IAAKA,C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;MAC1B,oBACE,oBAAC,WAAW;QAAC,GAAG,EAAElB,IAAI,CAACmB,WAAW;MAAG,GAAKpB,WAAW,CAACC,IAAI,CAAC,GACxDA,IAAI,CAACoB,OAAO,EAAE,CACH;IAElB;IACA,IAAIF,IAAI,KAAK,YAAY,EAAE;MACzB,oBACE,oBAAC,UAAU;QAAC,GAAG,EAAElB,IAAI,CAACmB,WAAW;MAAG,GAAKpB,WAAW,CAACC,IAAI,CAAC,GACvDA,IAAI,CAACoB,OAAO,EAAE,CACJ;IAEjB;IACA,IAAIF,IAAI,KAAK,OAAO,EAAE;MACpB,oBACE,oBAAC,KAAK;QAAC,GAAG,EAAElB,IAAI,CAACmB,WAAW;MAAG,GAAKpB,WAAW,CAACC,IAAI,CAAC,GAClDA,IAAI,CAACoB,OAAO,EAAE,CACT;IAEZ;IACA,oBACE,oBAAC,GAAG;MAAC,GAAG,EAAEpB,IAAI,CAACmB,WAAW;IAAG,GAAKpB,WAAW,CAACC,IAAI,CAAC,GAChDA,IAAI,CAACoB,OAAO,EAAE,CACX;EAEV,CAAC,CAAC,CACO,CACV;AAEP,CAAC;AAED,eAAehC,kBAAkB"}
@@ -1,11 +1,11 @@
1
1
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
2
  import styled from '@emotion/styled';
3
- import { defaultLocale, getAccessibilityDateLabel } from '@os-design/date-picker-utils';
3
+ import { defaultLocale, getAccessibilityDateLabel, useFormattedValue } from '@os-design/date-picker-utils';
4
4
  import { Down, Up } from '@os-design/icons';
5
5
  import { useIsMinWidth } from '@os-design/media';
6
6
  import { ThemeOverrider } from '@os-design/theming';
7
7
  import { useForwardedRef, useForwardedState, useKeyPress } from '@os-design/utils';
8
- import React, { forwardRef, useCallback, useMemo, useRef, useState } from 'react';
8
+ import React, { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react';
9
9
  import Button from '../Button';
10
10
  import Modal from '../Modal';
11
11
  import Popover from '../Popover';
@@ -29,6 +29,24 @@ const StyledTimePicker = styled(TimePicker)`
29
29
  width: 2.5em;
30
30
  text-align: center;
31
31
  `;
32
+ const timePickerStyle = {
33
+ style: {
34
+ width: 'auto'
35
+ }
36
+ };
37
+ const createDate = (date, time) => {
38
+ const d = new Date(0);
39
+ d.setFullYear(date.getFullYear());
40
+ d.setMonth(date.getMonth());
41
+ d.setDate(date.getDate());
42
+ d.setHours(0);
43
+ d.setMinutes(0);
44
+ if (time) {
45
+ d.setHours(time.getHours());
46
+ d.setMinutes(time.getMinutes());
47
+ }
48
+ return d;
49
+ };
32
50
 
33
51
  /**
34
52
  * The component to choose a date.
@@ -40,7 +58,8 @@ const DatePicker = /*#__PURE__*/forwardRef(({
40
58
  rightHasPadding = false,
41
59
  placeholder,
42
60
  disabled = false,
43
- format: formatFn,
61
+ autoFocus = false,
62
+ format,
44
63
  firstDayOfWeek = 'sunday',
45
64
  locale = defaultLocale,
46
65
  showTime = false,
@@ -60,30 +79,25 @@ const DatePicker = /*#__PURE__*/forwardRef(({
60
79
  defaultValue,
61
80
  onChange
62
81
  });
63
- const format = useMemo(() => {
64
- if (formatFn) return formatFn;
65
- const dateToStr = date => date.toLocaleString(undefined, {
66
- year: 'numeric',
67
- month: '2-digit',
68
- day: '2-digit',
69
- ...(showTime ? {
70
- hour: '2-digit',
71
- minute: '2-digit',
72
- hour12: timeNotation === '12-hour'
73
- } : {})
74
- });
75
- return v => Array.isArray(v) ? v.map(item => dateToStr(item)).join(' - ') : dateToStr(v);
76
- }, [formatFn, showTime, timeNotation]);
77
- const formattedValue = useMemo(() => {
78
- if (!forwardedValue) return null;
79
- return format(forwardedValue);
80
- }, [forwardedValue, format]);
82
+ useEffect(() => {
83
+ if (autoFocus) containerRef.current?.focus();
84
+ }, [autoFocus, containerRef]);
85
+ const formattedValue = useFormattedValue({
86
+ forwardedValue,
87
+ format,
88
+ showTime,
89
+ timeNotation
90
+ });
81
91
  const closeHandler = useCallback(() => {
82
92
  setOpened(false);
83
93
  }, []);
84
94
  useKeyPress(typeof window !== 'undefined' ? window : undefined, 'Escape', closeHandler);
85
95
  const isMinXs = useIsMinWidth('xs');
86
96
  const valueIsSpecified = useMemo(() => formattedValue !== undefined && formattedValue !== null, [formattedValue]);
97
+ const rightHasPaddingValue = useMemo(() => {
98
+ if (valueIsSpecified) return false;
99
+ return right ? rightHasPadding : true;
100
+ }, [right, rightHasPadding, valueIsSpecified]);
87
101
  const rightValue = useMemo(() => {
88
102
  if (valueIsSpecified) {
89
103
  return /*#__PURE__*/React.createElement("div", {
@@ -112,40 +126,52 @@ const DatePicker = /*#__PURE__*/forwardRef(({
112
126
  }
113
127
  return right || /*#__PURE__*/React.createElement(IconContainer, null, opened ? /*#__PURE__*/React.createElement(Up, null) : /*#__PURE__*/React.createElement(Down, null));
114
128
  }, [containerRef, disabled, locale.clearLabel, opened, right, setForwardedValue, valueIsSpecified]);
115
- const rightHasPaddingValue = useMemo(() => {
116
- if (valueIsSpecified) return false;
117
- return right ? rightHasPadding : true;
118
- }, [right, rightHasPadding, valueIsSpecified]);
129
+ const firstTimePickerRef = useRef(null);
130
+ const secondTimePickerRef = useRef(null);
131
+ const rangeStartedRef = useRef(false);
132
+ useEffect(() => {
133
+ rangeStartedRef.current = false;
134
+ }, [opened]);
135
+ const changeHandler = useCallback(date => {
136
+ setForwardedValue(prev => {
137
+ if (!range) return createDate(date, prev);
138
+ const d = createDate(date);
139
+ const nextValue = prev && Array.isArray(prev) && rangeStartedRef.current ? [new Date(prev[0].getTime()), createDate(date, prev[1])] : [d, d];
140
+ rangeStartedRef.current = !rangeStartedRef.current;
141
+ return nextValue.sort((a, b) => a.getTime() - b.getTime());
142
+ });
143
+ }, [range, setForwardedValue]);
119
144
  const calendarComponent = useMemo(() => /*#__PURE__*/React.createElement(DatePickerCalendar, {
120
145
  firstDayOfWeek: firstDayOfWeek,
121
146
  locale: locale,
122
- range: range,
123
147
  value: forwardedValue,
124
- onChange: v => {
125
- setForwardedValue(v);
126
- if (!showTime && !range) {
148
+ onSelect: v => {
149
+ changeHandler(v);
150
+ firstTimePickerRef.current?.focus();
151
+ if (!showTime && (!range || !rangeStartedRef.current)) {
127
152
  closeHandler();
128
153
  }
129
154
  }
130
- }), [closeHandler, firstDayOfWeek, forwardedValue, locale, range, setForwardedValue, showTime]);
131
- const secondTimePickerRef = useRef(null);
155
+ }), [changeHandler, closeHandler, firstDayOfWeek, forwardedValue, locale, range, showTime]);
132
156
  const tabOnInputComplete = useCallback(e => {
133
157
  if (e.target.selectionStart < 5) return;
134
158
  secondTimePickerRef.current?.focus();
135
- }, []);
159
+ setForwardedValue(([from]) => {
160
+ const nextTo = new Date(from.getTime());
161
+ nextTo.setHours(nextTo.getHours() + 1);
162
+ return [from, nextTo];
163
+ });
164
+ }, [setForwardedValue]);
136
165
  const closeOnInputComplete = useCallback(e => {
137
166
  if (e.target.selectionStart < 5) return;
138
167
  closeHandler();
139
168
  }, [closeHandler]);
140
169
  const timeComponent = useMemo(() => showTime && forwardedValue ? /*#__PURE__*/React.createElement(TimeContainer, null, Array.isArray(forwardedValue) ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledTimePicker, {
170
+ ref: firstTimePickerRef,
141
171
  autoFocus: true,
142
172
  disabled: disabled,
143
173
  notation: timeNotation,
144
- containerProps: {
145
- style: {
146
- width: 'auto'
147
- }
148
- },
174
+ containerProps: timePickerStyle,
149
175
  value: forwardedValue[0],
150
176
  onChange: v => setForwardedValue([v, forwardedValue[1]]),
151
177
  onKeyDown: e => {
@@ -159,11 +185,7 @@ const DatePicker = /*#__PURE__*/forwardRef(({
159
185
  ref: secondTimePickerRef,
160
186
  disabled: disabled,
161
187
  notation: timeNotation,
162
- containerProps: {
163
- style: {
164
- width: 'auto'
165
- }
166
- },
188
+ containerProps: timePickerStyle,
167
189
  value: forwardedValue[1],
168
190
  onChange: v => setForwardedValue([forwardedValue[0], v]),
169
191
  onKeyDown: e => {
@@ -174,14 +196,11 @@ const DatePicker = /*#__PURE__*/forwardRef(({
174
196
  },
175
197
  onKeyUp: closeOnInputComplete
176
198
  })) : /*#__PURE__*/React.createElement(StyledTimePicker, {
199
+ ref: firstTimePickerRef,
177
200
  autoFocus: true,
178
201
  disabled: disabled,
179
202
  notation: timeNotation,
180
- containerProps: {
181
- style: {
182
- width: 'auto'
183
- }
184
- },
203
+ containerProps: timePickerStyle,
185
204
  value: forwardedValue,
186
205
  onChange: setForwardedValue,
187
206
  onKeyDown: e => {
@@ -193,6 +212,7 @@ const DatePicker = /*#__PURE__*/forwardRef(({
193
212
  onKeyUp: closeOnInputComplete
194
213
  })) : null, [closeOnInputComplete, disabled, forwardedValue, opened, setForwardedValue, showTime, tabOnInputComplete, timeNotation]);
195
214
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SelectContainer, _extends({
215
+ onBlur: () => console.log('onBlur'),
196
216
  opened: opened,
197
217
  disabled: disabled,
198
218
  size: size,