CheckBoxList.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <FlexView :direction="vertical ? 'column' : 'row'" align="center" :gap="10" wrap>
  3. <ActivityIndicator v-if="loadStatus === 'loading'" />
  4. <Alert
  5. v-else-if="loadStatus === 'error'"
  6. message="加载失败"
  7. description="点击重新加载"
  8. type="error"
  9. @click="handleLoadData"
  10. />
  11. <CheckBoxGroup
  12. v-else
  13. :modelValue="modelValue"
  14. :disabled="disabled"
  15. :multiple="multiple"
  16. @update:modelValue="handleChange"
  17. >
  18. <CheckBox
  19. v-for="value in data2"
  20. :key="value.value"
  21. :name="value.value"
  22. :text="value.text"
  23. :disabled="value.disable"
  24. />
  25. </CheckBoxGroup>
  26. </FlexView>
  27. </template>
  28. <script setup lang="ts">
  29. import ActivityIndicator from '@/components/basic/ActivityIndicator.vue';
  30. import Alert from '@/components/feedback/Alert.vue';
  31. import CheckBox from '@/components/form/CheckBox.vue';
  32. import CheckBoxGroup from '@/components/form/CheckBoxGroup.vue';
  33. import FlexView from '@/components/layout/FlexView.vue';
  34. import { onMounted, ref, type PropType } from 'vue';
  35. export interface CheckBoxListItem {
  36. text: string;
  37. value: any;
  38. disable?: boolean;
  39. }
  40. export interface CheckBoxListProps {
  41. multiple?: boolean,
  42. disabled?: boolean,
  43. vertical?: boolean,
  44. className?: string,
  45. loadData: () => Promise<CheckBoxListItem[]>;
  46. }
  47. const props = defineProps({
  48. modelValue: {
  49. type: Array as PropType<string[]>,
  50. default: () => []
  51. },
  52. loadData: {
  53. type: Function as PropType<CheckBoxListProps['loadData']>,
  54. default: () => Promise.resolve([])
  55. },
  56. disabled: {
  57. type: Boolean,
  58. default: false
  59. },
  60. multiple: {
  61. type: Boolean,
  62. default: false
  63. },
  64. className: {
  65. type: String,
  66. default: ''
  67. },
  68. vertical: {
  69. type: Boolean,
  70. default: false
  71. },
  72. })
  73. const emit = defineEmits(['update:modelValue', 'change'])
  74. const loadStatus = ref<'loading' | 'error' | 'success'>('loading');
  75. const loadError = ref('');
  76. const data2 = ref<CheckBoxListItem[]>([]);
  77. const handleChange = (checkedValues: any[]) => {
  78. emit('update:modelValue', checkedValues);
  79. emit('change', checkedValues);
  80. }
  81. const handleLoadData = () => {
  82. loadStatus.value = 'loading';
  83. loadError.value = '';
  84. props.loadData().then((v) => {
  85. data2.value = v;
  86. loadStatus.value = 'success';
  87. }).catch((e) => {
  88. loadError.value = e.message || '加载失败';
  89. loadStatus.value = 'error';
  90. });
  91. }
  92. const reload = () => {
  93. handleLoadData();
  94. }
  95. defineExpose({ reload });
  96. onMounted(() => {
  97. handleLoadData();
  98. });
  99. </script>