OBPIH-6023 Add reusable Section and Subsection components#4491
OBPIH-6023 Add reusable Section and Subsection components#4491awalkowiak merged 2 commits intofeature/product-supplier-list-redesignfrom
Conversation
kchelstowski
commented
Feb 5, 2024

| <div className="v2-subsection"> | ||
| <div className="subsection-title-wrapper"> | ||
| <span | ||
| role="button" | ||
| tabIndex={0} | ||
| onClick={collapsable ? () => triggerCollapse() : null} | ||
| onKeyDown={collapsable ? () => triggerCollapse() : null} | ||
| style={collapsable ? { cursor: 'pointer' } : { cursor: 'unset' }} | ||
| > | ||
| <Translate id={title.label} defaultMessage={title.defaultMessage} /> | ||
| {collapsable | ||
| && (expanded | ||
| ? <RiArrowUpSLine className="arrow-up" /> | ||
| : <RiArrowDownSLine className="arrow-down" />)} | ||
| </span> | ||
| </div> | ||
| <div className={`subsection-body ${expanded ? 'subsection-body-expanded' : ''}`}> | ||
| {children} | ||
| </div> | ||
| </div> |
There was a problem hiding this comment.
Maybe it is possible to reduce the amount of JS code inside the HTML...
Maybe you can create an object:
{
collapsable: {
onClick: ....,
onKeyDown: ...,
style: ...,
},
...
}
and spread it on this span?
I am just thinking out loud, you can skip it
There was a problem hiding this comment.
I prefer implicit attributes (I've explained that recently), and moreover - the ESLint would shout about that.
| <Translate id={title.label} defaultMessage={title.defaultMessage} /> | ||
| {collapsable | ||
| && (expanded | ||
| ? <RiArrowUpSLine className="arrow-up" /> |
There was a problem hiding this comment.
this can be a single csv with a class that rotates it when it is active
<RiArrowUpSLine className={`arrow-up {expanded ? 'arrow-up--expanded' : ''}`} />.arrow-up--expanded {
transform: rotate(180deg);
}| const Subsection = ({ | ||
| title, | ||
| collapsable, | ||
| children, | ||
| }) => { | ||
| // If a subsection is not collapsable, it is always expanded | ||
| // (collapsable: false --> expanded: true) | ||
| // If a subsection is collapsable, it is not expanded by default | ||
| // (collapsable: true --> expanded: false) | ||
| const [expanded, setExpanded] = useState(!collapsable); | ||
|
|
||
| const triggerCollapse = () => { | ||
| setExpanded(!expanded); | ||
| }; |
There was a problem hiding this comment.
instead of a single collapsable prop I would also add isExpanded: boolean as an initial value, that way we can control if the section is collapsed or not for both states when it is collapsable or not collapsable, so something like:
const Subsection = ({
title,
isExpanded,
collapsable,
children,
}) => ...
...
Subsection.defaultProps = {
isExpanded: true,
collapsable: true,
};const [expanded, setExpanded] = useState(isExpanded);prevent any toggle action if section is not collapsible, maybe name disabled fro this prop sound better but collapsable should also be good.
const triggerCollapse = () => {
if (collapsable) {
setExpanded(!expanded);
}
};There was a problem hiding this comment.
Currently we are fine, because the onClick method would be null if a subsection is not collapseable, but I will add it just in case we would refactor something in the future.
There was a problem hiding this comment.
I've added additional prop expandedByDefault as you suggested, to make it more reusable in different scenarios. I didn't decide to go for isExpanded, not to mix isExpanded with expanded, as it might cause confusions.
* OBPIH-6023 Add reusable Section and Subsection components * OBPIH-6023 Fixes after review