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

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.1",
3
+ "version": "0.0.4-beta.3",
4
4
  "description": "Appflow-Chat AI聊天机器人组件库,提供聊天服务和UI组件",
5
5
  "type": "module",
6
6
  "main": "./dist/appflow-chat.cjs.js",
@@ -48,10 +48,12 @@
48
48
  "@types/react-dom": "^18.3.1",
49
49
  "@vitejs/plugin-react": "^4.3.3",
50
50
  "antd": "^5.24.8",
51
+ "dayjs": "^1.11.0",
51
52
  "eslint": "^9.13.0",
52
53
  "eslint-plugin-react-hooks": "^5.0.0",
53
54
  "eslint-plugin-react-refresh": "^0.4.14",
54
55
  "less": "^4.2.0",
56
+ "moment": "^2.29.0",
55
57
  "typescript": "~5.6.2",
56
58
  "typescript-eslint": "^8.11.0",
57
59
  "vite": "^5.4.10",
@@ -3,64 +3,43 @@ import { DatePicker, version } from 'antd';
3
3
  import { TimeFieldProps, TimeSubType } from './types';
4
4
  import styled from 'styled-components';
5
5
 
6
- // ==================== Styled Components ====================
7
-
8
- // 时间选择器容器
9
6
  const TimeFieldContainer = styled.div`
10
7
  width: 100%;
11
-
12
8
  .ant-picker {
13
9
  width: 100%;
14
10
  }
15
11
  `;
16
12
 
17
- // ==================== 版本检测 ====================
18
-
19
- /**
20
- * 检测 antd 版本是否为 5.x 或更高
21
- * antd 5.x 使用 dayjs,antd 4.x 使用 moment
22
- */
23
13
  const getAntdMajorVersion = (): number => {
24
14
  try {
25
15
  return parseInt(version.split('.')[0], 10);
26
16
  } catch {
27
- return 5; // 默认假设为 5.x
17
+ return 5;
28
18
  }
29
19
  };
30
20
 
31
21
  const isAntd5OrAbove = getAntdMajorVersion() >= 5;
32
22
 
33
- // ==================== 动态导入时间库 ====================
34
-
35
- // 根据 antd 版本动态选择时间库
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- let dayjs: any = null;
23
+ // 同步加载时间库
38
24
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- let moment: any = null;
25
+ let timeLib: any = null;
40
26
 
41
27
  if (isAntd5OrAbove) {
42
- // antd 5.x 使用 dayjs
43
28
  try {
44
29
  // eslint-disable-next-line @typescript-eslint/no-require-imports
45
- dayjs = require('dayjs');
30
+ timeLib = require('dayjs');
46
31
  } catch {
47
- console.warn('dayjs not found, TimeField may not work correctly with antd 5.x');
32
+ console.warn('dayjs not found');
48
33
  }
49
34
  } else {
50
- // antd 4.x 使用 moment
51
35
  try {
52
36
  // eslint-disable-next-line @typescript-eslint/no-require-imports
53
- moment = require('moment');
37
+ timeLib = require('moment');
54
38
  } catch {
55
- console.warn('moment not found, TimeField may not work correctly with antd 4.x');
39
+ console.warn('moment not found');
56
40
  }
57
41
  }
58
42
 
