mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 15:09:19 +08:00
fix: ColPage 回写左右栏宽度与收起状态以恢复侧栏记忆
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
---
|
||||
description: 通用企业级开发规范:分层、边界、可维护性;动手前先定结构
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
# 企业级开发规范
|
||||
|
||||
先想清楚再写代码。优先可维护、可扩展,不为「先跑通」牺牲结构。
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ColPageProps } from './types';
|
||||
|
||||
import { computed, ref, useSlots } from 'vue';
|
||||
import { computed, nextTick, onMounted, ref, useSlots, watch } from 'vue';
|
||||
|
||||
import {
|
||||
ResizableHandle,
|
||||
@@ -20,10 +20,21 @@ const props = withDefaults(defineProps<ColPageProps>(), {
|
||||
leftWidth: 30,
|
||||
rightWidth: 70,
|
||||
resizable: true,
|
||||
leftCollapsed: false,
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:leftWidth': [width: number];
|
||||
'update:leftCollapsed': [collapsed: boolean];
|
||||
}>();
|
||||
|
||||
const delegatedProps = computed(() => {
|
||||
const { leftWidth: _, ...delegated } = props;
|
||||
const {
|
||||
leftWidth: _lw,
|
||||
leftCollapsed: _lc,
|
||||
rightWidth: _rw,
|
||||
...delegated
|
||||
} = props;
|
||||
return delegated;
|
||||
});
|
||||
|
||||
@@ -40,7 +51,20 @@ const delegatedSlots = computed(() => {
|
||||
return resultSlots;
|
||||
});
|
||||
|
||||
const leftPanelRef = ref<InstanceType<typeof ResizablePanel>>();
|
||||
const leftPanelRef = ref<{
|
||||
collapse: () => void;
|
||||
expand: () => void;
|
||||
isCollapsed?: boolean | { value: boolean };
|
||||
} | null>(null);
|
||||
|
||||
function isLeftPanelCollapsed() {
|
||||
const flag = leftPanelRef.value?.isCollapsed;
|
||||
if (typeof flag === 'boolean') return flag;
|
||||
if (flag && typeof flag === 'object' && 'value' in flag) {
|
||||
return !!flag.value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function expandLeft() {
|
||||
leftPanelRef.value?.expand();
|
||||
@@ -50,6 +74,46 @@ function collapseLeft() {
|
||||
leftPanelRef.value?.collapse();
|
||||
}
|
||||
|
||||
/** 收起态的 size 不要写回宽度记忆 */
|
||||
function onLeftResize(size: number) {
|
||||
const collapsedSize = props.leftCollapsedWidth ?? 0;
|
||||
if (props.leftCollapsible && size <= collapsedSize + 0.5) {
|
||||
return;
|
||||
}
|
||||
emit('update:leftWidth', size);
|
||||
}
|
||||
|
||||
function onLeftCollapse() {
|
||||
emit('update:leftCollapsed', true);
|
||||
}
|
||||
|
||||
function onLeftExpand() {
|
||||
emit('update:leftCollapsed', false);
|
||||
}
|
||||
|
||||
function syncCollapsedFromProp(collapsed: boolean) {
|
||||
if (!leftPanelRef.value) return;
|
||||
const current = isLeftPanelCollapsed();
|
||||
if (collapsed && !current) {
|
||||
collapseLeft();
|
||||
} else if (!collapsed && current) {
|
||||
expandLeft();
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (props.leftCollapsed) {
|
||||
void nextTick(() => syncCollapsedFromProp(true));
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.leftCollapsed,
|
||||
(collapsed) => {
|
||||
syncCollapsedFromProp(!!collapsed);
|
||||
},
|
||||
);
|
||||
|
||||
defineExpose({
|
||||
expandLeft,
|
||||
collapseLeft,
|
||||
@@ -74,6 +138,9 @@ defineExpose({
|
||||
:default-size="leftWidth"
|
||||
:max-size="leftMaxWidth"
|
||||
:min-size="leftMinWidth"
|
||||
@collapse="onLeftCollapse"
|
||||
@expand="onLeftExpand"
|
||||
@resize="onLeftResize"
|
||||
>
|
||||
<template #default="slotProps">
|
||||
<slot
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { PageProps } from '../page/types';
|
||||
|
||||
export interface ColPageProps extends PageProps {
|
||||
/**
|
||||
* 左侧宽度
|
||||
* 左侧宽度(百分比);配合 update:leftWidth 可跨页记忆
|
||||
* @default 30
|
||||
*/
|
||||
leftWidth?: number;
|
||||
@@ -10,6 +10,11 @@ export interface ColPageProps extends PageProps {
|
||||
leftMaxWidth?: number;
|
||||
leftCollapsedWidth?: number;
|
||||
leftCollapsible?: boolean;
|
||||
/**
|
||||
* 左侧是否收起;配合 update:leftCollapsed 可跨页记忆
|
||||
* @default false
|
||||
*/
|
||||
leftCollapsed?: boolean;
|
||||
/**
|
||||
* 右侧宽度
|
||||
* @default 70
|
||||
|
||||
Reference in New Issue
Block a user