fix(jd): 类目面板超视窗定位钳制(B114 尾件入库)——面板打开/栏数与搜索态变化后按真实 rect 重算 left 偏移,右缘贴齐视口后下钻向左生长,末栏不再被窗缘裁掉;钳制数学抽纯函数 jd-category-cascader-position.ts+4 例纯逻辑测试

This commit is contained in:
2026-11-16 14:02:54 +08:00
parent a5e390750b
commit 79eb7a7f16
3 changed files with 98 additions and 3 deletions
@@ -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">