generated from nyu-software-engineering/generic-mern-stack-project
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from agiledev-students-spring2024/kiara
removing HTML tags from descriptions and read more option
- Loading branch information
Showing
2 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { useEffect, useState } from "react"; | ||
|
||
const LongText = ({ content, limit }) => { | ||
const [showAll, setShowAll] = useState(false); | ||
|
||
const showMore = () => setShowAll(true); | ||
const showLess = () => setShowAll(false); | ||
|
||
if (content.length <= limit) { | ||
// there is nothing more to show | ||
return <div>{content}</div> | ||
} | ||
if (showAll) { | ||
// We show the extended text and a link to reduce it | ||
return <div> | ||
{content} | ||
<button onClick={showLess}>Read less</button> | ||
</div> | ||
} | ||
// In the final case, we show a text with ellipsis and a `Read more` button | ||
const toShow = content.substring(0, limit) + "..."; | ||
return <div> | ||
{toShow} | ||
<button onClick={showMore}>Read more</button> | ||
</div> | ||
} | ||
export default LongText; |