@arkyn/components 1.3.142 → 1.3.143

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. package/dist/bundle.js +21241 -9924
  2. package/dist/bundle.umd.cjs +342 -67
  3. package/dist/components/ImageUpload/HasFileContent/index.d.ts +5 -0
  4. package/dist/components/ImageUpload/HasFileContent/index.d.ts.map +1 -0
  5. package/dist/components/ImageUpload/HasFileContent/index.js +20 -0
  6. package/dist/components/ImageUpload/ImageUploadError/index.d.ts +5 -0
  7. package/dist/components/ImageUpload/ImageUploadError/index.d.ts.map +1 -0
  8. package/dist/components/ImageUpload/ImageUploadError/index.js +10 -0
  9. package/dist/components/ImageUpload/ImageUploadLabel/index.d.ts +5 -0
  10. package/dist/components/ImageUpload/ImageUploadLabel/index.d.ts.map +1 -0
  11. package/dist/components/ImageUpload/ImageUploadLabel/index.js +9 -0
  12. package/dist/components/ImageUpload/NoFileContent/index.d.ts +5 -0
  13. package/dist/components/ImageUpload/NoFileContent/index.d.ts.map +1 -0
  14. package/dist/components/ImageUpload/NoFileContent/index.js +25 -0
  15. package/dist/components/ImageUpload/index.d.ts +5 -0
  16. package/dist/components/ImageUpload/index.d.ts.map +1 -0
  17. package/dist/components/ImageUpload/index.js +48 -0
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -0
  21. package/dist/style.css +1 -1
  22. package/package.json +1 -1
  23. package/src/components/ImageUpload/HasFileContent/index.tsx +64 -0
  24. package/src/components/ImageUpload/HasFileContent/styles.css +17 -0
  25. package/src/components/ImageUpload/ImageUploadError/index.tsx +12 -0
  26. package/src/components/ImageUpload/ImageUploadError/styles.css +6 -0
  27. package/src/components/ImageUpload/ImageUploadLabel/index.tsx +13 -0
  28. package/src/components/ImageUpload/ImageUploadLabel/styles.css +15 -0
  29. package/src/components/ImageUpload/NoFileContent/index.tsx +53 -0
  30. package/src/components/ImageUpload/NoFileContent/styles.css +19 -0
  31. package/src/components/ImageUpload/index.tsx +106 -0
  32. package/src/components/ImageUpload/styles.css +29 -0
  33. package/src/index.ts +1 -0
