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

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>
<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>
<script setup>
@ -22,19 +26,32 @@
type: String,
default: '200px',
},
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
});
// ------------ ------------
const optionList = ref([]);
function buildOptionList() {
const valueDescList = ref([]);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
const valueList = smartEnumPlugin.getValueDescList(props.enumName);
optionList.value = valueList.map((e) => Object.assign({}, { value: e.value, label: e.desc }));
}
onMounted(buildOptionList);
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
});
// ------------ ------------
@ -43,11 +60,14 @@
watch(
() => props.value,
(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']);
function handleChange(value) {
emit('update:value', value);
emit('change', value);

View File

@ -10,15 +10,15 @@
-->
<template>
<template v-if="isButton">
<a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid">
<a-radio-button v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value">
<a-radio-group v-model:value="selectValue" @change="handleChange" button-style="solid" :disabled="disabled">
<a-radio-button v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }}
</a-radio-button>
</a-radio-group>
</template>
<template v-else>
<a-radio-group v-model:value="selectValue" @change="handleChange">
<a-radio v-for="item in $smartEnumPlugin.getValueDescList(props.enumName)" :key="item.value" :value="item.value">
<a-radio-group v-model:value="selectValue" @change="handleChange" :disabled="disabled">
<a-radio v-for="item in valueDescList" :key="item.value" :value="item.value" :disabled="disabledOption.includes(item.value)">
{{ item.desc }}
</a-radio>
</a-radio-group>
@ -26,7 +26,7 @@
</template>
<script setup>
import { ref, watch } from 'vue';
import { ref, watch, onMounted, getCurrentInstance } from 'vue';
const props = defineProps({
enumName: String,
@ -43,19 +43,44 @@
type: Boolean,
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);
watch(
() => props.value,
(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) {
emit('update:value', e.target.value);
emit('change', e.target.value);

View File

@ -19,18 +19,18 @@
@change="handleChange"
: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 }}
</a-select-option>
</a-select>
</template>
<script setup>
import { ref, watch } from 'vue';
import { ref, watch, onMounted, getCurrentInstance } from 'vue';
const props = defineProps({
enumName: String,
value: [Number,String],
value: [Number, String],
width: {
type: String,
default: '100%',
@ -43,23 +43,44 @@
type: String,
default: 'default',
},
//
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);
watch(
() => props.value,
(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) {
emit('update:value', value);
emit('change', value);

View File

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

View File

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

View File

@ -9,7 +9,11 @@
*
-->
<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>
<script setup lang="ts">
@ -22,19 +26,32 @@
type: String,
default: '200px',
},
//
disabled: {
type: Boolean,
default: false,
},
//
disabledOption: {
type: Array,
default: () => [],
},
//
hiddenOption: {
type: Array,
default: () => [],
},
});
// ------------ ------------
const optionList = ref([]);
function buildOptionList() {
const valueDescList = ref([]);
onMounted(() => {
const internalInstance = getCurrentInstance(); //
const smartEnumPlugin = internalInstance.appContext.config.globalProperties.$smartEnumPlugin;
const valueList = smartEnumPlugin.getValueDescList(props.enumName);
optionList.value = valueList.map((e) => Object.assign({}, { value: e.value, label: e.desc }));
}
onMounted(buildOptionList);
valueDescList.value = smartEnumPlugin.getValueDescList(props.enumName).filter((item) => !props.hiddenOption.includes(item.value));
});
// ------------ ------------
@ -43,11 +60,14 @@
watch(
() => props.value,
(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']);
function handleChange(value) {
emit('update:value', value);
emit('change', value);

View File

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

View File

@ -19,18 +19,18 @@
@change="handleChange"
: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 }}
</a-select-option>
</a-select>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue';
import { ref, watch, onMounted, getCurrentInstance} from 'vue';
const props = defineProps({
enumName: String,
value: [Number,String],
value: [Number, String],
width: {
type: String,
default: '100%',
@ -43,21 +43,42 @@
type: String,
default: 'default',
},
//
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 selectValue = ref(props.value);
watch(
() => props.value,
(newValue) => {
selectValue.value = newValue;
}
//
selectValue.value = props.disabledOption.includes(newValue) || props.hiddenOption.includes(newValue) ? undefined : newValue;
},
{ immediate: true }
);
function handleChange(value) {

View File

@ -18,6 +18,7 @@
:allowClear="true"
:size="size"
@change="onChange"
:disabled="disabled"
>
<a-select-option v-for="item in dictKeyCodeList" :key="item.keyCode" :value="item.keyCode">
{{ item.keyName }}
@ -27,7 +28,7 @@
</template>
<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';
defineExpose({
@ -49,7 +50,7 @@
default: 'default',
},
//
disabledFlag: {
disabled: {
type: Number,
default: null,
},

View File

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