TagEdit.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <script setup lang="ts">
  2. import type { TagModel } from '@/model/system.model';
  3. import { tagEditMethod, tagMethod, tagsSearchMethod } from '@/request/api/system.api';
  4. import { useRequest } from 'alova/client';
  5. import { type VxeFormListeners, type VxeFormProps } from 'vxe-pc-ui';
  6. type FormModel = Partial<TagModel>
  7. const defaultModel = {};
  8. const props = defineProps<{ data: FormModel }>();
  9. const emits = defineEmits<{
  10. submit: [ data?: TagModel ],
  11. }>();
  12. const { loading, send: load } = useRequest(tagMethod, { immediate: false, initialData: props.data ?? defaultModel })
  13. .onSuccess(({ data }) => {
  14. formProps.data = { ...data };
  15. });
  16. const { loading: submitting, send: submit } = useRequest(tagEditMethod, { immediate: false }).onSuccess(({ data }) => {
  17. emits('submit');
  18. });
  19. const { data: tags, loading: tagsLoading } = useRequest(tagsSearchMethod, { initialData: { total: 0, data: [] } });
  20. const formProps = reactive<VxeFormProps<FormModel>>({
  21. titleWidth: 100,
  22. titleAlign: 'right',
  23. titleColon: true,
  24. data: { ...props.data },
  25. items: [
  26. { field: 'name', title: '标签名称', span: 24, itemRender: { name: 'VxeInput' } },
  27. {
  28. field: 'parentId', title: '上级标签', span: 24, itemRender: {
  29. name: 'VxeSelect',
  30. props: {
  31. loading: tagsLoading,
  32. options: computed(() => tags.value.data),
  33. optionProps: { value: 'id', label: 'name' },
  34. clearable: true, filterable: true,
  35. },
  36. },
  37. },
  38. { align: 'center', span: 24, slots: { default: 'active' } },
  39. ],
  40. rules: {
  41. name: [
  42. { required: true, message: '请输入标签名称' },
  43. ],
  44. },
  45. });
  46. const formEmits: VxeFormListeners<FormModel> = {
  47. submit({ data }) { submit(data); },
  48. reset() { formProps.data = { ...props.data }; },
  49. };
  50. onBeforeMount(() => {
  51. if ( props.data?.tagId ) load(props.data);
  52. });
  53. </script>
  54. <template>
  55. <vxe-form v-bind="formProps" v-on="formEmits" :loading>
  56. <template #active>
  57. <vxe-button type="submit" status="primary" content="提交" :loading="submitting"></vxe-button>
  58. <vxe-button type="reset" content="重置" :disabled="submitting"></vxe-button>
  59. </template>
  60. </vxe-form>
  61. </template>
  62. <style scoped lang="scss"></style>