@weser/forms 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +22 -0
- package/dist/defaultFormatErrorMessage.d.ts +2 -0
- package/dist/defaultFormatErrorMessage.js +3 -0
- package/dist/defaultParseValue.d.ts +2 -0
- package/dist/defaultParseValue.js +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/dist/useClickAway.d.ts +2 -0
- package/dist/useClickAway.js +19 -0
- package/dist/useClientOnly.d.ts +1 -0
- package/dist/useClientOnly.js +6 -0
- package/dist/useDisclosure.d.ts +16 -0
- package/dist/useDisclosure.js +25 -0
- package/dist/useField.d.ts +62 -0
- package/dist/useField.js +102 -0
- package/dist/useFocusTrap.d.ts +7 -0
- package/dist/useFocusTrap.js +77 -0
- package/dist/useForm.d.ts +70 -0
- package/dist/useForm.js +96 -0
- package/dist/useKeyDown.d.ts +7 -0
- package/dist/useKeyDown.js +22 -0
- package/dist/useRouteChange.d.ts +1 -0
- package/dist/useRouteChange.js +19 -0
- package/dist/useScrollBlocking.d.ts +1 -0
- package/dist/useScrollBlocking.js +28 -0
- package/dist/useTrigger.d.ts +7 -0
- package/dist/useTrigger.js +24 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present Robin Weser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @weser/hooks
|
|
2
|
+
|
|
3
|
+
<img alt="npm version" src="https://badge.fury.io/js/@weser%2Fhooks.svg"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/@weser/hooks.svg"> <a href="https://bundlephobia.com/result?p=@weser/hooks@latest"><img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@weser/hooks.svg"></a>
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
# npm
|
|
9
|
+
npm i --save @weser/hooks
|
|
10
|
+
# yarn
|
|
11
|
+
yarn add @weser/hooks
|
|
12
|
+
# pnpm
|
|
13
|
+
pnpm add @weser/hooks
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Documentation
|
|
17
|
+
|
|
18
|
+
## License
|
|
19
|
+
|
|
20
|
+
@weser/hooks is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
|
|
21
|
+
Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
|
|
22
|
+
Created with ♥ by [@robinweser](http://weser.io).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useForm } from './useForm.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useForm } from './useForm.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ZodIssue } from 'zod';
|
|
2
|
+
export type Field<T> = {
|
|
3
|
+
value: T;
|
|
4
|
+
disabled: boolean;
|
|
5
|
+
touched: boolean;
|
|
6
|
+
dirty: boolean;
|
|
7
|
+
valid: boolean;
|
|
8
|
+
errorMessage?: string;
|
|
9
|
+
};
|
|
10
|
+
export type Options<T> = {
|
|
11
|
+
name?: string;
|
|
12
|
+
value?: T;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
touched?: boolean;
|
|
15
|
+
showValidationOn?: 'submit' | 'blur' | 'change';
|
|
16
|
+
parseValue?: (e: any) => T;
|
|
17
|
+
formatErrorMessage?: (error: ZodIssue, name?: string) => string;
|
|
18
|
+
_onUpdateValue?: (value: T, dirty: boolean) => void;
|
|
19
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
export default function useClickAway(ref, callback, active = true) {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
const onClickAway = (e) => {
|
|
5
|
+
if (active && ref.current) {
|
|
6
|
+
const isClickOnInner = ref.current.contains(e.target);
|
|
7
|
+
if (!isClickOnInner) {
|
|
8
|
+
setTimeout(callback, 0);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
document.addEventListener('mousedown', onClickAway);
|
|
13
|
+
document.addEventListener('touchstart', onClickAway);
|
|
14
|
+
return () => {
|
|
15
|
+
document.removeEventListener('mousedown', onClickAway);
|
|
16
|
+
document.removeEventListener('touchstart', onClickAway);
|
|
17
|
+
};
|
|
18
|
+
}, [ref, active, callback]);
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useClientSide(): boolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default function useDisclosure(defaultExpanded?: boolean): {
|
|
2
|
+
toggleProps: {
|
|
3
|
+
id: string;
|
|
4
|
+
onClick: void;
|
|
5
|
+
type: string;
|
|
6
|
+
'aria-expanded': boolean;
|
|
7
|
+
'aria-controls': string;
|
|
8
|
+
};
|
|
9
|
+
contentProps: {
|
|
10
|
+
id: string;
|
|
11
|
+
'aria-hidden': boolean;
|
|
12
|
+
'aria-labelledby': string;
|
|
13
|
+
};
|
|
14
|
+
isExpanded: boolean;
|
|
15
|
+
toggle: void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useState, useEffect, useId } from 'react';
|
|
2
|
+
export default function useDisclosure(defaultExpanded = false) {
|
|
3
|
+
const [isExpanded, setExpanded] = useState(defaultExpanded);
|
|
4
|
+
const id = useId();
|
|
5
|
+
useEffect(() => setExpanded(defaultExpanded), [defaultExpanded]);
|
|
6
|
+
const toggle = setExpanded((isExpanded) => !isExpanded);
|
|
7
|
+
const toggleProps = {
|
|
8
|
+
id: id + '-toggle',
|
|
9
|
+
onClick: toggle,
|
|
10
|
+
type: 'button',
|
|
11
|
+
'aria-expanded': isExpanded,
|
|
12
|
+
'aria-controls': id + '-content',
|
|
13
|
+
};
|
|
14
|
+
const contentProps = {
|
|
15
|
+
id: id + '-content',
|
|
16
|
+
'aria-hidden': !isExpanded,
|
|
17
|
+
'aria-labelledby': id + '-toggle',
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
toggleProps,
|
|
21
|
+
contentProps,
|
|
22
|
+
isExpanded,
|
|
23
|
+
toggle,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ChangeEvent } from 'react';
|
|
2
|
+
import { ZodSchema } from 'zod';
|
|
3
|
+
import { Field, Options } from './types.js';
|
|
4
|
+
export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodSchema, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onUpdateValue, }?: Options<T>): {
|
|
5
|
+
required: boolean;
|
|
6
|
+
valid: boolean;
|
|
7
|
+
update: (data: Partial<Field<T>>) => void;
|
|
8
|
+
reset: () => void;
|
|
9
|
+
errorMessage: string | undefined;
|
|
10
|
+
inputProps: {
|
|
11
|
+
onFocus: () => void;
|
|
12
|
+
onBlur: () => void;
|
|
13
|
+
value: T;
|
|
14
|
+
disabled: boolean;
|
|
15
|
+
required: boolean;
|
|
16
|
+
name: string | undefined;
|
|
17
|
+
'data-valid': boolean;
|
|
18
|
+
onChange: (e: C) => void;
|
|
19
|
+
} | {
|
|
20
|
+
onFocus: () => void;
|
|
21
|
+
onBlur?: undefined;
|
|
22
|
+
value: T;
|
|
23
|
+
disabled: boolean;
|
|
24
|
+
required: boolean;
|
|
25
|
+
name: string | undefined;
|
|
26
|
+
'data-valid': boolean;
|
|
27
|
+
onChange: (e: C) => void;
|
|
28
|
+
};
|
|
29
|
+
initial: {
|
|
30
|
+
value: T;
|
|
31
|
+
disabled: boolean;
|
|
32
|
+
touched: boolean;
|
|
33
|
+
dirty: boolean;
|
|
34
|
+
valid: boolean;
|
|
35
|
+
errorMessage: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
props: {
|
|
38
|
+
onFocus: () => void;
|
|
39
|
+
onBlur: () => void;
|
|
40
|
+
value: T;
|
|
41
|
+
disabled: boolean;
|
|
42
|
+
name: string | undefined;
|
|
43
|
+
valid: boolean;
|
|
44
|
+
required: boolean;
|
|
45
|
+
errorMessage: string | undefined;
|
|
46
|
+
onChange: (e: C) => void;
|
|
47
|
+
} | {
|
|
48
|
+
onFocus: () => void;
|
|
49
|
+
onBlur?: undefined;
|
|
50
|
+
value: T;
|
|
51
|
+
disabled: boolean;
|
|
52
|
+
name: string | undefined;
|
|
53
|
+
valid: boolean;
|
|
54
|
+
required: boolean;
|
|
55
|
+
errorMessage: string | undefined;
|
|
56
|
+
onChange: (e: C) => void;
|
|
57
|
+
};
|
|
58
|
+
value: T;
|
|
59
|
+
disabled: boolean;
|
|
60
|
+
touched: boolean;
|
|
61
|
+
dirty: boolean;
|
|
62
|
+
};
|
package/dist/useField.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
|
|
3
|
+
import defaultParseValue from './defaultParseValue.js';
|
|
4
|
+
export default function useField(schema, { name, value = '', disabled = false, touched = false, showValidationOn = 'submit', parseValue = (defaultParseValue), formatErrorMessage = defaultFormatErrorMessage, _onUpdateValue, } = {}) {
|
|
5
|
+
const isOptional = schema.isOptional();
|
|
6
|
+
function validate(value) {
|
|
7
|
+
const res = schema.safeParse(value);
|
|
8
|
+
if (res.success) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return formatErrorMessage(res.error.errors[0], name);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const message = validate(value);
|
|
16
|
+
const initialField = {
|
|
17
|
+
value,
|
|
18
|
+
disabled,
|
|
19
|
+
touched,
|
|
20
|
+
dirty: false,
|
|
21
|
+
valid: !message,
|
|
22
|
+
errorMessage: message,
|
|
23
|
+
};
|
|
24
|
+
const [field, setField] = useState(initialField);
|
|
25
|
+
function update(data) {
|
|
26
|
+
if (data.value !== undefined) {
|
|
27
|
+
const dirty = data.value !== initialField.value;
|
|
28
|
+
const errorMessage = validate(data.value);
|
|
29
|
+
if (_onUpdateValue) {
|
|
30
|
+
_onUpdateValue(data.value, dirty);
|
|
31
|
+
}
|
|
32
|
+
setField((field) => ({
|
|
33
|
+
...field,
|
|
34
|
+
touched: showValidationOn === 'change' ? dirty : field.touched,
|
|
35
|
+
dirty,
|
|
36
|
+
...data,
|
|
37
|
+
errorMessage,
|
|
38
|
+
valid: !errorMessage,
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
setField((field) => ({
|
|
43
|
+
...field,
|
|
44
|
+
...data,
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function reset() {
|
|
49
|
+
setField(initialField);
|
|
50
|
+
}
|
|
51
|
+
function onChange(e) {
|
|
52
|
+
update({ value: parseValue(e) });
|
|
53
|
+
}
|
|
54
|
+
const required = !isOptional;
|
|
55
|
+
// Only show validation error when is touched
|
|
56
|
+
const valid = !field.touched ? true : !field.errorMessage;
|
|
57
|
+
// Only show errrorMessage and validation styles if the field is touched according to the config
|
|
58
|
+
const errorMessage = field.touched ? field.errorMessage : undefined;
|
|
59
|
+
const touch = () => update({ touched: false });
|
|
60
|
+
const untouch = () => update({ touched: false });
|
|
61
|
+
function getListeners() {
|
|
62
|
+
if (showValidationOn === 'blur') {
|
|
63
|
+
return {
|
|
64
|
+
onFocus: touch,
|
|
65
|
+
onBlur: untouch,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
onFocus: touch,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const inputProps = {
|
|
73
|
+
value: field.value,
|
|
74
|
+
disabled: field.disabled,
|
|
75
|
+
required,
|
|
76
|
+
name,
|
|
77
|
+
'data-valid': valid,
|
|
78
|
+
onChange,
|
|
79
|
+
...getListeners(),
|
|
80
|
+
};
|
|
81
|
+
const props = {
|
|
82
|
+
value: field.value,
|
|
83
|
+
disabled: field.disabled,
|
|
84
|
+
name,
|
|
85
|
+
valid,
|
|
86
|
+
required,
|
|
87
|
+
errorMessage,
|
|
88
|
+
onChange,
|
|
89
|
+
...getListeners(),
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
...field,
|
|
93
|
+
required,
|
|
94
|
+
valid,
|
|
95
|
+
update,
|
|
96
|
+
reset,
|
|
97
|
+
errorMessage,
|
|
98
|
+
inputProps,
|
|
99
|
+
initial: initialField,
|
|
100
|
+
props,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import useKeyDown from './useKeyDown.js';
|
|
3
|
+
const focusableSelector = `:is(
|
|
4
|
+
a[href],
|
|
5
|
+
area[href],
|
|
6
|
+
input:not([disabled]),
|
|
7
|
+
select:not([disabled]),
|
|
8
|
+
textarea:not([disabled]),
|
|
9
|
+
button:not([disabled]),
|
|
10
|
+
[tabindex]:not([tabindex="-1"]),
|
|
11
|
+
[contenteditable]
|
|
12
|
+
)`;
|
|
13
|
+
export default function useFocusTrap(ref, active, config = {}) {
|
|
14
|
+
const { visible, autoFocus = true } = config;
|
|
15
|
+
useKeyDown('Tab', (e) => {
|
|
16
|
+
const element = ref.current;
|
|
17
|
+
if (!active || !element) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const focusables = [
|
|
21
|
+
...element.querySelectorAll(focusableSelector),
|
|
22
|
+
].filter((el) => el.hasAttribute('tabindex')
|
|
23
|
+
? el.getAttribute('tabindex') !== '-1'
|
|
24
|
+
: true);
|
|
25
|
+
const firstFocusable = focusables[0];
|
|
26
|
+
const lastFocusable = focusables[focusables.length - 1];
|
|
27
|
+
const activeElement = document.activeElement;
|
|
28
|
+
let nextElement;
|
|
29
|
+
if (activeElement && focusables.includes(activeElement)) {
|
|
30
|
+
const index = focusables.indexOf(activeElement);
|
|
31
|
+
if (e.shiftKey) {
|
|
32
|
+
if (index === 0) {
|
|
33
|
+
nextElement = lastFocusable;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (index === focusables.length - 1) {
|
|
38
|
+
nextElement = firstFocusable;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (e.shiftKey) {
|
|
44
|
+
nextElement = lastFocusable;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
nextElement = firstFocusable;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (nextElement) {
|
|
51
|
+
;
|
|
52
|
+
nextElement.focus();
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
}
|
|
55
|
+
}, {
|
|
56
|
+
active,
|
|
57
|
+
});
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
const element = ref.current;
|
|
60
|
+
if (!autoFocus || !visible || !element) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const autoFocusElement = element.querySelector('[data-autofocus="true"]' + focusableSelector);
|
|
64
|
+
if (autoFocusElement) {
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
autoFocusElement.focus();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const nodeList = element.querySelectorAll(focusableSelector);
|
|
70
|
+
const elements = Array.from(nodeList);
|
|
71
|
+
const focusableElements = elements.filter((element) => !element.hasAttribute('tabindex') ||
|
|
72
|
+
element.getAttribute('tabindex') === '0');
|
|
73
|
+
// 1. focus the first focusable
|
|
74
|
+
// @ts-ignore, TODO: fix typing
|
|
75
|
+
focusableElements[0]?.focus();
|
|
76
|
+
}, [visible, ref, autoFocus]);
|
|
77
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { FormEvent, ChangeEvent } from 'react';
|
|
2
|
+
import { z, ZodObject, ZodError, ZodRawShape, ZodIssue } from 'zod';
|
|
3
|
+
import { Field, Options } from './types.js';
|
|
4
|
+
export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, formatErrorMessage?: (error: ZodIssue, name?: string) => string): {
|
|
5
|
+
useFormField: <T = string>(name: keyof S, options?: Omit<Options<T>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
|
|
6
|
+
reset: () => void;
|
|
7
|
+
required: boolean;
|
|
8
|
+
valid: boolean;
|
|
9
|
+
update: (data: Partial<Field<T>>) => void;
|
|
10
|
+
errorMessage: string | undefined;
|
|
11
|
+
inputProps: {
|
|
12
|
+
onFocus: () => void;
|
|
13
|
+
onBlur: () => void;
|
|
14
|
+
value: T;
|
|
15
|
+
disabled: boolean;
|
|
16
|
+
required: boolean;
|
|
17
|
+
name: string | undefined;
|
|
18
|
+
'data-valid': boolean;
|
|
19
|
+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
20
|
+
} | {
|
|
21
|
+
onFocus: () => void;
|
|
22
|
+
onBlur?: undefined;
|
|
23
|
+
value: T;
|
|
24
|
+
disabled: boolean;
|
|
25
|
+
required: boolean;
|
|
26
|
+
name: string | undefined;
|
|
27
|
+
'data-valid': boolean;
|
|
28
|
+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
29
|
+
};
|
|
30
|
+
initial: {
|
|
31
|
+
value: T;
|
|
32
|
+
disabled: boolean;
|
|
33
|
+
touched: boolean;
|
|
34
|
+
dirty: boolean;
|
|
35
|
+
valid: boolean;
|
|
36
|
+
errorMessage: string | undefined;
|
|
37
|
+
};
|
|
38
|
+
props: {
|
|
39
|
+
onFocus: () => void;
|
|
40
|
+
onBlur: () => void;
|
|
41
|
+
value: T;
|
|
42
|
+
disabled: boolean;
|
|
43
|
+
name: string | undefined;
|
|
44
|
+
valid: boolean;
|
|
45
|
+
required: boolean;
|
|
46
|
+
errorMessage: string | undefined;
|
|
47
|
+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
48
|
+
} | {
|
|
49
|
+
onFocus: () => void;
|
|
50
|
+
onBlur?: undefined;
|
|
51
|
+
value: T;
|
|
52
|
+
disabled: boolean;
|
|
53
|
+
name: string | undefined;
|
|
54
|
+
valid: boolean;
|
|
55
|
+
required: boolean;
|
|
56
|
+
errorMessage: string | undefined;
|
|
57
|
+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
58
|
+
};
|
|
59
|
+
value: T;
|
|
60
|
+
disabled: boolean;
|
|
61
|
+
touched: boolean;
|
|
62
|
+
dirty: boolean;
|
|
63
|
+
};
|
|
64
|
+
handleSubmit: (onSubmit: (data: z.infer<typeof schema>) => void, onError?: (error: ZodError) => void) => (e: FormEvent<HTMLFormElement>) => void;
|
|
65
|
+
formProps: {
|
|
66
|
+
noValidate: boolean;
|
|
67
|
+
};
|
|
68
|
+
isDirty: () => boolean;
|
|
69
|
+
reset: () => void;
|
|
70
|
+
};
|
package/dist/useForm.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
|
|
3
|
+
import useField from './useField.js';
|
|
4
|
+
function mapFieldsToData(fields) {
|
|
5
|
+
const obj = {};
|
|
6
|
+
for (const name in fields) {
|
|
7
|
+
obj[name] = fields[name].ref.current.value;
|
|
8
|
+
}
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
// TODO: accept refined schemas, not possible due to https://github.com/colinhacks/zod/issues/2474
|
|
12
|
+
export default function useForm(schema, formatErrorMessage = defaultFormatErrorMessage) {
|
|
13
|
+
const [fields, setFields] = useState({});
|
|
14
|
+
function useFormField(name, options = {}) {
|
|
15
|
+
const shape = schema.shape[name];
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
const field = useField(shape, {
|
|
18
|
+
...options,
|
|
19
|
+
name: name,
|
|
20
|
+
formatErrorMessage,
|
|
21
|
+
_onUpdateValue: (value, dirty) => {
|
|
22
|
+
ref.current = {
|
|
23
|
+
value,
|
|
24
|
+
dirty,
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const ref = useRef({
|
|
29
|
+
value: field.value,
|
|
30
|
+
dirty: false,
|
|
31
|
+
});
|
|
32
|
+
function reset() {
|
|
33
|
+
ref.current = {
|
|
34
|
+
value: field.initial.value,
|
|
35
|
+
dirty: false,
|
|
36
|
+
};
|
|
37
|
+
field.reset();
|
|
38
|
+
}
|
|
39
|
+
useEffect(() => setFields((fields) => ({
|
|
40
|
+
...fields,
|
|
41
|
+
[name]: {
|
|
42
|
+
ref,
|
|
43
|
+
update: field.update,
|
|
44
|
+
reset,
|
|
45
|
+
},
|
|
46
|
+
})), []);
|
|
47
|
+
return {
|
|
48
|
+
...field,
|
|
49
|
+
reset,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function touchFields() {
|
|
53
|
+
for (const name in fields) {
|
|
54
|
+
fields[name].update({ touched: true });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function reset() {
|
|
58
|
+
for (const name in fields) {
|
|
59
|
+
fields[name].reset();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function isDirty() {
|
|
63
|
+
for (const name in fields) {
|
|
64
|
+
if (fields[name].ref.current.dirty) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function handleSubmit(onSubmit, onError) {
|
|
71
|
+
return (e) => {
|
|
72
|
+
e.preventDefault();
|
|
73
|
+
touchFields();
|
|
74
|
+
const data = mapFieldsToData(fields);
|
|
75
|
+
const parsed = schema.safeParse(data);
|
|
76
|
+
if (parsed.success) {
|
|
77
|
+
onSubmit(parsed.data);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (onError) {
|
|
81
|
+
onError(parsed.error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const formProps = {
|
|
87
|
+
noValidate: true,
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
useFormField,
|
|
91
|
+
handleSubmit,
|
|
92
|
+
formProps,
|
|
93
|
+
isDirty,
|
|
94
|
+
reset,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
export default function useKeyDown(keyCode, callback, options = {}) {
|
|
3
|
+
const { active = true, target } = options;
|
|
4
|
+
const keyCodes = [].concat(keyCode);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
if (!active || target === null || target?.current === null) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const hasRef = target && target.current;
|
|
10
|
+
const element = hasRef ? target.current : document;
|
|
11
|
+
if (element) {
|
|
12
|
+
const handleKeyDown = (e) => {
|
|
13
|
+
if (keyCodes.includes(e.code) &&
|
|
14
|
+
(!hasRef || (hasRef && document.activeElement === element))) {
|
|
15
|
+
callback(e);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
element.addEventListener('keydown', handleKeyDown);
|
|
19
|
+
return () => element.removeEventListener('keydown', handleKeyDown);
|
|
20
|
+
}
|
|
21
|
+
}, [target, callback, active, keyCode]);
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useRouteChange(onRouteChange: (path: string) => void, pathname?: string): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
export default function useRouteChange(onRouteChange, pathname) {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
if (pathname) {
|
|
5
|
+
onRouteChange(pathname);
|
|
6
|
+
}
|
|
7
|
+
}, [pathname]);
|
|
8
|
+
// track clicks on links with the current path
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const onClick = (e) => {
|
|
11
|
+
const target = e.target;
|
|
12
|
+
if (target.tagName === 'A' && target.href === pathname) {
|
|
13
|
+
onRouteChange(pathname);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
window.addEventListener('click', onClick);
|
|
17
|
+
return () => window.removeEventListener('click', onClick);
|
|
18
|
+
}, []);
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useScrollBlocking(active: boolean): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
let scrollTop;
|
|
3
|
+
function blockScrolling(scrollElement) {
|
|
4
|
+
scrollTop = window.scrollY;
|
|
5
|
+
scrollElement.style.overflow = 'hidden';
|
|
6
|
+
scrollElement.style.position = 'fixed';
|
|
7
|
+
scrollElement.style.width = '100%';
|
|
8
|
+
scrollElement.style.top = -scrollTop + 'px';
|
|
9
|
+
}
|
|
10
|
+
function enableScrolling(scrollElement) {
|
|
11
|
+
scrollElement.style.removeProperty('position');
|
|
12
|
+
scrollElement.style.removeProperty('overflow');
|
|
13
|
+
scrollElement.style.removeProperty('top');
|
|
14
|
+
scrollElement.style.removeProperty('width');
|
|
15
|
+
window.scrollTo(0, scrollTop);
|
|
16
|
+
}
|
|
17
|
+
function toggleScrolling(isBlocked) {
|
|
18
|
+
const scrollElement = document.scrollingElement;
|
|
19
|
+
if (isBlocked) {
|
|
20
|
+
blockScrolling(scrollElement);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
enableScrolling(scrollElement);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export default function useScrollBlocking(active) {
|
|
27
|
+
useEffect(() => toggleScrolling(active), [active]);
|
|
28
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
type Props = {
|
|
3
|
+
defaultVisible?: boolean;
|
|
4
|
+
getTrigger?: () => HTMLElement | null;
|
|
5
|
+
};
|
|
6
|
+
export default function useTrigger({ defaultVisible, getTrigger, }?: Props): [boolean, (visible: boolean) => void, RefObject<HTMLElement>];
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
export default function useTrigger({ defaultVisible = false, getTrigger, } = {}) {
|
|
3
|
+
const triggerRef = useRef(null);
|
|
4
|
+
const [isVisible, _setVisible] = useState(false);
|
|
5
|
+
function getTriggerElement() {
|
|
6
|
+
if (getTrigger) {
|
|
7
|
+
return getTrigger();
|
|
8
|
+
}
|
|
9
|
+
return triggerRef.current;
|
|
10
|
+
}
|
|
11
|
+
function setVisible(visible) {
|
|
12
|
+
if (isVisible !== visible) {
|
|
13
|
+
_setVisible(visible);
|
|
14
|
+
if (!visible) {
|
|
15
|
+
const trigger = getTriggerElement();
|
|
16
|
+
if (trigger) {
|
|
17
|
+
trigger.focus();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
useEffect(() => _setVisible(defaultVisible), [defaultVisible]);
|
|
23
|
+
return [isVisible, setVisible, triggerRef];
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@weser/forms",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "React hooks for forms with zod schemas",
|
|
5
|
+
"author": "Robin Weser <robin@weser.io>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/robinweser/weser.git",
|
|
8
|
+
"repository": "https://github.com/robinweser/weser.git",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"module": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"README.md",
|
|
20
|
+
"dist/**"
|
|
21
|
+
],
|
|
22
|
+
"browserslist": [
|
|
23
|
+
"IE >= 11",
|
|
24
|
+
"Firefox >= 60",
|
|
25
|
+
"Safari >= 11.1",
|
|
26
|
+
"Chrome >= 66",
|
|
27
|
+
"ChromeAndroid >= 66",
|
|
28
|
+
"iOS >= 11.3",
|
|
29
|
+
"Edge >= 15"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"setup": "pnpm build",
|
|
33
|
+
"clean": "rimraf dist",
|
|
34
|
+
"build": "tsc -b",
|
|
35
|
+
"dev": "pnpm build -w",
|
|
36
|
+
"test": "echo 1"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"react",
|
|
40
|
+
"react-hook",
|
|
41
|
+
"react hooks",
|
|
42
|
+
"hooks",
|
|
43
|
+
"forms",
|
|
44
|
+
"zod"
|
|
45
|
+
],
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react": ">16.3.0",
|
|
48
|
+
"zod": ">=3"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/react": "^18.3.3",
|
|
52
|
+
"ava": "^6.1.3",
|
|
53
|
+
"react": "canary",
|
|
54
|
+
"rimraf": "^3.0.2",
|
|
55
|
+
"typescript": "^5.4.5"
|
|
56
|
+
},
|
|
57
|
+
"gitHead": "de1f00e9f6100c1e0ccfdb8145af827e027c280a"
|
|
58
|
+
}
|