import type { Meta, StoryObj } from "@storybook/react-vite";
import { useEffect, useRef, useState } from "react";
import { Errorsummary, Heading } from "../react";
import styles from "../styles.module.css";
const meta = {
title: "Designsystem/Error summary",
decorators: [
function Decorate(Story) {
useEffect(() => {
const handleClick = (event: Event) => {
const anchor = (event.target as Element)?.closest?.('a[href^="#"]');
const input = document.getElementById(
(anchor as HTMLAnchorElement)?.hash.slice(1),
);
if (input) {
event.preventDefault();
input.scrollIntoView({ behavior: "smooth" });
input.focus();
}
};
document.addEventListener("click", handleClick);
return () => document.removeEventListener("click", handleClick);
});
const klass = styles.errorsummary.split(" ")[0];
// Disable autofocus in storybook
return (
<>
>
);
},
],
} satisfies Meta;
export default meta;
type Story = StoryObj;
export const Default: Story = {
render: () => (
For å gå videre må du rette opp følgende feil:
),
};
export const React: Story = {
render: () => (
For å gå videre må du rette opp følgende feil:
),
};
export const WithForm: Story = {
render: function Render() {
const [showErrorSummary, setShowErrorSummary] = useState(false);
const [value, setValue] = useState("");
const errorTitle = useRef(null);
const handleSubmit = (event: React.FormEvent) => {
const isValid = value === "jeg tester";
event.preventDefault();
setShowErrorSummary(!isValid);
if (isValid) alert("Du fikk det til!");
else setTimeout(() => errorTitle.current?.focus()); // Move focus on next render
};
return (
);
},
};