Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(ui): introducing BBComboBox - a better dropdown component (#565)

Liu Jib164920e9709
diff --git a/frontend/src/bbkit/BBComboBox.vue b/frontend/src/bbkit/BBComboBox.vue
new file mode 100644
index 0000000000..d08fc9af20
--- /dev/null
+++ b/frontend/src/bbkit/BBComboBox.vue
@@ -0,0 +1,280 @@
+<template>
+ <VBinder>
+ <VTarget>
+ <div
+ ref="button"
+ class="btn-select relative pl-3 pr-10"
+ :class="[
+ disabled && 'bg-control-bg opacity-50 cursor-not-allowed',
+ state.open && 'ring-1 ring-control border-control',
+ ]"
+ v-bind="$attrs"
+ @click="toggle"
+ >
+ <div class="relative h-9 py-1.5">
+ <template v-if="state.value && !state.open">
+ <template v-if="state.value">
+ <slot name="menuItem" :item="state.value" />
+ </template>
+ </template>
+ <template v-if="!state.value && !state.query">
+ <slot name="placeholder" :placeholder="placeholder">
+ {{ placeholder }}
+ </slot>
+ </template>
+ <input
+ v-model="state.query"
+ type="text"
+ class="absolute w-full h-full inset-0 p-0 border-none focus:border-transparent focus:ring-0"
+ :class="{
+ 'opacity-0': !state.open,
+ }"
+ @compositionstart="state.compositing = true"
+ @compositionend="state.compositing = false"
+ @keydown="handleKeyEvent"
+ />
+ </div>
+ <span
+ class="ml-3 absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none"
+ >
+ <heroicons-solid:selector class="h-5 w-5 text-control-light" />
+ </span>
+ </div>
+ </VTarget>
+ <VFollower :show="state.open" placement="bottom-start">
+ <transition :appear="true" name="fade-fast">
+ <div
+ v-if="state.open"
+ ref="popup"
+ class="z-50 rounded-md bg-white shadow-lg"
+ :style="{ width: `${width}px` }"
+ >
+ <ul
+ tabindex="-1"
+ role="listbox"
+ aria-labelledby="listbox-label"
+ aria-activedescendant="listbox-item-3"
+ class="max-h-56 rounded-md py-1 ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm"
+ >
+ <!--
+ Select option, manage highlight styles based on mouseenter/mouseleave and keyboard navigation.
+ Highlighted: "text-white bg-indigo-600", Not Highlighted: "text-gray-900"
+ -->
+ <li
+ v-for="(item, index) in state.filteredOptions"
+ :key="index"
+ :ref="(el: any) => (itemRefs[index] = el)"
+ role="option"
+ class="text-main cursor-default select-none relative py-2 pl-3 pr-9"
+ :class="
+ index === state.activeIndex && 'text-main-text bg-main-hover'
+ "
+ @click="doSelect(item)"
+ @mouseover="state.activeIndex = index"
+ >
+ <slot name="menuItem" :item="item" />
+ <span
+ v-if="item === state.value"
+ class="absolute inset-y-0 right-0 flex items-center pr-4"
+ >
+ <heroicons-solid:check class="h-5 w-5" />
+ </span>
+ </li>
+ </ul>
+ </div>
+ </transition>
+ </VFollower>
+ </VBinder>
+</template>
+
+<script lang="ts" setup>
+import {
+ reactive,
+ watch,
+ ref,
+ defineProps,
+ defineEmits,
+ withDefaults,
+} from "vue";
+import { VBinder, VTarget, VFollower } from "vueuc";
+import { onClickOutside, useElementBounding } from "@vueuse/core";
+import { isAncestorOf, scrollIntoViewIfNeeded } from "./BBUtil";
+
+export type ItemType = number | string | object;
+
+type LocalState = {
+ open: boolean;
+ value: ItemType | undefined;
+ query: string;
+ compositing: boolean;
+ filteredOptions: ItemType[];
+ activeIndex: number;
+ activeItem: ItemType | undefined;
+};
+
+const props = withDefaults(
+ defineProps<{
+ value?: ItemType | undefined;
+ options: ItemType[];
+ placeholder?: string;
+ disabled?: boolean;
+ filter?: (
+ options: ItemType[],
+ query: string
+ ) => ItemType[] | Promise<ItemType[]>;
+ }>(),
+ {
+ value: undefined,
+ placeholder: "",
+ disabled: false,
+ filter: async (options) => options,
+ }
+);
+
+const emit = defineEmits<{
+ (event: "update:value", value: ItemType): void;
+}>();
+
+const state = reactive<LocalState>({
+ open: false,
+ value: props.value,
+ query: "",
+ compositing: false,
+ filteredOptions: props.options,
+ activeIndex: props.options.indexOf(props.value),
+ activeItem: props.value,
+});
+
+const button = ref<HTMLButtonElement | null>(null);
+const popup = ref<HTMLElement | null>(null);
+const itemRefs = ref<HTMLElement[]>([]);
+
+// Sync the popup's width to the button's
+const { width } = useElementBounding(button);
+
+watch(
+ () => props.value,
+ (cur) => {
+ state.value = cur;
+ state.activeIndex = props.options.indexOf(cur);
+ state.activeItem = cur;
+ }
+);
+
+const close = () => {
+ state.open = false;
+ state.compositing = false;
+};
+
+const open = () => {
+ state.open = true;
+ state.query = "";
+ state.compositing = false;
+};
+
+const toggle = () => {
+ if (!state.open) open();
+ else close();
+};
+
+onClickOutside(popup, (e) => {
+ if (button.value) {
+ if (isAncestorOf(button.value, e.target as HTMLElement)) {
+ // Do not close the popup when clicking on the button
+ return;
+ }
+ }
+ // Otherwise close the popup
+ close();
+});
+
+watch(
+ () => [state.query, props.options, state.compositing],
+ (args) => {
+ const [query, options, compositing] = args as [string, ItemType[], boolean];
+ if (compositing) {
+ // Will do nothing if the IME is compositing
+ return;
+ }
+ if (!query) {
+ // Won't call filter function if query is empty
+ state.filteredOptions = options;
+ } else {
+ const result = props.filter(options, query);
+ if (Array.isArray(result)) {
+ // If filter function returns an Array, set value directly
+ state.filteredOptions = result;
+ } else {
+ // Otherwise it's a Promise<Array>, wait.
+ result.then((filteredOptions) => {
+ if (query === state.query) {
+ // Only to set value when query matches
+ // In case that the async filter function does not response as quickly as query changes.
+ state.filteredOptions = filteredOptions;
+ }
+ });
+ }
+ }
+ }
+);
+
+watch(
+ () => state.filteredOptions,
+ (newList) => {
+ // Try to keep activeIndex correct when filteredOptions changed
+ // Find the new index of the selected item
+ if (state.activeItem) {
+ const newIndex = newList.indexOf(state.activeItem);
+ state.activeIndex = newIndex; // -1 or some index
+ state.activeItem = newList[newIndex]; // undefined or some item
+ }
+ }
+);
+
+const doSelect = (item: ItemType) => {
+ if (item !== state.value) {
+ emit("update:value", item);
+ }
+ close();
+};
+
+const moveIndex = (diff: number) => {
+ const max = state.filteredOptions.length;
+ const index = state.activeIndex + diff;
+ if (index < 0 || index >= max) return;
+
+ // Change activeIndex if it doesn't exceed the boundary
+ state.activeIndex = index;
+ state.activeItem = state.filteredOptions[index];
+
+ const elem = itemRefs.value[index];
+ if (elem) {
+ scrollIntoViewIfNeeded(elem);
+ }
+};
+
+const handleKeyEvent = (e: KeyboardEvent) => {
+ if (e.key === "ArrowUp") {
+ e.preventDefault();
+ moveIndex(-1);
+ }
+ if (e.key === "ArrowDown") {
+ e.preventDefault();
+ moveIndex(1);
+ }
+ if (e.key === "Enter") {
+ e.preventDefault();
+ const index = state.activeIndex;
+ const list = state.filteredOptions;
+ if (index >= 0 && index < list.length) {
+ doSelect(list[index]);
+ }
+ }
+};
+</script>
+
+<script lang="ts">
+export default {
+ inheritAttrs: false,
+};
+</script>
diff --git a/frontend/src/bbkit/BBUtil.ts b/frontend/src/bbkit/BBUtil.ts
index cdc4e010fc..18ea65d0ed 100644
--- a/frontend/src/bbkit/BBUtil.ts
+++ b/frontend/src/bbkit/BBUtil.ts
@@ -6,3 +6,24 @@ export function hashCode(s: string): number {
}
return hash;
}
+
+export function scrollIntoViewIfNeeded(elem: any, ...args: any[]) {
+ // this is not well-defined in typescript
+ if (typeof elem.scrollIntoViewIfNeeded === "function") {
+ elem.scrollIntoViewIfNeeded(...args);
+ } else if (typeof elem.scrollIntoView === "function") {
+ elem.scrollIntoView(...args);
+ }
+}
+
+export function isAncestorOf(
+ maybeAncestor: HTMLElement,
+ maybeDescendant: HTMLElement
+): boolean {
+ let element: HTMLElement | null = maybeDescendant;
+ while (element) {
+ if (element === maybeAncestor) return true;
+ element = element.parentElement;
+ }
+ return false;
+}
diff --git a/frontend/src/bbkit/index.ts b/frontend/src/bbkit/index.ts
index 96285838cd..f793b52af6 100644
--- a/frontend/src/bbkit/index.ts
+++ b/frontend/src/bbkit/index.ts
@@ -9,6 +9,7 @@ import BBModal from "./BBModal.vue";
import BBNotification from "./BBNotification.vue";
import BBOutline from "./BBOutline.vue";
import BBSelect from "./BBSelect.vue";
+import BBComboBox from "./BBComboBox.vue";
import BBSpin from "./BBSpin.vue";
import BBStepBar from "./BBStepBar.vue";
import BBStepTab from "./BBStepTab.vue";
@@ -36,6 +37,7 @@ export {
BBNotification,
BBOutline,
BBSelect,
+ BBComboBox,
BBSpin,
BBStepBar,
BBStepTab,
diff --git a/frontend/src/components/CreateDatabasePrepForm.vue b/frontend/src/components/CreateDatabasePrepForm.vue
index 8148ec192f..a99a6dee6c 100644
--- a/frontend/src/components/CreateDatabasePrepForm.vue
+++ b/frontend/src/components/CreateDatabasePrepForm.vue
@@ -170,7 +170,7 @@
<!-- eslint-disable vue/attribute-hyphenation -->
<MemberSelect
id="user"
- class="mt-1"
+ class="mt-1 w-full"
name="user"
:allowed-role-list="['OWNER', 'DBA']"
:selectedId="state.assigneeId"
diff --git a/frontend/src/components/DataSourceMemberForm.vue b/frontend/src/components/DataSourceMemberForm.vue
index 697625e3e0..c03ab0c876 100644
--- a/frontend/src/components/DataSourceMemberForm.vue
+++ b/frontend/src/components/DataSourceMemberForm.vue
@@ -93,7 +93,7 @@
<!-- eslint-disable vue/attribute-hyphenation -->
<MemberSelect
id="user"
- class="mt-1"
+ class="mt-1 w-full"
name="user"
:allowed-role-list="['DEVELOPER']"
:disabled="!allowUpdateDataSourceMember"
diff --git a/frontend/src/components/Issue/IssueSidebar.vue b/frontend/src/components/Issue/IssueSidebar.vue
index 669694e8d0..08e756e90f 100644
--- a/frontend/src/components/Issue/IssueSidebar.vue
+++ b/frontend/src/components/Issue/IssueSidebar.vue
@@ -24,6 +24,7 @@
<div class="col-span-2">
<!-- eslint-disable vue/attribute-hyphenation -->
<MemberSelect
+ class="w-full"
:disabled="!allowEditAssignee"
:selectedId="create ? issue.assigneeId : issue.assignee?.id"
:allowed-role-list="['OWNER', 'DBA']"
diff --git a/frontend/src/components/MemberSelect.vue b/frontend/src/components/MemberSelect.vue
index 2b5fffbbf6..c4907ecac6 100644
--- a/frontend/src/components/MemberSelect.vue
+++ b/frontend/src/components/MemberSelect.vue
@@ -1,10 +1,11 @@
<template>
- <BBSelect
- :selected-item="selectedPrincipal"
- :item-list="principalList"
+ <BBComboBox
+ :value="selectedPrincipal"
+ :options="principalList"
+ :filter="filter"
:placeholder="placeholder"
:disabled="disabled"
- @select-item="
+ @update:value="
(principal) => {
state.selectedId = principal.id;
$emit('select-principal-id', parseInt(principal.id));
@@ -33,11 +34,11 @@
}}</span>
</span>
</template>
- </BBSelect>
+ </BBComboBox>
</template>
<script lang="ts">
-import { reactive, computed, watch, PropType } from "vue";
+import { reactive, computed, watch, PropType, defineComponent } from "vue";
import { useStore } from "vuex";
import PrincipalAvatar from "./PrincipalAvatar.vue";
import {
@@ -48,18 +49,20 @@ import {
SYSTEM_BOT_ID,
} from "../types";
import { isDBA, isDeveloper, isOwner } from "../utils";
+import { BBComboBox } from "../bbkit";
interface LocalState {
- selectedId?: PrincipalId;
+ selectedId: PrincipalId | undefined;
showMenu: boolean;
}
-export default {
+export default defineComponent({
name: "MemberSelect",
- components: { PrincipalAvatar },
+ components: { BBComboBox, PrincipalAvatar },
props: {
selectedId: {
type: Number,
+ default: undefined,
},
disabled: {
default: false,
@@ -135,12 +138,20 @@ export default {
state.showMenu = false;
};
+ const filter = (options: Principal[], query: string): Principal[] => {
+ query = query.toLowerCase();
+ return options.filter((principal) =>
+ principal.name.toLowerCase().includes(query)
+ );
+ };
+
return {
state,
principalList,
selectedPrincipal,
+ filter,
close,
};
},
-};
+});
</script>
diff --git a/frontend/src/components/ProjectMemberPanel.vue b/frontend/src/components/ProjectMemberPanel.vue
index c04cb22f52..8d003600df 100644
--- a/frontend/src/components/ProjectMemberPanel.vue
+++ b/frontend/src/components/ProjectMemberPanel.vue
@@ -1,19 +1,23 @@
<template>
<div>
- <p class="text-lg font-medium leading-7 text-main">{{ $t("project.settings.manage-member") }}</p>
+ <p class="text-lg font-medium leading-7 text-main">
+ {{ $t("project.settings.manage-member") }}
+ </p>
<div v-if="allowAddMember" class="mt-4 w-full flex justify-start">
<!-- To prevent jiggling when showing the error text -->
<div :class="state.error ? 'space-y-1' : 'space-y-6'">
<div class="space-y-2">
- <div class="flex flex-row justify-between py-0.5 select-none space-x-4">
+ <div
+ class="flex flex-row justify-between py-0.5 select-none space-x-4"
+ >
<div class="w-64">
- <!-- eslint-disable vue/attribute-hyphenation -->
<MemberSelect
id="user"
name="user"
+ class="w-full"
:required="false"
:placeholder="$t('project.settings.member-placeholder')"
- :selectedId="state.principalId"
+ :selected-id="state.principalId"
@select-principal-id="
(principalId) => {
state.principalId = principalId;
@@ -64,7 +68,9 @@
</div>
<div id="state-error" class="flex justify-start">
- <span v-if="state.error" class="text-sm text-error">{{ state.error }}</span>
+ <span v-if="state.error" class="text-sm text-error">
+ {{ state.error }}
+ </span>
</div>
</div>
</div>
@@ -73,7 +79,7 @@
</template>
<script lang="ts">
-import { computed, PropType, reactive } from "vue";
+import { computed, defineComponent, PropType, reactive } from "vue";
import { useStore } from "vuex";
import MemberSelect from "../components/MemberSelect.vue";
import ProjectMemberTable from "../components/ProjectMemberTable.vue";
@@ -95,7 +101,7 @@ interface LocalState {
error: string;
}
-export default {
+export default defineComponent({
name: "ProjectMemberPanel",
components: { MemberSelect, ProjectMemberTable },
props: {
@@ -210,5 +216,5 @@ export default {
addMember,
};
},
-};
+});
</script>
diff --git a/frontend/src/views/IssueDashboard.vue b/frontend/src/views/IssueDashboard.vue
index 1fdbfb0326..6ddb3020f3 100644
--- a/frontend/src/views/IssueDashboard.vue
+++ b/frontend/src/views/IssueDashboard.vue
@@ -18,6 +18,7 @@
<!-- eslint-disable vue/attribute-hyphenation -->
<MemberSelect
v-if="scopeByPrincipal"
+ class="w-72"
:selectedId="state.selectedPrincipalId"
@select-principal-id="selectPrincipal"
/>