Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

4.1 KB
Raw Blame
<script setup lang="ts">
import { computed, nextTick, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import type { FocusOutsideEvent, PointerDownOutsideEvent } from "reka-ui";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";

const props = withDefaults(
  defineProps<{
    modelValue: string;
    values: string[];
    /** Optional labels aligned with `values`; the committed value is still `values[i]`. */
    displayValues?: string[];
    nullable?: boolean;
    initialNull?: boolean;
    cellLayout?: "grid" | "transpose";
  }>(),
  {
    nullable: false,
    initialNull: false,
    cellLayout: "grid",
  },
);

const emit = defineEmits<{
  "update:modelValue": [value: string];
  commit: [value?: string | null];
  cancel: [];
}>();

const { t } = useI18n();

const open = ref(true);
const triggerRef = ref<HTMLButtonElement | null>(null);
const closeHandled = ref(false);

const triggerClass = computed(() => ["cell-edit-input absolute inset-0 z-10 flex items-center gap-1 border-2 border-primary bg-background py-0 text-left text-xs outline-none", props.cellLayout === "transpose" ? "px-1.5" : "px-2.5"].join(" "));

function optionLabel(value: string, index: number): string {
  return props.displayValues?.[index] ?? value;
}

const displayValue = computed(() => {
  if (props.initialNull && props.modelValue === "") return t("grid.nullValue");
  if (props.modelValue === "") return "''";
  const index = props.values.indexOf(props.modelValue);
  return index >= 0 ? optionLabel(props.modelValue, index) : props.modelValue;
});

onMounted(() => {
  nextTick(() => triggerRef.value?.focus());
});

function selectValue(value: string | null) {
  if (value !== null) emit("update:modelValue", value);
  finishCommit(value);
}

function finishCommit(value?: string | null) {
  closeHandled.value = true;
  emit("commit", value);
}

function finishCancel() {
  closeHandled.value = true;
  emit("cancel");
}

function onKeydown(event: KeyboardEvent) {
  if (event.key === "Enter") {
    event.preventDefault();
    finishCommit();
  } else if (event.key === "Escape") {
    event.preventDefault();
    event.stopPropagation();
    finishCancel();
  }
}

function onPopoverInteractOutside(event: PointerDownOutsideEvent | FocusOutsideEvent) {
  const originalEvent = event.detail.originalEvent;
  if (originalEvent instanceof FocusEvent) {
    event.preventDefault();
    return;
  }
  finishCommit();
}
</script>

<template>
  <Popover
    :open="open"
    @update:open="
      (v) => {
        open = v;
        if (!v && !closeHandled) finishCommit();
      }
    "
  >
    <PopoverTrigger as-child>
      <button ref="triggerRef" type="button" :class="triggerClass" @keydown.stop="onKeydown" @click.stop>
        <span class="min-w-0 flex-1 truncate">{{ displayValue }}</span>
      </button>
    </PopoverTrigger>
    <PopoverContent align="start" side="bottom" class="w-auto min-w-[120px] rounded-md p-1" @click.stop @keydown.stop="onKeydown" @interact-outside="onPopoverInteractOutside">
      <div class="flex flex-col gap-0.5">
        <button v-if="nullable" type="button" class="flex min-h-7 w-full items-center rounded-sm px-2 py-1 text-left text-xs italic text-muted-foreground hover:bg-accent hover:text-accent-foreground" :class="{ 'bg-accent text-accent-foreground': initialNull }" @click="selectValue(null)">
          {{ t("grid.nullValue") }}
        </button>
        <button
          v-for="(val, index) in values"
          :key="val"
          type="button"
          class="flex min-h-7 w-full items-center rounded-sm px-2 py-1 text-left text-xs hover:bg-accent hover:text-accent-foreground"
          :class="{ 'bg-accent text-accent-foreground': modelValue === val && !(initialNull && val === '') }"
          @click="selectValue(val)"
        >
          <template v-if="val === ''">
            <code>''</code>
            <span class="ml-2 text-muted-foreground">{{ t("grid.emptyStringValue") }}</span>
          </template>
          <template v-else>{{ optionLabel(val, index) }}</template>
        </button>
      </div>
    </PopoverContent>
  </Popover>
</template>