@os-design/core 1.0.162 → 1.0.164

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: /*#__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 <>\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\n {right}\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,cAAc;IAAEC,iBAAiB;EAKxC,gBAAkC,IA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eACH,kEACGzB,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,EAEAb,KAAK,CAET;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,IAAAC,wBAAO,EAAC;MACvDpB,QAAQ,EAARA,QAAQ;MACRkB,SAAS,EAATA,SAAS;MACTC,YAAY,E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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"}
@@ -69,33 +69,23 @@ const SelectedDay = styled(Day)`
69
69
  background-color: ${p => clr(p.theme.datePickerSelectedDayColorBg)};
70
70
  color: ${p => clr(p.theme.datePickerSelectedDayColorText)};
71
71
  `;
72
+ const DayInRange = styled(Day)`
73
+ background-color: ${p => clr([...p.theme.colorText.slice(0, 3), (p.theme.colorText[3] || 1) * 0.05])};
74
+ `;
72
75
  const DatePickerCalendar = ({
73
76
  firstDayOfWeek,
74
77
  locale,
75
78
  value,
76
- onChange = () => {}
79
+ onSelect = () => {}
77
80
  }) => {
78
81
  const valueRef = useRef(value);
79
82
  useEffect(() => {
80
83
  valueRef.current = value;
81
84
  }, [value]);
82
- const changeHandler = useCallback(date => {
83
- const d = new Date(0);
84
- d.setFullYear(date.getFullYear());
85
- d.setMonth(date.getMonth());
86
- d.setDate(date.getDate());
87
- d.setHours(0);
88
- d.setMinutes(0);
89
- if (valueRef.current) {
90
- d.setHours(valueRef.current.getHours());
91
- d.setMinutes(valueRef.current.getMinutes());
92
- }
93
- onChange(d);
94
- }, [onChange]);
95
- const changeHandlerRef = useRef(changeHandler);
85
+ const onSelectRef = useRef(onSelect);
96
86
  useEffect(() => {
97
- changeHandlerRef.current = changeHandler;
98
- }, [changeHandler]);
87
+ onSelectRef.current = onSelect;
88
+ }, [onSelect]);
99
89
  const {
100
90
  selectedMonth,
101
91
  updateMonth,
@@ -109,11 +99,11 @@ const DatePickerCalendar = ({
109
99
  role: 'button',
110
100
  'aria-label': getAccessibilityDateLabel(date, locale),
111
101
  onClick: () => {
112
- changeHandlerRef.current(date);
102
+ onSelectRef.current(date);
113
103
  },
114
104
  onKeyDown: e => {
115
105
  if (['Enter', ' '].includes(e.key)) {
116
- changeHandlerRef.current(date);
106
+ onSelectRef.current(date);
117
107
  e.preventDefault();
118
108
  }
119
109
  },
@@ -147,6 +137,11 @@ const DatePickerCalendar = ({
147
137
  key: date.toISOString()
148
138
  }, getDayProps(date)), date.getDate());
149
139
  }
140
+ if (type === 'dayInRange') {
141
+ return /*#__PURE__*/React.createElement(DayInRange, _extends({
142
+ key: date.toISOString()
143
+ }, getDayProps(date)), date.getDate());
144
+ }
150
145
  if (type === 'today') {
151
146
  return /*#__PURE__*/React.createElement(Today, _extends({
152
147
  key: date.toISOString()
@@ -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","DatePickerCalendar","firstDayOfWeek","locale","value","onChange","valueRef","current","changeHandler","date","d","Date","setFullYear","getFullYear","setMonth","getMonth","setDate","getDate","setHours","setMinutes","getHours","getMinutes","changeHandlerRef","selectedMonth","updateMonth","days","getDayProps","tabIndex","role","onClick","onKeyDown","e","includes","key","preventDefault","onMouseDown","prevMonthLabel","months","month","year","nextMonthLabel","shortDaysOfWeek","slice","map","item","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 } 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 | null;\n onChange?: (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 DatePickerCalendar: React.FC<DatePickerCalendarProps> = ({\n firstDayOfWeek,\n locale,\n value,\n onChange = () => {},\n}) => {\n const valueRef = useRef(value);\n useEffect(() => {\n valueRef.current = value;\n }, [value]);\n\n const changeHandler = useCallback(\n (date: Date) => {\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 (valueRef.current) {\n d.setHours(valueRef.current.getHours());\n d.setMinutes(valueRef.current.getMinutes());\n }\n onChange(d);\n },\n [onChange]\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 === '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,QAAQ,oBAAoB;AACxC,OAAOC,KAAK,IAAoBC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,OAAO;AAC7E,OAA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AO,EAAE,CACH;IAElB;IACA,IAAI4B,IAAI,KAAK,OAAO,EAAE;MACpB,oBACE,oBAAC,KAAK;QAAC,GAAG,EAAEpC,IAAI,CAACqC,WAAW;MAAG,GAAKpB,WAAW,CAACjB,IAAI,CAAC,GAClDA,IAAI,CAACQ,OAAO,EAAE,CACT;IAEZ;IACA,oBACE,oBAAC,GAAG;MAAC,GAAG,EAAER,IAAI,CAACqC,WAAW;IAAG,GAAKpB,WAAW,CAACjB,IAAI,CAAC,GAChDA,IAAI,CAACQ,OAAO,EAAE,CACX;EAEV,CAAC,CAAC,CACO,CACV;AAEP,CAAC;AAED,eAAehB,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,4B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,KAAK,iBAAiB,EAAE;MAC9B,oBACE,oBAAC,eAAe;QAAC,GAAG,EAAElB,IAAI,CAACmB,WAAW;MAAG,GAAKpB,WAAW,CAACC,IAAI,CAAC,GAC5DA,IAAI,CAACoB,OAAO,EAAE,CACC;IAEtB;IACA,IAAIF,IAAI,KAAK,aAAa,EAAE;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, 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';
@@ -17,8 +17,36 @@ const StyledPopover = styled(Popover)`
17
17
  padding: ${p => p.theme.datePickerPadding}em;
18
18
  `;
19
19
  const TimeContainer = styled.div`
20
+ display: flex;
21
+ justify-content: center;
20
22
  margin-top: 0.5em;
21
23
  `;
24
+ const Dash = styled.div`
25
+ margin: 0 0.5em;
26
+ line-height: ${p => p.theme.baseHeight}em;
27
+ `;
28
+ const StyledTimePicker = styled(TimePicker)`
29
+ width: 2.5em;
30
+ text-align: center;
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
+ };
22
50
 
23
51
  /**
24
52
  * The component to choose a date.
@@ -30,11 +58,12 @@ const DatePicker = /*#__PURE__*/forwardRef(({
30
58
  rightHasPadding = false,
31
59
  placeholder,
32
60
  disabled = false,
33
- format: formatFn,
61
+ format,
34
62
  firstDayOfWeek = 'sunday',
35
63
  locale = defaultLocale,
36
64
  showTime = false,
37
65
  timeNotation = '12-hour',
66
+ range = false,
38
67
  value,
39
68
  defaultValue,
40
69
  onChange,
@@ -43,43 +72,28 @@ const DatePicker = /*#__PURE__*/forwardRef(({
43
72
  }, ref) => {
44
73
  const [containerRef, mergedContainerRef] = useForwardedRef(ref);
45
74
  const [opened, setOpened] = useState(false);
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
76
  const [forwardedValue, setForwardedValue] = useForwardedState({
47
77
  value,
48
78
  defaultValue,
49
79
  onChange
50
80
  });
51
- const format = useMemo(() => {
52
- if (formatFn) return formatFn;
53
- return date => date.toLocaleString(undefined, {
54
- year: 'numeric',
55
- month: '2-digit',
56
- day: '2-digit',
57
- ...(showTime ? {
58
- hour: '2-digit',
59
- minute: '2-digit',
60
- hour12: timeNotation === '12-hour'
61
- } : {})
62
- });
63
- }, [formatFn, showTime, timeNotation]);
64
- const formattedValue = useMemo(() => {
65
- if (!forwardedValue) return null;
66
- return format(forwardedValue);
67
- }, [forwardedValue, format]);
81
+ const formattedValue = useFormattedValue({
82
+ forwardedValue,
83
+ format,
84
+ showTime,
85
+ timeNotation
86
+ });
68
87
  const closeHandler = useCallback(() => {
69
88
  setOpened(false);
70
89
  }, []);
71
90
  useKeyPress(typeof window !== 'undefined' ? window : undefined, 'Escape', closeHandler);
72
91
  const isMinXs = useIsMinWidth('xs');
73
- const calendar = /*#__PURE__*/React.createElement(DatePickerCalendar, {
74
- firstDayOfWeek: firstDayOfWeek,
75
- locale: locale,
76
- value: forwardedValue,
77
- onChange: v => {
78
- setForwardedValue(v);
79
- if (!showTime) closeHandler();
80
- }
81
- });
82
92
  const valueIsSpecified = useMemo(() => formattedValue !== undefined && formattedValue !== null, [formattedValue]);
93
+ const rightHasPaddingValue = useMemo(() => {
94
+ if (valueIsSpecified) return false;
95
+ return right ? rightHasPadding : true;
96
+ }, [right, rightHasPadding, valueIsSpecified]);
83
97
  const rightValue = useMemo(() => {
84
98
  if (valueIsSpecified) {
85
99
  return /*#__PURE__*/React.createElement("div", {
@@ -108,14 +122,81 @@ const DatePicker = /*#__PURE__*/forwardRef(({
108
122
  }
109
123
  return right || /*#__PURE__*/React.createElement(IconContainer, null, opened ? /*#__PURE__*/React.createElement(Up, null) : /*#__PURE__*/React.createElement(Down, null));
110
124
  }, [containerRef, disabled, locale.clearLabel, opened, right, setForwardedValue, valueIsSpecified]);
111
- const rightHasPaddingValue = useMemo(() => {
112
- if (valueIsSpecified) return false;
113
- return right ? rightHasPadding : true;
114
- }, [right, rightHasPadding, valueIsSpecified]);
115
- const timeComponent = useMemo(() => showTime && forwardedValue ? /*#__PURE__*/React.createElement(TimeContainer, null, /*#__PURE__*/React.createElement(TimePicker, {
125
+ const firstTimePickerRef = useRef(null);
126
+ const secondTimePickerRef = useRef(null);
127
+ const rangeStartedRef = useRef(false);
128
+ useEffect(() => {
129
+ rangeStartedRef.current = false;
130
+ }, [opened]);
131
+ const changeHandler = useCallback(date => {
132
+ setForwardedValue(prev => {
133
+ if (!range) return createDate(date, prev);
134
+ const d = createDate(date);
135
+ const nextValue = prev && Array.isArray(prev) && rangeStartedRef.current ? [new Date(prev[0].getTime()), createDate(date, prev[1])] : [d, d];
136
+ rangeStartedRef.current = !rangeStartedRef.current;
137
+ return nextValue.sort((a, b) => a.getTime() - b.getTime());
138
+ });
139
+ }, [range, setForwardedValue]);
140
+ const calendarComponent = useMemo(() => /*#__PURE__*/React.createElement(DatePickerCalendar, {
141
+ firstDayOfWeek: firstDayOfWeek,
142
+ locale: locale,
143
+ value: forwardedValue,
144
+ onSelect: v => {
145
+ changeHandler(v);
146
+ firstTimePickerRef.current?.focus();
147
+ if (!showTime && (!range || !rangeStartedRef.current)) {
148
+ closeHandler();
149
+ }
150
+ }
151
+ }), [changeHandler, closeHandler, firstDayOfWeek, forwardedValue, locale, range, showTime]);
152
+ const tabOnInputComplete = useCallback(e => {
153
+ if (e.target.selectionStart < 5) return;
154
+ secondTimePickerRef.current?.focus();
155
+ setForwardedValue(([from]) => {
156
+ const nextTo = new Date(from.getTime());
157
+ nextTo.setHours(nextTo.getHours() + 1);
158
+ return [from, nextTo];
159
+ });
160
+ }, [setForwardedValue]);
161
+ const closeOnInputComplete = useCallback(e => {
162
+ if (e.target.selectionStart < 5) return;
163
+ closeHandler();
164
+ }, [closeHandler]);
165
+ const timeComponent = useMemo(() => showTime && forwardedValue ? /*#__PURE__*/React.createElement(TimeContainer, null, Array.isArray(forwardedValue) ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(StyledTimePicker, {
166
+ ref: firstTimePickerRef,
116
167
  autoFocus: true,
117
168
  disabled: disabled,
118
169
  notation: timeNotation,
170
+ containerProps: timePickerStyle,
171
+ value: forwardedValue[0],
172
+ onChange: v => setForwardedValue([v, forwardedValue[1]]),
173
+ onKeyDown: e => {
174
+ if (disabled) return;
175
+ if (e.key === 'Enter') {
176
+ setOpened(!opened);
177
+ }
178
+ },
179
+ onKeyUp: tabOnInputComplete
180
+ }), /*#__PURE__*/React.createElement(Dash, null, "\u2013"), /*#__PURE__*/React.createElement(StyledTimePicker, {
181
+ ref: secondTimePickerRef,
182
+ disabled: disabled,
183
+ notation: timeNotation,
184
+ containerProps: timePickerStyle,
185
+ value: forwardedValue[1],
186
+ onChange: v => setForwardedValue([forwardedValue[0], v]),
187
+ onKeyDown: e => {
188
+ if (disabled) return;
189
+ if (e.key === 'Enter') {
190
+ setOpened(!opened);
191
+ }
192
+ },
193
+ onKeyUp: closeOnInputComplete
194
+ })) : /*#__PURE__*/React.createElement(StyledTimePicker, {
195
+ ref: firstTimePickerRef,
196
+ autoFocus: true,
197
+ disabled: disabled,
198
+ notation: timeNotation,
199
+ containerProps: timePickerStyle,
119
200
  value: forwardedValue,
120
201
  onChange: setForwardedValue,
121
202
  onKeyDown: e => {
@@ -123,8 +204,9 @@ const DatePicker = /*#__PURE__*/forwardRef(({
123
204
  if (e.key === 'Enter') {
124
205
  setOpened(!opened);
125
206
  }
126
- }
127
- })) : null, [disabled, forwardedValue, opened, setForwardedValue, showTime, timeNotation]);
207
+ },
208
+ onKeyUp: closeOnInputComplete
209
+ })) : null, [closeOnInputComplete, disabled, forwardedValue, opened, setForwardedValue, showTime, tabOnInputComplete, timeNotation]);
128
210
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SelectContainer, _extends({
129
211
  opened: opened,
130
212
  disabled: disabled,
@@ -173,13 +255,14 @@ const DatePicker = /*#__PURE__*/forwardRef(({
173
255
  visible: opened,
174
256
  onClose: closeHandler,
175
257
  size: size
176
- }, calendar, timeComponent) : /*#__PURE__*/React.createElement(Modal, {
258
+ }, calendarComponent, timeComponent) : /*#__PURE__*/React.createElement(Modal, {
177
259
  title: placeholder,
260
+ header: null,
178
261
  footer: null,
179
262
  visible: opened,
180
263
  onClose: closeHandler,
181
264
  size: size
182
- }, calendar, timeComponent));
265
+ }, calendarComponent, timeComponent));
183
266
  });
184
267
  DatePicker.displayName = 'DatePicker';
185
268
  export default DatePicker;