Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created April 14, 2020 15:01
Show Gist options
  • Save jsmanifest/619d3bad7481d2f8e828b9ec700b7041 to your computer and use it in GitHub Desktop.
Save jsmanifest/619d3bad7481d2f8e828b9ec700b7041 to your computer and use it in GitHub Desktop.
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