Skip to content

Commit ba8e5c3

Browse files
committed
chore(ui): optimize calculate position
1 parent f1fa947 commit ba8e5c3

File tree

14 files changed

+125
-119
lines changed

14 files changed

+125
-119
lines changed

packages/ui/src/components/_date-input/DateInput.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ReactDOM from 'react-dom';
88
import { useAsync, useElement, useEvent, useEventCallback, useForkRef, useResize } from '@react-devui/hooks';
99
import { CloseCircleFilled, SwapRightOutlined } from '@react-devui/icons';
1010
import { checkNodeExist, getClassName, getOriginalSize, getVerticalSidePosition } from '@react-devui/utils';
11+
import { POSITION_CONFIG } from '@react-devui/utils/position/config';
1112

1213
import { usePrefixConfig, useTranslation, useMaxIndex, useLayout } from '../../hooks';
1314
import { TTANSITION_DURING_POPUP } from '../../utils';
@@ -129,7 +130,9 @@ function DateInput(props: DDateInputProps, ref: React.ForwardedRef<DDateInputRef
129130
const updatePosition = useEventCallback(() => {
130131
if (dVisible) {
131132
if (boxRef.current && popupRef.current) {
132-
const { width, height } = getOriginalSize(popupRef.current);
133+
const { height } = getOriginalSize(popupRef.current);
134+
const maxWidth = window.innerWidth - POSITION_CONFIG.space * 2;
135+
const width = Math.min(popupRef.current.scrollWidth, maxWidth);
133136
const { top, left, transformOrigin } = getVerticalSidePosition(
134137
boxRef.current,
135138
{ width, height },
@@ -141,6 +144,7 @@ function DateInput(props: DDateInputProps, ref: React.ForwardedRef<DDateInputRef
141144
setPopupPositionStyle({
142145
top,
143146
left,
147+
maxWidth,
144148
});
145149
setTransformOrigin(transformOrigin);
146150
}

packages/ui/src/components/auto-complete/AutoComplete.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ function AutoComplete<T extends DAutoCompleteItem>(
107107
const popupEl = popupRef.current;
108108
if (boxEl && popupEl) {
109109
const boxWidth = boxEl.getBoundingClientRect().width;
110-
const minWidth = Math.min(boxWidth, window.innerWidth - POSITION_CONFIG.space * 2);
111-
const { width, height } = getOriginalSize(popupEl);
110+
const { height } = getOriginalSize(popupEl);
111+
const maxWidth = window.innerWidth - POSITION_CONFIG.space * 2;
112+
const width = Math.min(Math.max(popupEl.scrollWidth, boxWidth), maxWidth);
112113
const { top, left, transformOrigin } = getVerticalSidePosition(
113114
boxEl,
114-
{ width: Math.max(width, minWidth), height },
115+
{ width, height },
115116
{
116117
placement: 'bottom-left',
117118
inWindow: true,
@@ -121,7 +122,8 @@ function AutoComplete<T extends DAutoCompleteItem>(
121122
setPopupPositionStyle({
122123
top,
123124
left,
124-
minWidth,
125+
minWidth: Math.min(boxWidth, maxWidth),
126+
maxWidth,
125127
});
126128
setTransformOrigin(transformOrigin);
127129
}

packages/ui/src/components/cascader/Cascader.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, { useCallback, useState, useId, useMemo, useRef } from 'react';
1111
import { useEventNotify } from '@react-devui/hooks';
1212
import { CloseOutlined, LoadingOutlined } from '@react-devui/icons';
1313
import { findNested, getClassName, getOriginalSize, getVerticalSidePosition } from '@react-devui/utils';
14+
import { POSITION_CONFIG } from '@react-devui/utils/position/config';
1415

1516
import { usePrefixConfig, useComponentConfig, useGeneralContext, useDValue, useTranslation } from '../../hooks';
1617
import { registerComponentMate } from '../../utils';
@@ -379,7 +380,9 @@ function Cascader<V extends DId, T extends DCascaderItem<V>>(
379380
}}
380381
dInputRef={dInputRef}
381382
dUpdatePosition={(boxEl, popupEl) => {
382-
const { width, height } = getOriginalSize(popupEl);
383+
const { height } = getOriginalSize(popupEl);
384+
const maxWidth = window.innerWidth - POSITION_CONFIG.space * 2;
385+
const width = Math.min(popupEl.scrollWidth, maxWidth);
383386
const { top, left, transformOrigin } = getVerticalSidePosition(
384387
boxEl,
385388
{ width, height },
@@ -393,6 +396,7 @@ function Cascader<V extends DId, T extends DCascaderItem<V>>(
393396
position: {
394397
top,
395398
left,
399+
maxWidth,
396400
},
397401
transformOrigin,
398402
};

packages/ui/src/components/select/Select.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, { useState, useId, useCallback, useMemo, useRef } from 'react';
88

99
import { CloseOutlined, LoadingOutlined, PlusOutlined } from '@react-devui/icons';
1010
import { findNested, getClassName, getOriginalSize, getVerticalSidePosition } from '@react-devui/utils';
11+
import { POSITION_CONFIG } from '@react-devui/utils/position/config';
1112

1213
import { usePrefixConfig, useComponentConfig, useTranslation, useGeneralContext, useDValue } from '../../hooks';
1314
import { registerComponentMate } from '../../utils';
@@ -444,7 +445,7 @@ function Select<V extends DId, T extends DSelectItem<V>>(
444445
}}
445446
dInputRef={dInputRef}
446447
dUpdatePosition={(boxEl, popupEl) => {
447-
const width = boxEl.getBoundingClientRect().width;
448+
const width = Math.min(boxEl.getBoundingClientRect().width, window.innerWidth - POSITION_CONFIG.space * 2);
448449
const { height } = getOriginalSize(popupEl);
449450
const { top, left, transformOrigin } = getVerticalSidePosition(
450451
boxEl,

packages/ui/src/components/table/TableFilter.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ function TableFilter<V extends DId, T extends DTableFilterItem<V>>(
248248
dTrigger="click"
249249
dPlacement="bottom-right"
250250
dArrow={false}
251+
dInWindow
251252
dContent={
252253
<>
253254
{dSearchable && (

packages/ui/src/components/table/TableSearch.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function TableSearch(props: DTableSearchProps, ref: React.ForwardedRef<DTableSea
6767
dTrigger="click"
6868
dPlacement="bottom-right"
6969
dArrow={false}
70+
dInWindow
7071
dContent={
7172
<DInput
7273
className={`${dPrefix}table__filter-search`}

packages/ui/src/components/tree-select/TreeSelect.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ function TreeSelect<V extends DId, T extends DTreeItem<V>>(
410410
dInputRef={dInputRef}
411411
dUpdatePosition={(boxEl, popupEl) => {
412412
const boxWidth = boxEl.getBoundingClientRect().width;
413-
const minWidth = Math.min(boxWidth, window.innerWidth - POSITION_CONFIG.space * 2);
414-
const { width, height } = getOriginalSize(popupEl);
413+
const { height } = getOriginalSize(popupEl);
414+
const maxWidth = window.innerWidth - POSITION_CONFIG.space * 2;
415+
const width = Math.min(Math.max(popupEl.scrollWidth, boxWidth), maxWidth);
415416
const { top, left, transformOrigin } = getVerticalSidePosition(
416417
boxEl,
417-
{ width: Math.max(width, minWidth), height },
418+
{ width, height },
418419
{
419420
placement: 'bottom-left',
420421
inWindow: true,
@@ -425,7 +426,8 @@ function TreeSelect<V extends DId, T extends DTreeItem<V>>(
425426
position: {
426427
top,
427428
left,
428-
minWidth,
429+
minWidth: Math.min(boxWidth, maxWidth),
430+
maxWidth,
429431
},
430432
transformOrigin,
431433
};

packages/ui/src/styles/_variables.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ $colors: (
1616
'black': #000,
1717
) !default;
1818

19-
$popover-space: 10px !default;
20-
21-
$placeholder-color: rgb(var(--#{$variable-prefix}text-color-rgb) / 24%);
22-
$placeholder-disabled-color: rgb(var(--#{$variable-prefix}text-color-rgb) / 8%);
19+
$placeholder-color: rgb(var(--#{$variable-prefix}text-color-rgb) / 24%) !default;
20+
$placeholder-disabled-color: rgb(var(--#{$variable-prefix}text-color-rgb) / 8%) !default;
2321

2422
:root {
2523
/** z-index **/

packages/ui/src/styles/components/auto-complete.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ $auto-complete-option-height: 32px;
22

33
@include b(auto-complete) {
44
position: fixed;
5-
max-width: calc(100vw - #{$popover-space} * 2);
65
background-color: var(--#{$variable-prefix}background-color);
76
border-radius: var(--#{$variable-prefix}border-radius);
87
box-shadow: var(--#{$variable-prefix}shadow-popup);

packages/ui/src/styles/components/cascader.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ $cascader-option-height: 32px;
3535

3636
@include e(popup) {
3737
position: fixed;
38-
max-width: calc(100vw - #{$popover-space} * 2);
3938
overflow: auto hidden;
4039
white-space: nowrap;
4140
background-color: var(--#{$variable-prefix}background-color);

0 commit comments

Comments
 (0)