Skip to content

Commit fda5244

Browse files
committed
fix(ellipsisText): update maxWidth handling
1 parent ab9a930 commit fda5244

2 files changed

Lines changed: 17 additions & 64 deletions

File tree

src/ellipsisText/__tests__/ellipsisText.test.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let wrapper: RenderResult, element;
1919
describe('test ellipsis text if not set max width', () => {
2020
beforeEach(() => {
2121
jest.spyOn(document.documentElement, 'scrollWidth', 'get').mockImplementation(() => 100);
22+
jest.spyOn(document.documentElement, 'clientWidth', 'get').mockImplementation(() => 600);
2223
jest.spyOn(document.documentElement, 'offsetWidth', 'get').mockImplementation(() => 600);
2324
Object.defineProperty(window, 'getComputedStyle', {
2425
value: jest.fn(() => ({
@@ -133,7 +134,7 @@ describe('test ellipsis text if set max width', () => {
133134
element = getByText(value);
134135

135136
expect(element).toBeInTheDocument();
136-
expect(element.style.maxWidth).toBe('900px');
137+
expect(element.style.maxWidth).toBe('1000px');
137138
expect(element.style.cursor).toBe('pointer');
138139

139140
fireEvent.mouseEnter(element);
@@ -150,7 +151,7 @@ describe('test ellipsis text if set max width', () => {
150151
element = getByText(value);
151152

152153
expect(element).toBeInTheDocument();
153-
expect(element.style.maxWidth).toBe('810px');
154+
expect(element.style.maxWidth).toBe('90%');
154155
expect(element.style.cursor).toBe('pointer');
155156

156157
fireEvent.mouseEnter(element);
@@ -167,24 +168,7 @@ describe('test ellipsis text if set max width', () => {
167168
element = getByText(value);
168169

169170
expect(element).toBeInTheDocument();
170-
expect(element.style.maxWidth).toBe('700px');
171-
expect(element.style.cursor).toBe('pointer');
172-
173-
fireEvent.mouseEnter(element);
174-
expect(container.querySelector('.ant-tooltip-open')).toBeNull();
175-
expect(getAllByText(value).length).toBe(1);
176-
177-
fireEvent.mouseLeave(element);
178-
});
179-
test('The maximum is a not comply with the rules,render correct value in ellipsis', () => {
180-
const { container, getByText, getAllByText } = render(
181-
<EllipsisText {...defaultProps} maxWidth="20.4" />
182-
);
183-
const { value } = defaultProps;
184-
element = getByText(value);
185-
186-
expect(element).toBeInTheDocument();
187-
expect(element.style.maxWidth).toBe('900px');
171+
expect(element.style.maxWidth).toBe('calc(100% - 200px)');
188172
expect(element.style.cursor).toBe('pointer');
189173

190174
fireEvent.mouseEnter(element);

src/ellipsisText/index.tsx

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const EllipsisText = (props: IEllipsisTextProps) => {
6464
: null;
6565

6666
const [visible, setVisible] = useState(false);
67-
const [width, setWidth] = useState<number | string>(DEFAULT_MAX_WIDTH);
67+
const [width, setWidth] = useState<number | string | undefined>(maxWidth);
6868
const [cursor, setCursor] = useState('default');
6969

7070
useLayoutEffect(() => {
@@ -96,40 +96,6 @@ const EllipsisText = (props: IEllipsisTextProps) => {
9696
return parseInt(getStyle(dom, attr));
9797
};
9898

99-
/**
100-
* @description: 10 -> 10,
101-
* @description: 10px -> 10,
102-
* @description: 90% -> ele.width * 0.9
103-
* @description: calc(100% - 32px) -> ele.width - 32
104-
* @param {*} ele
105-
* @param {string & number} maxWidth
106-
* @return {*}
107-
*/
108-
const transitionWidth = (ele: HTMLElement, maxWidth: string | number) => {
109-
const eleWidth = getActualWidth(ele);
110-
111-
if (typeof maxWidth === 'number') {
112-
return maxWidth > eleWidth ? eleWidth : maxWidth; // 如果父元素的宽度小于传入的最大宽度,返回父元素的宽度
113-
}
114-
115-
const numMatch = maxWidth.match(/^(\d+)(px)?$/);
116-
if (numMatch) {
117-
return +numMatch[1] > eleWidth ? eleWidth : +numMatch[1]; // 如果父元素的宽度小于传入的最大宽度,返回父元素的宽度
118-
}
119-
120-
const percentMatch = maxWidth.match(/^(\d+)%$/);
121-
if (percentMatch) {
122-
return eleWidth * (parseInt(percentMatch[1]) / 100);
123-
}
124-
125-
const relativeMatch = maxWidth.match(/^calc\(100% - (\d+)px\)$/);
126-
if (relativeMatch) {
127-
return eleWidth - parseInt(relativeMatch[1]);
128-
}
129-
130-
return eleWidth;
131-
};
132-
13399
const hideEleContent = (node: HTMLElement) => {
134100
node.style.display = 'none';
135101
};
@@ -144,21 +110,24 @@ const EllipsisText = (props: IEllipsisTextProps) => {
144110
* @return {*}
145111
*/
146112
const getContainerWidth = (ele: HTMLElement): number | string => {
113+
// 如果设置了最大宽度,则直接返回宽度
114+
if (maxWidth) return maxWidth;
147115
if (!ele) return DEFAULT_MAX_WIDTH;
148116

149-
const { scrollWidth, parentElement } = ele;
150-
151-
// 如果是行内元素,获取不到宽度,则向上寻找父元素
152-
if (scrollWidth === 0) {
153-
return getContainerWidth(parentElement!);
154-
}
155-
// 如果设置了最大宽度,则直接返回宽度
156-
if (maxWidth) {
157-
return transitionWidth(ele, maxWidth);
117+
// 如果是行内元素获取不到宽度或者本身没有宽度,则向上寻找父元素
118+
if (ele.scrollWidth === 0 || ele.clientWidth === 0) {
119+
return getContainerWidth(ele.parentElement!);
158120
}
159121

122+
// 隐藏当前文本元素后,从查找到的父组件宽度中计算剩余可用宽度
160123
hideEleContent(ellipsisRef.current!);
161124

125+
// 如果父组件是非行内元素,但其宽度是由子元素撑开的,仍需要向上查找
126+
if (ele.scrollWidth === 0 || ele.clientWidth === 0) {
127+
showEleContent(ellipsisRef.current!);
128+
return getContainerWidth(ele.parentElement!);
129+
}
130+
162131
const availableWidth = getAvailableWidth(ele);
163132

164133
return availableWidth < 0 ? 0 : availableWidth;

0 commit comments

Comments
 (0)