@ -6,6 +6,8 @@ import styles from './index.less';
/* eslint react/no-did-mount-set-state: 0 */
/* eslint react/no-did-mount-set-state: 0 */
/* eslint no-param-reassign: 0 */
/* eslint no-param-reassign: 0 */
const isSupportLineClamp = ( document . body . style . webkitLineClamp !== undefined ) ;
const EllipsisText = ( { text , length , tooltip , ... other } ) => {
const EllipsisText = ( { text , length , tooltip , ... other } ) => {
if ( typeof text !== 'string' ) {
if ( typeof text !== 'string' ) {
throw new Error ( 'Ellipsis children must be string.' ) ;
throw new Error ( 'Ellipsis children must be string.' ) ;
@ -22,7 +24,7 @@ const EllipsisText = ({ text, length, tooltip, ...other }) => {
}
}
if ( tooltip ) {
if ( tooltip ) {
return < span > { displayText } < Tooltip title = { text } > { tail } < / T o o l t i p > < / s p a n > ;
return < Tooltip title = { text } > < span > { displayText } { tail } < / s p a n > < / T o o l t i p > ;
}
}
return (
return (
@ -34,35 +36,25 @@ const EllipsisText = ({ text, length, tooltip, ...other }) => {
export default class Ellipsis extends Component {
export default class Ellipsis extends Component {
state = {
state = {
lineHeight : 0 ,
text : '' ,
text : '' ,
targetCount : 0 ,
targetCount : 0 ,
}
}
componentDidMount ( ) {
componentDidMount ( ) {
const { lines , cover } = this . props ;
if ( this . node ) {
if ( this . node ) {
if ( lines && cover ) {
this . setState ( {
lineHeight : parseInt ( window . getComputedStyle ( this . node ) . lineHeight , 10 ) ,
} ) ;
}
this . computeLine ( ) ;
this . computeLine ( ) ;
}
}
}
}
componentWillReceiveProps ( nextProps ) {
componentWillReceiveProps ( nextProps ) {
if ( this . props . lines !== nextProps . lines || this . props . cover !== nextProps . cover ) {
if ( this . props . lines !== nextProps . lines ) {
this . setState ( {
lineHeight : parseInt ( window . getComputedStyle ( this . node ) . lineHeight , 10 ) ,
} ) ;
this . computeLine ( ) ;
this . computeLine ( ) ;
}
}
}
}
computeLine = ( ) => {
computeLine = ( ) => {
const { lines , cover } = this . props ;
const { lines } = this . props ;
if ( lines && ! cover ) {
if ( lines && ! isSupportLineClamp ) {
const fontSize = parseInt ( window . getComputedStyle ( this . node ) . fontSize , 10 ) || 14 ;
const fontSize = parseInt ( window . getComputedStyle ( this . node ) . fontSize , 10 ) || 14 ;
const text = this . shadowChildren . innerText ;
const text = this . shadowChildren . innerText ;
const targetWidth = ( this . node . offsetWidth || this . node . parentNode . offsetWidth ) * lines ;
const targetWidth = ( this . node . offsetWidth || this . node . parentNode . offsetWidth ) * lines ;
@ -128,22 +120,20 @@ export default class Ellipsis extends Component {
}
}
render ( ) {
render ( ) {
const { text , targetCount , lineHeight } = this . state ;
const { text , targetCount } = this . state ;
const {
const {
children ,
children ,
lines ,
lines ,
length ,
length ,
cover = false ,
suffixColor = '#fff' ,
suffixOffset = 0 ,
className ,
className ,
tooltip ,
tooltip ,
... restProps
... restProps
} = this . props ;
} = this . props ;
const cls = classNames ( styles . ellipsis , className , {
const cls = classNames ( styles . ellipsis , className , {
[ styles . lines ] : ( lines && ! cover ) ,
[ styles . lines ] : ( lines && ! isSupportLineClamp ) ,
[ styles . linesCover ] : ( lines && cover ) ,
[ styles . lineClamp ] : ( lines && isSupportLineClamp ) ,
} ) ;
} ) ;
if ( ! lines && ! length ) {
if ( ! lines && ! length ) {
@ -155,29 +145,30 @@ export default class Ellipsis extends Component {
return ( < EllipsisText className = { cls } length = { length } text = { children || '' } tooltip = { tooltip } { ... restProps } / > ) ;
return ( < EllipsisText className = { cls } length = { length } text = { children || '' } tooltip = { tooltip } { ... restProps } / > ) ;
}
}
// lines cover
if ( cover ) {
const id = ` antd-pro-ellipsis- ${ ` ${ new Date ( ) . getTime ( ) } ${ Math . floor ( Math . random ( ) * 100 ) } ` } ` ;
const id = ` antd-pro-ellipsis- ${ ` ${ new Date ( ) . getTime ( ) } ${ Math . floor ( Math . random ( ) * 100 ) } ` } ` ;
const style = ` # ${ id } :before{background-color: ${ suffixColor } ;padding-left: ${ suffixOffset } px;} ` ;
// support document.body.style.webkitLineClamp
if ( isSupportLineClamp ) {
const style = ` # ${ id } {-webkit-line-clamp: ${ lines } ;} ` ;
return (
return (
< div
< div id = { id } className = { cls } { ... restProps } >
{ ... restProps }
id = { id }
ref = { this . handleRef }
className = { cls }
style = { {
... restProps . style ,
maxHeight : ` ${ lines * lineHeight } px ` ,
} }
>
< style > { style } < / s t y l e >
< style > { style } < / s t y l e >
{ children }
{
< / d i v >
tooltip ? ( < Tooltip title = { text } > { children } < / T o o l t i p > ) : c h i l d r e n
) ;
}
< / d i v > ) ;
}
}
// lines no cover
const childNode = (
const suffix = tooltip ? < Tooltip title = { text } > ... < / T o o l t i p > : ' . . . ' ;
< span >
{
( targetCount > 0 ) && text . substring ( 0 , targetCount )
}
{
( targetCount > 0 ) && ( targetCount < text . length ) && '...'
}
< / s p a n >
) ;
return (
return (
< div
< div
@ -186,10 +177,9 @@ export default class Ellipsis extends Component {
className = { cls }
className = { cls }
>
>
{
{
( targetCount > 0 ) && text . substring ( 0 , targetCount )
tooltip ? (
}
< Tooltip title = { text } > { childNode } < / T o o l t i p >
{
) : childNode
( targetCount > 0 ) && ( targetCount < text . length ) && suffix
}
}
< div className = { styles . shadow } ref = { this . handleShadowChildren } > { children } < / d i v >
< div className = { styles . shadow } ref = { this . handleShadowChildren } > { children } < / d i v >
< div className = { styles . shadow } ref = { this . handleShadow } > < span > { text } < / s p a n > < / d i v >
< div className = { styles . shadow } ref = { this . handleShadow } > < span > { text } < / s p a n > < / d i v >