0.0.23 • Published 4 years ago
@svelte-parts/form v0.0.23
@svelte-parts/form
Yet another form component.
The idea here is to define the fields in json. The form definition can then come from the server or be modified on the fly.
Try it out in the svelte REPL
Install
npm install @svelte-parts/formUsage
<script>
import Form from '@svelte-parts/form'
const fields = [
{ type: 'text', property: 'username', label: 'User name', pattern: '[a-z0-9-]+', minLength: 5 },
{ type: 'date', property: 'birthDate', label: 'Birth date' },
{ type: 'email', property: 'email', label: 'Email' },
{ type: 'checkbox', property: 'wantsSpam', label: 'Subscribe to the newsletter', value: true },
]
const onSubmit = data => alert(JSON.stringify(data))
</script>
<Form fields={fields} onSubmit={onSubmit}>Properties
fieldsthe form definition. It is the only required field (see type definition)onSubmita callback triggered when the form is submittedsubmitButtonis a string to use as a label for the submit button if you don not want to use the browser defaulterrorMessageis a string that will be shown between the last form field and the submit button
Types
<script lang="ts">
import type { Field } from '@svelte-parts/form'
import Form from '@svelte-parts/form'
const fields: Field[] = [
{ type: 'text', property: 'username', label: 'User name', pattern: '[a-z0-9-]+', minLength: 5 },
{ type: 'date', property: 'birthDate', label: 'Birth date' },
{ type: 'email', property: 'email', label: 'Email' },
{ type: 'checkbox', property: 'wantsSpam', label: 'Subscribe to the newsletter', value: true },
]
const onSubmit = data => alert(JSON.stringify(data))
</script>
<Form fields={fields} onSubmit={onSubmit} />