@lark-apaas/client-toolkit 1.1.13-userprofile-test.0 → 1.1.13-userprofile-test.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.
@@ -1,9 +1,13 @@
1
1
  import React from 'react';
2
2
  import type { IUserProfile } from '../../apis/udt-types';
3
3
  export interface UserSelectProps {
4
- value?: IUserProfile | IUserProfile[];
5
- onChange?: (value: IUserProfile | IUserProfile[]) => void;
6
- defaultValue?: IUserProfile | IUserProfile[];
4
+ /**
5
+ * 支持传入完整的用户资料(IUserProfile)/ 列表(IUserProfile[]
6
+ * 或仅传入用户 ID(string)/ 列表(string[])。
7
+ */
8
+ value?: IUserProfile | IUserProfile[] | string | string[];
9
+ onChange?: (value: IUserProfile | IUserProfile[] | string | string[]) => void;
10
+ defaultValue?: IUserProfile | IUserProfile[] | string | string[];
7
11
  mode?: 'single' | 'multiple';
8
12
  placeholder?: string;
9
13
  fetchUsers?: (keyword: string) => Promise<IUserProfile[]>;
@@ -3,6 +3,127 @@ import react from "react";
3
3
  import { getDataloom } from "../../integrations/dataloom.js";
4
4
  import { UserSelectUI } from "./UserSelectUI/index.js";
5
5
  const UserSelect = ({ mode = 'single', defaultValue, value, onChange, placeholder, fetchUsers })=>{
6
+ const normalizedIds = react.useMemo(()=>{
7
+ const src = value ?? defaultValue;
8
+ if (!src) return [];
9
+ if (Array.isArray(src)) {
10
+ if (0 === src.length) return [];
11
+ const first = src[0];
12
+ const isStringArray = 'string' == typeof first;
13
+ return isStringArray ? src.filter(Boolean).map((id)=>String(id)) : src.map((u)=>String(u.user_id)).filter(Boolean);
14
+ }
15
+ return 'string' == typeof src ? [
16
+ String(src)
17
+ ] : [
18
+ String(src.user_id)
19
+ ].filter(Boolean);
20
+ }, [
21
+ value,
22
+ defaultValue
23
+ ]);
24
+ const inputProfilesMap = react.useMemo(()=>{
25
+ const map = new Map();
26
+ const src = value ?? defaultValue;
27
+ if (!src) return map;
28
+ if (Array.isArray(src)) {
29
+ const first = src[0];
30
+ const isStringArray = 'string' == typeof first;
31
+ if (!isStringArray) {
32
+ for (const u of src)if (u?.user_id) map.set(String(u.user_id), {
33
+ user_id: String(u.user_id),
34
+ name: u?.name ?? '',
35
+ avatar: u?.avatar ?? '',
36
+ email: u?.email ?? '',
37
+ status: u?.status ?? 1
38
+ });
39
+ }
40
+ } else if ('string' != typeof src) {
41
+ const u = src;
42
+ if (u?.user_id) map.set(String(u.user_id), {
43
+ user_id: String(u.user_id),
44
+ name: u?.name ?? '',
45
+ avatar: u?.avatar ?? '',
46
+ email: u?.email ?? '',
47
+ status: u?.status ?? 1
48
+ });
49
+ }
50
+ return map;
51
+ }, [
52
+ value,
53
+ defaultValue
54
+ ]);
55
+ const [uiValue, setUiValue] = react.useState(()=>{
56
+ if (!normalizedIds.length) return;
57
+ const profiles = normalizedIds.map((id)=>inputProfilesMap.get(id) ?? {
58
+ user_id: id,
59
+ name: '',
60
+ avatar: '',
61
+ email: '',
62
+ status: 1
63
+ });
64
+ return 'single' === mode ? profiles[0] : profiles;
65
+ });
66
+ react.useEffect(()=>{
67
+ if (!normalizedIds.length) return void setUiValue(void 0);
68
+ const fetchProfiles = async ()=>{
69
+ try {
70
+ const ids = normalizedIds.map((id)=>Number(id)).filter((id)=>Number.isFinite(id));
71
+ if (!ids.length) {
72
+ const profiles = normalizedIds.map((id)=>inputProfilesMap.get(id) ?? {
73
+ user_id: id,
74
+ name: '',
75
+ avatar: '',
76
+ email: '',
77
+ status: 1
78
+ });
79
+ setUiValue('single' === mode ? profiles[0] : profiles);
80
+ return;
81
+ }
82
+ const dataloom = await getDataloom();
83
+ const response = await dataloom.service.user.getByIds(ids);
84
+ const fetchedList = Array.isArray(response?.data) ? response?.data : Array.isArray(response?.data?.user_list) ? response?.data?.user_list : [];
85
+ const fetchedMap = new Map();
86
+ fetchedList.forEach((profile)=>{
87
+ const id = String(profile?.user_id ?? '');
88
+ if (!id) return;
89
+ fetchedMap.set(id, {
90
+ user_id: id,
91
+ name: profile?.name ?? '',
92
+ avatar: profile?.avatar ?? '',
93
+ email: profile?.email ?? '',
94
+ status: profile?.status ?? 1
95
+ });
96
+ });
97
+ const merged = normalizedIds.map((id)=>{
98
+ const fetched = fetchedMap.get(id);
99
+ const given = inputProfilesMap.get(id);
100
+ return {
101
+ user_id: id,
102
+ name: given?.name?.trim() ? given.name : fetched?.name ?? '',
103
+ avatar: given?.avatar || fetched?.avatar || '',
104
+ email: given?.email || fetched?.email || '',
105
+ status: given?.status ?? fetched?.status ?? 1
106
+ };
107
+ });
108
+ setUiValue('single' === mode ? merged[0] : merged);
109
+ } catch (error) {
110
+ console.error('Failed to resolve select value profiles:', error);
111
+ const profiles = normalizedIds.map((id)=>inputProfilesMap.get(id) ?? {
112
+ user_id: id,
113
+ name: '',
114
+ avatar: '',
115
+ email: '',
116
+ status: 1
117
+ });
118
+ setUiValue('single' === mode ? profiles[0] : profiles);
119
+ }
120
+ };
121
+ fetchProfiles();
122
+ }, [
123
+ normalizedIds,
124
+ inputProfilesMap,
125
+ mode
126
+ ]);
6
127
  const fetchUsersImpl = react.useCallback(async (search)=>{
7
128
  if (fetchUsers) return fetchUsers(search);
8
129
  try {
@@ -30,15 +151,23 @@ const UserSelect = ({ mode = 'single', defaultValue, value, onChange, placeholde
30
151
  return /*#__PURE__*/ jsx("div", {
31
152
  className: "user-select-container",
32
153
  children: /*#__PURE__*/ jsx(UserSelectUI, {
33
- defaultValue: defaultValue,
154
+ defaultValue: void 0,
34
155
  mode: selectMode,
35
156
  onSearch: fetchUsersImpl,
36
157
  allowClear: true,
37
158
  placeholder: placeholder,
38
- value: value,
39
- onChange: (value)=>{
40
- if ('multiple' === selectMode) onChange?.(value ?? []);
41
- else onChange?.(value);
159
+ value: uiValue,
160
+ onChange: (next)=>{
161
+ if (!onChange) return;
162
+ const nextArray = Array.isArray(next) ? next : next ? [
163
+ next
164
+ ] : [];
165
+ const isIdInput = 'string' == typeof (value ?? defaultValue) || Array.isArray(value ?? defaultValue) && 'string' == typeof (value ?? defaultValue)?.[0];
166
+ if ('multiple' === selectMode) onChange(isIdInput ? nextArray.map((u)=>u.user_id) : nextArray);
167
+ else {
168
+ const single = nextArray[0];
169
+ onChange(isIdInput ? single ? single.user_id : '' : single ?? void 0);
170
+ }
42
171
  }
43
172
  })
44
173
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.1.13-userprofile-test.0",
3
+ "version": "1.1.13-userprofile-test.1",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [