优化枚举与字典相关组件,增加禁用与隐藏选项属性

This commit is contained in:
zhoumingfa 2025-02-23 22:23:18 +08:00
parent 81b7b2a3a4
commit e44b116184
10 changed files with 247 additions and 70 deletions

View File

@ -9,7 +9,11 @@
* *
--> -->
<template> <template>
<a-checkbox-group :style="`width: ${width}`" v-model:value="selectValue" :options="optionList" @change="handleChange" /> <a-checkbox-group :style="`width: ${width}`" v-model:value="selectValue" @change="handleChange" :disabled="disabled">
<a-checkbox v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }}
</a-checkbox>
</a-checkbox-group>
</template> </template>
<script setup> <script setup>
@ -22,19 +26,32 @@
type: String, type: String,
default: '200px', default: '200px',
}, },
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
// ------------ ------------ // ------------ ------------
const optionList = ref([]); const valueDescList = ref([]);
function buildOptionList() {
onMounted(() => {
const internalInstance = getCurrentInstance(); // const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin; const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
const valueList = smartEnumPlugin.getValueDescList(props.enumName); valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
optionList.value = valueList.map((e) => Object.assign({}, { value: e.value, label: e.desc })); });
}
onMounted(buildOptionList);
// ------------ ------------ // ------------ ------------
@ -43,11 +60,14 @@
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = newValue.filter((item) => !props.hiddenOption.includes(item) && !props.disabledOption.includes(item));
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
function handleChange(value) { function handleChange(value) {
emit('update:value', value); emit('update:value', value);
emit('change', value); emit('change', value);

View File

@ -10,15 +10,15 @@
--> -->
<template> <template>
<template v-if="isButton"> <template v-if="isButton">
<a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid"> <a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid" :disabled="disabled">
<a-radio-button v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-radio-button v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-radio-button> </a-radio-button>
</a-radio-group> </a-radio-group>
</template> </template>
<template v-else> <template v-else>
<a-radio-group v-model:value="selectValue" @change="handleChange"> <a-radio-group v-model:value="selectValue" @change="handleChange" :disabled="disabled">
<a-radio v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-radio v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-radio> </a-radio>
</a-radio-group> </a-radio-group>
@ -26,7 +26,7 @@
</template> </template>
<script setup> <script setup>
import { ref, watch } from 'vue'; import { ref, watch, onMounted, getCurrentInstance } from 'vue';
const props = defineProps({ const props = defineProps({
enumName: String, enumName: String,
@ -43,19 +43,44 @@
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
const emit = defineEmits(['update:value', 'change']); const valueDescList = ref([]);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
});
const selectValue = ref(props.value); const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = props.disabledOption.includes(newValue) || props.hiddenOption.includes(newValue) ? undefined : newValue;
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']);
function handleChange(e) { function handleChange(e) {
emit('update:value', e.target.value); emit('update:value', e.target.value);
emit('change', e.target.value); emit('change', e.target.value);

View File

@ -19,14 +19,14 @@
@change="handleChange" @change="handleChange"
:disabled="disabled" :disabled="disabled"
> >
<a-select-option v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-select-option v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-select-option> </a-select-option>
</a-select> </a-select>
</template> </template>
<script setup> <script setup>
import { ref, watch } from 'vue'; import { ref, watch, onMounted, getCurrentInstance } from 'vue';
const props = defineProps({ const props = defineProps({
enumName: String, enumName: String,
@ -43,23 +43,44 @@
type: String, type: String,
default: 'default', default: 'default',
}, },
//
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
const emit = defineEmits(['update:value', 'change']); const valueDescList = ref([]);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
});
const selectValue = ref(props.value); const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = props.disabledOption.includes(newValue) || props.hiddenOption.includes(newValue) ? undefined : newValue;
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']);
function handleChange(value) { function handleChange(value) {
emit('update:value', value); emit('update:value', value);
emit('change', value); emit('change', value);

View File

@ -18,6 +18,7 @@
:allowClear="true" :allowClear="true"
:size="size" :size="size"
@change="onChange" @change="onChange"
:disabled="disabled"
> >
<a-select-option v-for="item in dictKeyCodeList" :key="item.keyCode" :value="item.keyCode"> <a-select-option v-for="item in dictKeyCodeList" :key="item.keyCode" :value="item.keyCode">
{{ item.keyName }} {{ item.keyName }}
@ -27,7 +28,7 @@
</template> </template>
<script setup> <script setup>
import { computed, onMounted, ref, watch, defineExpose } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { dictApi } from '/@/api/support/dict-api'; import { dictApi } from '/@/api/support/dict-api';
defineExpose({ defineExpose({
@ -49,7 +50,7 @@
default: 'default', default: 'default',
}, },
// //
disabledFlag: { disabled: {
type: Number, type: Number,
default: null, default: null,
}, },

View File

@ -20,7 +20,12 @@
@change="onChange" @change="onChange"
:disabled="disabled" :disabled="disabled"
> >
<a-select-option v-for="item in dictValueList" :key="item.valueCode" :value="item.valueCode"> <a-select-option
v-for="item in dictValueList"
:key="item.valueCode"
:value="item.valueCode"
:disabled="disabledOption.includes(item.valueCode)"
>
{{ item.valueName }} {{ item.valueName }}
</a-select-option> </a-select-option>
</a-select> </a-select>
@ -28,7 +33,7 @@
</template> </template>
<script setup> <script setup>
import { computed, onMounted, ref, watch } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { dictApi } from '/@/api/support/dict-api'; import { dictApi } from '/@/api/support/dict-api';
const props = defineProps({ const props = defineProps({
@ -50,10 +55,21 @@
type: String, type: String,
default: 'default', default: 'default',
}, },
//
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
// -------------------------- -------------------------- // -------------------------- --------------------------
@ -61,7 +77,7 @@
const dictValueList = ref([]); const dictValueList = ref([]);
async function queryDict() { async function queryDict() {
let res = await dictApi.valueList(props.keyCode); let res = await dictApi.valueList(props.keyCode);
dictValueList.value = res.data; dictValueList.value = res.data.filter((item) => !props.hiddenOption.includes(item.valueCode));
} }
onMounted(queryDict); onMounted(queryDict);
@ -69,14 +85,22 @@
// -------------------------- -------------------------- // -------------------------- --------------------------
const selectValue = ref(props.value); const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(value) => { (newValue) => {
selectValue.value = value; //
if (Array.isArray(newValue)) {
selectValue.value = newValue.filter((item) => !props.disabledOption.includes(item) && !props.hiddenOption.includes(item));
} else {
selectValue.value = props.hiddenOption.includes(newValue) || props.disabledOption.includes(newValue) ? undefined : newValue;
} }
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
function onChange(value) { function onChange(value) {
if (!value) { if (!value) {
emit('update:value', []); emit('update:value', []);
@ -87,8 +111,8 @@
emit('update:value', value); emit('update:value', value);
emit('change', value); emit('change', value);
} else { } else {
emit('update:value', value); emit('update:value', [value]);
emit('change', value); emit('change', [value]);
} }
} }
</script> </script>

View File

@ -9,7 +9,11 @@
* *
--> -->
<template> <template>
<a-checkbox-group :style="`width: ${width}`" v-model:value="selectValue" :options="optionList" @change="handleChange" /> <a-checkbox-group :style="`width: ${width}`" v-model:value="selectValue" @change="handleChange" :disabled="disabled">
<a-checkbox v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }}
</a-checkbox>
</a-checkbox-group>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -22,19 +26,32 @@
type: String, type: String,
default: '200px', default: '200px',
}, },
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
// ------------ ------------ // ------------ ------------
const optionList = ref([]); const valueDescList = ref([]);
function buildOptionList() {
onMounted(() => {
const internalInstance = getCurrentInstance(); // const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin; const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
const valueList = smartEnumPlugin.getValueDescList(props.enumName); valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
optionList.value = valueList.map((e) => Object.assign({}, { value: e.value, label: e.desc })); });
}
onMounted(buildOptionList);
// ------------ ------------ // ------------ ------------
@ -43,11 +60,14 @@
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = newValue.filter((item) => !props.hiddenOption.includes(item) && !props.disabledOption.includes(item));
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
function handleChange(value) { function handleChange(value) {
emit('update:value', value); emit('update:value', value);
emit('change', value); emit('change', value);

View File

@ -10,15 +10,15 @@
--> -->
<template> <template>
<template v-if="isButton"> <template v-if="isButton">
<a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid"> <a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid" :disabled="disabled">
<a-radio-button v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-radio-button v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-radio-button> </a-radio-button>
</a-radio-group> </a-radio-group>
</template> </template>
<template v-else> <template v-else>
<a-radio-group v-model:value="selectValue" @change="handleChange"> <a-radio-group v-model:value="selectValue" @change="handleChange" :disabled="disabled">
<a-radio v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-radio v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-radio> </a-radio>
</a-radio-group> </a-radio-group>
@ -26,7 +26,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from 'vue'; import { ref, watch, onMounted, getCurrentInstance } from 'vue';
const props = defineProps({ const props = defineProps({
enumName: String, enumName: String,
@ -43,17 +43,42 @@
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
});
const valueDescList = ref([]);
const selectValue = ref(props.value);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
}); });
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = props.disabledOption.includes(newValue) || props.hiddenOption.includes(newValue) ? undefined : newValue;
},
{ immediate: true }
); );
function handleChange(e) { function handleChange(e) {

View File

@ -19,14 +19,14 @@
@change="handleChange" @change="handleChange"
:disabled="disabled" :disabled="disabled"
> >
<a-select-option v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value"> <a-select-option v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }} {{ item.desc }}
</a-select-option> </a-select-option>
</a-select> </a-select>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from 'vue'; import { ref, watch, onMounted, getCurrentInstance} from 'vue';
const props = defineProps({ const props = defineProps({
enumName: String, enumName: String,
@ -43,21 +43,42 @@
type: String, type: String,
default: 'default', default: 'default',
}, },
//
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
});
const valueDescList = ref([]);
const selectValue = ref(props.value);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
}); });
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(newValue) => { (newValue) => {
selectValue.value = newValue; //
} selectValue.value = props.disabledOption.includes(newValue) || props.hiddenOption.includes(newValue) ? undefined : newValue;
},
{ immediate: true }
); );
function handleChange(value) { function handleChange(value) {

View File

@ -18,6 +18,7 @@
:allowClear="true" :allowClear="true"
:size="size" :size="size"
@change="onChange" @change="onChange"
:disabled="disabled"
> >
<a-select-option v-for="item in dictKeyCodeList" :key="item.keyCode" :value="item.keyCode"> <a-select-option v-for="item in dictKeyCodeList" :key="item.keyCode" :value="item.keyCode">
{{ item.keyName }} {{ item.keyName }}
@ -27,7 +28,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, onMounted, ref, watch, defineExpose } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { dictApi } from '/@/api/support/dict-api'; import { dictApi } from '/@/api/support/dict-api';
defineExpose({ defineExpose({
@ -49,7 +50,7 @@
default: 'default', default: 'default',
}, },
// //
disabledFlag: { disabled: {
type: Number, type: Number,
default: null, default: null,
}, },

View File

@ -20,7 +20,7 @@
@change="onChange" @change="onChange"
:disabled="disabled" :disabled="disabled"
> >
<a-select-option v-for="item in dictValueList" :key="item.valueCode" :value="item.valueCode"> <a-select-option v-for="item in dictValueList" :key="item.valueCode" :value="item.valueCode" :disabled="disabledOption.includes(item.valueCode)">
{{ item.valueName }} {{ item.valueName }}
</a-select-option> </a-select-option>
</a-select> </a-select>
@ -28,7 +28,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { dictApi } from '/@/api/support/dict-api'; import { dictApi } from '/@/api/support/dict-api';
const props = defineProps({ const props = defineProps({
@ -50,10 +50,21 @@
type: String, type: String,
default: 'default', default: 'default',
}, },
//
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
}); });
// -------------------------- -------------------------- // -------------------------- --------------------------
@ -61,7 +72,7 @@
const dictValueList = ref([]); const dictValueList = ref([]);
async function queryDict() { async function queryDict() {
let res = await dictApi.valueList(props.keyCode); let res = await dictApi.valueList(props.keyCode);
dictValueList.value = res.data; dictValueList.value = res.data.filter((item) => !props.hiddenOption.includes(item.valueCode));
} }
onMounted(queryDict); onMounted(queryDict);
@ -69,14 +80,22 @@
// -------------------------- -------------------------- // -------------------------- --------------------------
const selectValue = ref(props.value); const selectValue = ref(props.value);
watch( watch(
() => props.value, () => props.value,
(value) => { (newValue) => {
selectValue.value = value; //
if (Array.isArray(newValue)) {
selectValue.value = newValue.filter((item) => !props.disabledOption.includes(item) && !props.hiddenOption.includes(item));
} else {
selectValue.value = props.hiddenOption.includes(newValue) || props.disabledOption.includes(newValue) ? undefined : newValue;
} }
},
{ immediate: true }
); );
const emit = defineEmits(['update:value', 'change']); const emit = defineEmits(['update:value', 'change']);
function onChange(value) { function onChange(value) {
if (!value) { if (!value) {
emit('update:value', []); emit('update:value', []);
@ -87,8 +106,8 @@
emit('update:value', value); emit('update:value', value);
emit('change', value); emit('change', value);
} else { } else {
emit('update:value', value); emit('update:value', [value]);
emit('change', value); emit('change', [value]);
} }
} }
</script> </script>