|
|
@@ -10,6 +10,7 @@
|
|
|
onMounted,
|
|
|
onBeforeUnmount,
|
|
|
computed,
|
|
|
+ nextTick,
|
|
|
} = Vue;
|
|
|
|
|
|
async function ensureOptionalAssets(name) {
|
|
|
@@ -894,6 +895,8 @@
|
|
|
setup(props, { emit }) {
|
|
|
const isOpen = ref(false);
|
|
|
const containerRef = ref(null);
|
|
|
+ const optionsRef = ref(null);
|
|
|
+ const optionsStyle = ref({});
|
|
|
const inputRef = ref(null);
|
|
|
const selectedValue = ref(props.modelValue);
|
|
|
const searchKeyword = ref("");
|
|
|
@@ -1182,6 +1185,74 @@
|
|
|
forceShowAll.value = false;
|
|
|
};
|
|
|
|
|
|
+ // 将选项浮层挂到 body,并按可视区域动态定位,避免被页面滚动容器裁切。
|
|
|
+ const updateOptionsPosition = async () => {
|
|
|
+ await nextTick();
|
|
|
+ const container = containerRef.value;
|
|
|
+ const menu = optionsRef.value;
|
|
|
+ if (!container || !menu) return;
|
|
|
+
|
|
|
+ const rect = container.getBoundingClientRect();
|
|
|
+ const viewportWidth =
|
|
|
+ document.documentElement.clientWidth || window.innerWidth;
|
|
|
+ const viewportHeight =
|
|
|
+ document.documentElement.clientHeight || window.innerHeight;
|
|
|
+ const margin = 12;
|
|
|
+ const gap = 6;
|
|
|
+ const naturalHeight = Math.min(menu.scrollHeight || 300, 320);
|
|
|
+ const spaceBelow = viewportHeight - rect.bottom - margin - gap;
|
|
|
+ const spaceAbove = rect.top - margin - gap;
|
|
|
+ const openUp =
|
|
|
+ spaceBelow < Math.min(naturalHeight, 220) &&
|
|
|
+ spaceAbove > spaceBelow;
|
|
|
+ const available = Math.max(openUp ? spaceAbove : spaceBelow, 80);
|
|
|
+ const maxHeight = Math.min(320, available);
|
|
|
+ const top = openUp
|
|
|
+ ? Math.max(margin, rect.top - maxHeight - gap)
|
|
|
+ : Math.min(
|
|
|
+ viewportHeight - margin - maxHeight,
|
|
|
+ rect.bottom + gap
|
|
|
+ );
|
|
|
+ const menuWidth = Math.min(
|
|
|
+ viewportWidth - margin * 2,
|
|
|
+ Math.max(rect.width, 260)
|
|
|
+ );
|
|
|
+ const left = Math.max(
|
|
|
+ margin,
|
|
|
+ Math.min(rect.left, viewportWidth - margin - menuWidth)
|
|
|
+ );
|
|
|
+
|
|
|
+ optionsStyle.value = {
|
|
|
+ display: "block",
|
|
|
+ position: "fixed",
|
|
|
+ fontSize: "14px",
|
|
|
+ lineHeight: "20px",
|
|
|
+ top: `${top}px`,
|
|
|
+ left: `${left}px`,
|
|
|
+ minWidth: `${Math.min(rect.width, viewportWidth - margin * 2)}px`,
|
|
|
+ maxWidth: `${viewportWidth - margin * 2}px`,
|
|
|
+ maxHeight: `${Math.max(80, maxHeight)}px`,
|
|
|
+ width: `${menuWidth}px`,
|
|
|
+ overflowY: "auto",
|
|
|
+ overflowX: "hidden",
|
|
|
+ WebkitOverflowScrolling: "touch",
|
|
|
+ overscrollBehavior: "contain",
|
|
|
+ zIndex: 10000,
|
|
|
+ };
|
|
|
+
|
|
|
+ await nextTick();
|
|
|
+ const menuRect = menu.getBoundingClientRect();
|
|
|
+ if (menuRect.right > viewportWidth - margin) {
|
|
|
+ optionsStyle.value = {
|
|
|
+ ...optionsStyle.value,
|
|
|
+ left: `${Math.max(
|
|
|
+ margin,
|
|
|
+ viewportWidth - margin - menuRect.width
|
|
|
+ )}px`,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
const openDropdown = (showAll = false) => {
|
|
|
if (props.disabled) return;
|
|
|
if (activeSsSelectCloser && activeSsSelectCloser !== closeDropdown) {
|
|
|
@@ -1190,6 +1261,7 @@
|
|
|
forceShowAll.value = showAll;
|
|
|
isOpen.value = true;
|
|
|
activeSsSelectCloser = closeDropdown;
|
|
|
+ updateOptionsPosition();
|
|
|
};
|
|
|
|
|
|
const closeDropdown = ({ restoreInput = true } = {}) => {
|
|
|
@@ -1242,6 +1314,7 @@
|
|
|
filterKeyword.value = value;
|
|
|
forceShowAll.value = false;
|
|
|
isOpen.value = true;
|
|
|
+ updateOptionsPosition();
|
|
|
emit("search", value);
|
|
|
};
|
|
|
|
|
|
@@ -1254,6 +1327,7 @@
|
|
|
filterKeyword.value = "";
|
|
|
forceShowAll.value = true;
|
|
|
isOpen.value = true;
|
|
|
+ updateOptionsPosition();
|
|
|
};
|
|
|
|
|
|
const selectOption = (option) => {
|
|
|
@@ -1269,11 +1343,18 @@
|
|
|
};
|
|
|
|
|
|
const handleClickOutside = (event) => {
|
|
|
- if (!containerRef.value?.contains(event.target)) {
|
|
|
+ if (
|
|
|
+ !containerRef.value?.contains(event.target) &&
|
|
|
+ !optionsRef.value?.contains(event.target)
|
|
|
+ ) {
|
|
|
closeDropdown();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ const handleViewportChange = () => {
|
|
|
+ if (isOpen.value) updateOptionsPosition();
|
|
|
+ };
|
|
|
+
|
|
|
watch(
|
|
|
() => props.modelValue,
|
|
|
(newValue) => {
|
|
|
@@ -1331,6 +1412,8 @@
|
|
|
|
|
|
onMounted(() => {
|
|
|
document.addEventListener("click", handleClickOutside);
|
|
|
+ window.addEventListener("resize", handleViewportChange);
|
|
|
+ window.addEventListener("scroll", handleViewportChange, true);
|
|
|
loadRemoteOptions();
|
|
|
if (!needRemoteData.value && Array.isArray(props.options)) {
|
|
|
emit("loaded", props.options);
|
|
|
@@ -1344,10 +1427,14 @@
|
|
|
activeSsSelectCloser = null;
|
|
|
}
|
|
|
document.removeEventListener("click", handleClickOutside);
|
|
|
+ window.removeEventListener("resize", handleViewportChange);
|
|
|
+ window.removeEventListener("scroll", handleViewportChange, true);
|
|
|
});
|
|
|
|
|
|
return {
|
|
|
containerRef,
|
|
|
+ optionsRef,
|
|
|
+ optionsStyle,
|
|
|
inputRef,
|
|
|
isOpen,
|
|
|
selectedValue,
|
|
|
@@ -1394,30 +1481,38 @@
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
- <div class="ss-options" v-show="isOpen">
|
|
|
- <div
|
|
|
- v-if="finalLoading"
|
|
|
- class="option-item loading-item"
|
|
|
- >
|
|
|
- <span class="loading-text">加载中...</span>
|
|
|
- </div>
|
|
|
- <div
|
|
|
- v-else-if="filteredOptions.length === 0"
|
|
|
- class="option-item no-options"
|
|
|
- >
|
|
|
- 无选项
|
|
|
- </div>
|
|
|
+ <teleport to="body">
|
|
|
<div
|
|
|
- v-else
|
|
|
- v-for="(option, index) in filteredOptions"
|
|
|
- :key="index"
|
|
|
- class="option-item"
|
|
|
- :class="{ selected: option[mapping.value] === selectedValue }"
|
|
|
- @click.stop="selectOption(option)"
|
|
|
+ ref="optionsRef"
|
|
|
+ class="ss-options"
|
|
|
+ :style="optionsStyle"
|
|
|
+ v-if="isOpen"
|
|
|
+ @click.stop
|
|
|
>
|
|
|
- {{ option[mapping.text] }}
|
|
|
+ <div
|
|
|
+ v-if="finalLoading"
|
|
|
+ class="option-item loading-item"
|
|
|
+ >
|
|
|
+ <span class="loading-text">加载中...</span>
|
|
|
+ </div>
|
|
|
+ <div
|
|
|
+ v-else-if="filteredOptions.length === 0"
|
|
|
+ class="option-item no-options"
|
|
|
+ >
|
|
|
+ 无选项
|
|
|
+ </div>
|
|
|
+ <div
|
|
|
+ v-else
|
|
|
+ v-for="(option, index) in filteredOptions"
|
|
|
+ :key="index"
|
|
|
+ class="option-item"
|
|
|
+ :class="{ selected: option[mapping.value] === selectedValue }"
|
|
|
+ @click.stop="selectOption(option)"
|
|
|
+ >
|
|
|
+ {{ option[mapping.text] }}
|
|
|
+ </div>
|
|
|
</div>
|
|
|
- </div>
|
|
|
+ </teleport>
|
|
|
</div>
|
|
|
`,
|
|
|
};
|