AccountModal.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
  3. <BasicForm @register="registerForm" />
  4. </BasicModal>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, ref, computed, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { BasicForm, useForm } from '/@/components/Form/index';
  10. import { accountFormSchema } from './account.data';
  11. import { getDeptList } from '/@/api/demo/system';
  12. export default defineComponent({
  13. name: 'AccountModal',
  14. components: { BasicModal, BasicForm },
  15. emits: ['success', 'register'],
  16. setup(_, { emit }) {
  17. const isUpdate = ref(true);
  18. const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
  19. labelWidth: 100,
  20. schemas: accountFormSchema,
  21. showActionButtonGroup: false,
  22. actionColOptions: {
  23. span: 23,
  24. },
  25. });
  26. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  27. resetFields();
  28. setModalProps({ confirmLoading: false });
  29. isUpdate.value = !!data?.isUpdate;
  30. if (unref(isUpdate)) {
  31. setFieldsValue({
  32. ...data.record,
  33. });
  34. }
  35. const treeData = await getDeptList();
  36. updateSchema([
  37. {
  38. field: 'pwd',
  39. show: !unref(isUpdate),
  40. },
  41. {
  42. field: 'dept',
  43. componentProps: { treeData },
  44. },
  45. ]);
  46. });
  47. const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
  48. async function handleSubmit() {
  49. try {
  50. const values = await validate();
  51. setModalProps({ confirmLoading: true });
  52. // TODO custom api
  53. console.log(values);
  54. closeModal();
  55. emit('success');
  56. } finally {
  57. setModalProps({ confirmLoading: false });
  58. }
  59. }
  60. return { registerModal, registerForm, getTitle, handleSubmit };
  61. },
  62. });
  63. </script>