A reusable wrapper for the HTML form element, simplifying form creation, submission handling, event management and controlling all the inputs from a single place.
About
The Form component is a wrapper around the native <form> element. You do not have to use it, but it is where the shared input settings live: set variant, size, rounded or floatingLabel once on the form and every field inside picks them up.
Example
To use the Form component, simply import it from the library and wrap your form inputs inside it. Here's a basic example:
<script>
import { Form } from "theui-svelte";
</script>
<Form>
<!-- Add your form inputs here -->
</Form>Every input inside it picks up those settings, so a change in one place reaches the whole form.
Form Method
The method prop specifies the HTTP method to be used when the form is submitted. It corresponds to the native HTML method attribute of the <form> element. Available values are GET and POST. Default POST.
<Form method="GET">
<!-- Form inputs go here -->
</Form>In this example, the form will use the GET method when submitted, appending the form data to the URL. If no method is specified, it defaults to POST.
Progressive Enhancement
The enhance prop accepts a Svelte action that is applied to the <form> element. Pass SvelteKit's enhance action to submit the form without a full page reload, while keeping it working when JavaScript is unavailable.
<script>
import { enhance } from "$app/forms";
import { Form } from "theui-svelte";
</script>
<Form action="?/login" {enhance}>
<!-- Form inputs go here -->
</Form>Accessibility
The Form component renders a native <form> element, which browsers and screen readers already understand. Any ARIA role, label or attribute you pass is spread onto it, so you can add whatever a particular page needs.
Configuration
Some of the Form props are not used by the form element itself. They are handed down to the inputs inside it, so you set them once instead of on every field.