Created
April 14, 2020 15:01
-
-
Save jsmanifest/619d3bad7481d2f8e828b9ec700b7041 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function ListRoot({ children, ...rest }) { | |
return <div {...rest}>{children}</div> | |
} | |
function ListHeader({ children }) { | |
return <h5>{children}</h5> | |
} | |
function ListComponent({ label, items = [], limit = 0 }) { | |
const [collapsed, setCollapsed] = React.useState(items.length > 3) | |
function toggle() { | |
setCollapsed((prevValue) => !prevValue) | |
} | |
const constrainedItems = collapsed ? items.slice(0, limit) : items | |
return ( | |
<ul> | |
<p>{label}</p> | |
{constrainedItems.map((member) => ( | |
<li key={member}>{member}</li> | |
))} | |
{items.length > limit && ( | |
<li className="expand"> | |
<button type="button" onClick={toggle}> | |
Expand | |
</button> | |
</li> | |
)} | |
</ul> | |
) | |
} | |
function List({ header, label, members = [], limit }) { | |
return ( | |
<ListRoot> | |
<ListHeader>{header}</ListHeader> | |
<ListComponent label={label} items={members} limit={limit} /> | |
</ListRoot> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment