index.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <cip-dialog v-model="visible"
  3. :title="title"
  4. :width="width"
  5. :size="size"
  6. :destroy-on-close="true"
  7. :show-close="showClose"
  8. :show-cancel="showCancel"
  9. :confirmText="confirmText"
  10. :fullscreen="fullscreen"
  11. :close-on-press-escape="closeOnPressEscape"
  12. :on-confirm="confirm">
  13. <cip-form ref="formRef"
  14. :label-width="labelWidth"
  15. v-model:model="innerModel"
  16. :grid="grid"
  17. :field-list="formFieldList" />
  18. </cip-dialog>
  19. </template>
  20. <script>
  21. import { defineComponent, ref, watch } from 'vue'
  22. import CipDialog from '../cip-dialog'
  23. import CipForm from '../cip-form'
  24. import { ElLoading } from 'element-plus'
  25. export default defineComponent({
  26. name: 'CipFormDialog',
  27. components: { CipDialog, CipForm },
  28. inheritAttrs: false,
  29. props: {
  30. title: { type: String, required: true },
  31. model: { type: Object, default: () => ({}) },
  32. width: { type: String },
  33. size: String, // 窗口大小
  34. formFieldList: { type: Array, default: () => [] },
  35. labelWidth: { type: String },
  36. onConfirm: { type: Function, default: (resolve, reject, model) => { resolve(model) } },
  37. confirmText: String,
  38. showClose: { },
  39. showCancel: {},
  40. fullscreen: {},
  41. grid: {},
  42. closeOnPressEscape: { type: Boolean, default: true }
  43. },
  44. emits: ['update:model', 'action', 'vanish'],
  45. setup (props, { emit }) {
  46. const visible = ref(false)
  47. const confirm = (resolve, reject) => {
  48. props.onConfirm((data) => {
  49. onAction('resolve', data)
  50. resolve() // 关闭弹窗
  51. unwatch() // 取消监听事件
  52. onVanish() // 销毁组件
  53. }, (err) => {
  54. reject(err)
  55. }, innerModel.value)
  56. }
  57. const unwatch = watch(visible, (val) => {
  58. if (val === false) {
  59. onAction('close')
  60. onVanish()
  61. }
  62. })
  63. const innerModel = ref({ ...props.model })
  64. // 如果这样写,会导致即使取消外层传入的model数据依然被修改的问题
  65. // const { model: innerModel } = toRefs(props)
  66. const onAction = (action, data) => {
  67. emit('action', action, data)
  68. }
  69. const onVanish = () => {
  70. emit('vanish')
  71. }
  72. return {
  73. innerModel,
  74. visible,
  75. confirm
  76. }
  77. }
  78. })
  79. </script>