FormControl
フォーム要素をラベルと共にラップするコンテナ。
インストール
import { FormControl } from "@ginga-ui/core";基本的な使い方
ラベル付きフォームコントロールの例です。
form-control.tsx
form-control.module.css
import { FormControl, Input } from "@ginga-ui/core";
import styles from "./form-control.module.css";
export function BasicExample() {
return (
<FormControl className={styles.basicField} label="ユーザー名">
<Input placeholder="ユーザー名を入力" />
</FormControl>
);
}ID指定
明示的にIDを指定した例です。
form-control.tsx
form-control.module.css
import { FormControl, Input } from "@ginga-ui/core";
import styles from "./form-control.module.css";
export function WithIdExample() {
return (
<FormControl
className={styles.idField}
label="メールアドレス"
htmlFor="email-input"
>
<Input id="email-input" type="email" placeholder="example@email.com" />
</FormControl>
);
}複数のフィールド
複数のフォームコントロールを配置した例です。
form-control.tsx
form-control.module.css
import { FormControl, Input } from "@ginga-ui/core";
import styles from "./form-control.module.css";
export function MultipleFieldsExample() {
return (
<div className={styles.fieldList}>
<FormControl label="名前">
<Input placeholder="山田 太郎" />
</FormControl>
<FormControl label="メールアドレス">
<Input type="email" placeholder="example@email.com" />
</FormControl>
<FormControl label="パスワード">
<Input type="password" placeholder="8文字以上" />
</FormControl>
</div>
);
}フォーム
完全なフォームの例です。
form-control.tsx
form-control.module.css
import { useState } from "react";
import { Button, Dialog, DialogTitle, FormControl, Input, Modal } from "@ginga-ui/core";
import { Send } from "lucide-react";
import styles from "./form-control.module.css";
export function FormExample() {
const [submitted, setSubmitted] = useState(false);
return (
<>
<form
className={styles.form}
onSubmit={(e) => {
e.preventDefault();
setSubmitted(true);
}}
>
<FormControl label="お名前" htmlFor="name">
<Input id="name" required />
</FormControl>
<FormControl label="メールアドレス" htmlFor="email">
<Input id="email" type="email" required />
</FormControl>
<Button
type="submit"
variant="filled"
startIcon={<Send />}
className={styles.submit}
>
送信
</Button>
</form>
<Modal isOpen={submitted} onOpenChange={setSubmitted}>
<Dialog>
<DialogTitle>フォームを送信しました</DialogTitle>
<p>入力した内容を受け付けました。</p>
<div className={styles.actions}>
<Button slot="close" variant="outline">
閉じる
</Button>
</div>
</Dialog>
</Modal>
</>
);
}Props
FormControl
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
label | ReactNode | - | 必須のpropsです。フィールドに関連付けるラベル。 |
children | ReactNode | - | 必須のpropsです。Input や Select などのフォームフィールド。 |
htmlFor | string | - | フィールドの id。省略すると id を生成して Input に自動で渡します。 |
render | (props) => ReactElement | - | ルート要素を差し替える関数。受け取った props を渡した要素(next/link など)を返します。 |
上記に加えて、HTML の div(新しいタブで開く) 要素の属性を受け取ります。