SelectGroup.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div class="select-group-container">
  3. <el-form :model="ruleForm" ref="ruleFormRef">
  4. <el-form-item label="选择分组:" prop="userGroupList" :rules="[{ required: true, message: '请选择分组' }]">
  5. <GroupSelect
  6. v-model="ruleForm.userGroupList"
  7. :groupOptions="groupOptions"
  8. @change="emits('userGroupListChange', $event)"
  9. />
  10. </el-form-item>
  11. </el-form>
  12. </div>
  13. </template>
  14. <script lang="ts" setup>
  15. import { ref, reactive, watch, onMounted } from 'vue';
  16. import GroupSelect from './components/GroupSelect.vue';
  17. import type { FormInstance } from 'element-plus';
  18. import { useGroupInfoHook } from './hook/groupInfo';
  19. const props = defineProps<{
  20. userGroupList: number[];
  21. }>();
  22. interface RuleForm {
  23. userGroupList: number[];
  24. }
  25. const { getUserGroupList, groupOptions } = useGroupInfoHook();
  26. const ruleFormRef = ref<FormInstance>();
  27. const ruleForm = reactive<RuleForm>({
  28. userGroupList: [],
  29. });
  30. const emits = defineEmits<{
  31. (e: 'userGroupListChange', userGroupList: number[]): void;
  32. }>();
  33. const validateForm = () => {
  34. return new Promise((resolve) => {
  35. ruleFormRef.value?.validate((valid) => {
  36. resolve(valid);
  37. });
  38. });
  39. };
  40. const clearValidate = () => {
  41. ruleFormRef.value?.resetFields();
  42. };
  43. defineExpose({
  44. validateForm,
  45. clearValidate,
  46. });
  47. onMounted(() => {
  48. getUserGroupList();
  49. });
  50. watch(
  51. () => props.userGroupList,
  52. (newUserGroupList) => {
  53. ruleForm.userGroupList = newUserGroupList ?? [];
  54. },
  55. { immediate: true },
  56. );
  57. </script>
  58. <style lang="scss" scoped>
  59. .select-group-container {
  60. width: 100%;
  61. }
  62. </style>