fix(components): edit findUser.vue
allow `initialOption` prop to be a number if it is, fetch username via api to provide as the select element's label
This commit is contained in:
parent
74722ddfd6
commit
72fa767db0
@ -1,24 +1,40 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { debounce } from "lodash-es";
|
import { debounce } from "lodash-es";
|
||||||
import { useField } from "vee-validate";
|
import { useField } from "vee-validate";
|
||||||
|
import { IUser } from "@models/user";
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
fieldName: string;
|
fieldName: string;
|
||||||
multi: boolean;
|
multi: boolean;
|
||||||
initialOption: {
|
initialOption:
|
||||||
label: string;
|
| number
|
||||||
value: number;
|
| {
|
||||||
} | null;
|
label: string;
|
||||||
|
value: number;
|
||||||
|
}
|
||||||
|
| null;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
let initO;
|
||||||
|
if (typeof props.initialOption == "number") {
|
||||||
|
const { data } = await useApiFetch<IUser>(`/user/${props.initialOption}`);
|
||||||
|
initO = {
|
||||||
|
label: data.value.username,
|
||||||
|
value: data.value._id,
|
||||||
|
};
|
||||||
|
} else if (typeof props.initialOption == "object") {
|
||||||
|
initO = props.initialOption;
|
||||||
|
}
|
||||||
|
|
||||||
const state = reactive<{
|
const state = reactive<{
|
||||||
data: { value: number; label: string }[];
|
data: { value: number; label: string }[];
|
||||||
fetching: boolean;
|
fetching: boolean;
|
||||||
}>({
|
}>({
|
||||||
data: props.initialOption ? [props.initialOption] : [],
|
data: props.initialOption ? [initO] : [],
|
||||||
fetching: false,
|
fetching: false,
|
||||||
});
|
});
|
||||||
let fieldo = useField<number | number[]>(props.fieldName, undefined, {
|
let fieldo = useField<number | number[]>(props.fieldName, undefined, {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
initialValue: props.initialOption?.value || null,
|
initialValue: initO.value || null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { value, errorMessage, name, setValue } = fieldo;
|
const { value, errorMessage, name, setValue } = fieldo;
|
||||||
|
Loading…
Reference in New Issue
Block a user