StdDataEntry.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <script setup lang="tsx">
  2. import { Form } from 'ant-design-vue'
  3. import type { Ref } from 'vue'
  4. import type { Column, JSXElements, StdDesignEdit } from '@/components/StdDesign/types'
  5. import StdFormItem from '@/components/StdDesign/StdDataEntry/StdFormItem.vue'
  6. import { labelRender } from '@/components/StdDesign/StdDataEntry'
  7. const props = defineProps<{
  8. dataList: Column[]
  9. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  10. dataSource: Record<string, any>
  11. errors?: Record<string, string>
  12. type?: 'search' | 'edit'
  13. layout?: 'horizontal' | 'vertical' | 'inline'
  14. }>()
  15. const emit = defineEmits<{
  16. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  17. 'update:dataSource': [data: Record<string, any>]
  18. }>()
  19. const dataSource = computed({
  20. get() {
  21. return props.dataSource
  22. },
  23. set(v) {
  24. emit('update:dataSource', v)
  25. },
  26. })
  27. const slots = useSlots()
  28. function extraRender(extra?: string | (() => string)) {
  29. if (typeof extra === 'function')
  30. return extra()
  31. return extra
  32. }
  33. const formRef = ref<InstanceType<typeof Form>>()
  34. defineExpose({
  35. formRef,
  36. })
  37. function Render() {
  38. const template: JSXElements = []
  39. const isCreate = inject<Ref<string>>('editMode', ref(''))?.value === 'create'
  40. props.dataList.forEach((v: Column) => {
  41. const dataIndex = (v.edit?.actualDataIndex ?? v.dataIndex) as string
  42. dataSource.value[dataIndex] = props.dataSource[dataIndex]
  43. if (props.type === 'search') {
  44. if (v.search) {
  45. const type = (v.search as StdDesignEdit)?.type || v.edit?.type
  46. template.push(
  47. <StdFormItem
  48. label={labelRender(v.title)}
  49. extra={extraRender(v.extra)}
  50. error={props.errors}
  51. >
  52. {type?.(v.edit!, dataSource.value, v.dataIndex)}
  53. </StdFormItem>,
  54. )
  55. }
  56. return
  57. }
  58. // console.log(isCreate && v.hiddenInCreate, !isCreate && v.hiddenInModify)
  59. if ((isCreate && v.hiddenInCreate) || (!isCreate && v.hiddenInModify))
  60. return
  61. let show = true
  62. if (v.edit?.show && typeof v.edit.show === 'function')
  63. show = v.edit.show(props.dataSource)
  64. if (v.edit?.type && show) {
  65. template.push(
  66. <StdFormItem
  67. key={dataIndex}
  68. dataIndex={dataIndex}
  69. label={labelRender(v.title)}
  70. extra={extraRender(v.extra)}
  71. error={props.errors}
  72. required={v.edit?.config?.required}
  73. hint={v.edit?.hint}
  74. >
  75. {v.edit.type(v.edit, dataSource.value, dataIndex)}
  76. </StdFormItem>,
  77. )
  78. }
  79. })
  80. if (slots.action)
  81. template.push(<div class={'std-data-entry-action'}>{slots.action()}</div>)
  82. return <Form ref={formRef} model={dataSource.value} layout={props.layout || 'vertical'}>{template}</Form>
  83. }
  84. </script>
  85. <template>
  86. <Render />
  87. </template>
  88. <style scoped lang="less">
  89. .std-data-entry-action {
  90. @media (max-width: 375px) {
  91. display: block;
  92. width: 100%;
  93. margin: 10px 0;
  94. }
  95. }
  96. </style>