import type { Meta, StoryObj } from "@storybook/react-vite";
import { useEffect, useMemo, useRef, useState } from "react";
import {
fixLawHtml,
getLawChecked,
parseLawIds,
setLawChecked,
toggleLawChecked,
} from "../";
import { Flex, Law } from "../react";
import styles from "../styles.module.css";
import { vinforskriften } from "./vinforskriften-new";
// TODO: Søkefunksjon
// TODO: Kommentar/tilbakemeldingsfunksjon
// TODO: Må finnes kommentarfelt med fritekst i alle systemer uansett, for å kunne utbrodere
// ? defaultP eksempel Artikkel 9, Nr. 1, Avsnitt 2, https://lovdata.no/dokument/SF/forskrift/2013-01-13-60/ARTIKKEL_9#ARTIKKEL_9
const meta = {
title: "Designsystem/Law",
parameters: {
layout: "padded",
showInOverview: false,
},
decorators: [
(Story) => {
return (
Valgte:
);
},
],
} satisfies Meta;
const console = {
log: (laws: ReturnType) => {
const log = document.getElementById("log") as HTMLElement;
log.textContent = laws.map(({ label }) => `\n- ${label}`).join("");
window.console.log(laws);
},
};
export default meta;
type Story = StoryObj;
const SIDEBAR_STYLE: React.CSSProperties = {
maxHeight: "calc(100vh - 32px)",
overflow: "auto",
fontFamily: "monospace",
whiteSpace: "pre-wrap",
position: "sticky",
fontSize: 14,
top: 16,
};
export const Default: Story = {
parameters: {
showInOverview: false,
},
render: () => {
const lawElement = useRef(null);
// Avoid re-processing on every render:
const html = useMemo(() => ({ __html: fixLawHtml(vinforskriften) }), []);
// Sync checked state to Law component:
useEffect(() => {
const self = lawElement.current;
const onClick = ({ target }: Event) => {
if (target instanceof HTMLButtonElement) {
toggleLawChecked(target, self);
const checked = getLawChecked(self);
console.log(parseLawIds(checked, html.__html));
}
};
self?.addEventListener("click", onClick);
return () => self?.removeEventListener("click", onClick);
}, [html]);
// Render:
return (
);
},
};
export const React: Story = {
render: () => {
const [checkedIds, setCheckedIds] = useState([]);
const lawElement = useRef(null);
// Avoid re-processing on every render:
const html = useMemo(() => ({ __html: fixLawHtml(vinforskriften) }), []);
// Sync checked state to Law component:
useEffect(() => {
setLawChecked(checkedIds, lawElement.current);
}, [checkedIds]);
// Render:
return (
{
if (event.target instanceof HTMLButtonElement) {
const id = event.target.value;
const checked = checkedIds.includes(id)
? checkedIds.filter((cid) => cid !== id)
: [...checkedIds, id];
setCheckedIds(checked);
console.log(parseLawIds(checked, html.__html));
}
}}
/>
);
},
};
export const VariantView: Story = {
render: () => {
// Avoid re-processing on every render:
const html = useMemo(() => ({ __html: fixLawHtml(vinforskriften) }), []);
// Render:
return (
);
},
};
export const VariantReadOnly: Story = {
render: () => {
// Avoid re-processing on every render:
const html = useMemo(() => ({ __html: fixLawHtml(vinforskriften) }), []);
// Render:
return (
);
},
};