feat(jd): 类目筛选改京麦同款级联面板(一/二级静态导航+末级全集搜索点选)

This commit is contained in:
2026-11-16 00:14:10 +08:00
parent 4506253989
commit ee3ff6fe8c
5 changed files with 505 additions and 27 deletions
@@ -0,0 +1,333 @@
<script setup lang="ts">
/** 京东数据库「类目」筛选的京麦同款级联面板(2026-11-16 客户口径「京东类目要做成京麦
* 同款样式」)。三栏形状照京麦:第一栏静态一级导航(jd-category-tree,京东公开类目
* 体系;服饰内衣子树二级按京麦面板真机样本逐字)、第二栏该一级的静态二级(hover/点击
* 展开;radio 不选中——纯导航)、第三栏「已落库末级类目全集+搜索」(选中发生地)。
*
* 数据边界如实声明:采集链只有末级类目名(categoryDetailVO.lastCategoryName),没有
* 一/二级归属,所以第二栏与第三栏**不联动**(点了二级第三栏照旧全集+搜索)——过滤语义
* 零风险(仍按 category_path 等值,后端零改动);采集链补齐层级后,把静态树换掉、第三栏
* 按选中二级过滤即可升级真联动。零依赖自绘(radio 圆点/箭头都是 CSS),无 element 依赖。
*
* 交互:点开面板 → 点/hover 一级展第二栏 → 第三栏搜索或点行选中(radio 蓝)→ 收面板;
* 「全部类目」行=清选;触发框已选时出 × 清除;面板外点透明遮罩关闭。 */
import { computed, ref } from 'vue'
import { jdCategoryNavChildren, jdCategoryNavRoots } from './jd-category-tree.ts'
const props = defineProps<{
/** 已落库末级类目选项(/categories 现算,页面传入:{name, productCount})。 */
options: { name: string; productCount: number }[]
/** 当前选中末级类目名;'' = 全部类目。 */
modelValue: string
}>()
const emit = defineEmits<{ (event: 'change', value: string): void }>()
const open = ref(false)
const activeRoot = ref('')
const searchText = ref('')
const roots = jdCategoryNavRoots()
/** 第二栏=点亮一级的静态二级;无静态子级的一级不出第二栏。 */
const activeChildren = computed(() => jdCategoryNavChildren(activeRoot.value))
/** 第三栏=已落库末级全集按搜索词过滤(大小写不敏感的 includes)。 */
const filteredLeaves = computed(() => {
const keyword = searchText.value.trim().toLowerCase()
if (!keyword) return props.options
return props.options.filter((option) => option.name.toLowerCase().includes(keyword))
})
function toggle(): void {
if (open.value) {
close()
return
}
activeRoot.value = ''
searchText.value = ''
open.value = true
}
function close(): void {
open.value = false
activeRoot.value = ''
}
function pickRoot(name: string): void {
activeRoot.value = name
}
function pickAll(): void {
emit('change', '')
close()
}
function pickLeaf(name: string): void {
emit('change', name)
close()
}
function clear(): void {
emit('change', '')
}
</script>
<template>
<div class="cat-cascader">
<button
class="cat-trigger"
type="button"
:class="{ open, filled: !!modelValue }"
@click="toggle"
>
<span class="cat-trigger-text" :title="modelValue || ''">{{ modelValue || '全部类目' }}</span>
<span
v-if="modelValue"
class="cat-clear"
role="button"
aria-label="清除类目"
@click.stop="clear"
>×</span>
<span class="cat-chevron" aria-hidden="true"></span>
</button>
<!-- 面板外点透明遮罩关闭(零 document 监听器,无泄漏面)。 -->
<div v-if="open" class="cat-mask" @click="close"></div>
<div v-if="open" class="cat-panel">
<!-- 第一栏:静态一级导航(radio 形状照京麦,点选只展开不落值)。 -->
<div class="cat-col">
<div
v-for="root in roots"
:key="root"
class="cat-row"
:class="{ active: activeRoot === root }"
@click="pickRoot(root)"
@mouseenter="pickRoot(root)"
>
<span class="cat-dot" aria-hidden="true"></span>
<span class="cat-name" :title="root">{{ root }}</span>
<span v-if="jdCategoryNavChildren(root).length" class="cat-arrow" aria-hidden="true">›</span>
</div>
</div>
<!-- 第二栏:点亮一级的静态二级(纯导航;归属数据缺,不联动第三栏——组件头注释)。 -->
<div v-if="activeChildren.length" class="cat-col">
<div
v-for="child in activeChildren"
:key="child"
class="cat-row"
>
<span class="cat-dot" aria-hidden="true"></span>
<span class="cat-name" :title="child">{{ child }}</span>
<span class="cat-arrow" aria-hidden="true">›</span>
</div>
</div>
<!-- 第三栏:已落库末级全集+搜索(选中发生地;点行=按 category_path 等值过滤)。 -->
<div class="cat-col cat-col-leaf">
<div class="cat-row cat-row-all" :class="{ active: !modelValue }" @click="pickAll">
<span class="cat-dot" aria-hidden="true"></span>
<span class="cat-name">全部类目</span>
</div>
<input
v-model="searchText"
type="text"
class="cat-search"
placeholder="搜索类目"
@click.stop
/>
<div v-if="!filteredLeaves.length" class="cat-empty">未找到类目</div>
<div
v-for="leaf in filteredLeaves"
:key="leaf.name"
class="cat-row"
:class="{ active: modelValue === leaf.name }"
@click="pickLeaf(leaf.name)"
>
<span class="cat-dot" :class="{ on: modelValue === leaf.name }" aria-hidden="true"></span>
<span class="cat-name" :title="leaf.name">{{ leaf.name }}</span>
<span class="cat-count">{{ leaf.productCount }}</span>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.cat-cascader {
position: relative;
min-width: 0;
}
/* 触发框像素对齐页面 .form-group select(42 高/9 圆角/#cbd9e6 边框/#f8fbfd 底)。 */
.cat-trigger {
display: flex;
align-items: center;
gap: 6px;
width: 100%;
min-height: 42px;
padding: 8px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
cursor: pointer;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.cat-trigger:hover {
border-color: #9fb7cd;
}
.cat-trigger.open,
.cat-trigger:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.cat-trigger-text {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
text-align: left;
}
.cat-trigger:not(.filled) .cat-trigger-text {
color: #8293a5;
}
.cat-clear {
flex: none;
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #e7eef5;
color: #5b6f83;
font-size: 13px;
line-height: 1;
}
.cat-clear:hover {
background: #dbe7f1;
color: #24384d;
}
/* 下拉箭头:CSS 三角(京麦同款向下 chevron,展开翻向上)。 */
.cat-chevron {
flex: none;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #7b8a99;
transition: transform 160ms ease;
}
.cat-trigger.open .cat-chevron {
transform: rotate(180deg);
}
.cat-mask {
position: fixed;
inset: 0;
z-index: 2550;
}
.cat-panel {
position: absolute;
top: calc(100% + 4px);
left: 0;
z-index: 2560;
display: flex;
align-items: stretch;
max-width: min(720px, 90vw);
border: 1px solid #d8e3ee;
border-radius: 10px;
background: #ffffff;
box-shadow: 0 16px 44px rgba(39, 67, 94, 0.18);
overflow: hidden;
}
.cat-col {
width: 210px;
max-height: 340px;
overflow-y: auto;
padding: 6px 0;
border-right: 1px solid #e7eef5;
}
.cat-col:last-child {
border-right: 0;
}
.cat-col-leaf {
width: 240px;
}
/* 行=京麦同款:圆点 radio + 名称 + 右箭头;hover 浅蓝底。 */
.cat-row {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
font-size: 13.5px;
color: #24384d;
cursor: pointer;
}
.cat-row:hover,
.cat-row.active {
background: #f1f7fb;
}
.cat-row.active {
color: #2f5d8b;
font-weight: 550;
}
.cat-dot {
flex: none;
width: 13px;
height: 13px;
border: 1px solid #c0ccda;
border-radius: 50%;
background: #ffffff;
}
/* radio 选中=京麦同款蓝芯;一级/二级的圆点是导航形状,不落选中态。 */
.cat-dot.on {
border-color: #3b82c4;
background: radial-gradient(circle, #3b82c4 0 4px, #ffffff 4.6px);
}
.cat-name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.cat-arrow {
flex: none;
color: #9fb0c0;
font-size: 15px;
line-height: 1;
}
.cat-count {
flex: none;
color: #8293a5;
font-size: 12px;
}
.cat-search {
display: block;
width: calc(100% - 20px);
margin: 4px 10px 8px;
min-height: 30px;
padding: 4px 10px;
border: 1px solid #cbd9e6;
border-radius: 8px;
background: #f8fbfd;
color: #24384d;
font-size: 12.5px;
font-family: inherit;
outline: none;
}
.cat-search:focus {
background: #ffffff;
border-color: #5f85ad;
}
.cat-empty {
padding: 16px 12px;
color: #8293a5;
font-size: 12.5px;
text-align: center;
}
</style>