Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table component: adding click event on expandable table rows #2127

Merged
merged 13 commits into from
Aug 7, 2023
Merged
6 changes: 6 additions & 0 deletions .changeset/heavy-pandas-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navikt/ds-css": minor
"@navikt/ds-react": minor
---

Adding click event on table row
KenAJoh marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion @navikt/core/css/table.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
display: table-row;
}

.navds-table__body .navds-table__row--shade-on-hover:hover {
.navds-table__body .navds-table__row--shade-on-hover:not(.navds-table__expandable-row--expansion-disabled):hover {
background-color: var(--ac-table-row-hover, var(--a-bg-subtle));
}

Expand Down Expand Up @@ -154,6 +154,10 @@
transition: border-bottom-color 190ms cubic-bezier(0.6, 0.04, 0.98, 0.335);
}

.navds-table__expandable-row:not(.navds-table__expandable-row--open):not(.navds-table__expandable-row--expansion-disabled):hover {
This conversation was marked as resolved.
Show resolved Hide resolved
cursor: pointer;
}

.navds-table__expandable-row--open .navds-table__data-cell {
border-bottom-color: transparent;
}
Expand Down
20 changes: 13 additions & 7 deletions @navikt/core/react/src/table/ExpandableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ExpandableRowProps extends Omit<RowProps, "content"> {
*/
onOpenChange?: (open: boolean) => void;
/**
* Disable expansio
* Disable expansion. shadeOnHover will not be visible.
* @default false
*/
expansionDisabled?: boolean;
Expand Down Expand Up @@ -68,14 +68,25 @@ export const ExpandableRow: ExpandableRowType = forwardRef(

const isOpen = open ?? internalOpen;

const clickHandler = (e) => {
onOpenChange?.(!isOpen);
if (open === undefined) {
setInternalOpen((open) => !open);
}
e.stopPropagation();
};

return (
<>
<Row
{...rest}
ref={ref}
className={cl("navds-table__expandable-row", className, {
"navds-table__expandable-row--open": isOpen,
"navds-table__expandable-row--expansion-disabled":
expansionDisabled,
})}
onClick={!expansionDisabled ? clickHandler : undefined}
>
{togglePlacement === "right" && children}
<DataCell
Expand All @@ -89,12 +100,7 @@ export const ExpandableRow: ExpandableRowType = forwardRef(
className="navds-table__toggle-expand-button"
aria-controls={id}
aria-expanded={isOpen}
onClick={() => {
onOpenChange?.(!isOpen);
if (open === undefined) {
setInternalOpen((open) => !open);
}
}}
onClick={clickHandler}
>
<ChevronDownIcon
className="navds-table__expandable-icon"
Expand Down