✨ Add TextField and Alert components
TextField supports label, description, error state and hidden labels with proper aria wiring. Alert ships the four Aksel variants with matching icons from @navikt/aksel-icons. Adds stylesheets for both.
This commit is contained in:
1 parent
1bcf7f5d53
commit
bb9501ea31
9 files changed
+286
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { forwardRef } from "react";
|
||||
import type { HTMLAttributes, ReactNode } from "react";
|
||||
import {
|
||||
CheckmarkIcon,
|
||||
InformationIcon,
|
||||
WarningIcon,
|
||||
XMarkIcon,
|
||||
} from "@navikt/aksel-icons";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface AlertProps extends HTMLAttributes<HTMLDivElement> {
|
||||
/**
|
||||
* Alert variant.
|
||||
* @default "info"
|
||||
*/
|
||||
variant?: "info" | "success" | "warning" | "error";
|
||||
/**
|
||||
* Alert size.
|
||||
* @default "medium"
|
||||
*/
|
||||
size?: "medium" | "small";
|
||||
/**
|
||||
* Stretches the alert to fill its container.
|
||||
* @default false
|
||||
*/
|
||||
fullWidth?: boolean;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
const variantIcons = {
|
||||
info: InformationIcon,
|
||||
success: CheckmarkIcon,
|
||||
warning: WarningIcon,
|
||||
error: XMarkIcon,
|
||||
} as const;
|
||||
|
||||
export const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
|
||||
{ variant = "info", size = "medium", fullWidth = false, className, children, ...rest },
|
||||
ref,
|
||||
) {
|
||||
const Icon = variantIcons[variant];
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
role={variant === "error" ? "alert" : "status"}
|
||||
className={cn(
|
||||
"navds-alert",
|
||||
`navds-alert--${variant}`,
|
||||
`navds-alert--${size}`,
|
||||
fullWidth && "navds-alert--full-width",
|
||||
className,
|
||||
)}
|
||||
{...rest}
|
||||
>
|
||||
<Icon className="navds-alert__icon" />
|
||||
<div className="navds-alert__content">{children}</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { Alert } from "./Alert";
|
||||
export type { AlertProps } from "./Alert";
|
||||
@@ -4,3 +4,7 @@ export { BodyShort } from "./body-short";
|
||||
export type { BodyShortProps } from "./body-short";
|
||||
export { Heading } from "./heading";
|
||||
export type { HeadingProps } from "./heading";
|
||||
export { TextField } from "./text-field";
|
||||
export type { TextFieldProps } from "./text-field";
|
||||
export { Alert } from "./alert";
|
||||
export type { AlertProps } from "./alert";
|
||||
@@ -0,0 +1,66 @@
|
||||
import { forwardRef, useId } from "react";
|
||||
import type { InputHTMLAttributes, ReactNode } from "react";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface TextFieldProps
|
||||
extends Omit<InputHTMLAttributes<HTMLInputElement>, "size"> {
|
||||
/** Field label. */
|
||||
label: ReactNode;
|
||||
/** Additional description shown below the label. */
|
||||
description?: ReactNode;
|
||||
/** Error message. When set, the field is marked invalid. */
|
||||
error?: ReactNode;
|
||||
/**
|
||||
* Input size.
|
||||
* @default "medium"
|
||||
*/
|
||||
size?: "medium" | "small";
|
||||
/**
|
||||
* Visually hides the label.
|
||||
* @default false
|
||||
*/
|
||||
hideLabel?: boolean;
|
||||
}
|
||||
|
||||
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(function TextField(
|
||||
{ label, description, error, size = "medium", hideLabel = false, id, className, ...rest },
|
||||
ref,
|
||||
) {
|
||||
const generatedId = useId();
|
||||
const inputId = id ?? generatedId;
|
||||
const errorId = `${inputId}-error`;
|
||||
const descriptionId = `${inputId}-description`;
|
||||
|
||||
return (
|
||||
<div className={cn("navds-form-field", `navds-form-field--${size}`, className)}>
|
||||
<label
|
||||
className={cn("navds-form-field__label", hideLabel && "navds-sr-only")}
|
||||
htmlFor={inputId}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
{description ? (
|
||||
<p className="navds-form-field__description" id={descriptionId}>
|
||||
{description}
|
||||
</p>
|
||||
) : null}
|
||||
<input
|
||||
ref={ref}
|
||||
id={inputId}
|
||||
className={cn("navds-text-field", error && "navds-text-field--error")}
|
||||
aria-invalid={error ? true : undefined}
|
||||
aria-describedby={
|
||||
[description ? descriptionId : undefined, error ? errorId : undefined]
|
||||
.filter(Boolean)
|
||||
.join(" ") || undefined
|
||||
}
|
||||
{...rest}
|
||||
/>
|
||||
{error ? (
|
||||
<p className="navds-form-field__error" id={errorId} role="alert">
|
||||
{error}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { TextField } from "./TextField";
|
||||
export type { TextFieldProps } from "./TextField";
|
||||
Reference in new issue
Block a user