(component): [dropdown]: contentOffset属性,控制触发器距离面板的偏移距离

This commit is contained in:
sight 2022-06-22 21:43:04 +08:00
parent 3dc698c99e
commit 8d9ef82cb3
2 changed files with 22 additions and 9 deletions

View File

@ -33,6 +33,7 @@ export interface LayDropdownProps {
updateAtScroll?: boolean; updateAtScroll?: boolean;
autoFixPosition?: boolean; autoFixPosition?: boolean;
clickOutsideToClose?: boolean; clickOutsideToClose?: boolean;
contentOffset?: number;
} }
const props = withDefaults(defineProps<LayDropdownProps>(), { const props = withDefaults(defineProps<LayDropdownProps>(), {
@ -45,6 +46,7 @@ const props = withDefaults(defineProps<LayDropdownProps>(), {
updateAtScroll: false, updateAtScroll: false,
autoFixPosition: true, autoFixPosition: true,
clickOutsideToClose: true, clickOutsideToClose: true,
contentOffset: 2,
}); });
const dropdownRef = shallowRef<HTMLElement | undefined>(); const dropdownRef = shallowRef<HTMLElement | undefined>();
@ -52,7 +54,6 @@ const contentRef = shallowRef<HTMLElement | undefined>();
const contentStyle = ref<CSSProperties>({}); const contentStyle = ref<CSSProperties>({});
const { width: windowWidth, height: windowHeight } = useWindowSize(); const { width: windowWidth, height: windowHeight } = useWindowSize();
const openState = ref(false); const openState = ref(false);
const contentSpace = 2;
const emit = defineEmits(["open", "hide"]); const emit = defineEmits(["open", "hide"]);
@ -156,11 +157,11 @@ const getFitPlacement = (
) => { ) => {
// //
if (triggerRect.bottom + contentRect.height > windowHeight.value) { if (triggerRect.bottom + contentRect.height > windowHeight.value) {
top = -contentRect.height - contentSpace; top = -contentRect.height - props.contentOffset;
} }
// //
if (triggerRect.top - contentRect.height < 0) { if (triggerRect.top - contentRect.height < 0) {
top = triggerRect.height + contentSpace; top = triggerRect.height + props.contentOffset;
} }
if (["bottom-right", "top-right"].includes(placement)) { if (["bottom-right", "top-right"].includes(placement)) {
// //
@ -208,32 +209,32 @@ const getContentOffset = (
switch (placement) { switch (placement) {
case "top": case "top":
return { return {
top: -contentRect.height - contentSpace, top: -contentRect.height - props.contentOffset,
left: -(contentRect.width - triggerRect.width) / 2, left: -(contentRect.width - triggerRect.width) / 2,
}; };
case "top-left": case "top-left":
return { return {
top: -contentRect.height - contentSpace, top: -contentRect.height - props.contentOffset,
left: 0, left: 0,
}; };
case "top-right": case "top-right":
return { return {
top: -contentRect.height - contentSpace, top: -contentRect.height - props.contentOffset,
left: -(contentRect.width - triggerRect.width), left: -(contentRect.width - triggerRect.width),
}; };
case "bottom": case "bottom":
return { return {
top: triggerRect.height + contentSpace, top: triggerRect.height + props.contentOffset,
left: -(contentRect.width - triggerRect.width) / 2, left: -(contentRect.width - triggerRect.width) / 2,
}; };
case "bottom-left": case "bottom-left":
return { return {
top: triggerRect.height + contentSpace, top: triggerRect.height + props.contentOffset,
left: 0, left: 0,
}; };
case "bottom-right": case "bottom-right":
return { return {
top: triggerRect.height + contentSpace, top: triggerRect.height + props.contentOffset,
left: -(contentRect.width - triggerRect.width), left: -(contentRect.width - triggerRect.width),
}; };
default: default:

View File

@ -240,6 +240,17 @@ export default {
</template> </template>
</lay-dropdown> </lay-dropdown>
&nbsp;&nbsp; &nbsp;&nbsp;
<lay-dropdown placement="bottom-left" updateAtScroll :contentOffset="8">
<lay-button type="primary">contentOffset: 8px</lay-button>
<template #content>
<lay-dropdown-menu>
<lay-dropdown-menu-item>选项一</lay-dropdown-menu-item>
<lay-dropdown-menu-item>选项二</lay-dropdown-menu-item>
<lay-dropdown-menu-item>选项三</lay-dropdown-menu-item>
</lay-dropdown-menu>
</template>
</lay-dropdown>
&nbsp;&nbsp;
<lay-dropdown placement="bottom-left" :autoFixPosition="true" :clickOutsideToClose="false"> <lay-dropdown placement="bottom-left" :autoFixPosition="true" :clickOutsideToClose="false">
<lay-button type="primary" :size="btnSize">autoFixPosition</lay-button> <lay-button type="primary" :size="btnSize">autoFixPosition</lay-button>
<template #content> <template #content>
@ -291,6 +302,7 @@ export default {
| updateAtScroll | 是否在容器滚动时更新下拉面板的位置,默认 `false` | `true` `false` | | updateAtScroll | 是否在容器滚动时更新下拉面板的位置,默认 `false` | `true` `false` |
| autoFixPosition | 是否在触发器或下拉面板尺寸变化时更新下拉面板位置,面板尺寸变化参见级联选择器,默认 `true` | `true` `false` | | autoFixPosition | 是否在触发器或下拉面板尺寸变化时更新下拉面板位置,面板尺寸变化参见级联选择器,默认 `true` | `true` `false` |
| clickOutsideToClose| 是否点击外部关闭下拉面板,默认 `true`| `true` `false`| | clickOutsideToClose| 是否点击外部关闭下拉面板,默认 `true`| `true` `false`|
| contentOffset | 下拉面板距离触发器的偏移距离,默认 2| -|
::: :::