TitleCommon.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div>
  3. <div class="form-tip">{{ '提示:' + getTipByTab(props.tab) }}</div>
  4. <el-form inline ref="ruleFormRef" :model="form">
  5. <el-form-item style="margin-top: 0px">
  6. <el-input
  7. v-model="form.name"
  8. :placeholder="`请输入` + props.type + `名称`"
  9. class="input-with-select"
  10. >
  11. <template #append>
  12. <el-button :icon="Search" @click="findDataByName(ruleFormRef)" />
  13. </template>
  14. </el-input>
  15. <!-- <el-input style="width: 200px" :placeholder="`请输入` + props.type + `名称`" v-model="form.name" clearable
  16. @clear="clearData" /> -->
  17. </el-form-item>
  18. <!-- <el-button type="primary" :icon="Search" @click="findDataByName(ruleFormRef)">查询</el-button> -->
  19. <el-button
  20. type="primary"
  21. :icon="Plus"
  22. @click="addForm"
  23. style="position: absolute; right: 0; width: 100px"
  24. >
  25. {{ props.type }}</el-button
  26. >
  27. </el-form>
  28. </div>
  29. <el-drawer
  30. v-model="titleDrawer"
  31. direction="rtl"
  32. :title="props.type === '模板' ? '添加模板' : '添加标签'"
  33. :before-close="handleClose"
  34. >
  35. <DrawerCommonVue :detail="drawerFormData" :type="props.type" @submit-drawer="submitDrawer" />
  36. </el-drawer>
  37. </template>
  38. <script setup lang="ts">
  39. import { Search, Plus } from '@element-plus/icons-vue';
  40. import { onMounted, ref, watch } from 'vue';
  41. import { ElMessageBox, FormInstance } from 'element-plus';
  42. import DrawerCommonVue, { FormModelCommon } from './DrawerCommon.vue';
  43. import { useKeyPress } from 'vue-hooks-plus';
  44. import { LabelType, getTipByTab } from './constant';
  45. export type CreateType = '添加模板' | '修改模板';
  46. const props = defineProps<{
  47. type: string;
  48. pagesize: number;
  49. pagenumber: number;
  50. drawer: boolean;
  51. tab: LabelType;
  52. }>();
  53. const emit = defineEmits([
  54. 'findDataByName',
  55. 'submitDrawer',
  56. 'handleClose',
  57. 'addForm',
  58. 'clearData',
  59. ]);
  60. const form = ref({
  61. name: '',
  62. });
  63. const ruleFormRef = ref<FormInstance>();
  64. const currentType = ref<CreateType>('添加模板');
  65. const defaultFormValue = { id: 0, code: '', name: '', remark: '', status: 0 };
  66. const drawerFormData = ref<FormModelCommon>({ ...defaultFormValue } as FormModelCommon);
  67. export interface DataCommon {
  68. name: string;
  69. size: number;
  70. page: number;
  71. }
  72. useKeyPress('enter', () => {
  73. findDataByName(ruleFormRef.value);
  74. });
  75. const titleDrawer = ref(false);
  76. watch(
  77. () => props.drawer,
  78. (newdrawer) => {
  79. titleDrawer.value = newdrawer;
  80. },
  81. { immediate: true },
  82. );
  83. // 根据名称找到数据-finish
  84. function findDataByName(formEl: FormInstance | undefined) {
  85. console.log('findDataByName');
  86. if (!formEl) return;
  87. formEl.validate((valid, fields) => {
  88. if (valid) {
  89. const data = ref<DataCommon>({
  90. name: form.value.name,
  91. size: props.pagesize,
  92. page: props.pagenumber,
  93. });
  94. emit('findDataByName', data.value);
  95. } else {
  96. console.log('error submit!', fields);
  97. }
  98. });
  99. }
  100. function addForm() {
  101. currentType.value = '添加模板';
  102. emit('addForm');
  103. }
  104. // 添加
  105. function submitDrawer(data) {
  106. emit('submitDrawer', data);
  107. setTimeout(() => {
  108. drawerFormData.value = { ...defaultFormValue };
  109. }, 500);
  110. }
  111. const handleClose = (done: () => void) => {
  112. done();
  113. emit('handleClose');
  114. };
  115. // function clearData() {
  116. // emit('clearData');
  117. // }
  118. onMounted(() => {});
  119. </script>
  120. <style scoped>
  121. .form-tip {
  122. font-family: PingFangSC, PingFang SC;
  123. font-weight: 400;
  124. font-size: 12px;
  125. color: #909399;
  126. line-height: 38px;
  127. text-align: left;
  128. font-style: normal;
  129. }
  130. </style>