59
- // ==================== 工具函数 ====================
60
-
61
- /**
62
- * 根据时间子类型获取日期格式
63
- */
64
43
  const getDateFormat = (subType?: TimeSubType): string => {
65
44
  switch (subType) {
66
45
  case 'year-month':
@@ -74,68 +53,21 @@ const getDateFormat = (subType?: TimeSubType): string => {
74
53
  }
75
54
  };
76
55
 
77
- /**
78
- * 根据时间子类型获取 picker 类型
79
- */
80
56
  const getPickerType = (subType?: TimeSubType): 'date' | 'month' | undefined => {
81
57
  switch (subType) {
82
58
  case 'year-month':
83
59
  return 'month';
84
- case 'year-month-day':
85
- case 'datetime':
86
60
  default:
87
61
  return 'date';
88
62
  }
89
63
  };
90
64
 
91
- /**
92
- * 将字符串值转换为时间对象(根据 antd 版本使用 dayjs 或 moment)
93
- */
94
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
- const parseValue = (value: string | null | undefined, format: string): any => {
96
- if (!value) return null;
97
-
98
- if (isAntd5OrAbove && dayjs) {
99
- return dayjs(value, format);
100
- } else if (moment) {
101
- return moment(value, format);
102
- }
103
-
104
- return null;
105
- };
106
-
107
- /**
108
- * 将时间对象格式化为字符串
109
- */
110
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
- const formatValue = (date: any, format: string): string | null => {
112
- if (!date) return null;
113
-
114
- // dayjs 和 moment 都有 format 方法
115
- if (typeof date.format === 'function') {
116
- return date.format(format);
117
- }
118
-
119
- return null;
120
- };
121
-
122
- /**
123
- * 时间字段组件
124
- * 根据 SubType 渲染不同的时间选择器
125
- * 优先从 AssociationPropertyMetadata.SubType 读取,兼容旧的 TimeSubType 字段
126
- *
127
- * 兼容性说明:
128
- * - antd 5.x: 使用 dayjs
129
- * - antd 4.x: 使用 moment
130
- */
131
65
  export const TimeField: React.FC<TimeFieldProps> = ({
132
66
  schema,
133
67
  value,
134
68
  onChange,
135
69
  disabled = false,
136
70
  }) => {
137
- // 优先从 AssociationPropertyMetadata.SubType 读取,取数组第一个元素
138
- // 兼容旧的 TimeSubType 字段
139
71
  const subType = useMemo((): TimeSubType | undefined => {
140
72
  const subTypeArray = schema.AssociationPropertyMetadata?.SubType;
141
73
  if (Array.isArray(subTypeArray) && subTypeArray.length > 0) {
@@ -148,19 +80,30 @@ export const TimeField: React.FC<TimeFieldProps> = ({
148
80
  const picker = getPickerType(subType);
149
81
  const showTime = subType === 'datetime';
150
82
 
151
- // 处理值变化
83
+ // 将字符串值转换为时间对象 - 同步方式
84
+ const dateValue = useMemo(() => {
85
+ if (!value || !timeLib) return null;
86
+
87
+ try {
88
+ const parsed = timeLib(value, format);
89
+ if (parsed && typeof parsed.isValid === 'function' && !parsed.isValid()) {
90
+ // 如果严格解析失败,尝试宽松解析
91
+ return timeLib(value);
92
+ }
93
+ return parsed;
94
+ } catch {
95
+ return null;
96
+ }
97
+ }, [value, format]);
98
+
152
99
  const handleChange = useCallback(
153
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
- (date: any) => {
155
- const formattedValue = formatValue(date, format);
156
- onChange?.(formattedValue);
100
+ (_date: any, dateString: string | string[]) => {
101
+ const stringValue = Array.isArray(dateString) ? dateString[0] : dateString;
102
+ onChange?.(stringValue || null);
157
103
  },
158
- [onChange, format]
104
+ [onChange]
159
105
  );
160
106
 
161
- // 将字符串值转换为时间对象
162
- const dateValue = parseValue(value, format);
163
-
164
107
  return (
165
108
  <TimeFieldContainer>
166
109
  <DatePicker
@@ -171,10 +114,10 @@ export const TimeField: React.FC<TimeFieldProps> = ({
171
114
  showTime={showTime ? { format: 'HH:mm:ss' } : false}
172
115
  disabled={disabled}
173
116
  style={{ width: '100%' }}
174
- placeholder={`请选择`}
117
+ placeholder="请选择"
175
118
  />
176
119
  </TimeFieldContainer>
177
120
  );
178
121
  };
179
122
 
180
- export default TimeField;
123
+ export default TimeField;