@arkyn/components 1.3.133 → 1.3.135

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/Form/FormController/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAW/E,OAAO,cAAc,CAAC;AAItB,iBAAS,cAAc,CAAC,KAAK,EAAE,mBAAmB,2CA6CjD;AAED,iBAAS,iBAAiB,+BAEzB;AAED,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/Form/FormController/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAW/E,OAAO,cAAc,CAAC;AAKtB,iBAAS,cAAc,CAAC,KAAK,EAAE,mBAAmB,2CA4BjD;AAED,iBAAS,iBAAiB,+BAEzB;AAED,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,CAAC"}
@@ -1,24 +1,11 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useActionData, useFetchers } from "@remix-run/react";
3
- import { createContext, useContext, useEffect, useId, useRef, useState, } from "react";
2
+ import { createContext, useContext, useId, useRef, } from "react";
4
3
  import "./styles.css";
4
+ import { useFieldErrors } from "../../../hooks/useFieldErrors";
5
5
  const FormControllerContext = createContext({});
6
6
  function FormController(props) {
7
7
  const { children, className: baseClassName, id: formControllerId, ...rest } = props;
8
- const actionData = useActionData();
9
- const fetchers = useFetchers();
10
- const [fieldErrors, setFieldErrors] = useState({});
11
- useEffect(() => {
12
- let newFieldErrors = { ...actionData?.fieldErrors };
13
- fetchers.forEach((fetcher) => {
14
- if (fetcher.data?.fieldErrors) {
15
- newFieldErrors = { ...newFieldErrors, ...fetcher.data.fieldErrors };
16
- }
17
- });
18
- if (JSON.stringify(newFieldErrors) !== JSON.stringify(fieldErrors)) {
19
- setFieldErrors(newFieldErrors);
20
- }
21
- }, [fetchers, actionData, fieldErrors]);
8
+ const fieldErrors = useFieldErrors();
22
9
  const inputRef = useRef(null);
23
10
  const name = inputRef.current?.name || "";
24
11
  const error = fieldErrors?.[name] || null;
@@ -0,0 +1,5 @@
1
+ declare function useFieldErrors(): {
2
+ [x: string]: string;
3
+ };
4
+ export { useFieldErrors };
5
+ //# sourceMappingURL=useFieldErrors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFieldErrors.d.ts","sourceRoot":"","sources":["../../src/hooks/useFieldErrors.ts"],"names":[],"mappings":"AAGA,iBAAS,cAAc;;EA0CtB;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { useActionData, useFetchers, useNavigation } from "@remix-run/react";
2
+ import { useEffect, useState } from "react";
3
+ function useFieldErrors() {
4
+ const actionData = useActionData();
5
+ const navigation = useNavigation();
6
+ const fetchers = useFetchers();
7
+ const [fetcherFieldErrors, setFetcherFieldErrors] = useState(null);
8
+ function compareObjects(obj1, obj2) {
9
+ return JSON.stringify(obj1) === JSON.stringify(obj2);
10
+ }
11
+ function handleClearFields() {
12
+ if (fetcherFieldErrors)
13
+ setFetcherFieldErrors(null);
14
+ return;
15
+ }
16
+ // create field errors
17
+ useEffect(() => {
18
+ let newFieldErrors = fetchers[0]?.data?.fieldErrors || {};
19
+ if (!compareObjects(fetcherFieldErrors, newFieldErrors)) {
20
+ if (Object.entries(newFieldErrors).length !== 0) {
21
+ setFetcherFieldErrors(newFieldErrors);
22
+ }
23
+ }
24
+ }, [fetchers, actionData]);
25
+ // clear field errors
26
+ useEffect(() => {
27
+ if (fetchers[0]?.state === "submitting")
28
+ handleClearFields();
29
+ }, [fetchers, navigation]);
30
+ const responseFieldErrors = actionData?.fieldErrors || fetcherFieldErrors;
31
+ let mappedResponse = {};
32
+ Object.entries(responseFieldErrors || {}).forEach(([key, value]) => {
33
+ if (typeof value === "string" && typeof key === "string") {
34
+ mappedResponse[key] = value;
35
+ }
36
+ });
37
+ return mappedResponse;
38
+ }
39
+ export { useFieldErrors };
package/dist/index.d.ts CHANGED
@@ -26,6 +26,7 @@ export { Toast } from "./components/Toast";
26
26
  export { Tooltip } from "./components/Tooltip";
27
27
  export { useAutomation } from "./hooks/useAutomation";
28
28
  export { useDrawer } from "./hooks/useDrawer";
29
+ export { useFieldErrors } from "./hooks/useFieldErrors";
29
30
  export { useModal } from "./hooks/useModal";
30
31
  export { useScopedParams } from "./hooks/useScopedParams";
31
32
  export { useToast } from "./hooks/useToast";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,SAAS,EACT,UAAU,GACX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EACL,SAAS,EACT,YAAY,EACZ,cAAc,EACd,WAAW,EACX,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,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AAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC"}
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ export { Tooltip } from "./components/Tooltip";
31
31
  // Hooks
32
32
  export { useAutomation } from "./hooks/useAutomation";
33
33
  export { useDrawer } from "./hooks/useDrawer";
34
+ export { useFieldErrors } from "./hooks/useFieldErrors";
34
35
  export { useModal } from "./hooks/useModal";
35
36
  export { useScopedParams } from "./hooks/useScopedParams";
36
37
  export { useToast } from "./hooks/useToast";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/components",
3
- "version": "1.3.133",
3
+ "version": "1.3.135",
4
4
  "main": "./dist/bundle.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Lucas Gonçalves",
@@ -10,6 +10,7 @@ import {
10
10
  } from "react";
11
11
 
12
12
  import "./styles.css";
13
+ import { useFieldErrors } from "../../../hooks/useFieldErrors";
13
14
 
14
15
  const FormControllerContext = createContext({} as FormControllerContextProps);
15
16
 
@@ -21,24 +22,7 @@ function FormController(props: FormControllerProps) {
21
22
  ...rest
22
23
  } = props;
23
24
 
24
- const actionData = useActionData<any>();
25
- const fetchers = useFetchers();
26
-
27
- const [fieldErrors, setFieldErrors] = useState<{ [x: string]: string }>({});
28
-
29
- useEffect(() => {
30
- let newFieldErrors = { ...actionData?.fieldErrors };
31
- fetchers.forEach((fetcher) => {
32
- if (fetcher.data?.fieldErrors) {
33
- newFieldErrors = { ...newFieldErrors, ...fetcher.data.fieldErrors };
34
- }
35
- });
36
-
37
- if (JSON.stringify(newFieldErrors) !== JSON.stringify(fieldErrors)) {
38
- setFieldErrors(newFieldErrors);
39
- }
40
- }, [fetchers, actionData, fieldErrors]);
41
-
25
+ const fieldErrors = useFieldErrors();
42
26
  const inputRef = useRef<HTMLInputElement>(null);
43
27
 
44
28
  const name = inputRef.current?.name || "";
@@ -0,0 +1,48 @@
1
+ import { useActionData, useFetchers, useNavigation } from "@remix-run/react";
2
+ import { useEffect, useState } from "react";
3
+
4
+ function useFieldErrors() {
5
+ const actionData = useActionData<any>();
6
+ const navigation = useNavigation();
7
+ const fetchers = useFetchers();
8
+
9
+ const [fetcherFieldErrors, setFetcherFieldErrors] = useState<any>(null);
10
+
11
+ function compareObjects(obj1: any, obj2: any) {
12
+ return JSON.stringify(obj1) === JSON.stringify(obj2);
13
+ }
14
+
15
+ function handleClearFields() {
16
+ if (fetcherFieldErrors) setFetcherFieldErrors(null);
17
+ return;
18
+ }
19
+
20
+ // create field errors
21
+ useEffect(() => {
22
+ let newFieldErrors = fetchers[0]?.data?.fieldErrors || {};
23
+
24
+ if (!compareObjects(fetcherFieldErrors, newFieldErrors)) {
25
+ if (Object.entries(newFieldErrors).length !== 0) {
26
+ setFetcherFieldErrors(newFieldErrors);
27
+ }
28
+ }
29
+ }, [fetchers, actionData]);
30
+
31
+ // clear field errors
32
+ useEffect(() => {
33
+ if (fetchers[0]?.state === "submitting") handleClearFields();
34
+ }, [fetchers, navigation]);
35
+
36
+ const responseFieldErrors = actionData?.fieldErrors || fetcherFieldErrors;
37
+ let mappedResponse: { [x: string]: string | undefined | null } = {};
38
+
39
+ Object.entries(responseFieldErrors || {}).forEach(([key, value]) => {
40
+ if (typeof value === "string" && typeof key === "string") {
41
+ mappedResponse[key] = value;
42
+ }
43
+ });
44
+
45
+ return mappedResponse;
46
+ }
47
+
48
+ export { useFieldErrors };
package/src/index.ts CHANGED
@@ -47,6 +47,7 @@ export { Tooltip } from "./components/Tooltip";
47
47
  // Hooks
48
48
  export { useAutomation } from "./hooks/useAutomation";
49
49
  export { useDrawer } from "./hooks/useDrawer";
50
+ export { useFieldErrors } from "./hooks/useFieldErrors";
50
51
  export { useModal } from "./hooks/useModal";
51
52
  export { useScopedParams } from "./hooks/useScopedParams";
52
53
  export { useToast } from "./hooks/useToast";