Skip to content

Commit d9b91e1

Browse files
authored
Merge pull request #204 from scientist-softserv/199-truncate-item-descriptions
Truncate descriptions
2 parents 01621c4 + b27bcf5 commit d9b91e1

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/compounds/Item/CardBody.jsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import PropTypes from 'prop-types'
3-
import { Card } from 'react-bootstrap'
3+
import { Button, Card, Collapse } from 'react-bootstrap'
44
import NextLink from '../../components/NextLink/NextLink'
55
import LinkedButton from '../LinkedButton/LinkedButton'
66

77
const CardBody = ({ buttonLink, buttonProps, item,
88
orientation, titleLink, withButtonLink, withTitleLink }) => {
9-
const { id, description, name } = item
9+
const { id, description = '', name } = item
10+
const [open, setOpen] = useState(false)
1011

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)
1120
return (
1221
<Card.Body className={withButtonLink && 'd-flex flex-column'}>
1322
<div className={orientation === 'horizontal' ? 'd-block d-md-flex align-items-center justify-content-between' : ''}>
@@ -25,9 +34,19 @@ const CardBody = ({ buttonLink, buttonProps, item,
2534
)}
2635
</Card.Title>
2736
{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+
</>
3150
)}
3251
</div>
3352
{(withButtonLink) && (

src/compounds/Item/item.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212
opacity: .8;
1313
}
1414

15+
.mh-300 {
16+
max-height: 300px;
17+
}
18+

0 commit comments

Comments
 (0)