gaoluyang
2026-06-26 4a71c6beb268ba17ca472f45401c8ee97846e97e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { Component } from 'vue';
 
import type { AnyPromiseFunction } from '../../../../../types/src';
 
export type ApiComponentOptionsItem = {
  [name: string]: any;
  children?: ApiComponentOptionsItem[];
  disabled?: boolean;
  label?: string;
  value?: number | string;
};
 
export type ApiComponentLabelFn = (item: ApiComponentOptionsItem) => string;
 
export interface ApiComponentProps {
  /** 组件 */
  component: Component;
  /** 是否将value从数字转为string */
  numberToString?: boolean;
  /** 获取options数据的函数 */
  api?: (arg?: any) => Promise<ApiComponentOptionsItem[] | Record<string, any>>;
  /** 传递给api的参数 */
  params?: Record<string, any>;
  /** 从api返回的结果中提取options数组的字段名 */
  resultField?: string;
  /** label字段名 */
  labelField?: string;
  /** 通过选项数据自定义label */
  labelFn?: ApiComponentLabelFn;
  /** children字段名,需要层级数据的组件可用 */
  childrenField?: string;
  /** value字段名 */
  valueField?: string;
  /** disabled字段名 */
  disabledField?: string;
  /** 组件接收options数据的属性名 */
  optionsPropName?: string;
  /** 是否立即调用api */
  immediate?: boolean;
  /** 每次`visibleEvent`事件发生时都重新请求数据 */
  alwaysLoad?: boolean;
  /** 在api请求之前的回调函数 */
  beforeFetch?: AnyPromiseFunction<any, any>;
  /** 在api请求之前的判断是否允许请求的回调函数 */
  shouldFetch?: AnyPromiseFunction<any, boolean>;
  /** 在api请求之后的回调函数 */
  afterFetch?: AnyPromiseFunction<any, any>;
  /** 直接传入选项数据,也作为api返回空数据时的后备数据 */
  options?: ApiComponentOptionsItem[];
  /** 组件的插槽名称,用来显示一个"加载中"的图标 */
  loadingSlot?: string;
  /** 触发api请求的事件名 */
  visibleEvent?: string;
  /** 组件的v-model属性名,默认为modelValue。部分组件可能为value */
  modelPropName?: string;
  /**
   * 自动选择
   * - `first`:自动选择第一个选项
   * - `last`:自动选择最后一个选项
   * - `one`: 当请求的结果只有一个选项时,自动选择该选项
   * - 函数:自定义选择逻辑,函数的参数为请求的结果数组,返回值为选择的选项
   * - false:不自动选择(默认)
   */
  autoSelect?:
    | 'first'
    | 'last'
    | 'one'
    | ((item: ApiComponentOptionsItem[]) => ApiComponentOptionsItem)
    | false;
}
 
export type ApiComponentSharedProps = Omit<ApiComponentProps, 'component'>;