Package Exports
- sveltejs-forms
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sveltejs-forms) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
sveltejs-forms
Declarative forms for Svelte.
Install
$ npm i sveltejs-formsor
$ yarn add sveltejs-formsHow to use
Example
<script>
import { Form, Input, Select, Choice } from 'sveltejs-forms';
import * as yup from 'yup';
function handleSubmit({ detail: { values, setSubmitting, resetForm } }) {
setTimeout(() => {
console.log(values);
setSubmitting(false);
resetForm({
user: { email: 'test@user.com' }, // optional
});
}, 2000);
/**
* {
* user: {
* email: 'email@example.com'
* },
* password: '123456',
* language: 'svelte',
* os: 'osx,linux'
* }
*/
}
function handleReset() {
console.log('form has been reset');
}
const schema = yup.object().shape({
user: yup.object().shape({
email: yup
.string()
.required()
.email(),
}),
password: yup.string().min(4),
language: yup.string().required(),
os: yup.string(),
});
const langOptions = [
{ id: 'svelte', title: 'Svelte' },
{ id: 'react', title: 'React' },
{ id: 'angular', title: 'Angular' },
];
const osOptions = [
{ id: 'macos', title: 'macOS' },
{ id: 'linux', title: 'Linux 🐧' },
{ id: 'windows', title: 'Windows' },
];
const initialValues = {
language: 'svelte',
};
</script>
<style lang="scss">
:global(.sveltejs-forms) {
background-color: lightgray;
display: inline-block;
padding: 1rem;
.field {
margin-bottom: 1rem;
&.error {
input {
border: 1px solid red;
}
.message {
margin-top: 0.2rem;
color: red;
font-size: 0.8rem;
}
}
}
}
</style>
<Form
{schema} <!-- optional -->
{initialValues} <!-- optional -->
validateOnBlur={false} <!-- optional, default: true -->
validateOnChange={false} <!-- optional, default: true -->
on:submit={handleSubmit}
on:reset={handleReset}
let:isSubmitting
let:isValid
>
<Input name="user.email" placeholder="Email" /> <!-- nested field -->
<Input name="password" type="password" placeholder="Password" />
<Select name="language" options={langOptions} />
<Choice name="os" options={osOptions} multiple />
<button type="reset">Reset</button>
<button type="submit" disabled={isSubmitting}>Sign in</button>
The form is valid: {isValid}
</Form>