Skip to content

Commit

Permalink
fix(select-input): fix select-input popup content width calculation p…
Browse files Browse the repository at this point in the history
…roblem (#2647)

* fix(select-input): fix select-input popup content width calculation problem

* fix(select-input): fix select-input can't get width
  • Loading branch information
HaixingOoO authored Dec 6, 2023
1 parent c10e966 commit c31a0e2
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/select-input/useOverlayInnerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,27 @@ export default function useOverlayInnerStyle(

const matchWidthFunc = (triggerElement: HTMLElement, popupElement: HTMLElement) => {
if (!triggerElement || !popupElement) return;
// 避免因滚动条出现文本省略,预留宽度 8
const SCROLLBAR_WIDTH = popupElement.scrollHeight > popupElement.offsetHeight ? 8 : 0;

// 设置display来可以获取popupElement的宽度
// eslint-disable-next-line no-param-reassign
popupElement.style.display = '';
// popupElement的scrollBar宽度
const overlayScrollWidth = popupElement.offsetWidth - popupElement.scrollWidth;

/**
* issue:https://github.com/Tencent/tdesign-react/issues/2642
*
* popupElement的内容宽度不超过triggerElement的宽度,就使用triggerElement的宽度减去popup的滚动条宽度,
* 让popupElement的宽度加上scrollBar的宽度等于triggerElement的宽度;
*
* popupElement的内容宽度超过triggerElement的宽度,就使用popupElement的scrollWidth,
* 不用offsetWidth是会包含scrollBar的宽度
*/
const width =
popupElement.offsetWidth + SCROLLBAR_WIDTH >= triggerElement.offsetWidth
? popupElement.offsetWidth
: triggerElement.offsetWidth;
popupElement.offsetWidth - overlayScrollWidth > triggerElement.offsetWidth
? popupElement.scrollWidth
: triggerElement.offsetWidth - overlayScrollWidth;

let otherOverlayInnerStyle: React.CSSProperties = {};
if (popupProps && typeof popupProps.overlayInnerStyle === 'object' && !popupProps.overlayInnerStyle.width) {
otherOverlayInnerStyle = popupProps.overlayInnerStyle;
Expand Down

0 comments on commit c31a0e2

Please sign in to comment.