@@ -2,7 +2,7 @@ import { SuperComponent, wxComponent } from '../common/src/index';
22import config from '../common/config' ;
33import props from './props' ;
44import type { TdBadgeProps } from './type' ;
5- import { uniqueFactory } from '../common/utils' ;
5+ import { getRect , uniqueFactory } from '../common/utils' ;
66
77const { prefix } = config ;
88const name = `${ prefix } -badge` ;
@@ -26,11 +26,29 @@ export default class Badge extends SuperComponent {
2626 value : '' ,
2727 labelID : '' ,
2828 descriptionID : '' ,
29+ // ribbon
30+ ribbonStyle : '' ,
31+ ribbonOuterStyle : '' ,
2932 } ;
3033
3134 lifetimes = {
3235 ready ( ) {
3336 const uniqueID = getUniqueID ( ) ;
37+ if ( this . properties . shape === 'ribbon' ) {
38+ getRect ( this , `.${ prefix } -badge--ribbon` ) . then ( ( rect ) => {
39+ const outerBoundingRect = rect . width / Math . SQRT2 + rect . height * Math . SQRT2 ; // 外接矩形的宽度:height * cos45deg + width / cos45deg
40+ const translateX = rect . width - rect . width / Math . SQRT2 + outerBoundingRect - rect . width ; // 旋转后的位移:原宽度 - 旋转后的宽度 + 外接矩形的宽度 - 原宽度
41+ const ribbonHeightHypotenuse = rect . height * Math . SQRT2 ; // 斜边的长度:height * sin45deg
42+ const ribbonWidthCatheti = rect . width / Math . SQRT2 ; // 直角边的长度:width / sin45deg
43+ this . setData ( {
44+ ribbonStyle : `transform: translateX(${ translateX } px) rotate(45deg);` ,
45+ ribbonOuterStyle : `width: ${ outerBoundingRect } px; height: ${ outerBoundingRect } px;
46+ clip-path: polygon(0 0, ${ ribbonHeightHypotenuse } px 0,
47+ ${ outerBoundingRect } px ${ ribbonWidthCatheti } px,
48+ ${ outerBoundingRect } px ${ ribbonWidthCatheti + ribbonHeightHypotenuse } px);` ,
49+ } ) ;
50+ } ) ;
51+ }
3452
3553 this . setData ( {
3654 labelID : `${ uniqueID } _label` ,
0 commit comments