@@ -14,6 +14,8 @@ Declarative forms for [Svelte](https://svelte.dev/).
14
14
15
15
- optional schema-based validation through [ Yup] ( https://github.com/jquense/yup )
16
16
- access to nested properties using paths
17
+ - supports custom components
18
+ - provides ` Input ` , ` Select ` , ` Choice ` components to reduce boilerplate
17
19
18
20
## Install
19
21
@@ -29,7 +31,7 @@ $ yarn add sveltejs-forms
29
31
30
32
## How to use
31
33
32
- ### Example
34
+ ### With provided ` Input ` , ` Select ` , ` Choice ` helper components
33
35
34
36
``` html
35
37
<script >
@@ -132,3 +134,84 @@ $ yarn add sveltejs-forms
132
134
The form is valid: {isValid}
133
135
</Form >
134
136
```
137
+
138
+ ### With custom component:
139
+
140
+ ``` html
141
+ <script >
142
+ import { Form } from ' sveltejs-forms' ;
143
+ import Select from ' svelte-select' ;
144
+ import * as yup from ' yup' ;
145
+
146
+ let svelteSelect;
147
+
148
+ function handleSubmit ({ detail: { values, setSubmitting, resetForm } }) {
149
+ setTimeout (() => {
150
+ console .log (values);
151
+ setSubmitting (false );
152
+ svelteSelect .handleClear ();
153
+ resetForm ();
154
+ }, 2000 );
155
+ }
156
+
157
+ const schema = yup .object ().shape ({
158
+ food: yup
159
+ .array ()
160
+ .of (yup .string ().required ())
161
+ .min (2 ),
162
+ });
163
+
164
+ let items = [
165
+ { value: ' chocolate' , label: ' Chocolate' },
166
+ { value: ' pizza' , label: ' Pizza' },
167
+ { value: ' cake' , label: ' Cake' },
168
+ { value: ' chips' , label: ' Chips' },
169
+ { value: ' ice-cream' , label: ' Ice Cream' },
170
+ ];
171
+ </script >
172
+
173
+ <Form
174
+ {schema}
175
+ on:submit ={handleSubmit}
176
+ let:setValue
177
+ let:validate
178
+ let:values
179
+ let:errors
180
+ let:touched >
181
+
182
+ <Select
183
+ {items}
184
+ isMulti ={true}
185
+ bind:this ={svelteSelect}
186
+ inputAttributes =" {{ name: 'food' }}"
187
+ hasError =" {touched['food'] && errors['food']}"
188
+ on:select =" {({ detail }) => {
189
+ setValue('food', detail && detail.map(item => item.value));
190
+ validate();
191
+ }}"
192
+ on:clear =" {() => {
193
+ setValue('food', []);
194
+ validate();
195
+ }}"
196
+ selectedValue =" {items.filter(item => values['food'].includes(item.value))}" />
197
+
198
+ <button type =" submit" >Sign in</button >
199
+ </Form >
200
+ ```
201
+
202
+ ## Slot props
203
+
204
+ | Name | Type |
205
+ | ------| ------|
206
+ | isSubmitting | ` boolean `
207
+ | isValid | ` boolean `
208
+ | setValue(path, value) | ` function `
209
+ | touchField(path) | ` function `
210
+ | validate() | ` function `
211
+ | values | ` object `
212
+ | errors | ` object `
213
+ | touched | ` object `
214
+
215
+ ## Contributions
216
+
217
+ ** All contributions (no matter if small) are always welcome.**
0 commit comments