@oceanbase/ui 1.0.0-alpha.17 → 1.0.0-alpha.19

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.
@@ -1,149 +1,58 @@
1
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
4
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
5
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
6
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
8
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
9
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function (_e) { function e(_x) { return _e.apply(this, arguments); } e.toString = function () { return _e.toString(); }; return e; }(function (e) { throw e; }), f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function (_e2) { function e(_x2) { return _e2.apply(this, arguments); } e.toString = function () { return _e2.toString(); }; return e; }(function (e) { didErr = true; err = e; }), f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
10
7
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
11
8
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
12
- import { useRef, useCallback, useState, useMemo } from 'react';
9
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
10
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
11
+ import { useRef, useCallback, useState, useMemo, useEffect } from 'react';
13
12
  import dayjs from 'dayjs';
13
+ import 'dayjs/locale/en';
14
+ import 'dayjs/locale/zh-cn';
14
15
  import moment from 'moment';
16
+ // 常量
17
+ var RANGE_SEPARATORS = [' ~ ', '~', ' - ', ' – ', ' — '];
18
+ var TIMEZONE_PATTERN = /(\s*\(?(?:UTC|GMT)[+-]?\d*\)?)\s*$/i;
15
19
 
16
- // 日期时间段的类型定义
17
-
18
- // 分隔符
19
- var RANGE_SEPARATOR = ' ~ ';
20
-
21
- // 段落配置表
22
- var SEGMENT_CONFIG = {
23
- YYYY: {
24
- type: 'year',
25
- length: 4,
26
- min: 1900,
27
- max: 2100
28
- },
29
- MM: {
30
- type: 'month',
31
- length: 2,
32
- min: 1,
33
- max: 12
34
- },
35
- DD: {
36
- type: 'day',
37
- length: 2,
38
- min: 1,
39
- max: 31
40
- },
41
- HH: {
42
- type: 'hour',
43
- length: 2,
44
- min: 0,
45
- max: 23
46
- },
47
- mm: {
48
- type: 'minute',
49
- length: 2,
50
- min: 0,
51
- max: 59
52
- },
53
- ss: {
54
- type: 'second',
55
- length: 2,
56
- min: 0,
57
- max: 59
20
+ // 格式化日期值,确保使用正确的 locale
21
+ var formatDateValue = function formatDateValue(date, formatStr, isMoment, isCn) {
22
+ if (!date) return '';
23
+ var locale = isCn ? 'zh-cn' : 'en';
24
+ if (isMoment || moment.isMoment(date)) {
25
+ return date.locale(locale).format(formatStr);
26
+ } else {
27
+ var dayjsInstance = dayjs.isDayjs(date) ? date : dayjs(date);
28
+ return dayjsInstance.locale(locale).format(formatStr);
58
29
  }
59
30
  };
60
31
 
61
- // 解析单个日期时间字符串的段落 - 根据 format 动态计算
62
- var parseDateTimeSegments = function parseDateTimeSegments(format, dateValue, rangeType, offset) {
63
- if (!dateValue) return [];
64
- var segments = [];
65
- var patterns = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss'];
66
- for (var _i = 0, _patterns = patterns; _i < _patterns.length; _i++) {
67
- var pattern = _patterns[_i];
68
- var formatIndex = format.indexOf(pattern);
69
- if (formatIndex === -1) continue;
70
- var config = SEGMENT_CONFIG[pattern];
71
- var valueStart = formatIndex;
72
- var valueEnd = formatIndex + config.length;
73
- segments.push({
74
- type: config.type,
75
- range: rangeType,
76
- start: offset + valueStart,
77
- end: offset + valueEnd,
78
- value: dateValue.substring(valueStart, valueEnd),
79
- min: config.min,
80
- max: config.max,
81
- padLength: config.length
82
- });
83
- }
84
- segments.sort(function (a, b) {
85
- return a.start - b.start;
86
- });
87
- return segments;
88
- };
89
-
90
- // 根据光标位置找到当前段落
91
- var findSegmentByPosition = function findSegmentByPosition(segments, position) {
92
- var _iterator = _createForOfIteratorHelper(segments),
93
- _step;
94
- try {
95
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
96
- var segment = _step.value;
97
- if (position >= segment.start && position <= segment.end) {
98
- return segment;
99
- }
100
- }
101
- } catch (err) {
102
- _iterator.e(err);
103
- } finally {
104
- _iterator.f();
105
- }
106
- var minDistance = Infinity;
107
- var closestSegment = null;
108
- var _iterator2 = _createForOfIteratorHelper(segments),
109
- _step2;
110
- try {
111
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
112
- var _segment = _step2.value;
113
- var distance = Math.min(Math.abs(position - _segment.start), Math.abs(position - _segment.end));
114
- if (distance < minDistance) {
115
- minDistance = distance;
116
- closestSegment = _segment;
32
+ // 解析粘贴文本,提取开始和结束时间字符串
33
+ var parsePastedText = function parsePastedText(text) {
34
+ for (var _i = 0, _RANGE_SEPARATORS = RANGE_SEPARATORS; _i < _RANGE_SEPARATORS.length; _i++) {
35
+ var sep = _RANGE_SEPARATORS[_i];
36
+ if (text.includes(sep)) {
37
+ var parts = text.split(sep);
38
+ if (parts.length === 2) {
39
+ return [parts[0].trim(), parts[1].trim()];
117
40
  }
118
41
  }
119
- } catch (err) {
120
- _iterator2.e(err);
121
- } finally {
122
- _iterator2.f();
123
42
  }
124
- return closestSegment;
43
+ // 如果没有找到分隔符,使用整个文本作为开始和结束时间
44
+ return [text, text];
125
45
  };
126
46
 
127
- // 获取下一个段落
128
- var getNextSegment = function getNextSegment(segments, currentSegment) {
129
- var currentIndex = segments.findIndex(function (s) {
130
- return s.range === currentSegment.range && s.type === currentSegment.type;
131
- });
132
- if (currentIndex < segments.length - 1) {
133
- return segments[currentIndex + 1];
134
- }
135
- return null;
136
- };
47
+ // 解析日期字符串
48
+ var parseDateString = function parseDateString(dateStr, formatStr, isMoment) {
49
+ var strictParsed = isMoment ? moment(dateStr, formatStr, true) : dayjs(dateStr, formatStr, true);
137
50
 
138
- // 获取上一个段落
139
- var getPrevSegment = function getPrevSegment(segments, currentSegment) {
140
- var currentIndex = segments.findIndex(function (s) {
141
- return s.range === currentSegment.range && s.type === currentSegment.type;
142
- });
143
- if (currentIndex > 0) {
144
- return segments[currentIndex - 1];
51
+ // 如果严格解析失败,尝试宽松解析
52
+ if (strictParsed.isValid()) {
53
+ return strictParsed;
145
54
  }
146
- return null;
55
+ return isMoment ? moment(dateStr) : dayjs(dateStr);
147
56
  };
148
57
  export var useSegmentedInput = function useSegmentedInput(options) {
149
58
  var value = options.value,
@@ -154,274 +63,242 @@ export var useSegmentedInput = function useSegmentedInput(options) {
154
63
  isMoment = _options$isMoment === void 0 ? false : _options$isMoment,
155
64
  _options$isCn = options.isCn,
156
65
  isCn = _options$isCn === void 0 ? true : _options$isCn,
157
- onClick = options.onClick;
66
+ onClick = options.onClick,
67
+ _options$open = options.open,
68
+ open = _options$open === void 0 ? false : _options$open;
158
69
  var inputRef = useRef(null);
159
70
  var _useState = useState(false),
160
71
  _useState2 = _slicedToArray(_useState, 2),
161
72
  isEditing = _useState2[0],
162
73
  setIsEditing = _useState2[1];
163
- var _useState3 = useState(''),
74
+ // 粘贴值:仅作为预览,只有确认后才触发 onChange
75
+ var _useState3 = useState(null),
164
76
  _useState4 = _slicedToArray(_useState3, 2),
165
- inputBuffer = _useState4[0],
166
- setInputBuffer = _useState4[1];
167
- var _useState5 = useState(null),
168
- _useState6 = _slicedToArray(_useState5, 2),
169
- currentSegmentKey = _useState6[0],
170
- setCurrentSegmentKey = _useState6[1];
77
+ pastedValue = _useState4[0],
78
+ setPastedValue = _useState4[1];
171
79
 
172
- // 格式化日期值为字符串
173
- var formatValue = useCallback(function (v) {
174
- var includeTimezone = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
175
- if (!v) return '';
176
- var formatToUse = includeTimezone ? format : baseFormat;
177
- return v.format(formatToUse);
178
- }, [format, baseFormat]);
80
+ // 根据语言环境设置分隔符
81
+ var rangeSeparator = useMemo(function () {
82
+ return isCn ? ' ~ ' : ' - ';
83
+ }, [isCn]);
179
84
 
180
- // 开始时间不包含时区后缀,结束时间包含
181
- var startValue = useMemo(function () {
182
- return formatValue((value === null || value === void 0 ? void 0 : value[0]) || null, false);
183
- }, [value, formatValue]);
184
- var endValue = useMemo(function () {
185
- return formatValue((value === null || value === void 0 ? void 0 : value[1]) || null, true);
186
- }, [value, formatValue]);
187
-
188
- // 合并后的显示值
189
- var displayValue = useMemo(function () {
190
- if (!startValue && !endValue) return '';
191
- return "".concat(startValue).concat(RANGE_SEPARATOR).concat(endValue);
192
- }, [startValue, endValue]);
193
-
194
- // 解析所有段落
195
- var allSegments = useMemo(function () {
196
- var startSegments = parseDateTimeSegments(baseFormat, startValue, 'start', 0);
197
- var endOffset = startValue.length + RANGE_SEPARATOR.length;
198
- var endSegments = parseDateTimeSegments(format, endValue, 'end', endOffset);
199
- return [].concat(_toConsumableArray(startSegments), _toConsumableArray(endSegments));
200
- }, [baseFormat, format, startValue, endValue]);
201
-
202
- // 动态获取当前段落
203
- var currentSegment = useMemo(function () {
204
- if (!currentSegmentKey) return null;
205
- return allSegments.find(function (s) {
206
- return "".concat(s.range, "-").concat(s.type) === currentSegmentKey;
207
- }) || null;
208
- }, [currentSegmentKey, allSegments]);
85
+ // 显示值:有粘贴值时显示粘贴值(预览),否则显示原始 value
86
+ var _useState5 = useState(''),
87
+ _useState6 = _slicedToArray(_useState5, 2),
88
+ displayValue = _useState6[0],
89
+ setDisplayValue = _useState6[1];
209
90
 
210
- // 创建新的日期对象
211
- var createDate = useCallback(function (dateStr, rangeType) {
212
- var formatToUse = rangeType === 'start' ? baseFormat : format;
213
- if (isMoment) {
214
- return moment(dateStr, formatToUse);
91
+ // 同步更新显示值
92
+ useEffect(function () {
93
+ var displayDate = pastedValue || value;
94
+ var startDate = displayDate === null || displayDate === void 0 ? void 0 : displayDate[0];
95
+ var endDate = displayDate === null || displayDate === void 0 ? void 0 : displayDate[1];
96
+ var startValue = formatDateValue(startDate, baseFormat, isMoment, isCn);
97
+ var endValue = formatDateValue(endDate, format, isMoment, isCn);
98
+ if (!startValue && !endValue) {
99
+ setDisplayValue('');
100
+ } else {
101
+ setDisplayValue("".concat(startValue).concat(rangeSeparator).concat(endValue));
215
102
  }
216
- return dayjs(dateStr, formatToUse);
217
- }, [isMoment, format, baseFormat]);
103
+ }, [pastedValue, value, baseFormat, format, rangeSeparator, isMoment, isCn]);
218
104
 
219
- // 更新日期值
220
- var updateValue = useCallback(function (segment, newSegmentValue) {
221
- var which = segment.range;
222
- var currentDateValue = which === 'start' ? startValue : endValue;
223
- var paddedValue = newSegmentValue.padStart(segment.padLength, '0');
224
- var baseOffset = which === 'start' ? 0 : startValue.length + RANGE_SEPARATOR.length;
225
- var localStart = segment.start - baseOffset;
226
- var localEnd = segment.end - baseOffset;
227
- var newDateValue = currentDateValue.substring(0, localStart) + paddedValue + currentDateValue.substring(localEnd);
228
- var newDate = createDate(newDateValue, which);
229
- if (newDate.isValid()) {
230
- var newRange = which === 'start' ? [newDate, (value === null || value === void 0 ? void 0 : value[1]) || newDate] : [(value === null || value === void 0 ? void 0 : value[0]) || newDate, newDate];
231
- onChange === null || onChange === void 0 || onChange(newRange);
105
+ // 当 value 更新为 pastedValue 时,清除 pastedValue(说明确定按钮已生效)
106
+ useEffect(function () {
107
+ if (pastedValue && value) {
108
+ var _pastedValue$, _value$, _pastedValue$2, _value$2;
109
+ var isSameAsPasted = ((_pastedValue$ = pastedValue[0]) === null || _pastedValue$ === void 0 ? void 0 : _pastedValue$.valueOf()) === ((_value$ = value[0]) === null || _value$ === void 0 ? void 0 : _value$.valueOf()) && ((_pastedValue$2 = pastedValue[1]) === null || _pastedValue$2 === void 0 ? void 0 : _pastedValue$2.valueOf()) === ((_value$2 = value[1]) === null || _value$2 === void 0 ? void 0 : _value$2.valueOf());
110
+ if (isSameAsPasted) {
111
+ setPastedValue(null);
112
+ setIsEditing(false);
113
+ }
232
114
  }
233
- }, [startValue, endValue, value, onChange, createDate]);
115
+ }, [value, pastedValue]);
234
116
 
235
- // 选中指定段落
236
- var selectSegment = useCallback(function (segment) {
237
- if (inputRef.current) {
238
- var _inputRef$current;
239
- inputRef.current.focus();
240
- (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || (_inputRef$current = _inputRef$current.input) === null || _inputRef$current === void 0 || _inputRef$current.setSelectionRange(segment.start, segment.end);
241
- setCurrentSegmentKey("".concat(segment.range, "-").concat(segment.type));
242
- setInputBuffer('');
117
+ // 确认粘贴:触发 onChange,清除粘贴状态
118
+ var confirmPastedValue = useCallback(function () {
119
+ if (pastedValue) {
120
+ onChange === null || onChange === void 0 || onChange(pastedValue);
121
+ setPastedValue(null);
122
+ setIsEditing(false);
243
123
  }
124
+ }, [pastedValue, onChange]);
125
+
126
+ // 取消粘贴:清除粘贴值,回到初始值
127
+ var cancelPastedValue = useCallback(function () {
128
+ var _inputRef$current;
129
+ setPastedValue(null);
130
+ setIsEditing(false);
131
+ (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || (_inputRef$current = _inputRef$current.input) === null || _inputRef$current === void 0 || _inputRef$current.blur();
244
132
  }, []);
245
133
 
246
- // 处理单击事件
134
+ // 单击获取焦点
247
135
  var handleClick = useCallback(function (e) {
248
- var _inputRef$current2;
249
136
  e.stopPropagation();
250
137
  setIsEditing(true);
138
+ // 让 input 获得焦点
139
+ setTimeout(function () {
140
+ var _inputRef$current2;
141
+ (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || (_inputRef$current2 = _inputRef$current2.input) === null || _inputRef$current2 === void 0 || _inputRef$current2.focus();
142
+ }, 0);
251
143
  onClick === null || onClick === void 0 || onClick();
252
- var cursorPos = ((_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || (_inputRef$current2 = _inputRef$current2.input) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.selectionStart) || 0;
253
- var segment = findSegmentByPosition(allSegments, cursorPos);
254
- if (segment) {
255
- selectSegment(segment);
256
- }
257
- }, [allSegments, selectSegment, onClick]);
144
+ }, [onClick]);
258
145
 
259
- // 处理双击事件
146
+ // 双击获取焦点并全选
260
147
  var handleDoubleClick = useCallback(function (e) {
261
148
  var _inputRef$current3;
262
149
  e.stopPropagation();
263
- e.preventDefault();
264
150
  setIsEditing(true);
265
- setCurrentSegmentKey(null);
266
151
  (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 || (_inputRef$current3 = _inputRef$current3.input) === null || _inputRef$current3 === void 0 || _inputRef$current3.select();
267
152
  }, []);
268
153
 
269
- // 处理键盘输入
270
- var handleKeyDown = useCallback(function (e) {
271
- if (!currentSegment) return;
272
- var key = e.key;
273
-
274
- // 处理数字输入
275
- if (/^\d$/.test(key)) {
276
- e.preventDefault();
277
- var newBuffer = inputBuffer + key;
278
- setInputBuffer(newBuffer);
279
- var numValue = parseInt(newBuffer, 10);
280
- var shouldJump = newBuffer.length >= currentSegment.padLength || numValue * 10 > currentSegment.max;
281
- if (shouldJump) {
282
- var clampedValue = Math.max(currentSegment.min, Math.min(currentSegment.max, numValue));
283
- updateValue(currentSegment, String(clampedValue));
284
- var nextSegment = getNextSegment(allSegments, currentSegment);
285
- if (nextSegment) {
286
- setTimeout(function () {
287
- return selectSegment(nextSegment);
288
- }, 0);
289
- }
290
- } else {
291
- updateValue(currentSegment, newBuffer);
292
- }
293
- return;
154
+ // 面板打开时,让 input 获得焦点
155
+ useEffect(function () {
156
+ if (open) {
157
+ // 延迟获取焦点,确保面板已渲染
158
+ var timer = setTimeout(function () {
159
+ var _inputRef$current4;
160
+ (_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 || (_inputRef$current4 = _inputRef$current4.input) === null || _inputRef$current4 === void 0 || _inputRef$current4.focus();
161
+ }, 100);
162
+ return function () {
163
+ return clearTimeout(timer);
164
+ };
294
165
  }
166
+ }, [open]);
295
167
 
296
- // 处理左右箭头
297
- if (key === 'ArrowLeft') {
298
- e.preventDefault();
299
- var prevSegment = getPrevSegment(allSegments, currentSegment);
300
- if (prevSegment) {
301
- selectSegment(prevSegment);
302
- }
303
- return;
304
- }
305
- if (key === 'ArrowRight') {
306
- e.preventDefault();
307
- var _nextSegment = getNextSegment(allSegments, currentSegment);
308
- if (_nextSegment) {
309
- selectSegment(_nextSegment);
310
- }
311
- return;
312
- }
168
+ // 检查焦点是否在面板内(参考 ant-design 的实现)
169
+ var isFocusInPanel = useCallback(function (relatedTarget) {
170
+ if (!relatedTarget) return false;
171
+ var panelElement = document.querySelector('.ant-picker-dropdown, .ant-dropdown, [class*="ranger-picker-panel"]');
172
+ return panelElement ? panelElement.contains(relatedTarget) : false;
173
+ }, []);
313
174
 
314
- // 处理上下箭头调整数值
315
- if (key === 'ArrowUp' || key === 'ArrowDown') {
316
- e.preventDefault();
317
- var currentNum = parseInt(currentSegment.value, 10);
318
- var delta = key === 'ArrowUp' ? 1 : -1;
319
- var newNum = currentNum + delta;
320
- if (newNum > currentSegment.max) {
321
- newNum = currentSegment.min;
322
- } else if (newNum < currentSegment.min) {
323
- newNum = currentSegment.max;
324
- }
325
- updateValue(currentSegment, String(newNum));
326
- setTimeout(function () {
327
- var _inputRef$current4;
328
- if ((_inputRef$current4 = inputRef.current) !== null && _inputRef$current4 !== void 0 && _inputRef$current4.input && currentSegment) {
329
- inputRef.current.input.setSelectionRange(currentSegment.start, currentSegment.end);
330
- }
331
- }, 0);
332
- return;
333
- }
175
+ // 失去焦点处理(如果焦点在面板内,直接返回,不处理)
176
+ var handleBlur = useCallback(function (e) {
177
+ var relatedTarget = e.relatedTarget;
334
178
 
335
- // 处理 Tab
336
- if (key === 'Tab') {
337
- var _nextSegment2 = e.shiftKey ? getPrevSegment(allSegments, currentSegment) : getNextSegment(allSegments, currentSegment);
338
- if (_nextSegment2) {
339
- e.preventDefault();
340
- selectSegment(_nextSegment2);
341
- }
179
+ // 不强制重新获取焦点,允许用户在面板中的 input 正常编辑
180
+ if (isFocusInPanel(relatedTarget)) {
342
181
  return;
343
182
  }
344
183
 
345
- // 阻止删除和其他非法输入
346
- if (key === 'Backspace' || key === 'Delete' || !e.ctrlKey && !e.metaKey && key.length === 1 && !/\d/.test(key)) {
347
- e.preventDefault();
348
- return;
349
- }
350
- }, [currentSegment, inputBuffer, allSegments, selectSegment, updateValue]);
184
+ // 焦点不在面板内,清除粘贴值
185
+ // 但是,如果 value 已经更新为 pastedValue,说明确定按钮已经生效,不应该清除
186
+ if (pastedValue) {
187
+ var _pastedValue$3, _value$3, _pastedValue$4, _value$4;
188
+ // 检查 value 是否已经更新为 pastedValue
189
+ var isValueUpdated = value && ((_pastedValue$3 = pastedValue[0]) === null || _pastedValue$3 === void 0 ? void 0 : _pastedValue$3.valueOf()) === ((_value$3 = value[0]) === null || _value$3 === void 0 ? void 0 : _value$3.valueOf()) && ((_pastedValue$4 = pastedValue[1]) === null || _pastedValue$4 === void 0 ? void 0 : _pastedValue$4.valueOf()) === ((_value$4 = value[1]) === null || _value$4 === void 0 ? void 0 : _value$4.valueOf());
351
190
 
352
- // 处理失焦事件
353
- var handleBlur = useCallback(function (e) {
354
- if (currentSegment && inputBuffer) {
355
- var numValue = parseInt(inputBuffer, 10);
356
- var clampedValue = Math.max(currentSegment.min, Math.min(currentSegment.max, numValue));
357
- updateValue(currentSegment, String(clampedValue));
191
+ // 只有当 value 还没有更新为 pastedValue 时,才清除 pastedValue
192
+ // 这样可以避免在确定按钮点击后,失去焦点时清除已确认的值
193
+ if (!isValueUpdated) {
194
+ setPastedValue(null);
195
+ setIsEditing(false);
196
+ }
358
197
  }
359
- setIsEditing(false);
360
- setCurrentSegmentKey(null);
361
- setInputBuffer('');
362
- }, [currentSegment, inputBuffer, updateValue]);
198
+ }, [isFocusInPanel, pastedValue, value]);
363
199
 
364
- // 处理粘贴事件
365
- var handlePaste = useCallback(function (e) {
366
- e.preventDefault();
367
- var pastedText = e.clipboardData.getData('text').trim();
368
- if (!pastedText) return;
369
- var separators = [' ~ ', '~', ' - ', ' – ', ' — '];
370
- var startStr = '';
371
- var endStr = '';
372
- for (var _i2 = 0, _separators = separators; _i2 < _separators.length; _i2++) {
373
- var sep = _separators[_i2];
374
- if (pastedText.includes(sep)) {
375
- var parts = pastedText.split(sep);
376
- if (parts.length === 2) {
377
- startStr = parts[0].trim();
378
- endStr = parts[1].trim();
379
- break;
200
+ // 解析粘贴文本并设置预览值
201
+ var processPaste = useCallback( /*#__PURE__*/function () {
202
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pastedText) {
203
+ var _parsePastedText, _parsePastedText2, startStr, endStr, formatWithoutTimezone, parsedStart, parsedEnd;
204
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
205
+ while (1) switch (_context.prev = _context.next) {
206
+ case 0:
207
+ if (pastedText.trim()) {
208
+ _context.next = 2;
209
+ break;
210
+ }
211
+ return _context.abrupt("return");
212
+ case 2:
213
+ _parsePastedText = parsePastedText(pastedText.trim()), _parsePastedText2 = _slicedToArray(_parsePastedText, 2), startStr = _parsePastedText2[0], endStr = _parsePastedText2[1];
214
+ formatWithoutTimezone = format.replace(TIMEZONE_PATTERN, '');
215
+ parsedStart = parseDateString(startStr, baseFormat, isMoment);
216
+ parsedEnd = parseDateString(endStr, formatWithoutTimezone, isMoment);
217
+ if (parsedStart.isValid() && parsedEnd.isValid()) {
218
+ setPastedValue([parsedStart, parsedEnd]);
219
+ setIsEditing(true);
220
+ }
221
+ case 7:
222
+ case "end":
223
+ return _context.stop();
380
224
  }
381
- }
382
- }
383
- if (!startStr && !endStr) {
384
- startStr = pastedText;
385
- endStr = pastedText;
386
- }
387
- var formats = ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm', 'YYYY/MM/DD HH:mm:ss', 'YYYY/MM/DD HH:mm', 'MM/DD/YYYY HH:mm:ss', 'MM/DD/YYYY HH:mm', 'YYYY-MM-DD', 'YYYY/MM/DD', 'MM/DD/YYYY'];
388
- var parsedStart = null;
389
- var parsedEnd = null;
390
- for (var _i3 = 0, _formats = formats; _i3 < _formats.length; _i3++) {
391
- var fmt = _formats[_i3];
392
- var tryStart = isMoment ? moment(startStr, fmt, true) : dayjs(startStr, fmt, true);
393
- if (tryStart.isValid()) {
394
- parsedStart = tryStart;
395
- break;
396
- }
397
- }
398
- for (var _i4 = 0, _formats2 = formats; _i4 < _formats2.length; _i4++) {
399
- var _fmt = _formats2[_i4];
400
- var tryEnd = isMoment ? moment(endStr, _fmt, true) : dayjs(endStr, _fmt, true);
401
- if (tryEnd.isValid()) {
402
- parsedEnd = tryEnd;
403
- break;
404
- }
405
- }
406
- if (parsedStart && parsedEnd) {
407
- onChange === null || onChange === void 0 || onChange([parsedStart, parsedEnd]);
408
- } else if (parsedStart) {
409
- onChange === null || onChange === void 0 || onChange([parsedStart, (value === null || value === void 0 ? void 0 : value[1]) || parsedStart]);
410
- } else if (parsedEnd) {
411
- onChange === null || onChange === void 0 || onChange([(value === null || value === void 0 ? void 0 : value[0]) || parsedEnd, parsedEnd]);
412
- }
413
- }, [isMoment, onChange, value]);
225
+ }, _callee);
226
+ }));
227
+ return function (_x) {
228
+ return _ref.apply(this, arguments);
229
+ };
230
+ }(), [isMoment, format, baseFormat]);
231
+
232
+ // 键盘事件:Ctrl+V 粘贴,Enter 确认,Escape 取消
233
+ var handleKeyDown = useCallback( /*#__PURE__*/function () {
234
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(e) {
235
+ var text;
236
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
237
+ while (1) switch (_context2.prev = _context2.next) {
238
+ case 0:
239
+ if (!((e.ctrlKey || e.metaKey) && e.key === 'v')) {
240
+ _context2.next = 14;
241
+ break;
242
+ }
243
+ e.preventDefault();
244
+ _context2.prev = 2;
245
+ _context2.next = 5;
246
+ return navigator.clipboard.readText();
247
+ case 5:
248
+ text = _context2.sent;
249
+ _context2.next = 8;
250
+ return processPaste(text);
251
+ case 8:
252
+ _context2.next = 13;
253
+ break;
254
+ case 10:
255
+ _context2.prev = 10;
256
+ _context2.t0 = _context2["catch"](2);
257
+ console.warn('Failed to read clipboard:', _context2.t0);
258
+ case 13:
259
+ return _context2.abrupt("return");
260
+ case 14:
261
+ if (pastedValue) {
262
+ _context2.next = 16;
263
+ break;
264
+ }
265
+ return _context2.abrupt("return");
266
+ case 16:
267
+ // Enter 确认粘贴
268
+ if (e.key === 'Enter') {
269
+ e.preventDefault();
270
+ confirmPastedValue();
271
+ }
272
+ // Escape 取消粘贴
273
+ else if (e.key === 'Escape') {
274
+ e.preventDefault();
275
+ cancelPastedValue();
276
+ }
277
+ case 17:
278
+ case "end":
279
+ return _context2.stop();
280
+ }
281
+ }, _callee2, null, [[2, 10]]);
282
+ }));
283
+ return function (_x2) {
284
+ return _ref2.apply(this, arguments);
285
+ };
286
+ }(), [pastedValue, confirmPastedValue, cancelPastedValue, processPaste]);
287
+ // 获取粘贴值
288
+ var getPastedValue = useCallback(function () {
289
+ return pastedValue;
290
+ }, [pastedValue]);
414
291
  return {
415
292
  inputRef: inputRef,
416
293
  isEditing: isEditing,
417
294
  displayValue: displayValue,
418
- currentSegment: currentSegment,
419
- allSegments: allSegments,
420
295
  handleClick: handleClick,
421
296
  handleDoubleClick: handleDoubleClick,
422
- handleKeyDown: handleKeyDown,
423
297
  handleBlur: handleBlur,
424
- handlePaste: handlePaste
298
+ handleKeyDown: handleKeyDown,
299
+ confirmPastedValue: confirmPastedValue,
300
+ hasPastedValue: !!pastedValue,
301
+ getPastedValue: getPastedValue
425
302
  };
426
303
  };
427
304
  export default useSegmentedInput;