@autobusal/utilities 0.0.1

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.
Files changed (4) hide show
  1. package/form.tsx +92 -0
  2. package/index.ts +10 -0
  3. package/notify.ts +13 -0
  4. package/package.json +12 -0
package/form.tsx ADDED
@@ -0,0 +1,92 @@
1
+ import { TFunction } from 'i18next';
2
+ import { FieldError, Merge, FieldErrorsImpl } from 'react-hook-form';
3
+ import styled from 'styled-components';
4
+
5
+ interface ValidateType {
6
+ required?: string
7
+ pattern?: any
8
+ maxLength?: any
9
+ minLength?: any
10
+ min?: any
11
+ }
12
+
13
+ export const validate = (rules: string, t: TFunction<'common'>) => {
14
+ const analyze = rules.split('|');
15
+
16
+ let validation: ValidateType = {};
17
+
18
+ analyze.forEach(rule => {
19
+ const type = rule.split(':');
20
+
21
+ switch (type[0]) {
22
+ case 'required':
23
+ validation.required = t('errors.required', { ns: 'common' });
24
+ break;
25
+
26
+ case 'email':
27
+ validation.pattern = {
28
+ value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
29
+ message: t('errors.email', { ns: 'common' })
30
+ };
31
+ break;
32
+
33
+ case 'min_length':
34
+ validation.minLength = {
35
+ value: type[1],
36
+ message: t('errors.minLength', { ns: 'common', count: type[1] } as any)
37
+ };
38
+ break;
39
+
40
+ case 'max_length':
41
+ validation.maxLength = {
42
+ value: type[1],
43
+ message: t('errors.maxLength', { ns: 'common', count: type[1] } as any)
44
+ };
45
+ break;
46
+
47
+ case 'min':
48
+ validation.min = {
49
+ value: Number(type[1]),
50
+ message: t('errors.min', { ns: 'common', count: type[1] } as any)
51
+ };
52
+ break;
53
+
54
+ // case 'max':
55
+ // validation.max = {
56
+ // value: type[1],
57
+ // message: t('general:errors.max', { count: type[1] })
58
+ // };
59
+ // break;
60
+
61
+ // case 'confirm':
62
+ // validation.validate = value => {
63
+ // if (value == type[1]) {
64
+ // return true;
65
+ // } else {
66
+ // return t('general:errors.confirmed');
67
+ // }
68
+ // };
69
+ // break;
70
+ }
71
+ });
72
+
73
+ return validation;
74
+ };
75
+
76
+ const Error = styled.div`
77
+ margin-top: 4px;
78
+ color: ${ props => props.theme.font.error };
79
+ font-size: ${ props => props.theme.size.s };
80
+ `;
81
+
82
+ export const display = (err: (FieldError | Merge<FieldError, FieldErrorsImpl<any>> | undefined)): (JSX.Element | null) => {
83
+ if (!err || !err.message) {
84
+ return null;
85
+ }
86
+
87
+ return (
88
+ <Error>
89
+ { err.message as string }
90
+ </Error>
91
+ );
92
+ }
package/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { validate, display } from './form';
2
+ import { error, notify, success } from './notify';
3
+
4
+ export {
5
+ validate,
6
+ display,
7
+ error,
8
+ notify,
9
+ success
10
+ };
package/notify.ts ADDED
@@ -0,0 +1,13 @@
1
+ import toast from 'react-hot-toast';
2
+
3
+ export const error = (message: string): void => {
4
+ toast.error(message);
5
+ };
6
+
7
+ export const notify = (message: string): void => {
8
+ toast(message);
9
+ };
10
+
11
+ export const success = (message: string): void => {
12
+ toast.success(message);
13
+ };
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@autobusal/utilities",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "index.ts",
6
+ "dependencies": {
7
+ "i18next": "^23.7.18",
8
+ "react-hook-form": "^7.49.3",
9
+ "react-hot-toast": "^2.4.1",
10
+ "styled-components": "^6.1.8"
11
+ }
12
+ }