@choice-ui/react 1.5.9 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1222,54 +1222,71 @@ function parseCompositeFormat(input, format10, locale) {
1222
1222
  }
1223
1223
  }
1224
1224
  function removeWeekdayFromFormat(format10) {
1225
- return format10.replace(/\s*[eEic]{1,4}\s*/g, "").replace(/\s+/g, " ").replace(/\s*([,,、])\s*/g, "$1").trim();
1225
+ return format10.replace(/\s*[eEic]{1,4}\s*/g, "").replace(/MMM(?!M)/g, "M月").replace(/\s+/g, " ").replace(/\s*([,,、])\s*/g, "$1").trim();
1226
1226
  }
1227
1227
  function removeWeekdayFromInput(input, locale) {
1228
1228
  try {
1229
- const weekdayNames = generateWeekdayNames2(locale);
1230
- if (weekdayNames.length === 0) {
1231
- return input;
1229
+ const { longNames, shortNames } = generateWeekdayNames2(locale);
1230
+ let result = input;
1231
+ if (longNames.length > 0) {
1232
+ const sortedLongNames = longNames.sort((a, b) => b.length - a.length);
1233
+ const longPattern = new RegExp(`\\s*(${sortedLongNames.join("|")})\\s*`, "gi");
1234
+ result = result.replace(longPattern, " ");
1235
+ }
1236
+ if (shortNames.length > 0) {
1237
+ const sortedShortNames = shortNames.sort((a, b) => b.length - a.length);
1238
+ const singleCharNames = sortedShortNames.filter((name) => name.length === 1);
1239
+ const multiCharNames = sortedShortNames.filter((name) => name.length > 1);
1240
+ if (multiCharNames.length > 0) {
1241
+ const multiPattern = new RegExp(`\\s*(${multiCharNames.join("|")})\\s*`, "gi");
1242
+ result = result.replace(multiPattern, " ");
1243
+ }
1244
+ if (singleCharNames.length > 0) {
1245
+ const singlePattern = new RegExp(
1246
+ `([((「【])(${singleCharNames.join("|")})([))」】])`,
1247
+ "gi"
1248
+ );
1249
+ result = result.replace(singlePattern, "$1$3");
1250
+ }
1232
1251
  }
1233
- const sortedNames = weekdayNames.sort((a, b) => b.length - a.length);
1234
- const pattern = new RegExp(`\\s*(${sortedNames.join("|")})\\s*`, "gi");
1235
- return input.replace(pattern, "").replace(/\s+/g, " ").trim();
1252
+ return result.replace(/\s+/g, " ").trim();
1236
1253
  } catch {
1237
1254
  return input.replace(/\s*(星期[一二三四五六日天]|周[一二三四五六日天])\s*/g, "").replace(/\s+/g, " ").trim();
1238
1255
  }
1239
1256
  }
1240
1257
  function generateWeekdayNames2(locale) {
1241
- const names = [];
1258
+ const longNames = [];
1259
+ const shortNames = [];
1242
1260
  try {
1243
1261
  const baseDate = new Date(2024, 0, 7);
1244
1262
  for (let i = 0; i < 7; i++) {
1245
1263
  const currentDate = new Date(baseDate);
1246
1264
  currentDate.setDate(baseDate.getDate() + i);
1247
1265
  const fullName = format(currentDate, "eeee", { locale });
1248
- if (fullName && fullName !== "eeee") {
1249
- names.push(fullName);
1266
+ if (fullName && fullName !== "eeee" && !fullName.includes("e")) {
1267
+ longNames.push(fullName);
1250
1268
  }
1251
1269
  const shortName = format(currentDate, "eee", { locale });
1252
- if (shortName && shortName !== "eee" && shortName !== fullName) {
1253
- names.push(shortName);
1254
- }
1255
- const veryShortName = format(currentDate, "ee", { locale });
1256
- if (veryShortName && veryShortName !== "ee" && veryShortName !== shortName && veryShortName !== fullName) {
1257
- names.push(veryShortName);
1270
+ if (shortName && shortName !== "eee" && shortName !== fullName && !shortName.includes("e")) {
1271
+ shortNames.push(shortName);
1258
1272
  }
1259
1273
  const localeKey = getLocaleKey(locale);
1260
- if (localeKey === "zh") {
1261
- const weekFormat = format(currentDate, "eeee", { locale }).replace("星期", "周");
1274
+ if (localeKey === "zh" && fullName) {
1275
+ const weekFormat = fullName.replace("星期", "周");
1262
1276
  if (weekFormat !== fullName) {
1263
- names.push(weekFormat);
1277
+ shortNames.push(weekFormat);
1264
1278
  }
1265
1279
  }
1266
1280
  }
1267
- return [...new Set(names)].filter(
1268
- (name) => name && name.length > 0 && !name.includes("e") && // 过滤掉格式化失败的情况
1269
- name !== "Invalid Date"
1281
+ const filterNames = (names) => [...new Set(names)].filter(
1282
+ (name) => name && name.length > 0 && !/^\d+$/.test(name) && name !== "Invalid Date"
1270
1283
  );
1284
+ return {
1285
+ longNames: filterNames(longNames),
1286
+ shortNames: filterNames(shortNames)
1287
+ };
1271
1288
  } catch {
1272
- return [];
1289
+ return { longNames: [], shortNames: [] };
1273
1290
  }
1274
1291
  }
1275
1292
  function parseInvalidFormattedDate(input, targetFormat, locale) {
@@ -3116,6 +3133,23 @@ var TimeCalendarBase = memo(function TimeCalendar(props) {
3116
3133
  const createPrefixElement = useCallback((isSelected) => {
3117
3134
  return isSelected ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(Fragment, {});
3118
3135
  }, []);
3136
+ useEffect(() => {
3137
+ if (!isOpen || !normalizedTimeString) return;
3138
+ let attempts = 0;
3139
+ const maxAttempts = 10;
3140
+ const scrollToSelected = () => {
3141
+ const selectedItem = document.querySelector(
3142
+ `[data-testid="${normalizedTimeString}"]`
3143
+ );
3144
+ if (selectedItem) {
3145
+ selectedItem.scrollIntoView({ block: "center" });
3146
+ } else if (attempts < maxAttempts) {
3147
+ attempts++;
3148
+ requestAnimationFrame(scrollToSelected);
3149
+ }
3150
+ };
3151
+ requestAnimationFrame(scrollToSelected);
3152
+ }, [isOpen, normalizedTimeString]);
3119
3153
  const handleTimeSelect = useEventCallback((timeValue) => {
3120
3154
  if (readOnly) return;
3121
3155
  const dateValue = timeStringToDate(timeValue);
@@ -1,8 +1,8 @@
1
1
  import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
- import { Check } from "@choiceform/icons-react";
3
2
  import { Dropdown as Dropdown2 } from "../../../dropdown/dist/index.js";
4
3
  import { MenuTrigger } from "../../../menus/dist/index.js";
5
- import React__default, { memo, useState, useMemo, useCallback } from "react";
4
+ import { Check } from "@choiceform/icons-react";
5
+ import React__default, { memo, useState, useMemo, useCallback, useEffect } from "react";
6
6
  import { useEventCallback } from "usehooks-ts";
7
7
  import { generateTimeOptions, normalizeTimeValue, timeStringToDate } from "../utils/time.js";
8
8
  import { useMergedValue } from "../../../../shared/hooks/use-merged-value/use-merged-value.js";
@@ -87,6 +87,23 @@ const TimeCalendarBase = memo(function TimeCalendar2(props) {
87
87
  const createPrefixElement = useCallback((isSelected) => {
88
88
  return isSelected ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(Fragment, {});
89
89
  }, []);
90
+ useEffect(() => {
91
+ if (!isOpen || !normalizedTimeString) return;
92
+ let attempts = 0;
93
+ const maxAttempts = 10;
94
+ const scrollToSelected = () => {
95
+ const selectedItem = document.querySelector(
96
+ `[data-testid="${normalizedTimeString}"]`
97
+ );
98
+ if (selectedItem) {
99
+ selectedItem.scrollIntoView({ block: "center" });
100
+ } else if (attempts < maxAttempts) {
101
+ attempts++;
102
+ requestAnimationFrame(scrollToSelected);
103
+ }
104
+ };
105
+ requestAnimationFrame(scrollToSelected);
106
+ }, [isOpen, normalizedTimeString]);
90
107
  const handleTimeSelect = useEventCallback((timeValue) => {
91
108
  if (readOnly) return;
92
109
  const dateValue = timeStringToDate(timeValue);
@@ -501,54 +501,71 @@ function parseCompositeFormat(input, format2, locale) {
501
501
  }
502
502
  }
503
503
  function removeWeekdayFromFormat(format2) {
504
- return format2.replace(/\s*[eEic]{1,4}\s*/g, "").replace(/\s+/g, " ").replace(/\s*([,,、])\s*/g, "$1").trim();
504
+ return format2.replace(/\s*[eEic]{1,4}\s*/g, "").replace(/MMM(?!M)/g, "M月").replace(/\s+/g, " ").replace(/\s*([,,、])\s*/g, "$1").trim();
505
505
  }
506
506
  function removeWeekdayFromInput(input, locale) {
507
507
  try {
508
- const weekdayNames = generateWeekdayNames(locale);
509
- if (weekdayNames.length === 0) {
510
- return input;
508
+ const { longNames, shortNames } = generateWeekdayNames(locale);
509
+ let result = input;
510
+ if (longNames.length > 0) {
511
+ const sortedLongNames = longNames.sort((a, b) => b.length - a.length);
512
+ const longPattern = new RegExp(`\\s*(${sortedLongNames.join("|")})\\s*`, "gi");
513
+ result = result.replace(longPattern, " ");
511
514
  }
512
- const sortedNames = weekdayNames.sort((a, b) => b.length - a.length);
513
- const pattern = new RegExp(`\\s*(${sortedNames.join("|")})\\s*`, "gi");
514
- return input.replace(pattern, "").replace(/\s+/g, " ").trim();
515
+ if (shortNames.length > 0) {
516
+ const sortedShortNames = shortNames.sort((a, b) => b.length - a.length);
517
+ const singleCharNames = sortedShortNames.filter((name) => name.length === 1);
518
+ const multiCharNames = sortedShortNames.filter((name) => name.length > 1);
519
+ if (multiCharNames.length > 0) {
520
+ const multiPattern = new RegExp(`\\s*(${multiCharNames.join("|")})\\s*`, "gi");
521
+ result = result.replace(multiPattern, " ");
522
+ }
523
+ if (singleCharNames.length > 0) {
524
+ const singlePattern = new RegExp(
525
+ `([((「【])(${singleCharNames.join("|")})([))」】])`,
526
+ "gi"
527
+ );
528
+ result = result.replace(singlePattern, "$1$3");
529
+ }
530
+ }
531
+ return result.replace(/\s+/g, " ").trim();
515
532
  } catch {
516
533
  return input.replace(/\s*(星期[一二三四五六日天]|周[一二三四五六日天])\s*/g, "").replace(/\s+/g, " ").trim();
517
534
  }
518
535
  }
519
536
  function generateWeekdayNames(locale) {
520
- const names = [];
537
+ const longNames = [];
538
+ const shortNames = [];
521
539
  try {
522
540
  const baseDate = new Date(2024, 0, 7);
523
541
  for (let i = 0; i < 7; i++) {
524
542
  const currentDate = new Date(baseDate);
525
543
  currentDate.setDate(baseDate.getDate() + i);
526
544
  const fullName = format(currentDate, "eeee", { locale });
527
- if (fullName && fullName !== "eeee") {
528
- names.push(fullName);
545
+ if (fullName && fullName !== "eeee" && !fullName.includes("e")) {
546
+ longNames.push(fullName);
529
547
  }
530
548
  const shortName = format(currentDate, "eee", { locale });
531
- if (shortName && shortName !== "eee" && shortName !== fullName) {
532
- names.push(shortName);
533
- }
534
- const veryShortName = format(currentDate, "ee", { locale });
535
- if (veryShortName && veryShortName !== "ee" && veryShortName !== shortName && veryShortName !== fullName) {
536
- names.push(veryShortName);
549
+ if (shortName && shortName !== "eee" && shortName !== fullName && !shortName.includes("e")) {
550
+ shortNames.push(shortName);
537
551
  }
538
552
  const localeKey = getLocaleKey(locale);
539
- if (localeKey === "zh") {
540
- const weekFormat = format(currentDate, "eeee", { locale }).replace("星期", "周");
553
+ if (localeKey === "zh" && fullName) {
554
+ const weekFormat = fullName.replace("星期", "周");
541
555
  if (weekFormat !== fullName) {
542
- names.push(weekFormat);
556
+ shortNames.push(weekFormat);
543
557
  }
544
558
  }
545
559
  }
546
- return [...new Set(names)].filter(
547
- (name) => name && name.length > 0 && !name.includes("e") && // 过滤掉格式化失败的情况
548
- name !== "Invalid Date"
560
+ const filterNames = (names) => [...new Set(names)].filter(
561
+ (name) => name && name.length > 0 && !/^\d+$/.test(name) && name !== "Invalid Date"
549
562
  );
563
+ return {
564
+ longNames: filterNames(longNames),
565
+ shortNames: filterNames(shortNames)
566
+ };
550
567
  } catch {
551
- return [];
568
+ return { longNames: [], shortNames: [] };
552
569
  }
553
570
  }
554
571
  function parseInvalidFormattedDate(input, targetFormat, locale) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@choice-ui/react",
3
- "version": "1.5.9",
3
+ "version": "1.6.1",
4
4
  "description": "A desktop-first React UI component library built for professional desktop applications with comprehensive documentation",
5
5
  "sideEffects": false,
6
6
  "type": "module",