diff --git a/demo/js/base/button/demo.button.js b/demo/js/base/button/demo.button.js index 981683e84..2514b949e 100644 --- a/demo/js/base/button/demo.button.js +++ b/demo/js/base/button/demo.button.js @@ -385,6 +385,11 @@ Demo.Button = BI.inherit(BI.Widget, { type: "bi.button", text: "小于最小宽度的按钮", width: 50, + }, { + type: "bi.button", + text: "一个文字超级超级长的 button, 他比按钮宽度还长。", + textWidth: 500, + width: 100, }]; return { diff --git a/src/base/single/button/buttons/button.js b/src/base/single/button/buttons/button.js index a06f23191..bc1468450 100644 --- a/src/base/single/button/buttons/button.js +++ b/src/base/single/button/buttons/button.js @@ -81,7 +81,7 @@ }, hgap: o.hgap, vgap: o.vgap, - items: self.generateItems() + items: self.generateItems(), }); // 如果 options 对应的属性为 true 则给元素添加 class @@ -102,7 +102,7 @@ // 由于button默认情况下有个边框,所以要主动算行高 var lineHeight, textHeight = o.textHeight; - var hasBorder = false + var hasBorder = false; if (BI.isNumber(o.height)) { if (!isVertical(o.iconPosition)) { if (!(o.clear && o.block && o.light)) { @@ -121,12 +121,12 @@ var iconInvisible = !(o.loading || o.iconCls || o.icon || defaultRenderIcon); - var textWidth = o.textWidth; - if (BI.isNull(o.textWidth)) { - textWidth = (o.minWidth > 0 && o.width < o.minWidth) ? o.minWidth : o.width; - textWidth -= (o.hgap * 2 + o.iconGap) - textWidth -= iconInvisible || isVertical(o.iconPosition) ? 0 : this._const.iconWidth - } + var maxTextWidth = Math.max(o.minWidth, o.width); + maxTextWidth -= (o.hgap * 2 + o.iconGap); + // 减去图标水平占位宽度 + maxTextWidth -= iconInvisible || isVertical(o.iconPosition) ? 0 : this._const.iconWidth; + var textWidth = BI.isNull(o.textWidth) ? maxTextWidth : Math.min(o.textWidth, maxTextWidth); + this.text = BI.createWidget({ type: "bi.label", text: o.text, @@ -140,7 +140,7 @@ }); if (iconInvisible) { - return [this.text] + return [this.text]; } this._iconRendered = true;