From 604bfd09ce1c0b1f0d1edbca7dd95368d6da031f Mon Sep 17 00:00:00 2001 From: Jo Emil Holen Date: Wed, 30 Nov 2022 12:20:12 +0100 Subject: [PATCH] feat: add sticky table header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit reverts 8a555fee131ebceac4b7f47820385242769441e5 to add support for sticky heading. this is added through an optional prop, and exposing a css variable to be able to set the background color of the head. this defaults to the Jøkul background color --- doc-utils/ComponentExample.tsx | 1 + doc-utils/ExampleBase.tsx | 4 +- .../table-react/documentation/Example.tsx | 8 ++ .../documentation/StickyTableExample.tsx | 88 +++++++++++++++++++ packages/table-react/src/TableHead.tsx | 4 +- packages/table/_table-head.scss | 17 ++++ 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 packages/table-react/documentation/StickyTableExample.tsx diff --git a/doc-utils/ComponentExample.tsx b/doc-utils/ComponentExample.tsx index c9d8d35b458..a2a49e2d44b 100644 --- a/doc-utils/ComponentExample.tsx +++ b/doc-utils/ComponentExample.tsx @@ -11,6 +11,7 @@ export interface Props { choiceProps?: Array; }; codeExample?: CodeExample; + style?: React.CSSProperties; } export const ComponentExample: FC = ({ component, ...rest }) => { diff --git a/doc-utils/ExampleBase.tsx b/doc-utils/ExampleBase.tsx index 709b8279464..614528862e1 100644 --- a/doc-utils/ExampleBase.tsx +++ b/doc-utils/ExampleBase.tsx @@ -19,6 +19,7 @@ export interface Props { choiceProps?: Array; }; codeExample?: CodeExample; + style?: React.CSSProperties; } function useLocalStorage(key: string, defaultValue: T): [T, (newValue: T) => void] { @@ -46,7 +47,7 @@ function useLocalStorage(key: string, defaultValue: T): [T, (newValue: T) => return [state, setState]; } -export const ExampleBase: FC = ({ component, knobs, title = "Komponent", codeExample, scrollable }) => { +export const ExampleBase: FC = ({ component, knobs, title = "Komponent", codeExample, scrollable, style }) => { const uid = useId("example"); const [theme, setTheme] = useLocalStorage("jkl-example-theme", "light"); const [density, setDensity] = useLocalStorage("jkl-example-density", "comfortable"); @@ -96,6 +97,7 @@ export const ExampleBase: FC = ({ component, knobs, title = "Komponent", } ${scrollable ? "jkl-portal-component-example__example-wrapper--scrollable" : ""} ${ density === "comfortable" ? "jkl-body" : "" } ${density === "compact" ? "jkl-small" : ""}`.trim()} + style={style} > {example} diff --git a/packages/table-react/documentation/Example.tsx b/packages/table-react/documentation/Example.tsx index 021532fd3f1..e23c02ccba8 100644 --- a/packages/table-react/documentation/Example.tsx +++ b/packages/table-react/documentation/Example.tsx @@ -17,6 +17,7 @@ import "../../table/table.scss"; import "../../button/button.scss"; import "../../icons/animated-icons.scss"; import "../../expand-button/expand-button.scss"; +import StickyTableExample, { stickyTableExampleCode } from "./StickyTableExample"; export default function Example() { return ( @@ -69,6 +70,13 @@ export default function Example() { knobs={expandableTableExampleKnobs} codeExample={expandableTableExampleCode} /> + ); } diff --git a/packages/table-react/documentation/StickyTableExample.tsx b/packages/table-react/documentation/StickyTableExample.tsx new file mode 100644 index 00000000000..1a0371bea44 --- /dev/null +++ b/packages/table-react/documentation/StickyTableExample.tsx @@ -0,0 +1,88 @@ +import React, { FC } from "react"; +import { ExampleComponentProps } from "../../../doc-utils"; +import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "../src"; + +const columns = ["Dato", "Saksnummer", "Kundenummer", "Kundenavn", "Milepæl", "Følger saken"]; + +const rows = [ + ["24.02.2020", "20-1234567", "010203 99887", "Ola Nordmann", "Opprettet", "Siri Saksbehandler"], + ["13.04.2019", "20-8382811", "010203 99887", "Kari Nordkvinne", "Opprettet", "Siri Saksbehandler"], + ["31.07.2017", "20-1111", "010203 99887", "Kari Nordkvinne", "Opprettet", "Per Persen"], + ["24.02.2020", "20-1234567", "010203 99887", "Ola Nordmann", "Opprettet", "Siri Saksbehandler"], + ["13.04.2019", "20-8382811", "010203 99887", "Kari Nordkvinne", "Opprettet", "Siri Saksbehandler"], + ["31.07.2017", "20-1111", "010203 99887", "Kari Nordkvinne", "Opprettet", "Per Persen"], + ["24.02.2020", "20-1234567", "010203 99887", "Ola Nordmann", "Opprettet", "Siri Saksbehandler"], + ["13.04.2019", "20-8382811", "010203 99887", "Kari Nordkvinne", "Opprettet", "Siri Saksbehandler"], + ["31.07.2017", "20-1111", "010203 99887", "Kari Nordkvinne", "Opprettet", "Per Persen"], + ["24.02.2020", "20-1234567", "010203 99887", "Ola Nordmann", "Opprettet", "Siri Saksbehandler"], + ["13.04.2019", "20-8382811", "010203 99887", "Kari Nordkvinne", "Opprettet", "Siri Saksbehandler"], + ["31.07.2017", "20-1111", "010203 99887", "Kari Nordkvinne", "Opprettet", "Per Persen"], +]; + +const StickyTableExample: FC = ({ boolValues, choiceValues }) => { + const headless = boolValues?.["Skjul overskrift"]; + const type = choiceValues?.["Mobilvisning"]; + const props = type === "Liste" ? { "data-collapse": "true", collapseToList: true } : {}; + + return ( + + Overskrift for skjermlesere + + + {columns.map((column, index) => ( + + {column} + + ))} + + + + {rows.map((row, rowIndex) => ( + + {row.map((cell, cellIndex) => ( + + {cell} + + ))} + + ))} + +
+ ); +}; + +export default StickyTableExample; + +export const stickyTableExampleCode = ({ boolValues, choiceValues }: ExampleComponentProps): string => ` + + Overskrift for skjermlesere + + + {columns.map((header, index) => ( + + {header} + + ))} + + + + {rows.map((row, rowIndex) => ( + + {row.map((cell, cellIndex) => ( + + {cell} + + ))} + + ))} + +
+`; diff --git a/packages/table-react/src/TableHead.tsx b/packages/table-react/src/TableHead.tsx index 276ed529f02..b90f1a22551 100644 --- a/packages/table-react/src/TableHead.tsx +++ b/packages/table-react/src/TableHead.tsx @@ -4,14 +4,16 @@ import { TableSectionContextProvider } from "./tableSectionContext"; export interface TableHeadProps extends HTMLAttributes { srOnly?: boolean; + sticky?: boolean; } -const TableHead = forwardRef(({ className, srOnly, ...rest }, ref) => { +const TableHead = forwardRef(({ className, srOnly, sticky, ...rest }, ref) => { return ( .jkl-table-row { + position: sticky; + top: 0; + z-index: 1; + background-color: var(--jkl-table-head-sticky-color); + } + } }