Skip to content

Commit 10129ec

Browse files
committed
fix: the width of title label should auto fit content width when autoWidth is true, fix #3880
1 parent 37266da commit 10129ec

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/vchart/src/plugin/components/tooltip-handler/dom-tooltip-handler.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,32 @@ export class DomTooltipHandler extends BaseTooltipHandler {
304304
}
305305
protected _updateDomStyle(sizeKey: 'width' | 'height' = 'width') {
306306
const rootDom = this._rootDom;
307-
308307
const contentDom = [...(rootDom.children as any)].find(child =>
309308
child.className.includes(TOOLTIP_CONTENT_BOX_CLASS_NAME)
310309
);
310+
const titleDom = [...(rootDom.children as any)].find(child => child.className.includes(TOOLTIP_TITLE_CLASS_NAME));
311311

312312
if (contentDom) {
313313
const tooltipSpec = this._component.getSpec() as ITooltipSpec;
314314
const contentStyle: Partial<CSSStyleDeclaration> = {};
315+
const titleLabel = tooltipSpec.style?.titleLabel;
316+
const autoFixTitleWidth = titleLabel && titleLabel.autoWidth && titleLabel.multiLine !== false;
317+
318+
if (autoFixTitleWidth && titleDom) {
319+
const maxWidth = [...(contentDom.children as any)].reduce((res, col) => {
320+
return sizeKey === 'height'
321+
? res + col.getBoundingClientRect().width
322+
: Math.max(res, col.getBoundingClientRect().width);
323+
}, 0);
324+
325+
if (maxWidth > 0) {
326+
titleDom.style.maxWidth = `${maxWidth}px`;
327+
// 需要再计算一次,因为之前可能因为没有设置maxWidth, content的dom被撑宽了
328+
titleDom.style.maxWidth = `${Math.ceil(contentDom.getBoundingClientRect().width)}px`;
329+
}
330+
}
315331

316332
if (isValid(tooltipSpec?.style?.maxContentHeight)) {
317-
const titleDom = rootDom.children[0];
318333
const titleHeight =
319334
titleDom && titleDom.className.includes(TOOLTIP_TITLE_CLASS_NAME)
320335
? titleDom.getBoundingClientRect().height + (tooltipSpec.style.spaceRow ?? 0)

0 commit comments

Comments
 (0)