✨ 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,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