Files
certd/packages/ui/certd-client/src/components/plugins/common/remote-select.vue
T

85 lines
1.6 KiB
Vue
Raw Normal View History

2024-09-27 02:15:41 +08:00
<script setup lang="ts">
import { ComponentPropsType, doRequest } from "/@/components/plugins/lib";
import { ref, watch } from "vue";
defineOptions({
name: "RemoteSelect"
});
2024-09-27 02:15:41 +08:00
const props = defineProps<
{
watches: string[];
} & ComponentPropsType
>();
const emit = defineEmits<{
"update:value": any;
}>();
const optionsRef = ref([]);
2024-10-03 01:29:12 +08:00
const message = ref("");
2024-09-27 02:15:41 +08:00
const getOptions = async () => {
2024-10-03 01:29:12 +08:00
return await doRequest(
{
type: props.type,
typeName: props.typeName,
action: props.action,
input: props.form
},
{
onError(err) {
message.value = err.message;
}
}
);
2024-09-27 02:15:41 +08:00
};
2024-09-30 18:00:51 +08:00
const filterOption = (input: string, option: any) => {
return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0 || String(option.value).toLowerCase().indexOf(input.toLowerCase());
};
let isFirst = true;
async function onClick() {
if (!isFirst) {
return;
}
isFirst = false;
optionsRef.value = await getOptions();
}
2024-09-27 02:15:41 +08:00
watch(
() => {
const values = [];
for (const item of props.watches) {
values.push(props.form[item]);
}
return {
form: props.form,
watched: values
};
},
async () => {
optionsRef.value = await getOptions();
2024-09-30 18:00:51 +08:00
}
2024-09-27 02:15:41 +08:00
);
</script>
<template>
2024-10-03 01:29:12 +08:00
<div>
<a-select
class="remote-select"
show-search
:filter-option="filterOption"
:options="optionsRef"
:value="value"
@click="onClick"
@update:value="emit('update:value', $event)"
/>
2024-10-03 01:29:12 +08:00
<div class="helper">
{{ message }}
</div>
</div>
2024-09-27 02:15:41 +08:00
</template>
<style lang="less"></style>