@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.
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ export { FieldArray, useFieldArray } from 'react-final-form-arrays';
2
+ export { FORM_ERROR } from 'final-form';
3
+ export { Field, FormSpy, useField, useForm, useFormState } from 'react-final-form';
4
+ export { ErrorProvider, useErrors } from './providers/ErrorContext/index.js';
5
+ export { CheckboxField } from './components/CheckboxField/index.js';
6
+ export { DateField } from './components/DateField/index.js';
7
+ export { Form } from './components/Form/index.js';
8
+ export { RadioField } from './components/RadioField/index.js';
9
+ export { SelectableCardField } from './components/SelectableCardField/index.js';
10
+ export { SelectInputField } from './components/SelectInputField/index.js';
11
+ export { NumberInputField } from './components/NumberInputField/index.js';
12
+ export { Submit } from './components/Submit/index.js';
13
+ export { SubmitErrorAlert } from './components/SubmitErrorAlert/index.js';
14
+ export { TagInputField } from './components/TagInputField/index.js';
15
+ export { TextInputField } from './components/TextInputField/index.js';
16
+ export { TimeField } from './components/TimeField/index.js';
17
+ export { ToggleField } from './components/ToggleField/index.js';
18
+ export { useValidation } from './hooks/useValidation.js';
19
+ export { useOnFieldChange } from './hooks/useOnFieldChange.js';
20
+ export { pickValidators } from './helpers/pickValidators.js';
@@ -0,0 +1,74 @@
1
+ import { useCallback, useMemo, useContext, createContext } from 'react';
2
+ import { useFormState } from 'react-final-form';
3
+ import { jsx } from '@emotion/react/jsx-runtime';
4
+
5
+ const ErrorContext = /*#__PURE__*/createContext(undefined);
6
+ const ErrorProvider = _ref => {
7
+ let {
8
+ children,
9
+ errors
10
+ } = _ref;
11
+ const {
12
+ values
13
+ } = useFormState();
14
+ const getFirstError = useCallback(_ref2 => {
15
+ let {
16
+ label,
17
+ name,
18
+ value,
19
+ meta,
20
+ ...additionalParams
21
+ } = _ref2;
22
+ if (meta?.error && Array.isArray(meta.error)) {
23
+ return meta.error.map(untypedKey => {
24
+ const key = untypedKey;
25
+ if (typeof errors[key] === 'function') {
26
+ return errors[key]({
27
+ label,
28
+ meta,
29
+ name,
30
+ value,
31
+ values,
32
+ ...additionalParams
33
+ });
34
+ }
35
+ return errors[key];
36
+ }).filter(errorString => !!errorString)[0] ?? '';
37
+ }
38
+ // With a custom validate function the user may return a string directly
39
+ if (meta?.error && typeof meta?.error === 'string') return meta?.error;
40
+ return '';
41
+ }, [errors, values]);
42
+ const getError = useCallback(_ref3 => {
43
+ let {
44
+ meta,
45
+ errorProp,
46
+ value,
47
+ ...props
48
+ } = _ref3;
49
+ if (errorProp) return errorProp;
50
+ const hasInitialValueAndNotTouched = value !== undefined && value !== null && value !== '' && value !== false && meta?.dirty === false;
51
+ return meta?.error && (hasInitialValueAndNotTouched || meta.touched) ? getFirstError({
52
+ meta,
53
+ value,
54
+ ...props
55
+ }) : undefined;
56
+ }, [getFirstError]);
57
+ const value = useMemo(() => ({
58
+ errors,
59
+ getError
60
+ }), [errors, getError]);
61
+ return jsx(ErrorContext.Provider, {
62
+ value: value,
63
+ children: children
64
+ });
65
+ };
66
+ const useErrors = () => {
67
+ const context = useContext(ErrorContext);
68
+ if (context === undefined) {
69
+ throw new Error('useErrors must be used within an ErrorProvider ');
70
+ }
71
+ return context;
72
+ };
73
+
74
+ export { ErrorProvider, useErrors };
@@ -0,0 +1,21 @@
1
+ import { maxValidator } from './max.js';
2
+ import { maxDateValidator } from './maxDate.js';
3
+ import { maxLengthValidator } from './maxLength.js';
4
+ import { minValidator } from './min.js';
5
+ import { minDateValidator } from './minDate.js';
6
+ import { minLengthValidator } from './minLength.js';
7
+ import { regexValidator } from './regex.js';
8
+ import { requiredValidator } from './required.js';
9
+
10
+ var validators = {
11
+ max: maxValidator,
12
+ maxDate: maxDateValidator,
13
+ maxLength: maxLengthValidator,
14
+ min: minValidator,
15
+ minDate: minDateValidator,
16
+ minLength: minLengthValidator,
17
+ regex: regexValidator,
18
+ required: requiredValidator
19
+ };
20
+
21
+ export { validators as default };
@@ -0,0 +1,6 @@
1
+ const maxValidator = max => ({
2
+ error: 'TOO_HIGH',
3
+ validate: value => value === undefined || Number(value) <= max
4
+ });
5
+
6
+ export { maxValidator };
@@ -0,0 +1,6 @@
1
+ const maxDateValidator = maxDate => ({
2
+ error: 'MAX_DATE',
3
+ validate: value => value === undefined || value <= maxDate
4
+ });
5
+
6
+ export { maxDateValidator };
@@ -0,0 +1,6 @@
1
+ const maxLengthValidator = maxLength => ({
2
+ error: 'MAX_LENGTH',
3
+ validate: value => value === undefined || value.length <= maxLength
4
+ });
5
+
6
+ export { maxLengthValidator };
@@ -0,0 +1,6 @@
1
+ const minValidator = min => ({
2
+ error: 'TOO_LOW',
3
+ validate: value => value === undefined || Number(value) >= min
4
+ });
5
+
6
+ export { minValidator };
@@ -0,0 +1,6 @@
1
+ const minDateValidator = minDate => ({
2
+ error: 'MIN_DATE',
3
+ validate: value => value === undefined || value >= minDate
4
+ });
5
+
6
+ export { minDateValidator };
@@ -0,0 +1,6 @@
1
+ const minLengthValidator = minLength => ({
2
+ error: 'MIN_LENGTH',
3
+ validate: value => value === undefined || value.length >= minLength
4
+ });
5
+
6
+ export { minLengthValidator };
@@ -0,0 +1,6 @@
1
+ const regexValidator = regexes => ({
2
+ error: 'REGEX',
3
+ validate: value => regexes.every(regex => value === undefined || value === '' || (Array.isArray(regex) ? regex.some(regexOr => regexOr.test(value)) : regex.test(value)))
4
+ });
5
+
6
+ export { regexValidator };
@@ -0,0 +1,6 @@
1
+ const requiredValidator = required => ({
2
+ error: 'REQUIRED',
3
+ validate: value => required ? !!value : true
4
+ });
5
+
6
+ export { requiredValidator };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@ultraviolet/form",
3
+ "version": "1.0.0",
4
+ "description": "Ultraviolet Form",
5
+ "homepage": "https://github.com/scaleway/ultraviolet#readme",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/scaleway/ultraviolet",
9
+ "directory": "packages/form"
10
+ },
11
+ "keywords": [
12
+ "react",
13
+ "reactjs",
14
+ "ui"
15
+ ],
16
+ "license": "Apache-2.0",
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "engines": {
21
+ "node": ">=18.x",
22
+ "pnpm": ">=8.0.0"
23
+ },
24
+ "os": [
25
+ "darwin",
26
+ "linux"
27
+ ],
28
+ "sideEffects": false,
29
+ "main": "dist/index.js",
30
+ "module": "dist/index.js",
31
+ "jsnext:main": "dist/index.js",
32
+ "types": "dist/index.d.ts",
33
+ "peerDependencies": {
34
+ "@emotion/react": "11.11.1",
35
+ "@emotion/styled": "11.11.0",
36
+ "react": "18.x",
37
+ "react-dom": "18.x"
38
+ },
39
+ "devDependencies": {
40
+ "@babel/core": "7.22.5",
41
+ "@types/final-form-focus": "1.1.3",
42
+ "@types/react": "18.2.8",
43
+ "@types/react-dom": "18.2.4",
44
+ "react": "18.2.0",
45
+ "react-dom": "18.2.0"
46
+ },
47
+ "dependencies": {
48
+ "@babel/runtime": "7.22.5",
49
+ "@emotion/babel-plugin": "11.11.0",
50
+ "@emotion/react": "11.11.1",
51
+ "@emotion/styled": "11.11.0",
52
+ "final-form": "4.20.9",
53
+ "final-form-arrays": "3.0.2",
54
+ "final-form-focus": "1.1.2",
55
+ "react-final-form": "6.5.9",
56
+ "react-final-form-arrays": "3.1.4",
57
+ "react-select": "5.7.3",
58
+ "@ultraviolet/ui": "1.0.0"
59
+ }
60
+ }