spst-contact-fields
実サービスへ通信しない見本です。モデルの描画は静止した代替表示です。
元 commit: d204666f04a0ee7b6f18543f8aec414b4128874e · shadcn build 4.23.1
Noto Sans JP · @fontsource/noto-sans-jp 5.3.1 · OFL-1.1
操作を試す
取り込む(公開予定)
以下の URL は公開予定です。公開前は取り込めません。既存ファイルとの差分を確認してから実行し、--overwrite を既定にしないでください。
npx shadcn@latest add https://spritestack.dev/r/spst-contact-fields.jsoncomponents.json の registries に追加
{"registries":{"@spritestack":"https://spritestack.dev/r/{name}.json"}}npx shadcn@latest add @spritestack/spst-contact-fieldsReact 19・Tailwind v4・shadcn の src/ を持つ構成が対象です。src/ の無い構成は未検証です。取り込みはソースのコピーで、自動更新されません。
import "./styles/spritestack/theme.css";
<div className="spst-theme" data-theme="light">
{/* Render components here. */}
</div>Portal の container / portalContainer は .spst-theme 内の要素を渡してください。Noto Sans JP の 400・600・700 は利用先が自己ホストしてください。未導入時は system-ui です。
依存
Props / API
以下は実際のソースを構文解析して生成しています。… の行は継承する React / Radix の公開型です。既定欄の — は、この関数の引数で既定を指定していないことを表します。union の必須条件は型宣言も確認してください。
src/components/spritestack/contact-fields.tsx
ContactFields
ContactFields({
messages,
value,
onValueChange,
onSubmit,
errors = {},
error,
pending,
completed,
portalContainer,
}: ContactFieldsProps)| Prop | 型 | 必須 | 既定 | イベント |
|---|---|---|---|---|
messages | Partial<ContactFieldsMessages> | いいえ | — | — |
value | ContactValue | はい | — | — |
onValueChange | (value: ContactValue) => void | はい | — | はい |
onSubmit | () => void | はい | — | はい |
errors | Partial<Record<keyof ContactValue, string>> | いいえ | {} | — |
error | string | いいえ | — | — |
pending | boolean | いいえ | — | — |
completed | boolean | いいえ | — | — |
portalContainer | HTMLElement | null | いいえ | — | — |
分割代入の既定値(継承した props も含む)
errors = {}型宣言とソースを確認
// 出典: SpriteStack デザインシステム設計 §4.2・§4.4(Issue 313)。基本部品は同梱の shadcn/ui 派生を合成。
"use client";
import { useId } from "react";
import { Alert } from "../ui/alert";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select";
import { Textarea } from "../ui/textarea";
export const contactKinds = [
"要望",
"不具合",
"取材",
"権利の申し立て",
"個人情報の開示訂正削除",
"脆弱性の報告",
"その他",
] as const;
export type ContactValue = { kind: string; body: string; replyTo: string };
export type ContactFieldsMessages = {
kindLabel: (kind: (typeof contactKinds)[number]) => string;
kind: string;
placeholder: string;
body: string;
replyTo: string;
submit: string;
completed: string;
pending: string;
};
const defaultMessages: ContactFieldsMessages = {
kindLabel: (kind) => kind,
kind: "種類",
placeholder: "選択してください",
body: "本文",
replyTo: "返信先",
submit: "送信",
completed: "送信が完了しました",
pending: "処理中",
};
export type ContactFieldsProps = {
messages?: Partial<ContactFieldsMessages>;
value: ContactValue;
onValueChange: (value: ContactValue) => void;
onSubmit: () => void;
errors?: Partial<Record<keyof ContactValue, string>>;
error?: string;
pending?: boolean;
completed?: boolean;
portalContainer?: HTMLElement | null;
};
export function ContactFields({
messages,
value,
onValueChange,
onSubmit,
errors = {},
error,
pending,
completed,
portalContainer,
}: ContactFieldsProps) {
const m = { ...defaultMessages, ...messages };
const id = useId();
const disabled = pending || completed;
return (
<form
data-component="contact-fields"
className="grid gap-4"
aria-busy={pending || undefined}
onSubmit={(event) => {
event.preventDefault();
if (!disabled && !Object.values(errors).some(Boolean)) onSubmit();
}}
>
<Label htmlFor={`${id}-kind`}>{m.kind}</Label>
<Select
value={value.kind}
onValueChange={(kind) => onValueChange({ ...value, kind })}
disabled={disabled}
>
<SelectTrigger
id={`${id}-kind`}
aria-invalid={!!errors.kind}
aria-describedby={errors.kind ? `${id}-kind-error` : undefined}
>
<SelectValue placeholder={m.placeholder} />
</SelectTrigger>
<SelectContent container={portalContainer}>
{contactKinds.map((kind) => (
<SelectItem key={kind} value={kind}>
{m.kindLabel(kind)}
</SelectItem>
))}
</SelectContent>
</Select>
{errors.kind && (
<Alert variant="error" id={`${id}-kind-error`}>
{errors.kind}
</Alert>
)}
{(
[
["body", m.body],
["replyTo", m.replyTo],
] as const
).map(([key, label]) => {
const props = {
id: `${id}-${key}`,
value: value[key],
disabled,
"aria-invalid": !!errors[key],
"aria-describedby": errors[key] ? `${id}-${key}-error` : undefined,
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) =>
onValueChange({ ...value, [key]: event.target.value }),
};
return (
<div key={key} className="grid gap-2">
<Label htmlFor={props.id}>{label}</Label>
{key === "body" ? <Textarea {...props} /> : <Input type="email" {...props} />}
{errors[key] && (
<Alert variant="error" id={`${id}-${key}-error`}>
{errors[key]}
</Alert>
)}
</div>
);
})}
{error && <Alert variant="error">{error}</Alert>}
<Button
type="submit"
pending={pending}
pendingLabel={m.pending}
disabled={completed || Object.values(errors).some(Boolean)}
>
{m.submit}
</Button>
<p role="status">{completed ? m.completed : ""}</p>
</form>
);
}
アクセシビリティの利用条件
- 入力には Label または読み上げ名を与え、エラーを aria-describedby で関連づけます。色だけで状態を伝えません。
- 画像にはモデル名と代替表示を与えます。単独アイコンのボタンにも名前が必要です。
- Dialog / Sheet に題名と説明を与え、Portal をテーマ内に置きます。Tab・矢印・Escape と閉じた後のフォーカスを利用先でも確認します。
- Slider は名前と有限の min < max、ToggleGroup は空でない single の値が必要です。pending は親が制御します。
対応画像: 10 · 暗色とエラーは設計による補完です。