add DropdownMenuNavbar.vue

This commit is contained in:
Timi
2026-10-19 14:59:27 +08:00
parent fd1ff4526f
commit 77e0e7a8e0
3 changed files with 173 additions and 72 deletions
@@ -0,0 +1,105 @@
<template>
<div class="filters glass dropdown-menu-navbar">
<t-input
:model-value="props.modelValue"
class="search-input"
type="search"
borderless
clearable
:placeholder="props.placeholder"
@update:model-value="handleKeywordChange"
>
<template v-if="$slots['suffix-icon']" #suffix-icon>
<slot name="suffix-icon" />
</template>
</t-input>
<configured-dropdown-menu
:filters="props.filters"
:menu-props="props.menuProps"
@filter-change="handleDropdownChange"
/>
</div>
</template>
<script setup lang="ts">
import { defineComponent, h, type PropType } from "vue";
import { DropdownItem, DropdownMenu } from "tdesign-mobile-vue";
import type { DropdownMenuProps, DropdownValue } from "tdesign-mobile-vue";
defineOptions({ name: "DropdownMenuNavbar" });
type DropdownMenuNavbarOption = {
label: string;
value: DropdownValue;
disabled?: boolean;
};
interface DropdownMenuNavbarFilter {
key: string;
label: string;
value: DropdownValue;
options: DropdownMenuNavbarOption[];
multiple?: boolean;
optionsColumns?: string | number;
disabled?: boolean;
}
interface DropdownMenuNavbarFilterChange {
key: string;
value: DropdownValue;
}
const props = withDefaults(defineProps<{
modelValue?: string;
placeholder?: string;
filters: DropdownMenuNavbarFilter[];
menuProps?: DropdownMenuProps;
}>(), {
modelValue: "",
placeholder: "搜索"
});
const emit = defineEmits<{
(event: "update:modelValue", value: string): void;
(event: "filter-change", change: DropdownMenuNavbarFilterChange): void;
}>();
const ConfiguredDropdownMenu = defineComponent({
name: "ConfiguredDropdownMenu",
props: {
filters: {
type: Array as PropType<DropdownMenuNavbarFilter[]>,
required: true
},
menuProps: {
type: Object as PropType<DropdownMenuProps>,
default: undefined
}
},
emits: ["filter-change"],
setup(menu, { emit: emitMenu }) {
return () => h(DropdownMenu, { ...menu.menuProps, class: "menus" }, {
default: () => menu.filters.map(filter => h(DropdownItem, {
key: filter.key,
modelValue: filter.value,
label: filter.label,
options: filter.options,
multiple: filter.multiple,
optionsColumns: filter.optionsColumns,
disabled: filter.disabled,
onChange: (value: DropdownValue) => emitMenu("filter-change", { key: filter.key, value })
}))
});
}
});
function handleKeywordChange(value: string | number): void {
const keyword = String(value ?? "");
emit("update:modelValue", keyword);
emit("filter-change", { key: "keyword", value: keyword });
}
function handleDropdownChange(change: DropdownMenuNavbarFilterChange): void {
emit("filter-change", change);
}
</script>