!45 update src/components/Dict/index.vue.
Merge pull request !45 from 如今/N/A
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
|
<!-- Select / Select-Multiple -->
|
||||||
<el-select
|
<el-select
|
||||||
v-if="type === 'select' || type === 'select-multiple'"
|
v-if="['select', 'select-multiple'].includes(type)"
|
||||||
v-model="selectedValue"
|
v-model="selectedValue"
|
||||||
:placeholder="placeholder"
|
:placeholder="placeholder"
|
||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
/>
|
/>
|
||||||
</el-select>
|
</el-select>
|
||||||
|
|
||||||
|
<!-- Radio -->
|
||||||
<el-radio-group
|
<el-radio-group
|
||||||
v-else-if="type === 'radio'"
|
v-else-if="type === 'radio'"
|
||||||
v-model="selectedValue"
|
v-model="selectedValue"
|
||||||
@@ -34,6 +36,7 @@
|
|||||||
</el-radio>
|
</el-radio>
|
||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
|
|
||||||
|
<!-- Checkbox -->
|
||||||
<el-checkbox-group
|
<el-checkbox-group
|
||||||
v-else-if="type === 'checkbox'"
|
v-else-if="type === 'checkbox'"
|
||||||
v-model="selectedValue"
|
v-model="selectedValue"
|
||||||
@@ -53,6 +56,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { ref, watch, onMounted } from "vue";
|
||||||
import { useDictStore } from "@/store";
|
import { useDictStore } from "@/store";
|
||||||
|
|
||||||
const dictStore = useDictStore();
|
const dictStore = useDictStore();
|
||||||
@@ -69,7 +73,8 @@ const props = defineProps({
|
|||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
default: "select",
|
default: "select",
|
||||||
validator: (value: string) => ["select", "radio", "checkbox"].includes(value),
|
validator: (value: string) =>
|
||||||
|
["select", "select-multiple", "radio", "checkbox"].includes(value),
|
||||||
},
|
},
|
||||||
placeholder: {
|
placeholder: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -81,11 +86,7 @@ const props = defineProps({
|
|||||||
},
|
},
|
||||||
style: {
|
style: {
|
||||||
type: Object,
|
type: Object,
|
||||||
default: () => {
|
default: () => ({ width: "300px" }),
|
||||||
return {
|
|
||||||
width: "300px",
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -93,37 +94,29 @@ const emit = defineEmits(["update:modelValue"]);
|
|||||||
|
|
||||||
const options = ref<Array<{ label: string; value: string | number }>>([]);
|
const options = ref<Array<{ label: string; value: string | number }>>([]);
|
||||||
|
|
||||||
|
// 动态初始化 selectedValue
|
||||||
const selectedValue = ref<any>(
|
const selectedValue = ref<any>(
|
||||||
["select-multiple", "checkbox"].includes(props.type)
|
["select-multiple", "checkbox"].includes(props.type)
|
||||||
? Array.isArray(props.modelValue)
|
? Array.isArray(props.modelValue)
|
||||||
? props.modelValue
|
? props.modelValue
|
||||||
: []
|
: []
|
||||||
: typeof props.modelValue === "string" || typeof props.modelValue === "number"
|
: (props.modelValue ?? undefined)
|
||||||
? props.modelValue
|
|
||||||
: undefined
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 监听 modelValue 和 options 的变化
|
// 同步 selectedValue 与 props.modelValue
|
||||||
watch(
|
watch(
|
||||||
[() => props.modelValue, () => props.type, () => options.value],
|
() => props.modelValue,
|
||||||
([newValue, newType, newOptions]) => {
|
(newValue) => {
|
||||||
if (["select-multiple", "checkbox"].includes(newType)) {
|
if (["select-multiple", "checkbox"].includes(props.type)) {
|
||||||
selectedValue.value = Array.isArray(newValue) ? newValue : [];
|
selectedValue.value = Array.isArray(newValue) ? newValue : [];
|
||||||
} else {
|
} else {
|
||||||
if (newOptions.length > 0 && newValue !== undefined && newValue !== null) {
|
selectedValue.value = newValue ?? undefined;
|
||||||
const matchedOption = newOptions.find(
|
|
||||||
(option) => String(option.value) === String(newValue)
|
|
||||||
);
|
|
||||||
selectedValue.value = matchedOption?.value ?? undefined;
|
|
||||||
} else {
|
|
||||||
selectedValue.value = undefined;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
// 监听 selectedValue 的变化并触发 update:modelValue
|
// selectedValue 改变时回传给父组件
|
||||||
function handleChange(val: any) {
|
function handleChange(val: any) {
|
||||||
emit("update:modelValue", val);
|
emit("update:modelValue", val);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user