@alicloud/appflow-chat 0.0.4-beta.3 → 0.0.4-beta.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alicloud/appflow-chat",
3
- "version": "0.0.4-beta.3",
3
+ "version": "0.0.4-beta.5",
4
4
  "description": "Appflow-Chat AI聊天机器人组件库,提供聊天服务和UI组件",
5
5
  "type": "module",
6
6
  "main": "./dist/appflow-chat.cjs.js",
@@ -1,4 +1,4 @@
1
- import React, { useCallback, useMemo } from 'react';
1
+ import React, { useCallback, useEffect, useMemo, useState } from 'react';
2
2
  import { DatePicker, version } from 'antd';
3
3
  import { TimeFieldProps, TimeSubType } from './types';
4
4
  import styled from 'styled-components';
@@ -20,25 +20,52 @@ const getAntdMajorVersion = (): number => {
20
20
 
21
21
  const isAntd5OrAbove = getAntdMajorVersion() >= 5;
22
22
 
23
- // 同步加载时间库
23
+ // 兼容 ESM 和 CJS 环境的时间库加载
24
24
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
25
  let timeLib: any = null;
26
26
 
27
- if (isAntd5OrAbove) {
28
- try {
29
- // eslint-disable-next-line @typescript-eslint/no-require-imports
30
- timeLib = require('dayjs');
31
- } catch {
32
- console.warn('dayjs not found');
27
+ // 同步尝试加载时间库(CJS 环境下可用)
28
+ const loadTimeLibSync = (): any => {
29
+ if (timeLib) return timeLib;
30
+
31
+ if (isAntd5OrAbove) {
32
+ try {
33
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
34
+ timeLib = require('dayjs');
35
+ } catch {
36
+ // require 在 ESM 环境中可能失败,后续通过异步 import 兜底
37
+ }
38
+ } else {
39
+ try {
40
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
41
+ timeLib = require('moment');
42
+ } catch {
43
+ // require 在 ESM 环境中可能失败,后续通过异步 import 兜底
44
+ }
33
45
  }
34
- } else {
46
+ return timeLib;
47
+ };
48
+
49
+ // 异步加载时间库(ESM 环境下的兜底方案)
50
+ const loadTimeLibAsync = async (): Promise<any> => {
51
+ if (timeLib) return timeLib;
52
+
35
53
  try {
36
- // eslint-disable-next-line @typescript-eslint/no-require-imports
37
- timeLib = require('moment');
54
+ if (isAntd5OrAbove) {
55
+ const dayjs = await import('dayjs');
56
+ timeLib = dayjs.default || dayjs;
57
+ } else {
58
+ const moment = await import('moment');
59
+ timeLib = moment.default || moment;
60
+ }
38
61
  } catch {
39
- console.warn('moment not found');
62
+ console.warn('Failed to load time library (both sync and async)');
40
63
  }
41
- }
64
+ return timeLib;
65
+ };
66
+
67
+ // 先尝试同步加载
68
+ loadTimeLibSync();
42
69
 
43
70
  const getDateFormat = (subType?: TimeSubType): string => {
44
71
  switch (subType) {
@@ -68,6 +95,25 @@ export const TimeField: React.FC<TimeFieldProps> = ({
68
95
  onChange,
69
96
  disabled = false,
70
97
  }) => {
98
+ // 使用 state 管理时间库引用,确保异步加载完成后能触发重新渲染
99
+ const [lib, setLib] = useState<any>(() => timeLib);
100
+
101
+ // 如果同步加载失败,通过异步 import 兜底加载
102
+ useEffect(() => {
103
+ if (lib) return;
104
+
105
+ let cancelled = false;
106
+ loadTimeLibAsync().then((loaded) => {
107
+ if (!cancelled && loaded) {
108
+ setLib(loaded);
109
+ }
110
+ });
111
+
112
+ return () => {
113
+ cancelled = true;
114
+ };
115
+ }, [lib]);
116
+
71
117
  const subType = useMemo((): TimeSubType | undefined => {
72
118
  const subTypeArray = schema.AssociationPropertyMetadata?.SubType;
73
119
  if (Array.isArray(subTypeArray) && subTypeArray.length > 0) {
@@ -80,21 +126,21 @@ export const TimeField: React.FC<TimeFieldProps> = ({
80
126
  const picker = getPickerType(subType);
81
127
  const showTime = subType === 'datetime';
82
128
 
83
- // 将字符串值转换为时间对象 - 同步方式
129
+ // 将字符串值转换为时间对象
84
130
  const dateValue = useMemo(() => {
85
- if (!value || !timeLib) return null;
131
+ if (!value || !lib) return null;
86
132
 
87
133
  try {
88
- const parsed = timeLib(value, format);
134
+ const parsed = lib(value, format);
89
135
  if (parsed && typeof parsed.isValid === 'function' && !parsed.isValid()) {
90
136
  // 如果严格解析失败,尝试宽松解析
91
- return timeLib(value);
137
+ return lib(value);
92
138
  }
93
139
  return parsed;
94
140
  } catch {
95
141
  return null;
96
142
  }
97
- }, [value, format]);
143
+ }, [value, format, lib]);
98
144
 
99
145
  const handleChange = useCallback(
100
146
  (_date: any, dateString: string | string[]) => {