index.vue 2.2 KB

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