model.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="register" @ok="confirm" :title="getTitle" >
  3. <BasicForm @register="registerForm" :model="model" />
  4. </BasicModal>
  5. </template>
  6. <script lang="ts">
  7. import { computed, defineComponent, reactive, ref, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { BasicForm, useForm } from '/@/components/Form/index';
  10. import { adapt } from '/@/utils/adapt'
  11. import { formSchema, } from './data'
  12. import { useMessage } from '/@/hooks/web/useMessage';
  13. interface Menu {
  14. id: string|number
  15. }
  16. export default defineComponent({
  17. components: { BasicModal, BasicForm, },
  18. setup(_, { emit }) {
  19. const { notification } = useMessage();
  20. const menu = reactive<Menu>({
  21. id: ''
  22. })
  23. const modelRef = ref({});
  24. const adaptWidth = adapt()
  25. const isUpdate = ref(true);
  26. const [registerForm, { getFieldsValue, updateSchema, setFieldsValue, resetFields/* validate*/ }] = useForm({
  27. labelWidth: 100,
  28. schemas: formSchema,
  29. showActionButtonGroup: false,
  30. actionColOptions: { span: 24 },
  31. });
  32. const [register, { closeModal } ] = useModalInner(data => {
  33. console.log('-------------------data---------------')
  34. console.log(data)
  35. resetFields()
  36. isUpdate.value = !!data?.isUpdate;
  37. // if (unref(isUpdate)) {
  38. // setFieldsValue(data);
  39. // }
  40. setFieldsValue(data);
  41. menu.id = data.id
  42. updateSchema({
  43. field: 'parent',
  44. componentProps: { treeData: data.treeData },
  45. });
  46. });
  47. const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));
  48. function confirm() {
  49. const info = getFieldsValue()
  50. if (menu.id) {
  51. info.id = menu.id
  52. if(info.id === info.parent) {
  53. notification.error({
  54. message: '错误!',
  55. description: "不能选择当前菜单作为上级菜单!",
  56. duration: 3,
  57. });
  58. return
  59. }
  60. }
  61. const data = {}
  62. for(let key in info){
  63. if (info[key]) {
  64. data[key] = info[key]
  65. }
  66. // else {
  67. // data[key] = null
  68. // }
  69. }
  70. console.log(data)
  71. console.log('-------------11--------------')
  72. emit('saveData', data)
  73. closeModal() // 关闭弹窗
  74. }
  75. return {
  76. register,
  77. registerForm,
  78. getTitle,
  79. model: modelRef,
  80. confirm,
  81. adaptWidth,
  82. };
  83. },
  84. });
  85. </script>
  86. <style lang='less'>
  87. .ant-form-item-label {
  88. text-align: center !important;
  89. }
  90. .tree-label {
  91. width: 20.6%;
  92. margin-top: 8px;
  93. margin-bottom: 1em;
  94. text-align: center;
  95. }
  96. @media (max-width: 639px) {
  97. .ant-form-item-label {
  98. line-height: 2.5715 !important;
  99. text-align: center !important;
  100. }
  101. .tree-label {
  102. width: 33%;
  103. margin-top: 8px;
  104. margin-bottom: 1em;
  105. text-align: center;
  106. }
  107. }
  108. </style>