1
- import React from 'react'
1
+ import React , { useState } from 'react'
2
2
import PropTypes from 'prop-types'
3
- import { Card } from 'react-bootstrap'
3
+ import { Button , Card , Collapse } from 'react-bootstrap'
4
4
import NextLink from '../../components/NextLink/NextLink'
5
5
import LinkedButton from '../LinkedButton/LinkedButton'
6
6
7
7
const CardBody = ( { buttonLink, buttonProps, item,
8
8
orientation, titleLink, withButtonLink, withTitleLink } ) => {
9
- const { id, description, name } = item
9
+ const { id, description = '' , name } = item
10
+ const [ open , setOpen ] = useState ( false )
10
11
12
+ const truncateDescription = ( desc , maxLength , isOpen ) => {
13
+ if ( desc . length <= maxLength || isOpen ) return { truncated : desc , cutOffIndex : desc . length } ;
14
+ const lastSpaceIndex = desc . substring ( 0 , maxLength ) . lastIndexOf ( ' ' ) ;
15
+ const ellipsis = isOpen ? '' : '...' ;
16
+ return { truncated : desc . slice ( 0 , lastSpaceIndex ) + ellipsis , cutOffIndex : lastSpaceIndex } ;
17
+ }
18
+
19
+ const { truncated, cutOffIndex } = truncateDescription ( description , 300 , open )
11
20
return (
12
21
< Card . Body className = { withButtonLink && 'd-flex flex-column' } >
13
22
< div className = { orientation === 'horizontal' ? 'd-block d-md-flex align-items-center justify-content-between' : '' } >
@@ -25,9 +34,19 @@ const CardBody = ({ buttonLink, buttonProps, item,
25
34
) }
26
35
</ Card . Title >
27
36
{ description && (
28
- < Card . Text className = 'fw-light' >
29
- { description }
30
- </ Card . Text >
37
+ < >
38
+ < div className = 'fw-light mh-300 overflow-auto mt-3' >
39
+ { truncated }
40
+ < Collapse in = { open } >
41
+ < div className = 'fw-light' > { description . slice ( cutOffIndex ) . trimStart ( ) } </ div >
42
+ </ Collapse >
43
+ </ div >
44
+ { description . length > 300 && (
45
+ < Button variant = "link" onClick = { ( ) => setOpen ( ! open ) } className = "p-0 mt-3" >
46
+ { open ? ' Show less' : ' Read more' }
47
+ </ Button >
48
+ ) }
49
+ </ >
31
50
) }
32
51
</ div >
33
52
{ ( withButtonLink ) && (
0 commit comments