FormControl
A container that wraps a form element together with its label.
View in Storybook (opens in a new tab)
Installation
import { FormControl } from "@ginga-ui/core";Basic usage
An example of a form control with a label.
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="Username">
<Input placeholder="Enter your username" />
</FormControl>
);
}Custom ID
An example with an explicitly specified 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="Email address"
htmlFor="email-input"
>
<Input id="email-input" type="email" placeholder="example@email.com" />
</FormControl>
);
}Multiple fields
An example with multiple form controls.
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="Name">
<Input placeholder="John Smith" />
</FormControl>
<FormControl label="Email address">
<Input type="email" placeholder="example@email.com" />
</FormControl>
<FormControl label="Password">
<Input type="password" placeholder="At least 8 characters" />
</FormControl>
</div>
);
}Form
A complete form example.
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="Full name" htmlFor="name">
<Input id="name" required />
</FormControl>
<FormControl label="Email address" htmlFor="email">
<Input id="email" type="email" required />
</FormControl>
<Button
type="submit"
variant="filled"
startIcon={<Send />}
className={styles.submit}
>
Submit
</Button>
</form>
<Modal isOpen={submitted} onOpenChange={setSubmitted}>
<Dialog>
<DialogTitle>Form submitted</DialogTitle>
<p>We received what you entered.</p>
<div className={styles.actions}>
<Button slot="close" variant="outline">
Close
</Button>
</div>
</Dialog>
</Modal>
</>
);
}Props
FormControl
| Name | Type | Default | Description |
|---|---|---|---|
label | ReactNode | - | Required. Label associated with the field. |
children | ReactNode | - | Required. Form field such as Input or Select. |
htmlFor | string | - | id of the field. When omitted, an id is generated and passed to Input automatically. |
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML div (opens in a new tab) element.