fix(grid): improve condition history dropdown behavior
This commit is contained in:
1 parent
4efff9a190
commit
b249768322
3 files changed
+211
-127
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getDataGridConditionSuggestionPosition } from "@/lib/dataGridConditionSuggestionPosition";
|
||||
|
||||
describe("getDataGridConditionSuggestionPosition", () => {
|
||||
test("anchors the dropdown to the input instead of the caret", () => {
|
||||
const position = getDataGridConditionSuggestionPosition({ left: 120, bottom: 40, width: 420 }, { viewportWidth: 1000 });
|
||||
|
||||
expect(position).toEqual({ left: 120, top: 42, width: 420 });
|
||||
});
|
||||
|
||||
test("keeps long history rows inside the viewport", () => {
|
||||
const position = getDataGridConditionSuggestionPosition({ left: 780, bottom: 40, width: 420 }, { viewportWidth: 1000 });
|
||||
|
||||
expect(position.left + position.width).toBeLessThanOrEqual(992);
|
||||
expect(position.width).toBe(420);
|
||||
});
|
||||
|
||||
test("uses a minimum width for narrow inputs", () => {
|
||||
const position = getDataGridConditionSuggestionPosition({ left: 20, bottom: 40, width: 120 }, { viewportWidth: 1000 });
|
||||
|
||||
expect(position.width).toBe(180);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
export interface DataGridConditionSuggestionRect {
|
||||
left: number;
|
||||
bottom: number;
|
||||
width: number;
|
||||
}
|
||||
|
||||
export interface DataGridConditionSuggestionPositionOptions {
|
||||
viewportWidth: number;
|
||||
minWidth?: number;
|
||||
viewportMargin?: number;
|
||||
topOffset?: number;
|
||||
}
|
||||
|
||||
export interface DataGridConditionSuggestionPosition {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
}
|
||||
|
||||
export function getDataGridConditionSuggestionPosition(inputRect: DataGridConditionSuggestionRect, options: DataGridConditionSuggestionPositionOptions): DataGridConditionSuggestionPosition {
|
||||
const minWidth = options.minWidth ?? 180;
|
||||
const viewportMargin = options.viewportMargin ?? 8;
|
||||
const topOffset = options.topOffset ?? 2;
|
||||
const availableWidth = Math.max(minWidth, options.viewportWidth - viewportMargin * 2);
|
||||
const width = Math.min(Math.max(inputRect.width, minWidth), availableWidth);
|
||||
const maxLeft = Math.max(viewportMargin, options.viewportWidth - viewportMargin - width);
|
||||
const left = Math.min(Math.max(inputRect.left, viewportMargin), maxLeft);
|
||||
|
||||
return {
|
||||
left,
|
||||
top: inputRect.bottom + topOffset,
|
||||
width,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user