|
@@ -24,9 +24,10 @@ let selected = ref('');
|
|
|
|
|
|
const attrs = useAttrs();
|
|
const attrs = useAttrs();
|
|
|
|
|
|
-const emit = defineEmits(['update:modelValue', 'change']);
|
|
|
|
|
|
+const emit = defineEmits(['update:modelValue', 'change', 'default-selected']);
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
modelValue: { type: [Number, String, Array], default: '' },
|
|
modelValue: { type: [Number, String, Array], default: '' },
|
|
|
|
+ defaultSelect: { type: Boolean, default: false },
|
|
filterParams: {
|
|
filterParams: {
|
|
type: Object,
|
|
type: Object,
|
|
default() {
|
|
default() {
|
|
@@ -49,16 +50,27 @@ const search = async () => {
|
|
if (!res) return;
|
|
if (!res) return;
|
|
|
|
|
|
optionList.value = res;
|
|
optionList.value = res;
|
|
|
|
+
|
|
|
|
+ if (props.defaultSelect && !props.modelValue) selectDefault();
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const selectDefault = () => {
|
|
|
|
+ const defaultOption = optionList.value[0];
|
|
|
|
+ if (defaultOption) {
|
|
|
|
+ selected.value = isMultiple.value ? [defaultOption.id] : defaultOption.id;
|
|
|
|
+ onChange();
|
|
|
|
+ emit('default-selected', defaultOption);
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
const onChange = () => {
|
|
const onChange = () => {
|
|
- const selectedData = isMultiple
|
|
|
|
|
|
+ const selectedData = isMultiple.value
|
|
? optionList.value.filter(
|
|
? optionList.value.filter(
|
|
(item) => selected.value && selected.value.includes(item.id)
|
|
(item) => selected.value && selected.value.includes(item.id)
|
|
)
|
|
)
|
|
: optionList.value.filter((item) => selected.value === item.id);
|
|
: optionList.value.filter((item) => selected.value === item.id);
|
|
emit('update:modelValue', selected.value);
|
|
emit('update:modelValue', selected.value);
|
|
- emit('change', isMultiple ? selectedData : selectedData[0]);
|
|
|
|
|
|
+ emit('change', isMultiple.value ? selectedData : selectedData[0]);
|
|
};
|
|
};
|
|
|
|
|
|
onMounted(() => {
|
|
onMounted(() => {
|
|
@@ -77,7 +89,7 @@ watch(
|
|
if (JSON.stringify(val) !== JSON.stringify(oldval)) {
|
|
if (JSON.stringify(val) !== JSON.stringify(oldval)) {
|
|
search();
|
|
search();
|
|
emit('update:modelValue', null);
|
|
emit('update:modelValue', null);
|
|
- emit('change', isMultiple ? [] : null);
|
|
|
|
|
|
+ emit('change', isMultiple.value ? [] : null);
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
{
|