fix(jd): 类目面板超视窗定位钳制(B114 尾件入库)——面板打开/栏数与搜索态变化后按真实 rect 重算 left 偏移,右缘贴齐视口后下钻向左生长,末栏不再被窗缘裁掉;钳制数学抽纯函数 jd-category-cascader-position.ts+4 例纯逻辑测试
This commit is contained in:
@@ -10,13 +10,14 @@
|
||||
* 全路径);树外命中(已落库但京麦类目体系变更后的历史名)从 options 兜底段选,语义
|
||||
* 同旧版第三栏。「全部类目」行=清选;触发框已选时出 × 清除;面板外点透明遮罩关闭。
|
||||
* 零依赖自绘(radio 圆点/箭头都是 CSS),无 element 依赖。 */
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, nextTick, ref, watch } from 'vue'
|
||||
import {
|
||||
jdCategoryChildren,
|
||||
jdCategoryRoots,
|
||||
jdCategorySearchLeaves,
|
||||
type JdCategoryNode,
|
||||
} from './jd-category-tree.ts'
|
||||
import { clampPanelLeftX } from './jd-category-cascader-position.ts'
|
||||
|
||||
const props = defineProps<{
|
||||
/** 已落库末级类目全集(/categories 现算,页面传入)——只作**树外兜底**(搜索无树内
|
||||
@@ -33,6 +34,25 @@ const open = ref(false)
|
||||
const activePath = ref<string[]>([])
|
||||
const searchText = ref('')
|
||||
|
||||
/** 面板水平定位钳制(2026-11-16「超出视窗了」):触发框在筛选行中部偏右时,锚 left:0
|
||||
* 的多栏面板会伸出视窗右缘。打开与栏数/搜索态变化后用真实 rect 重算 left 偏移——
|
||||
* 右缘贴齐视口后继续下钻即向左生长,不再截掉末栏。钳制数学在
|
||||
* ./jd-category-cascader-position.ts(纯函数,tests/jd-category-cascader-position.test.ts)。 */
|
||||
const hostRef = ref<HTMLElement | null>(null)
|
||||
const panelRef = ref<HTMLElement | null>(null)
|
||||
const panelOffsetX = ref(0)
|
||||
|
||||
function reposition(): void {
|
||||
const host = hostRef.value
|
||||
const panel = panelRef.value
|
||||
if (!host || !panel) return
|
||||
panelOffsetX.value = clampPanelLeftX({
|
||||
hostLeft: host.getBoundingClientRect().left,
|
||||
panelWidth: panel.offsetWidth,
|
||||
viewportWidth: window.innerWidth,
|
||||
})
|
||||
}
|
||||
|
||||
const roots = jdCategoryRoots()
|
||||
/** 树内全部末级名集合(判定 options 哪些是「树外历史」)。 */
|
||||
const treeLeafNames = new Set(
|
||||
@@ -68,6 +88,14 @@ const fallbackOptions = computed(() => {
|
||||
.filter((option) => option.name.toLowerCase().includes(keyword))
|
||||
})
|
||||
|
||||
/** 栏数随下钻动态 1~4、搜索态切单栏 ⇒ 面板宽度变,开着就要重算偏移(下钻向左生长)。 */
|
||||
watch(
|
||||
() => [columns.value.length, searching.value] as const,
|
||||
() => {
|
||||
if (open.value) nextTick(reposition)
|
||||
},
|
||||
)
|
||||
|
||||
function toggle(): void {
|
||||
if (open.value) {
|
||||
close()
|
||||
@@ -76,6 +104,7 @@ function toggle(): void {
|
||||
activePath.value = []
|
||||
searchText.value = ''
|
||||
open.value = true
|
||||
nextTick(reposition)
|
||||
}
|
||||
|
||||
function close(): void {
|
||||
@@ -110,7 +139,7 @@ function clear(): void {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="cat-cascader">
|
||||
<div ref="hostRef" class="cat-cascader">
|
||||
<button
|
||||
class="cat-trigger"
|
||||
type="button"
|
||||
@@ -130,7 +159,7 @@ function clear(): void {
|
||||
|
||||
<!-- 面板外点透明遮罩关闭(零 document 监听器,无泄漏面)。 -->
|
||||
<div v-if="open" class="cat-mask" @click="close"></div>
|
||||
<div v-if="open" class="cat-panel">
|
||||
<div v-if="open" ref="panelRef" class="cat-panel" :style="{ left: `${panelOffsetX}px` }">
|
||||
<!-- 搜索态:树内末级命中+树外兜底段(扁平列表,行 title 带全路径)。 -->
|
||||
<template v-if="searching">
|
||||
<div class="cat-col cat-col-search">
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/** 类目级联面板的水平定位钳制(2026-11-16 客户截图「超出视窗了」:面板锚在触发框
|
||||
* left:0,下钻到 3~4 栏时宽 600~800px,而「类目」筛选框在 jd-filter-row 流式布局里
|
||||
* 常落在页面中部偏右 ⇒ 面板右半截伸出视窗,末栏被窗缘裁到只剩一列 radio 圆点)。
|
||||
*
|
||||
* 本文件是**纯钳制数学**(零依赖,node --test 可直测):默认锚左(偏移 0);视口右缘
|
||||
* 装不下时整体左移到右缘贴齐(留 margin),左移后越出左缘则贴左缘为止。组件侧在面板
|
||||
* 打开与栏数/搜索态变化后用真实 rect 调它(JdCategoryCascader.vue),栏数随下钻动态
|
||||
* 1~4,宽度变了要重算——右贴齐后继续下钻即向左生长,不会再伸出右缘。 */
|
||||
|
||||
export interface PanelClampInput {
|
||||
/** 触发框(.cat-cascader)的视口左缘 x。 */
|
||||
hostLeft: number
|
||||
/** 面板当前实际渲染宽(含边框;栏数不同宽度不同)。 */
|
||||
panelWidth: number
|
||||
/** 视口宽(window.innerWidth)。 */
|
||||
viewportWidth: number
|
||||
/** 视口两侧最小留白,默认 8px。 */
|
||||
margin?: number
|
||||
}
|
||||
|
||||
/** 返回面板相对触发框左缘应加的 left 偏移(px;负=左移、0=默认锚左即可、正=触发框
|
||||
* 本身已贴近视口左缘时把面板推回 margin 内,物理上只在窄窗极端值出现)。 */
|
||||
export function clampPanelLeftX(input: PanelClampInput): number {
|
||||
const margin = input.margin ?? 8
|
||||
const overflowRight = input.hostLeft + input.panelWidth - (input.viewportWidth - margin)
|
||||
if (overflowRight <= 0) return 0
|
||||
const desiredLeft = Math.max(margin, input.hostLeft - overflowRight)
|
||||
return desiredLeft - input.hostLeft
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { clampPanelLeftX } from '../src/pages/jd/jd-category-cascader-position.ts'
|
||||
|
||||
/** 类目面板水平钳制(2026-11-16 客户截图「超出视窗了」回归判据):面板锚 left:0 最宽
|
||||
* 4 栏 ×200px,触发框落在筛选行中部偏右时面板伸出视窗右缘。判据形状统一=「右缘装
|
||||
* 不下必须左移、左移过头贴左缘、装得下不动」三种写法一格定红;把组件改回无条件锚左
|
||||
* 即红(JdCategoryCascader.vue 的 reposition 必须经本函数算偏移)。 */
|
||||
|
||||
test('视口装得下=不偏移(默认锚左)', () => {
|
||||
// 宽视口:触发框在 x=300,4 栏 800px,右缘 1100 < 1920-8。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 300, panelWidth: 800, viewportWidth: 1920 }), 0)
|
||||
// 恰好贴齐:右缘正好压在 viewport-margin 上=零偏移(≤0 分支)。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 300, panelWidth: 800, viewportWidth: 1108 }), 0)
|
||||
})
|
||||
|
||||
test('右缘越界=整体左移到右缘贴齐(客户截图形状:末栏被窗缘裁掉)', () => {
|
||||
// 截图形状:触发框 x=250、3 栏 600px、窗宽 830 ⇒ 右缘 850 越界 28px ⇒ 左移 28。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 250, panelWidth: 600, viewportWidth: 830 }), -28)
|
||||
// 宽视口下钻到 4 栈仍越界(触发框靠右):1920 窗、触发框 x=1200、800px 面板。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 1200, panelWidth: 800, viewportWidth: 1920 }), -88)
|
||||
})
|
||||
|
||||
test('越界过头不得把面板推出左缘=钳在 margin', () => {
|
||||
// 窄窗:触发框 x=100、面板 800、窗宽 830 ⇒ 直移要 -78,但那样左缘到 22>8 可行;
|
||||
// 极端:触发框 x=4、面板 800、窗宽 500 ⇒ 直移 -312 ⇒ 左缘 -308 越左,钳到 margin=8。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 4, panelWidth: 800, viewportWidth: 500, margin: 8 }), 4)
|
||||
// 钳制后面板左缘=margin:偏移=margin-hostLeft。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 2, panelWidth: 600, viewportWidth: 400, margin: 8 }), 6)
|
||||
})
|
||||
|
||||
test('窄窗越界:直移后左缘仍够留白就正常左移;不够则钳在 margin(物理极限,不假装能塞下)', () => {
|
||||
// 窗宽 400、margin 8:面板 380 ⇒ 直移 -138 后左缘 12 ≥ 8 可行;
|
||||
// 面板 390 ⇒ 直移后左缘 2 < 8 ⇒ 钳到左缘 8(偏移 -142),右侧溢出是物理极限。
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 150, panelWidth: 380, viewportWidth: 400, margin: 8 }), -138)
|
||||
assert.equal(clampPanelLeftX({ hostLeft: 150, panelWidth: 390, viewportWidth: 400, margin: 8 }), -142)
|
||||
})
|
||||
Reference in New Issue
Block a user