SPB Git

Repository Public

form.tsx 6.3 KB 138 lines tsx

1"use client";2import { useId, type InputHTMLAttributes, type ReactNode, type SelectHTMLAttributes, type TextareaHTMLAttributes } from "react";3import { useI18n } from "@/i18n";4import { cx } from "./cx";5import { Icon, type IconName } from "./Icon";67interface FieldChrome {8  label: string;9  hint?: ReactNode;10  error?: string | undefined;11  required?: boolean;12  hideLabel?: boolean;13}14interface A11y {15  id: string;16  "aria-describedby"?: string;17  "aria-invalid"?: true;18  required?: boolean;19}2021/** Label + hint + error wiring for any control. */22export function Field({ label, hint, error, required, hideLabel, children }: FieldChrome & { children: (a11y: A11y) => ReactNode }) {23  const id = useId();24  const hintId = hint ? `${id}-h` : undefined;25  const errId = error ? `${id}-e` : undefined;26  const describedBy = [hintId, errId].filter(Boolean).join(" ") || undefined;27  return (28    <div className="mcc-field">29      <label htmlFor={id} className={hideLabel ? "sr-only" : "mcc-label"}>30        {label}31        {required ? <span className="req" aria-hidden="true">*</span> : null}32      </label>33      {children({ id, ...(describedBy ? { "aria-describedby": describedBy } : {}), ...(error ? { "aria-invalid": true as const } : {}), ...(required ? { required } : {}) })}34      {hint ? <span id={hintId} className="mcc-hint">{hint}</span> : null}35      {error ? <span id={errId} className="mcc-error-text" role="alert"><Icon name="critical" size={13} />{error}</span> : null}36    </div>37  );38}3940type InputProps = FieldChrome & Omit<InputHTMLAttributes<HTMLInputElement>, "className" | "onChange" | "value" | "required"> & {41  value: string;42  onChange: (value: string) => void;43  mono?: boolean;44  icon?: IconName;45};46export function TextField({ label, hint, error, required, hideLabel, value, onChange, mono, icon, type = "text", ...rest }: InputProps) {47  return (48    <Field label={label} hint={hint} error={error} required={required ?? false} hideLabel={hideLabel ?? false}>49      {(a) => {50        const input = <input {...rest} {...a} type={type} value={value} onChange={(e) => onChange(e.target.value)} className={cx("mcc-input", mono && "mono")} />;51        return icon ? <span className="mcc-input-wrap"><Icon name={icon} size={15} />{input}</span> : input;52      }}53    </Field>54  );55}5657type AreaProps = FieldChrome & Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, "className" | "onChange" | "value" | "required"> & { value: string; onChange: (value: string) => void };58export function TextAreaField({ label, hint, error, required, hideLabel, value, onChange, ...rest }: AreaProps) {59  return (60    <Field label={label} hint={hint} error={error} required={required ?? false} hideLabel={hideLabel ?? false}>61      {(a) => <textarea {...rest} {...a} value={value} onChange={(e) => onChange(e.target.value)} className="mcc-textarea" />}62    </Field>63  );64}6566type SelectProps<V extends string> = FieldChrome & Omit<SelectHTMLAttributes<HTMLSelectElement>, "className" | "onChange" | "value" | "required"> & {67  value: V;68  onChange: (value: V) => void;69  options: readonly { value: V; label: string; disabled?: boolean }[];70};71export function SelectField<V extends string>({ label, hint, error, required, hideLabel, value, onChange, options, ...rest }: SelectProps<V>) {72  return (73    <Field label={label} hint={hint} error={error} required={required ?? false} hideLabel={hideLabel ?? false}>74      {(a) => (75        <select {...rest} {...a} value={value} onChange={(e) => onChange(e.target.value as V)} className="mcc-select">76          {options.map((o) => <option key={o.value} value={o.value} disabled={o.disabled}>{o.label}</option>)}77        </select>78      )}79    </Field>80  );81}8283export function Checkbox({ label, checked, onChange, disabled, hint }: { label: ReactNode; checked: boolean; onChange: (v: boolean) => void; disabled?: boolean; hint?: string }) {84  const id = useId();85  return (86    <div className="mcc-field">87      <label className="mcc-check" htmlFor={id}>88        <input id={id} type="checkbox" checked={checked} disabled={disabled} onChange={(e) => onChange(e.target.checked)} aria-describedby={hint ? `${id}-h` : undefined} />89        <span>{label}</span>90      </label>91      {hint ? <span id={`${id}-h`} className="mcc-hint">{hint}</span> : null}92    </div>93  );94}9596/** Switch: the on/off state is also written as text, not only shown by position and colour. */97export function Switch({ label, checked, onChange, disabled, hint }: { label: string; checked: boolean; onChange: (v: boolean) => void; disabled?: boolean; hint?: string }) {98  const { t } = useI18n();99  const id = useId();100  return (101    <div className="mcc-field">102      <label className="mcc-switch" htmlFor={id}>103        <input id={id} type="checkbox" role="switch" checked={checked} disabled={disabled} onChange={(e) => onChange(e.target.checked)} aria-describedby={hint ? `${id}-h` : undefined} />104        <span>{label}</span>105        <span className="state" aria-hidden="true">{checked ? t("common.on") : t("common.off")}</span>106      </label>107      {hint ? <span id={`${id}-h`} className="mcc-hint">{hint}</span> : null}108    </div>109  );110}111112/** Radio group rendered as a segmented control. */113export function Segmented<V extends string>({ label, value, onChange, options }: { label: string; value: V; onChange: (v: V) => void; options: readonly { value: V; label: string; icon?: IconName; title?: string }[] }) {114  const move = (dir: 1 | -1) => {115    const i = options.findIndex((o) => o.value === value);116    const next = options[(i + dir + options.length) % options.length];117    if (next) onChange(next.value);118  };119  return (120    <div121      role="radiogroup"122      aria-label={label}123      className="mcc-seg"124      onKeyDown={(e) => {125        if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); move(1); }126        if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); move(-1); }127      }}128    >129      {options.map((o) => (130        <button key={o.value} type="button" role="radio" aria-checked={o.value === value} aria-label={o.icon ? o.label : undefined} title={o.title} tabIndex={o.value === value ? 0 : -1} onClick={() => onChange(o.value)}131          ref={(el) => { if (el && o.value === value && el.parentElement?.contains(document.activeElement) && document.activeElement !== el) el.focus(); }}>132          {o.icon ? <Icon name={o.icon} size={14} /> : o.label}133        </button>134      ))}135    </div>136  );137}138
Files main
Menu

Theme