useSearch.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import type { Form, FormExpose } from '@/components/Form'
  2. import type { ElForm, ElFormItem } from 'element-plus'
  3. import { ref, unref, nextTick } from 'vue'
  4. import { FormSchema, FormSetProps, FormProps } from '@/components/Form'
  5. export const useSearch = () => {
  6. // Search实例
  7. const formRef = ref<typeof Form & FormExpose>()
  8. // ElForm实例
  9. const elFormRef = ref<ComponentRef<typeof ElForm>>()
  10. /**
  11. * @param ref Form实例
  12. * @param elRef ElForm实例
  13. */
  14. const register = (ref: typeof Form & FormExpose, elRef: ComponentRef<typeof ElForm>) => {
  15. formRef.value = ref
  16. elFormRef.value = elRef
  17. }
  18. const getForm = async () => {
  19. await nextTick()
  20. const form = unref(formRef)
  21. if (!form) {
  22. console.error('The Search is not registered. Please use the register method to register')
  23. }
  24. return form
  25. }
  26. // 一些内置的方法
  27. const methods = {
  28. /**
  29. * @description 设置form组件的props
  30. * @param field FormItem的field
  31. */
  32. setProps: async (props: FormProps = {}) => {
  33. const form = await getForm()
  34. form?.setProps(props)
  35. if (props.model) {
  36. form?.setValues(props.model)
  37. }
  38. },
  39. /**
  40. * @description 设置form的值
  41. * @param data 需要设置的数据
  42. */
  43. setValues: async (data: Recordable) => {
  44. const form = await getForm()
  45. form?.setValues(data)
  46. },
  47. /**
  48. * @description 设置schema
  49. * @param schemaProps 需要设置的schemaProps
  50. */
  51. setSchema: async (schemaProps: FormSetProps[]) => {
  52. const form = await getForm()
  53. form?.setSchema(schemaProps)
  54. },
  55. /**
  56. * @description 新增schema
  57. * @param formSchema 需要新增数据
  58. * @param index 在哪里新增
  59. */
  60. addSchema: async (formSchema: FormSchema, index?: number) => {
  61. const form = await getForm()
  62. form?.addSchema(formSchema, index)
  63. },
  64. /**
  65. * @description 删除schema
  66. * @param field 删除哪个数据
  67. */
  68. delSchema: async (field: string) => {
  69. const form = await getForm()
  70. form?.delSchema(field)
  71. },
  72. /**
  73. * @description 获取表单数据
  74. * @returns form data
  75. */
  76. getFormData: async <T = Recordable>(): Promise<T> => {
  77. const form = await getForm()
  78. return form?.formModel as T
  79. },
  80. /**
  81. * @description 获取表单组件的实例
  82. * @param field 表单项唯一标识
  83. * @returns component instance
  84. */
  85. getComponentExpose: async (field: string) => {
  86. const form = await getForm()
  87. return form?.getComponentExpose(field)
  88. },
  89. /**
  90. * @description 获取formItem组件的实例
  91. * @param field 表单项唯一标识
  92. * @returns formItem instance
  93. */
  94. getFormItemExpose: async (field: string) => {
  95. const form = await getForm()
  96. return form?.getFormItemExpose(field) as ComponentRef<typeof ElFormItem>
  97. },
  98. /**
  99. * @description 获取ElForm组件的实例
  100. * @returns ElForm instance
  101. */
  102. getElFormExpose: async () => {
  103. await getForm()
  104. return unref(elFormRef)
  105. }
  106. }
  107. return {
  108. register,
  109. methods
  110. }
  111. }