All files / src types.ts

100% Statements 125/125
100% Branches 1/1
100% Functions 0/0
100% Lines 125/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1261x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import type { RuleItem } from 'async-validator'
import type { Component, Ref, StyleValue } from 'vue'
import type { LabelPoistion } from './inject'
 
export type ObjectModel = Record<string, any>
 
export enum FormItemType {
  /**
   * 自定义组件
   */
  CUSTOM = 'CUSTOM',
 
  /**
   * 预设组件,通过provide/inject 注册
   */
  PREDEFINE = 'PREDEFINE',
}
 
/**
 * 表单项基础数据
 */
export type BaseFormItemData<T extends FormItemType> = {
  /**
   * 类型
   */
  type: T | `${T}`
 
  /**
   * 唯一key
   */
  key: string
 
  /**
   * 初始值
   */
  initial?: number | string | Function
 
  /**
   * 是否必填
   */
  rules?: RuleItem | RuleItem[]
 
  /**
   * label
   */
  label?: string
 
  /**
   * label
   */
  placeholder?: string
 
  /**
   * FormItem 样式
   */
  itemClass?: string
  itemStyle?: StyleValue
  labelPosition?: LabelPoistion
 
  disabled?: boolean
  modelValueKey?: string
 
  /**
   * [modelValueKey]: your model key
   */
  modelKeyMap?: Record<string, string>
 
  attrs?: Record<string, any>
 
  /**
   * 自定义属性
   */
  // [x: string | symbol | number]: any
}
 
export type FormItemData = CustomCompData | PreDefineCompData
 
/**
 * 表单提交结果
 */
 
export type FormSchema = FormItemData[]
 
export interface CustomCompData<C = any>
  extends BaseFormItemData<FormItemType.CUSTOM> {
  component: C
  props?: C extends Component<infer P>
  ? P extends { $props: infer Props }
  ? MaybeRefOpions<Omit<Props, 'modelValue'>>
  : any
  : any
  slots?: SlotType<C>
  slot?: DefaultSlot<SlotType<C>>
}
 
export type MaybeRefOpions<T> = {
  [P in keyof T]?: T[P] | Ref<T[P]>
}
 
export interface PreDefineCompData<Alias extends string = any, C = any>
  extends BaseFormItemData<FormItemType.PREDEFINE> {
  component: C
  props?: C extends Component<infer P>
  ? P extends { $props: infer Props }
  ? MaybeRefOpions<Omit<Props, 'modelValue'>>
  : any
  : any
  slots?: SlotType<C>
  slot?: DefaultSlot<SlotType<C>>
  alias: Alias
}
 
type SlotType<C> = C extends Component<infer P>
  ? P extends { $slots: infer Slots }
  ? Convert<Required<Slots>>
  : any
  : any
 
type Convert<M> = {
  [K in keyof M]?: M[K] extends (...args: any[]) => any
  ? Component<Parameters<M[K]>[0]> | string
  : Component | string
}
 
type DefaultSlot<S> = S extends { [x: string]: any } ? S['default'] : never