Forms

Input

A labeled field with connected descriptions and errors.

OSCL StudioComponents, in context.Live preview
FOUNDATION / FORMS

Input

A real label. A little extra context.

Interactive preview. Real components.View docs

Use it in your project

Install oscl-ui from npm and import oscl-ui/styles.css once. See installation.

input-example.tsx
import { Input } from "oscl-ui";

<Input label="Email" type="email" placeholder="you@example.com" />

Accessibility

Keep visible labels and keyboard focus. Supply an accessible name to icon-only actions. Try this preview with Tab, Enter, Space, and Escape where appropriate.

The source, in full

This is the actual source from the isolated OSCL package used by this website. Native props and TypeScript declarations are included in the npm package.

input.tsx
"use client";

import { forwardRef, useId } from "react";
import type { InputHTMLAttributes, ReactNode } from "react";

export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
  label: ReactNode;
  description?: string;
  error?: string;
}

export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
  { label, description, error, id, className = "", "aria-describedby": describedBy, ...props }, ref,
) {
  const generatedId = useId();
  const fieldId = id || generatedId;
  const descriptionIds = [describedBy, description && `${fieldId}-description`, error && `${fieldId}-error`].filter(Boolean).join(" ");
  return <div className={`oscl-field ${className}`}>
    <label className="oscl-label" htmlFor={fieldId}>{label}{props.required && <span aria-hidden="true"> *</span>}</label>
    <input {...props} ref={ref} id={fieldId} className="oscl-input" aria-invalid={error ? true : props["aria-invalid"]}
      aria-describedby={descriptionIds || undefined} />
    {description && <p className="oscl-description" id={`${fieldId}-description`}>{description}</p>}
    {error && <p className="oscl-error" id={`${fieldId}-error`}>{error}</p>}
  </div>;
});