index.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. export default defineComponent({
  25. name: 'CipFormDialog',
  26. components: { CipDialog, CipForm },
  27. inheritAttrs: false,
  28. props: {
  29. title: { type: String, required: true },
  30. model: { type: Object, default: () => ({}) },
  31. width: { type: String },
  32. size: String, // 窗口大小
  33. formFieldList: { type: Array, default: () => [] },
  34. labelWidth: { type: String },
  35. onConfirm: { type: Function, default: (resolve, reject, model) => { resolve(model) } },
  36. confirmText: String,
  37. showClose: { },
  38. showCancel: {},
  39. fullscreen: {},
  40. grid: {},
  41. closeOnPressEscape: { type: Boolean, default: true }
  42. },
  43. emits: ['update:model', 'action', 'vanish'],
  44. setup (props, { emit }) {
  45. const visible = ref(false)
  46. const confirm = (resolve, reject) => {
  47. props.onConfirm((data) => {
  48. onAction('resolve', data)
  49. resolve() // 关闭弹窗
  50. unwatch() // 取消监听事件
  51. onVanish() // 销毁组件
  52. }, (err) => {
  53. reject(err)
  54. }, innerModel.value)
  55. }
  56. const unwatch = watch(visible, (val) => {
  57. if (val === false) {
  58. onAction('close')
  59. onVanish()
  60. }
  61. })
  62. const innerModel = ref({ ...props.model })
  63. // 如果这样写,会导致即使取消外层传入的model数据依然被修改的问题
  64. // const { model: innerModel } = toRefs(props)
  65. const onAction = (action, data) => {
  66. emit('action', action, data)
  67. }
  68. const onVanish = () => {
  69. emit('vanish')
  70. }
  71. return {
  72. innerModel,
  73. visible,
  74. confirm
  75. }
  76. }
  77. })
  78. </script>