Skip to content

Commit 3c46bc1

Browse files
author
Larry Botha
authored
Merge pull request tjinauyeung#90 from pixelmund/feat/typedefs
2 parents b9aecf7 + 43bc0cd commit 3c46bc1

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

lib/index.d.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import type {SvelteComponentTyped} from 'svelte';
2+
import type {Readable, Writable} from 'svelte/store';
3+
import type {ObjectSchema} from 'yup';
4+
5+
/**
6+
* Unfortunately svelte currently does not support generics in components so we export it to use it in scripts like this
7+
*
8+
* const formProps: FormProps = {
9+
* intitialValues: {...},
10+
* onSubmit: values => {...} -> values will be inffered from initialValues
11+
* }
12+
*
13+
* */
14+
export type FormProps<Inf = Record<string, unknown>> = {
15+
initialValues: Inf;
16+
onSubmit: ((values: Inf) => any) | ((values: Inf) => Promise<any>);
17+
validate?: (values: Inf) => any | undefined;
18+
validationSchema?: ObjectSchema<any>;
19+
}
20+
21+
type FieldProps = {
22+
name: string;
23+
type?: string;
24+
value?: string;
25+
};
26+
27+
type SelectProps = {
28+
name: string;
29+
class?: string;
30+
value?: string;
31+
};
32+
33+
type ErrorProps = {
34+
name: string;
35+
class?: string;
36+
};
37+
38+
type TextareaProps = {
39+
name: string;
40+
class?: string;
41+
cols?: number;
42+
rows?: number;
43+
};
44+
45+
type FormState<Inf = Record<string, unknown>> = {
46+
form: Writable<Inf>;
47+
errors: Writable<Record<keyof Inf, string>>;
48+
touched: Writable<Record<keyof Inf, boolean>>;
49+
modified: Readable<Record<keyof Inf, boolean>>;
50+
isValid: Readable<boolean>;
51+
isSubmitting: Writable<boolean>;
52+
isValidating: Writable<boolean>;
53+
isModified: Readable<boolean>;
54+
updateField: (field: keyof Inf, value: any) => void;
55+
updateValidateField: (field: keyof Inf, value: any) => void;
56+
updateTouched: (field: keyof Inf, value: any) => void;
57+
validateField: (field: keyof Inf) => Promise<any>;
58+
updateInitialValues: (newValues: Inf) => void;
59+
handleReset: () => void;
60+
state: Readable<{
61+
form: Inf;
62+
errors: Record<keyof Inf, string>;
63+
touched: Record<keyof Inf, boolean>;
64+
modified: Record<keyof Inf, boolean>;
65+
isValid: boolean;
66+
isSubmitting: boolean;
67+
isValidating: boolean;
68+
isModified: boolean;
69+
}>;
70+
handleChange: () => void;
71+
handleSubmit: () => any;
72+
};
73+
74+
declare function createForm<Inf = Record<string, unknown>>(formProps: {
75+
initialValues: Inf;
76+
onSubmit: (values: Inf) => any | Promise<any>;
77+
validate?: (values: Inf) => any | undefined;
78+
validationSchema?: ObjectSchema<any>;
79+
}): FormState<Inf>;
80+
81+
declare class Form extends SvelteComponentTyped<
82+
FormProps & {
83+
class?: string;
84+
},
85+
{},
86+
{
87+
default: Pick<
88+
FormState,
89+
| 'errors'
90+
| 'touched'
91+
| 'updateField'
92+
| 'updateTouched'
93+
| 'handleChange'
94+
| 'handleSubmit'
95+
| 'form'
96+
| 'state'
97+
>;
98+
}
99+
> {}
100+
101+
declare class Field extends SvelteComponentTyped<FieldProps, {}, {}> {}
102+
103+
declare class Textarea extends SvelteComponentTyped<TextareaProps, {}, {}> {}
104+
105+
declare class Select extends SvelteComponentTyped<
106+
SelectProps,
107+
{},
108+
{default: any}
109+
> {}
110+
111+
declare class ErrorMessage extends SvelteComponentTyped<
112+
ErrorProps,
113+
{},
114+
{default: any}
115+
> {}
116+
117+
export {createForm, Form, Field, Select, ErrorMessage, Textarea};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "svelte-forms-lib",
33
"version": "0.0.0-semantically-released",
44
"description": "Svelte forms lib - A lightweight library for managing forms in Svelte v3",
5+
"typings": "./lib/index.d.ts",
56
"scripts": {
67
"start": "rollup -c -w rollup.config.js",
78
"build": "cross-env NODE_ENV=production && rollup -c rollup.config.js",

0 commit comments

Comments
 (0)