@ultraviolet/form 1.0.0

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.
@@ -0,0 +1,75 @@
1
+ import { useMemo } from 'react';
2
+ import { useField } from 'react-final-form';
3
+ import { useValidation } from './useValidation.js';
4
+ import { pickValidators } from '../helpers/pickValidators.js';
5
+
6
+ const useFormField = (name, _ref) => {
7
+ let {
8
+ afterSubmit,
9
+ disabled,
10
+ allowNull,
11
+ beforeSubmit,
12
+ defaultValue,
13
+ format,
14
+ formatOnBlur,
15
+ initialValue,
16
+ isEqual,
17
+ multiple,
18
+ parse,
19
+ subscription,
20
+ type,
21
+ validate,
22
+ validateFields,
23
+ value,
24
+ max,
25
+ maxLength,
26
+ min,
27
+ minLength,
28
+ regex,
29
+ required,
30
+ maxDate,
31
+ minDate
32
+ } = _ref;
33
+ const serializedRegex = useMemo(() => regex?.toString(), [regex]);
34
+ const validators = useMemo(() => pickValidators({
35
+ max,
36
+ maxDate,
37
+ maxLength,
38
+ min,
39
+ minDate,
40
+ minLength,
41
+ regex,
42
+ required
43
+ }),
44
+ // eslint-disable-next-line react-hooks/exhaustive-deps
45
+ [max, maxLength, min, minLength, required, serializedRegex, maxDate, minDate]);
46
+ const validateFn = useValidation({
47
+ validate,
48
+ validators
49
+ });
50
+
51
+ // eslint-disable-next-line react-hooks/exhaustive-deps
52
+ const data = useMemo(() => ({
53
+ key: Math.random()
54
+ }), [validateFn, disabled]);
55
+ return useField(name, {
56
+ afterSubmit,
57
+ allowNull,
58
+ beforeSubmit,
59
+ defaultValue,
60
+ format,
61
+ formatOnBlur,
62
+ initialValue,
63
+ isEqual,
64
+ multiple,
65
+ parse,
66
+ subscription,
67
+ type,
68
+ validate: disabled ? undefined : validateFn,
69
+ validateFields,
70
+ value,
71
+ data
72
+ });
73
+ };
74
+
75
+ export { useFormField };
@@ -0,0 +1,30 @@
1
+ import { useRef, useEffect } from 'react';
2
+ import { useFormState, useField } from 'react-final-form';
3
+
4
+ const useOnFieldChange = function (name, callback, enabled) {
5
+ if (enabled === void 0) {
6
+ enabled = true;
7
+ }
8
+ const {
9
+ values
10
+ } = useFormState();
11
+ const {
12
+ input: {
13
+ value
14
+ }
15
+ } = useField(name, {
16
+ allowNull: true,
17
+ subscription: {
18
+ value: true
19
+ }
20
+ });
21
+ const previousValues = useRef(value);
22
+ useEffect(() => {
23
+ if (previousValues.current !== value && enabled) {
24
+ previousValues.current = value;
25
+ callback(value, values);
26
+ }
27
+ }, [value, values, callback, enabled]);
28
+ };
29
+
30
+ export { useOnFieldChange };
@@ -0,0 +1,25 @@
1
+ import { useCallback } from 'react';
2
+
3
+ const useValidation = _ref => {
4
+ let {
5
+ validators,
6
+ validate
7
+ } = _ref;
8
+ return useCallback((value, allValues, meta) => {
9
+ if (validate) {
10
+ const validateErr = validate(value, allValues, meta);
11
+ if (validateErr !== undefined && validateErr !== true) {
12
+ return validateErr;
13
+ }
14
+ }
15
+ const errors = validators.filter(validator => !validator.validate(value, allValues, meta)).map(_ref2 => {
16
+ let {
17
+ error
18
+ } = _ref2;
19
+ return error;
20
+ });
21
+ return errors.length > 0 ? errors : undefined;
22
+ }, [validate, validators]);
23
+ };
24
+
25
+ export { useValidation };