parent
89c6b36090
commit
adca6350ce
5 changed files with 113 additions and 79 deletions
@ -1,43 +1,43 @@ |
||||
import { useState } from 'react'; |
||||
import { cn } from '../../lib/classname'; |
||||
|
||||
type ActivityTopicTitlesProps = { |
||||
topicTitles: string[]; |
||||
className?: string; |
||||
onSelectActivity?: () => void; |
||||
}; |
||||
|
||||
export function ActivityTopicTitles(props: ActivityTopicTitlesProps) { |
||||
const { topicTitles, onSelectActivity } = props; |
||||
const firstThreeTopics = topicTitles?.slice(0, 3); |
||||
const remainingTopics = topicTitles?.slice(3); |
||||
const { topicTitles, onSelectActivity, className } = props; |
||||
|
||||
return ( |
||||
<> |
||||
{firstThreeTopics.map((topicTitle, index) => { |
||||
return ( |
||||
<span className="font-medium" key={`topic-${topicTitle}-${index}`}> |
||||
<> |
||||
{index > 0 && ', '} |
||||
{index === firstThreeTopics.length - 1 && |
||||
remainingTopics?.length === 0 && |
||||
firstThreeTopics.length > 1 |
||||
? 'and ' |
||||
: ''} |
||||
{topicTitle} |
||||
</> |
||||
</span> |
||||
); |
||||
})} |
||||
const [showAll, setShowAll] = useState(false); |
||||
const filteredTopicTitles = topicTitles.slice( |
||||
0, |
||||
showAll ? topicTitles.length : 3, |
||||
); |
||||
|
||||
{remainingTopics?.length > 0 && ( |
||||
<> |
||||
, and |
||||
<button |
||||
className="font-medium underline underline-offset-2 hover:text-black" |
||||
onClick={onSelectActivity} |
||||
> |
||||
{remainingTopics.length} more topic |
||||
{remainingTopics.length > 1 ? 's' : ''} |
||||
</button> |
||||
</> |
||||
const shouldShowButton = topicTitles.length > 3; |
||||
|
||||
return ( |
||||
<div |
||||
className={cn( |
||||
'flex flex-wrap gap-1 text-sm font-normal text-gray-600', |
||||
className, |
||||
)} |
||||
> |
||||
{filteredTopicTitles.map((topicTitle, index) => ( |
||||
<span key={index} className="rounded-md bg-gray-200 px-1"> |
||||
{topicTitle} |
||||
</span> |
||||
))} |
||||
{shouldShowButton && ( |
||||
<button |
||||
onClick={() => setShowAll(!showAll)} |
||||
className="text-gray-600 underline underline-offset-2 hover:text-black" |
||||
> |
||||
{showAll ? '- Show less' : `+${topicTitles.length - 3} more`} |
||||
</button> |
||||
)} |
||||
</> |
||||
</div> |
||||
); |
||||
} |
||||
|
Loading…
Reference in new issue