Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions resources/js/components/Combobox.vue

This file was deleted.

29 changes: 15 additions & 14 deletions resources/js/components/form/CraftCombobox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
import {t} from '@craftcms/cp';
import InputCombobox from '@/components/form/InputCombobox.vue';
import type {SelectItem} from '@/types';
import {computed} from 'vue';
import {computed, useSlots} from 'vue';

const emit = defineEmits<{
(e: 'update:modelValue', value: string): void;
}>();
const modelValue = defineModel<string | number | boolean>();
const props = defineProps<{
modelValue: string;
label: string;
id?: string;
name?: string;
disabled?: boolean;
options?: Array<SelectItem>;
callouts?: Array<string>;
error?: string;
requireOptionMatch?: boolean;
}>();

const valueProxy = computed({
get() {
return props.modelValue;
},
set(newValue) {
emit('update:modelValue', newValue);
},
const slots = useSlots();
const forwardedSlots = computed(() => {
const {default: _, ...rest} = slots;
return rest;
});
</script>

Expand All @@ -35,14 +30,20 @@
:name="name"
:disabled="disabled"
:has-feedback-for="error ? 'error' : ''"
:require-options-match="requireOptionMatch"
v-bind="$attrs"
>
<InputCombobox
slot="input"
v-model="valueProxy"
v-model="modelValue"
:options="options"
:label="label"
/>
>
<!-- Forward all other slots -->
<template v-for="(_, name) in forwardedSlots" #[name]="slotData">
<slot :name="name" v-bind="slotData || {}"></slot>
</template>
</InputCombobox>

<div slot="after">
<slot name="after">
Expand Down
12 changes: 6 additions & 6 deletions resources/js/components/form/InputCombobox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import InputComboboxOption from '@/components/form/InputComboboxOption.vue';

const emit = defineEmits<{
(e: 'update:modelValue', value: string): void;
(e: 'update:modelValue', value: string | number): void;
}>();
const props = withDefaults(
defineProps<{
label?: string;
options?: Array<SelectItem>;
modelValue?: string;
modelValue?: string | number | boolean;
requireOptionMatch?: boolean;
transformModelValue?: (newValue: SelectOption | null) => string;
class?: HTMLAttributes['class'];
Expand Down Expand Up @@ -60,7 +60,7 @@

if (!selectedItem && !props.requireOptionMatch) {
selectedItem = {
label: props.modelValue,
label: String(props.modelValue),
value: props.modelValue,
};
}
Expand All @@ -76,7 +76,7 @@
});

const reference = useTemplateRef<HTMLElement | null>('reference');
const query = ref(props.modelValue ?? '');
const query = ref(String(props.modelValue ?? ''));

const referenceCoordinates = computed(() => {
const coordinates = reference.value?.getBoundingClientRect();
Expand All @@ -88,12 +88,12 @@
});

function matchesQuery(query: MaybeRef<string>, item: MaybeRef<SelectOption>) {
const lowerQuery = unref(query).toLowerCase();
const lowerQuery = String(unref(query)).toLowerCase();
const option = unref(item);

return (
option.label.toLowerCase().includes(lowerQuery) ||
option.value.toLowerCase().includes(lowerQuery) ||
option.value.toString().toLowerCase().includes(lowerQuery) ||
(option.data?.keywords?.toLowerCase().includes(lowerQuery) ?? false)
);
}
Expand Down
23 changes: 14 additions & 9 deletions resources/js/components/form/InputComboboxOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
:checked="selected"
:hint="option.data?.hint"
>
<template
v-if="option.label.startsWith('$') || option.label.startsWith('@')"
>
<code>
<div class="flex gap-2 items-center">
<template v-if="option.data?.indicator">
<craft-indicator v-bind="option.data.indicator"></craft-indicator>
</template>
<template
v-if="option.label.startsWith('$') || option.label.startsWith('@')"
>
<code>
{{ option.label }}
</code>
</template>
<template v-else>
{{ option.label }}
</code>
</template>
<template v-else>
{{ option.label }}
</template>
</template>
</div>
</craft-option>
</slot>
</ComboboxOption>
Expand Down
Loading
Loading