@56os/ui-components 0.0.0-stage → 0.1.0

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/README.md CHANGED
@@ -1,3 +1,63 @@
1
- # Temporary Holding Version
1
+ # @56os/ui-components
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 56os 通用 Vue 表单控件包。首批:`CustomerSelect`。
4
+
5
+ > 包内**不**绑定业务仓 `@/api`,须 `setupUiComponents` 注入。
6
+ > 检索编排仍用 `@56os/search-fields`;本包只提供可复用控件。
7
+
8
+ ## 安装
9
+
10
+ ```bash
11
+ pnpm add @56os/ui-components
12
+ # 本地联调
13
+ pnpm add @56os/ui-components@file:../56os-ui-components
14
+ ```
15
+
16
+ ## 接入
17
+
18
+ ```ts
19
+ import { setupUiComponents, CustomerSelect } from '@56os/ui-components';
20
+ import {
21
+ customerNameSelectPage,
22
+ entSupplierNameSelectPage,
23
+ fleetNameSelectPage,
24
+ supplierNameSelectPage
25
+ } from '@/api/bus/warehouseOrder';
26
+
27
+ setupUiComponents({
28
+ customerSelectApis: {
29
+ customerNameSelectPage,
30
+ entSupplierNameSelectPage,
31
+ fleetNameSelectPage,
32
+ supplierNameSelectPage
33
+ }
34
+ });
35
+
36
+ // 可选:交给 search-fields 默认渲染 customerSelect
37
+ import { setupSearchFields } from '@56os/search-fields';
38
+ setupSearchFields({
39
+ userTableConfigApi: { getConfigList, saveConfig },
40
+ customerSelectComponent: CustomerSelect
41
+ });
42
+ ```
43
+
44
+ ## CustomerSelect
45
+
46
+ | prop | 说明 |
47
+ | --- | --- |
48
+ | `partyType` | 1 供应商 / 2 客户 / 3 服务商 / 4 车队 |
49
+ | `labelMode` | `fullName` / `shortName` |
50
+ | `officeId` | 归属公司(仅供应商/客户) |
51
+ | `unitType` | 客户定价类型收窄 |
52
+
53
+ 子应用可将原 `src/components/common/CustomerSelect.vue` 改为薄 re-export:
54
+
55
+ ```ts
56
+ export { default } from '@56os/ui-components';
57
+ // 或
58
+ export { CustomerSelect as default } from '@56os/ui-components';
59
+ ```
60
+
61
+ ## 版本
62
+
63
+ `0.1.0` 首发:CustomerSelect + setupUiComponents。
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@56os/ui-components",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "56os 通用 Vue 表单控件:CustomerSelect 等;API 经 setupUiComponents 注入",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
7
+ "module": "./src/index.ts",
8
+ "types": "./src/index.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./src/index.ts",
12
+ "import": "./src/index.ts",
13
+ "default": "./src/index.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "src",
18
+ "README.md"
19
+ ],
20
+ "sideEffects": [
21
+ "**/*.vue",
22
+ "**/*.css"
23
+ ],
24
+ "peerDependencies": {
25
+ "element-plus": "^2.7.0",
26
+ "vue": "^3.4.0"
27
+ },
28
+ "keywords": [
29
+ "56os",
30
+ "ui-components",
31
+ "vue3",
32
+ "element-plus",
33
+ "CustomerSelect"
34
+ ],
35
+ "license": "UNLICENSED",
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }
@@ -0,0 +1,319 @@
1
+ <template>
2
+ <!-- 客商/车队名称下拉:API 经 setupUiComponents 注入;选项溢出悬停展示全文 -->
3
+ <el-select-v2
4
+ :key="selectRenderKey"
5
+ v-model="innerValue"
6
+ filterable
7
+ remote
8
+ :clearable="clearable"
9
+ :disabled="disabled"
10
+ :options="selectOptions"
11
+ :props="{ label: 'label', value: 'value' }"
12
+ :loading="loading"
13
+ :remote-method="onRemoteMethod"
14
+ :placeholder="placeholder"
15
+ style="width: 100%"
16
+ @change="onChange"
17
+ @clear="onClear"
18
+ >
19
+ <template #default="{ item }">
20
+ <el-tooltip
21
+ :content="item.label"
22
+ placement="right"
23
+ :show-after="300"
24
+ :disabled="!item.label"
25
+ >
26
+ <span class="customer-select-option-label">{{ item.label }}</span>
27
+ </el-tooltip>
28
+ </template>
29
+ </el-select-v2>
30
+ </template>
31
+
32
+ <script setup lang="ts">
33
+ import { computed, onMounted, ref, watch } from 'vue';
34
+ import { getCustomerSelectApis } from '../setup';
35
+
36
+ /**
37
+ * 客商/车队名称下拉
38
+ * partyType:1 供应商 / 2 客户 / 3 服务商 / 4 车队
39
+ * labelMode:fullName → 接口 type=1;shortName → type=2
40
+ * 出参统一 { label, value }
41
+ */
42
+
43
+ interface NameSelectItem {
44
+ label?: string;
45
+ value?: string | number;
46
+ [key: string]: unknown;
47
+ }
48
+
49
+ interface SelectOption {
50
+ label: string;
51
+ value: string | number;
52
+ }
53
+
54
+ /** 1 供应商 2 客户 3 服务商 4 车队 */
55
+ type PartyType = 1 | 2 | 3 | 4;
56
+
57
+ interface Props {
58
+ modelValue?: string | number | '';
59
+ partyType?: PartyType;
60
+ labelMode?: 'fullName' | 'shortName';
61
+ /** 可选:自定义远程(覆盖内置注入接口) */
62
+ remoteMethod?: (query: string) => Promise<NameSelectItem[]>;
63
+ pageSize?: number;
64
+ placeholder?: string;
65
+ clearable?: boolean;
66
+ disabled?: boolean;
67
+ /** 归属公司 id:仅 1 供应商 / 2 客户 生效;空表示按登录身份范围 */
68
+ officeId?: string | number | '';
69
+ /** 客户定价类型:0 档位定价 / 1 单一客户定价;仅 2 客户 生效,空表示不收窄 */
70
+ unitType?: string | number | '';
71
+ }
72
+
73
+ const props = withDefaults(defineProps<Props>(), {
74
+ modelValue: '',
75
+ partyType: 3,
76
+ labelMode: 'fullName',
77
+ pageSize: 50,
78
+ placeholder: '请选择',
79
+ clearable: true,
80
+ disabled: false,
81
+ officeId: '',
82
+ unitType: ''
83
+ });
84
+
85
+ const emit = defineEmits<{
86
+ 'update:modelValue': [value: string | number | ''];
87
+ change: [value: string | number | '', raw?: NameSelectItem];
88
+ }>();
89
+
90
+ const loading = ref(false);
91
+ const selectOptions = ref<SelectOption[]>([]);
92
+ const rawList = ref<NameSelectItem[]>([]);
93
+ /** options 异步补齐后递增,强制 el-select-v2 重算展示文案 */
94
+ const selectRenderKey = ref(0);
95
+ let remoteTimer: ReturnType<typeof setTimeout> | null = null;
96
+ let fetchSeq = 0;
97
+
98
+ /**
99
+ * 统一主体 id 为 string。
100
+ * el-select-v2 用 === 匹配;接口 value 常为 number、页面常为 string。
101
+ */
102
+ const normalizeSelectId = (val: string | number | '' | null | undefined): string => {
103
+ if (val === '' || val === null || val === undefined) {
104
+ return '';
105
+ }
106
+ return String(val);
107
+ };
108
+
109
+ const innerValue = computed({
110
+ get: () => normalizeSelectId(props.modelValue),
111
+ set: (val: string | number | '') => {
112
+ emit('update:modelValue', normalizeSelectId(val));
113
+ }
114
+ });
115
+
116
+ /** 全称 type=1,简称 type=2 */
117
+ const nameType = computed(() => (props.labelMode === 'shortName' ? 2 : 1));
118
+
119
+ /** 解析 TableDataInfo / 包装结构中的 rows */
120
+ const extractRows = (res: any): NameSelectItem[] => {
121
+ if (Array.isArray(res?.rows)) return res.rows;
122
+ if (Array.isArray(res?.data?.rows)) return res.data.rows;
123
+ if (Array.isArray(res?.data)) return res.data;
124
+ if (Array.isArray(res)) return res;
125
+ return [];
126
+ };
127
+
128
+ /** 仅供应商/客户支持按归属公司收窄 */
129
+ const withOffice = computed(() => props.partyType === 1 || props.partyType === 2);
130
+
131
+ const fetchNameSelectPage = async (
132
+ keywords: string,
133
+ extra: Record<string, unknown> = {}
134
+ ): Promise<NameSelectItem[]> => {
135
+ const apis = getCustomerSelectApis();
136
+ const officeId = props.officeId;
137
+ const needOffice = withOffice.value && officeId !== '' && officeId !== null && officeId !== undefined;
138
+ const officeParams = needOffice ? { officeId } : {};
139
+ const unitType = props.unitType;
140
+ const needUnitType = props.partyType === 2 && unitType !== '' && unitType !== null && unitType !== undefined;
141
+ const unitTypeParams = needUnitType ? { unitType } : {};
142
+ const params = {
143
+ type: nameType.value,
144
+ keywords: keywords || undefined,
145
+ pageNum: 1,
146
+ pageSize: props.pageSize,
147
+ ...officeParams,
148
+ ...unitTypeParams,
149
+ ...extra
150
+ };
151
+ let res: unknown;
152
+ if (props.partyType === 1) {
153
+ res = await apis.supplierNameSelectPage(params);
154
+ } else if (props.partyType === 2) {
155
+ res = await apis.customerNameSelectPage(params);
156
+ } else if (props.partyType === 4) {
157
+ res = await apis.fleetNameSelectPage(params);
158
+ } else {
159
+ res = await apis.entSupplierNameSelectPage(params);
160
+ }
161
+ return extractRows(res);
162
+ };
163
+
164
+ const mapToOptions = (list: NameSelectItem[]): SelectOption[] => {
165
+ return list
166
+ .filter((item) => item.value !== undefined && item.value !== null && item.value !== '')
167
+ .map((item) => ({
168
+ label: String(item.label ?? ''),
169
+ value: normalizeSelectId(item.value as string | number)
170
+ }));
171
+ };
172
+
173
+ const ensureSelectedOption = (next: SelectOption[]): SelectOption[] => {
174
+ const selectedVal = normalizeSelectId(props.modelValue);
175
+ if (!selectedVal) {
176
+ return next;
177
+ }
178
+ const aligned = next.map((item) =>
179
+ String(item.value) === selectedVal ? { ...item, value: selectedVal } : item
180
+ );
181
+ const exists = aligned.some((item) => item.value === selectedVal);
182
+ if (exists) return aligned;
183
+ const prev = selectOptions.value.find((item) => String(item.value) === selectedVal);
184
+ if (prev) return [{ ...prev, value: selectedVal }, ...aligned];
185
+ return aligned;
186
+ };
187
+
188
+ const loadOptions = async (query: string) => {
189
+ const seq = ++fetchSeq;
190
+ const keyword = (query || '').trim();
191
+ const showLoading = selectOptions.value.length === 0;
192
+ if (showLoading) {
193
+ loading.value = true;
194
+ }
195
+ try {
196
+ let list: NameSelectItem[] = [];
197
+ if (typeof props.remoteMethod === 'function') {
198
+ list = await props.remoteMethod(query || '');
199
+ list = Array.isArray(list) ? list : [];
200
+ } else {
201
+ list = await fetchNameSelectPage(keyword);
202
+ }
203
+ if (seq !== fetchSeq) {
204
+ return;
205
+ }
206
+ rawList.value = list;
207
+ selectOptions.value = ensureSelectedOption(mapToOptions(list));
208
+ } catch {
209
+ if (seq !== fetchSeq) {
210
+ return;
211
+ }
212
+ selectOptions.value = ensureSelectedOption([]);
213
+ } finally {
214
+ if (seq === fetchSeq) {
215
+ loading.value = false;
216
+ }
217
+ }
218
+ };
219
+
220
+ const onRemoteMethod = (query: string) => {
221
+ if (remoteTimer) {
222
+ clearTimeout(remoteTimer);
223
+ remoteTimer = null;
224
+ }
225
+ const keyword = (query || '').trim();
226
+ if (!keyword) {
227
+ loadOptions('');
228
+ return;
229
+ }
230
+ remoteTimer = setTimeout(() => loadOptions(keyword), 300);
231
+ };
232
+
233
+ const hasOption = (val: string | number | '' | null | undefined) => {
234
+ if (val === '' || val === null || val === undefined) {
235
+ return false;
236
+ }
237
+ return selectOptions.value.some((item) => String(item.value) === String(val));
238
+ };
239
+
240
+ const echoSelected = async () => {
241
+ if (props.modelValue === '' || props.modelValue == null) {
242
+ return;
243
+ }
244
+ const list = await fetchNameSelectPage('', { id: props.modelValue });
245
+ const mapped = mapToOptions(list);
246
+ const echoIds = new Set(mapped.map((item) => String(item.value)));
247
+ rawList.value = [
248
+ ...list,
249
+ ...rawList.value.filter((item) => !echoIds.has(String(item.value ?? '')))
250
+ ];
251
+ const merged = [
252
+ ...mapped,
253
+ ...selectOptions.value.filter((item) => !echoIds.has(String(item.value)))
254
+ ];
255
+ const beforeHas = hasOption(props.modelValue);
256
+ selectOptions.value = ensureSelectedOption(merged);
257
+ if (!beforeHas && mapped.some((item) => String(item.value) === String(props.modelValue))) {
258
+ selectRenderKey.value += 1;
259
+ }
260
+ };
261
+
262
+ const echoIfMissing = () => {
263
+ const val = props.modelValue;
264
+ if (val === '' || val == null) {
265
+ return;
266
+ }
267
+ if (!hasOption(val)) {
268
+ echoSelected();
269
+ }
270
+ };
271
+
272
+ const onChange = (value: string | number | '') => {
273
+ const normalized = normalizeSelectId(value);
274
+ const raw = rawList.value.find((item) => String(item.value) === normalized);
275
+ emit('change', normalized, raw);
276
+ };
277
+
278
+ const onClear = () => {
279
+ emit('update:modelValue', '');
280
+ emit('change', '');
281
+ };
282
+
283
+ watch(
284
+ () => [props.labelMode, props.partyType, props.officeId, props.unitType],
285
+ () => {
286
+ selectOptions.value = [];
287
+ if (props.modelValue !== '' && props.modelValue != null) {
288
+ echoSelected();
289
+ }
290
+ }
291
+ );
292
+
293
+ watch(
294
+ () => props.modelValue,
295
+ (val) => {
296
+ if (val === '' || val == null) {
297
+ selectOptions.value = [];
298
+ rawList.value = [];
299
+ return;
300
+ }
301
+ echoIfMissing();
302
+ }
303
+ );
304
+
305
+ onMounted(() => {
306
+ echoSelected();
307
+ });
308
+ </script>
309
+
310
+ <style lang="scss" scoped>
311
+ .customer-select-option-label {
312
+ display: inline-block;
313
+ max-width: 100%;
314
+ overflow: hidden;
315
+ text-overflow: ellipsis;
316
+ white-space: nowrap;
317
+ vertical-align: bottom;
318
+ }
319
+ </style>
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export { setupUiComponents, getCustomerSelectApis } from './setup';
2
+ export type {
3
+ CustomerSelectApis,
4
+ CustomerNameSelectParams,
5
+ SetupUiComponentsOptions
6
+ } from './setup';
7
+
8
+ export { default as CustomerSelect } from './components/CustomerSelect.vue';
package/src/setup.ts ADDED
@@ -0,0 +1,42 @@
1
+ /** nameSelectPage 类接口入参(与现网仓库 / 客商接口对齐) */
2
+ export type CustomerNameSelectParams = Record<string, unknown>;
3
+
4
+ /** 各主体名称分页接口(子应用注入,包内不绑 @/api) */
5
+ export interface CustomerSelectApis {
6
+ customerNameSelectPage: (params: CustomerNameSelectParams) => Promise<unknown>;
7
+ supplierNameSelectPage: (params: CustomerNameSelectParams) => Promise<unknown>;
8
+ entSupplierNameSelectPage: (params: CustomerNameSelectParams) => Promise<unknown>;
9
+ fleetNameSelectPage: (params: CustomerNameSelectParams) => Promise<unknown>;
10
+ }
11
+
12
+ export interface SetupUiComponentsOptions {
13
+ customerSelectApis: CustomerSelectApis;
14
+ }
15
+
16
+ let customerSelectApis: CustomerSelectApis | null = null;
17
+
18
+ /**
19
+ * 应用启动时注入客商下拉等 API。
20
+ * 包内禁止写死任一业务仓的 `@/api`。
21
+ */
22
+ export function setupUiComponents(options: SetupUiComponentsOptions) {
23
+ const apis = options?.customerSelectApis;
24
+ if (
25
+ !apis?.customerNameSelectPage ||
26
+ !apis?.supplierNameSelectPage ||
27
+ !apis?.entSupplierNameSelectPage ||
28
+ !apis?.fleetNameSelectPage
29
+ ) {
30
+ throw new Error(
31
+ '[@56os/ui-components] setupUiComponents 需要传入 customerSelectApis 四套 nameSelectPage'
32
+ );
33
+ }
34
+ customerSelectApis = apis;
35
+ }
36
+
37
+ export function getCustomerSelectApis(): CustomerSelectApis {
38
+ if (!customerSelectApis) {
39
+ throw new Error('[@56os/ui-components] 请先调用 setupUiComponents 注入 customerSelectApis');
40
+ }
41
+ return customerSelectApis;
42
+ }
@@ -0,0 +1,5 @@
1
+ declare module '*.vue' {
2
+ import type { DefineComponent } from 'vue';
3
+ const component: DefineComponent<Record<string, unknown>, Record<string, unknown>, unknown>;
4
+ export default component;
5
+ }