Skip to content

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

feat: support multiple line for SQLRuleEditDialog (#5094)

Liu Ji52b2d816f86a
diff --git a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
index 035ec233e2..7829c7dd86 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
@@ -103,18 +103,15 @@
{{ $t("common.description") }}
</h3>
<div class="flex flex-col gap-x-2">
- <input
- v-model="state.comment"
- type="text"
+ <AutoHeightTextarea
+ v-model:value="state.comment"
:disabled="disabled"
- :class="[
- 'shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full border-gray-300 rounded-md',
- disabled && 'cursor-not-allowed',
- ]"
:placeholder="
getRuleLocalization(rule.type).description ||
$t('common.description')
"
+ rows="1"
+ :max-height="120"
/>
</div>
</div>
@@ -149,6 +146,7 @@
<script lang="ts" setup>
import { cloneDeep } from "lodash-es";
import { computed, nextTick, reactive, watch } from "vue";
+import { useI18n } from "vue-i18n";
import {
getRuleLocalization,
@@ -157,6 +155,7 @@ import {
RuleLevel,
RuleTemplate,
} from "@/types/sqlReview";
+import AutoHeightTextarea from "@/components/misc/AutoHeightTextarea.vue";
import RuleEngineIcons from "./RuleEngineIcons.vue";
import RuleLevelSwitch from "./RuleLevelSwitch.vue";
import {
@@ -167,7 +166,6 @@ import {
TemplateComponent,
PayloadValueType,
} from "./RuleConfigComponents";
-import { useI18n } from "vue-i18n";
type LocalState = {
payload: PayloadValueType[];
diff --git a/frontend/src/components/misc/AutoHeightTextarea.vue b/frontend/src/components/misc/AutoHeightTextarea.vue
new file mode 100644
index 0000000000..a9647db03e
--- /dev/null
+++ b/frontend/src/components/misc/AutoHeightTextarea.vue
@@ -0,0 +1,60 @@
+<template>
+ <textarea
+ ref="textareaRef"
+ v-model="state.value"
+ class="text-main focus:ring-control focus:border-control sm:text-sm resize-none whitespace-pre-wrap border-control-border rounded-md disabled:cursor-not-allowed"
+ />
+</template>
+
+<script lang="ts" setup>
+import { useEventListener } from "@vueuse/core";
+import { onMounted, reactive, ref, watch } from "vue";
+
+import { useVModel } from "@/composables/useVModel";
+import { sizeToFit } from "@/utils";
+import { debounce } from "lodash-es";
+
+type LocalState = {
+ value: string | undefined;
+};
+
+const props = withDefaults(
+ defineProps<{
+ value?: string;
+ padding?: number;
+ maxHeight?: number;
+ minHeight?: number;
+ }>(),
+ {
+ value: undefined,
+ padding: 2,
+ maxHeight: -1,
+ minHeight: -1,
+ }
+);
+
+const emit = defineEmits<{
+ (name: "update:value", value: string | undefined): void;
+}>();
+
+const state = reactive<LocalState>({
+ value: props.value,
+});
+const textareaRef = ref<HTMLTextAreaElement>();
+
+useVModel(props, state, emit, "value");
+
+const resize = () => {
+ sizeToFit(textareaRef.value, props.padding, props.maxHeight);
+};
+
+const debouncedResize = debounce(resize, 50);
+
+onMounted(() => {
+ resize();
+});
+
+useEventListener("resize", debouncedResize);
+
+watch(() => state.value, debouncedResize);
+</script>
diff --git a/frontend/src/composables/useVModel.ts b/frontend/src/composables/useVModel.ts
new file mode 100644
index 0000000000..06fea06bf8
--- /dev/null
+++ b/frontend/src/composables/useVModel.ts
@@ -0,0 +1,26 @@
+import { watch } from "vue";
+
+export const useVModel = <
+ PS extends { [key in K]: PS[K] },
+ K extends string,
+ E extends (name: `update:${K}`, value: PS[K]) => void
+>(
+ props: PS,
+ state: PS,
+ emit: E,
+ key: K,
+ immediate = false
+) => {
+ watch(
+ () => props[key],
+ (v) => {
+ state[key] = v;
+ },
+ { immediate }
+ );
+
+ watch(
+ () => state[key],
+ (v) => emit(`update:${key}`, v)
+ );
+};
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 947b959378..31a200aa44 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -102,12 +102,22 @@ export function arraySwap(arr: any[], old_index: number, new_index: number) {
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
}
-export function sizeToFit(el: HTMLTextAreaElement | undefined) {
+export function sizeToFit(
+ el: HTMLTextAreaElement | undefined,
+ padding = 2,
+ max = -1,
+ min = -1
+) {
if (!el) return;
el.style.height = "auto";
- // Extra 2px is to prevent jiggling upon entering the text
- el.style.height = `${el.scrollHeight + 2}px`;
+ requestAnimationFrame(() => {
+ // Extra several pixels are to prevent jiggling upon entering the text
+ let height = el.scrollHeight + padding;
+ if (max >= 0 && height > max) height = max;
+ if (min >= 0 && height < min) height = min;
+ el.style.height = `${height}px`;
+ });
}
export function isValidEmail(email: string) {