jws-frontend/src/components/shared/SelectInput.vue

138 lines
3.3 KiB
Vue
Raw Normal View History

<script lang="ts" setup generic="T extends Record<string, unknown>">
import { onMounted, ref, watch } from 'vue';
2024-09-18 14:29:32 +07:00
import { selectFilterOptionRefMod } from 'src/stores/utils';
import { QSelect } from 'quasar';
2024-09-18 14:29:32 +07:00
const model = defineModel<string | Record<string, unknown> | null>();
2024-09-18 14:29:32 +07:00
const options = ref<Record<string, unknown>[]>([]);
let defaultFilter: (
value: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
) => void;
const props = withDefaults(
defineProps<{
id?: string;
label?: string;
option: T[];
optionLabel?: keyof T;
optionValue?: keyof T;
placeholder?: string;
hideSelected?: boolean;
readonly?: boolean;
clearable?: boolean;
incremental?: boolean;
fillInput?: boolean;
2024-10-04 09:45:05 +07:00
disable?: boolean;
rules?: ((value: string) => string | true)[];
}>(),
{
option: () => [],
optionLabel: 'label',
optionValue: 'value',
hideSelected: true,
fillInput: true,
2024-10-04 09:45:05 +07:00
disable: false,
},
);
defineEmits<{
(
e: 'filter',
val: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
): void;
}>();
onMounted(() => {
defaultFilter = selectFilterOptionRefMod(
ref(props.option),
options,
typeof props.optionLabel === 'string' ? props.optionLabel : 'label',
);
});
watch(
() => props.option,
() => {
defaultFilter = selectFilterOptionRefMod(
ref(props.option),
options,
typeof props.optionLabel === 'string' ? props.optionLabel : 'label',
);
},
);
2024-09-18 14:29:32 +07:00
</script>
<template>
<q-select
:placeholder="placeholder"
2024-09-18 14:29:32 +07:00
outlined
:clearable
2024-10-04 09:45:05 +07:00
:disable
2024-09-18 14:29:32 +07:00
use-input
emit-value
map-options
:hideSelected
2024-09-18 14:29:32 +07:00
hide-bottom-space
:fill-input="fillInput && !!model"
2024-09-18 14:29:32 +07:00
:hide-dropdown-icon="readonly"
2024-11-11 13:31:47 +07:00
input-debounce="500"
:option-value="
typeof props.optionValue === 'string' ? props.optionValue : 'value'
"
:option-label="
typeof props.optionLabel === 'string' ? props.optionLabel : 'label'
"
2024-09-18 14:29:32 +07:00
v-model="model"
dense
autocomplete="off"
:readonly
2024-09-18 14:29:32 +07:00
:label="label"
:options="incremental ? option : options"
:for="`${id}`"
@filter="
(val, update) => {
incremental ? $emit('filter', val, update) : defaultFilter(val, update);
}
"
:rules
2024-09-18 14:29:32 +07:00
>
<template v-if="$slots.prepend" v-slot:prepend>
<slot name="prepend"></slot>
</template>
<template v-if="$slots.append" v-slot:append>
<slot name="append"></slot>
</template>
2024-09-18 14:29:32 +07:00
<template v-slot:no-option>
2024-09-30 11:37:18 +07:00
<slot name="noOption"></slot>
<slot name="no-option"></slot>
2024-09-30 11:37:18 +07:00
<q-item v-if="!$slots.noOption">
2024-09-18 14:29:32 +07:00
<q-item-section class="text-grey">
{{ $t('general.noData') }}
</q-item-section>
</q-item>
</template>
<template
v-if="$slots.selectedItem || $slots['selected-item']"
v-slot:selected-item="scope"
>
<slot name="selectedItem" :scope="scope" :opt="scope.opt as T"></slot>
<slot name="selected-item" :scope="scope" :opt="scope.opt as T"></slot>
</template>
<template v-if="$slots.option" v-slot:option="scope">
<slot name="option" :scope="scope" :opt="scope.opt as T"></slot>
</template>
<template v-if="$slots['before-options']" #before-options>
<slot name="before-options"></slot>
</template>
2024-09-18 14:29:32 +07:00
</q-select>
</template>