@@ -0,0 +1,106 @@
1
+ import { useFieldErrors } from "@arkyn/components";
2
+ import { ImageUploadProps } from "@arkyn/types";
3
+ import { useState } from "react";
4
+
5
+ import { HasFileContent } from "./HasFileContent";
6
+ import { ImageUploadError } from "./ImageUploadError";
7
+ import { ImageUploadLabel } from "./ImageUploadLabel";
8
+ import { NoFileContent } from "./NoFileContent";
9
+
10
+ import "./styles.css";
11
+
12
+ function ImageUpload(props: ImageUploadProps) {
13
+ const {
14
+ name,
15
+ defaultValue = "",
16
+ label,
17
+ showAsterisk = false,
18
+ action,
19
+ fileName = "file",
20
+ method = "POST",
21
+ acceptImage = "image/*",
22
+ fileResponseName = "url",
23
+ changeImageButtonText = "Alterar imagem",
24
+ selectImageButtonText = "Selecionar imagem",
25
+ dropImageText = "Ou arraste e solte a imagem aqui",
26
+ onUpload,
27
+ } = props;
28
+
29
+ const fieldErrors = useFieldErrors();
30
+ const fieldError = fieldErrors[name];
31
+
32
+ const [value, setValue] = useState(defaultValue);
33
+ const [error, setError] = useState("");
34
+ const [file, setFile] = useState<File | null>(null);
35
+ const [filePath, setFilePath] = useState(defaultValue);
36
+ const [isLoading, setIsLoading] = useState(false);
37
+
38
+ async function handleUploadImage(file: File) {
39
+ setIsLoading(true);
40
+ setFile(file);
41
+
42
+ const formData = new FormData();
43
+ formData.append(fileName, file);
44
+
45
+ await fetch(action, { method: method, body: formData })
46
+ .then(async (response) => await response.json())
47
+ .then((response) => {
48
+ if (!!response?.error) setError(response.error);
49
+ else setValue(response?.[fileResponseName]);
50
+ onUpload && onUpload(response?.[fileResponseName]);
51
+ })
52
+ .catch((error) => {
53
+ console.error(error);
54
+ setError("Erro ao enviar imagem");
55
+ })
56
+ .finally(() => setIsLoading(false));
57
+ }
58
+
59
+ function handleSelectFile(file: File) {
60
+ setFilePath(URL.createObjectURL(file));
61
+ handleUploadImage(file);
62
+ }
63
+
64
+ const errorMessage = fieldError || error;
65
+
66
+ const hasErrorClassName = errorMessage ? "hasError" : "noHasError";
67
+ const hasImageClassName = filePath ? "hasImage" : "noHasImage";
68
+ const className = `arkynImageUpload ${hasErrorClassName} ${hasImageClassName}`;
69
+
70
+ return (
71
+ <div className="arkynImageUploadContainer">
72
+ {label && <ImageUploadLabel label={label} showAsterisk={showAsterisk} />}
73
+
74
+ <div className={className}>
75
+ <input type="hidden" name={name} value={value || ""} />
76
+
77
+ {!filePath && (
78
+ <NoFileContent
79
+ isLoading={isLoading}
80
+ acceptImage={acceptImage}
81
+ dropImageText={dropImageText}
82
+ handleSelectFile={handleSelectFile}
83
+ selectImageButtonText={selectImageButtonText}
84
+ />
85
+ )}
86
+
87
+ {filePath && (
88
+ <HasFileContent
89
+ isLoading={isLoading}
90
+ acceptImage={acceptImage}
91
+ filePath={filePath}
92
+ handleSelectFile={handleSelectFile}
93
+ changeImageButtonText={changeImageButtonText}
94
+ reSendImage={
95
+ !!errorMessage && file ? () => handleUploadImage(file) : undefined
96
+ }
97
+ />
98
+ )}
99
+ </div>
100
+
101
+ {errorMessage && <ImageUploadError error={errorMessage} />}
102
+ </div>
103
+ );
104
+ }
105
+
106
+ export { ImageUpload };
@@ -0,0 +1,29 @@
1
+ .arkynImageUploadContainer {
2
+ display: flex;
3
+ flex-direction: column;
4
+ gap: 6px;
5
+ }
6
+
7
+ .arkynImageUpload {
8
+ border-width: 2px;
9
+ border-color: var(--border);
10
+ border-radius: var(--rounded-inputs);
11
+
12
+ width: 100%;
13
+ height: 174px;
14
+ max-width: 475px;
15
+
16
+ background: rgba(var(--input-background), 1);
17
+ }
18
+
19
+ .arkynImageUpload.hasImage {
20
+ border-style: solid;
21
+ }
22
+
23
+ .arkynImageUpload.noHasImage {
24
+ border-style: dashed;
25
+ }
26
+
27
+ .arkynImageUpload.hasError {
28
+ border-color: rgb(var(--spotlight-danger));
29
+ }
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export { Checkbox } from "./components/Checkbox";
25
25
  export { FormController, FormError, FormLabel } from "./components/Form";
26
26
  export { GoogleSearchPlaces } from "./components/GoogleSearchPlaces";
27
27
  export { IconButton } from "./components/IconButton";
28
+ export { ImageUpload } from "./components/ImageUpload";
28
29
  export { Input } from "./components/Input";
29
30
  export { RadioBox, RadioGroup } from "./components/Radio";
30
31
  export { RichText } from "./components/RichText";