Skip to content

Commit d484e1f

Browse files
committed
fix: rangePicker popup pos, close #6073, #6074
1 parent 7fa1fe4 commit d484e1f

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

components/vc-picker/RangePicker.tsx

+49-29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import useState from '../_util/hooks/useState';
3737
import classNames from '../_util/classNames';
3838
import { useProviderTrigger } from '../vc-trigger/context';
3939
import { legacyPropsWarning } from './utils/warnUtil';
40+
import { useElementSize } from '../_util/hooks/_vueuse/useElementSize';
4041

4142
function reorderValues<DateType>(
4243
values: RangeValue<DateType>,
@@ -372,15 +373,55 @@ function RangerPicker<DateType>() {
372373

373374
const startOpen = computed(() => mergedOpen.value && mergedActivePickerIndex.value === 0);
374375
const endOpen = computed(() => mergedOpen.value && mergedActivePickerIndex.value === 1);
375-
376+
const panelLeft = ref(0);
377+
const arrowLeft = ref(0);
376378
// ============================= Popup =============================
377379
// Popup min width
378380
const popupMinWidth = ref(0);
379-
watch(mergedOpen, () => {
381+
const { width: containerWidth } = useElementSize(containerRef);
382+
watch([mergedOpen, containerWidth], () => {
380383
if (!mergedOpen.value && containerRef.value) {
381-
popupMinWidth.value = containerRef.value.offsetWidth;
384+
popupMinWidth.value = containerWidth.value;
382385
}
383386
});
387+
const { width: panelDivWidth } = useElementSize(panelDivRef);
388+
const { width: arrowWidth } = useElementSize(arrowRef);
389+
const { width: startInputDivWidth } = useElementSize(startInputDivRef);
390+
const { width: separatorWidth } = useElementSize(separatorRef);
391+
watch(
392+
[
393+
mergedActivePickerIndex,
394+
mergedOpen,
395+
panelDivWidth,
396+
arrowWidth,
397+
startInputDivWidth,
398+
separatorWidth,
399+
() => props.direction,
400+
],
401+
() => {
402+
arrowLeft.value = 0;
403+
if (mergedOpen.value && mergedActivePickerIndex.value) {
404+
if (startInputDivRef.value && separatorRef.value && panelDivRef.value) {
405+
arrowLeft.value = startInputDivWidth.value + separatorWidth.value;
406+
if (
407+
panelDivWidth.value &&
408+
arrowWidth.value &&
409+
arrowLeft.value >
410+
panelDivWidth.value -
411+
arrowWidth.value -
412+
(props.direction === 'rtl' || arrowRef.value.offsetLeft > arrowLeft.value
413+
? 0
414+
: arrowRef.value.offsetLeft)
415+
) {
416+
panelLeft.value = arrowLeft.value;
417+
}
418+
}
419+
} else if (mergedActivePickerIndex.value === 0) {
420+
panelLeft.value = 0;
421+
}
422+
},
423+
{ immediate: true },
424+
);
384425

385426
// ============================ Trigger ============================
386427
const triggerRef = ref<any>();
@@ -981,32 +1022,11 @@ function RangerPicker<DateType>() {
9811022
direction,
9821023
autocomplete = 'off',
9831024
} = props;
984-
let arrowLeft = 0;
985-
let panelLeft = 0;
986-
if (
987-
mergedActivePickerIndex.value &&
988-
startInputDivRef.value &&
989-
separatorRef.value &&
990-
panelDivRef.value
991-
) {
992-
// Arrow offset
993-
arrowLeft = startInputDivRef.value.offsetWidth + separatorRef.value.offsetWidth;
994-
if (
995-
panelDivRef.value.offsetWidth &&
996-
arrowRef.value.offsetWidth &&
997-
arrowLeft >
998-
panelDivRef.value.offsetWidth -
999-
arrowRef.value.offsetWidth -
1000-
(direction === 'rtl' || arrowRef.value.offsetLeft > arrowLeft
1001-
? 0
1002-
: arrowRef.value.offsetLeft)
1003-
) {
1004-
panelLeft = arrowLeft;
1005-
}
1006-
}
10071025

10081026
const arrowPositionStyle =
1009-
direction === 'rtl' ? { right: `${arrowLeft}px` } : { left: `${arrowLeft}px` };
1027+
direction === 'rtl'
1028+
? { right: `${arrowLeft.value}px` }
1029+
: { left: `${arrowLeft.value}px` };
10101030

10111031
function renderPanels() {
10121032
let panels: VueNode;
@@ -1097,7 +1117,7 @@ function RangerPicker<DateType>() {
10971117
return (
10981118
<div
10991119
class={`${prefixCls}-panel-container`}
1100-
style={{ marginLeft: `${panelLeft}px` }}
1120+
style={{ marginLeft: `${panelLeft.value}px` }}
11011121
ref={panelDivRef}
11021122
onMousedown={e => {
11031123
e.preventDefault();
@@ -1168,7 +1188,7 @@ function RangerPicker<DateType>() {
11681188
if (mergedActivePickerIndex.value === 0) {
11691189
activeBarWidth = startInputDivRef.value.offsetWidth;
11701190
} else {
1171-
activeBarLeft = arrowLeft;
1191+
activeBarLeft = arrowLeft.value;
11721192
activeBarWidth = endInputDivRef.value.offsetWidth;
11731193
}
11741194
}

0 commit comments

Comments
 (0)