Skip to content

Commit b32737f

Browse files
committed
feat(index.d.ts): added type definitions
fix tjinauyeung#81
1 parent 03d1813 commit b32737f

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

lib/index.d.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
class?: string;
16+
initialValues: Inf;
17+
onSubmit: ((values: Inf) => any) | ((values: Inf) => Promise<any>);
18+
validate?: (values: Inf) => any | undefined;
19+
validationSchema?: ObjectSchema<any>;
20+
};
21+
22+
type FieldProps = {
23+
name: string;
24+
type?: string;
25+
value?: string;
26+
};
27+
28+
type SelectProps = {
29+
name: string;
30+
class?: string;
31+
value?: string;
32+
};
33+
34+
type ErrorProps = {
35+
name: string;
36+
class?: string;
37+
};
38+
39+
type TextareaProps = {
40+
name: string;
41+
class?: string;
42+
cols?: number;
43+
rows?: number;
44+
};
45+
46+
declare function createForm<Inf = Record<string, unknown>>(formProps: {
47+
initialValues: Inf;
48+
onSubmit: (values: Inf) => any | Promise<any>;
49+
validate?: (values: Inf) => any | undefined;
50+
validationSchema?: ObjectSchema<any>;
51+
}): {
52+
form: Writable<Inf>;
53+
errors: Writable<Record<keyof Inf, string>>;
54+
touched: Writable<Record<keyof Inf, boolean>>;
55+
modified: Readable<Record<keyof Inf, boolean>>;
56+
isValid: Readable<boolean>;
57+
isSubmitting: Writable<boolean>;
58+
isValidating: Writable<boolean>;
59+
isModified: Readable<boolean>;
60+
updateField: (field: keyof Inf, value: any) => void;
61+
updateValidateField: (field: keyof Inf, value: any) => void;
62+
updateTouched: (field: keyof Inf, value: any) => void;
63+
validateField: (field: keyof Inf) => Promise<any>;
64+
updateInitialValues: (newValues: Inf) => void;
65+
handleReset: () => void;
66+
state: Readable<{
67+
form: Inf;
68+
errors: Record<keyof Inf, string>;
69+
touched: Record<keyof Inf, boolean>;
70+
modified: Record<keyof Inf, boolean>;
71+
isValid: boolean;
72+
isSubmitting: boolean;
73+
isValidating: boolean;
74+
isModified: boolean;
75+
}>;
76+
handleChange: () => void;
77+
handleSubmit: () => any;
78+
};
79+
80+
declare class Form extends SvelteComponentTyped<
81+
FormProps & {
82+
class?: string;
83+
},
84+
{},
85+
{default: any}
86+
> {}
87+
88+
declare class Field extends SvelteComponentTyped<FieldProps, {}, {}> {}
89+
90+
declare class Textarea extends SvelteComponentTyped<TextareaProps, {}, {}> {}
91+
92+
declare class Select extends SvelteComponentTyped<
93+
SelectProps,
94+
{},
95+
{default: any}
96+
> {}
97+
98+
declare class ErrorMessage extends SvelteComponentTyped<
99+
ErrorProps,
100+
{},
101+
{default: any}
102+
> {}
103+
104+
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)