| 1 |
import { Fragment, useEffect, useRef, useState } from "@wordpress/element"; |
| 2 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 3 |
|
| 4 |
const Render = ({ attributes, posts }) => { |
| 5 |
const { postListLayout, contentVerticalPosition, showHideDivider, catTabCategoryPosition } = attributes; |
| 6 |
const containerRef = useRef(null); |
| 7 |
|
| 8 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 9 |
|
| 10 |
useEffect(() => { |
| 11 |
const handleClickOutside = (event) => { |
| 12 |
if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 13 |
setSelectedPostId(null); |
| 14 |
} |
| 15 |
}; |
| 16 |
|
| 17 |
document.addEventListener("mousedown", handleClickOutside); |
| 18 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 19 |
}, []); |
| 20 |
|
| 21 |
return ( |
| 22 |
<div |
| 23 |
className={`sp-smart-post-list-one-block ${postListLayout} sp-cat-position-${catTabCategoryPosition}`} |
| 24 |
ref={containerRef} |
| 25 |
> |
| 26 |
<div className={`sp-smart-post-list-one-container ${postListLayout}`}> |
| 27 |
{posts?.map((post, i) => { |
| 28 |
return ( |
| 29 |
<Fragment key={post?.post_id}> |
| 30 |
<div className={`sp-smart-post-show-list-one-card-wrapper`}> |
| 31 |
<TemplateOne |
| 32 |
data={post} |
| 33 |
attributes={attributes} |
| 34 |
posts={posts} |
| 35 |
contentPosition={contentVerticalPosition} |
| 36 |
isSelected={selectedPostId === post?.post_id} |
| 37 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 38 |
/> |
| 39 |
</div> |
| 40 |
{showHideDivider && <div className="sp-smart-post-list-divider"></div>} |
| 41 |
</Fragment> |
| 42 |
); |
| 43 |
})} |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
); |
| 47 |
}; |
| 48 |
|
| 49 |
export default Render; |
| 50 |
|