@entur/fileupload 0.7.1 → 0.8.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/FileUpload.d.ts +7 -5
- package/dist/fileupload.cjs +60 -19
- package/dist/fileupload.cjs.map +1 -1
- package/dist/fileupload.mjs +61 -20
- package/dist/fileupload.mjs.map +1 -1
- package/package.json +12 -10
- package/dist/index.test.d.ts +0 -1
package/dist/FileUpload.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DropEvent, DropzoneOptions, FileRejection } from 'react-dropzone';
|
|
2
|
-
type FileUploadProps = DropzoneOptions & {
|
|
1
|
+
import { Accept, DropEvent, DropzoneOptions, FileRejection } from 'react-dropzone';
|
|
2
|
+
type FileUploadProps = Omit<DropzoneOptions, 'accept'> & {
|
|
3
3
|
/** Tekst som vises ved fullført opplasting
|
|
4
4
|
* @default "Opplasting fullført"
|
|
5
5
|
*/
|
|
@@ -20,8 +20,10 @@ type FileUploadProps = DropzoneOptions & {
|
|
|
20
20
|
onDrop?<T extends File>(acceptedFiles: T[], fileRejections: FileRejection[], event: DropEvent): void;
|
|
21
21
|
/** Callback for når en fil slettes fra lista */
|
|
22
22
|
onDelete?: (file: File) => void;
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/** Hvilke filtyper som skal aksepteres, som MIME-typer og/eller filendelser.
|
|
24
|
+
* F.eks. `".csv"`, `['.json', 'application/json']` eller `{ 'application/json': ['.json'] }`
|
|
25
|
+
*/
|
|
26
|
+
accept?: string | string[] | Accept;
|
|
25
27
|
/** Filene som er aktive i komponenten */
|
|
26
28
|
files: File[];
|
|
27
29
|
/** Beskrivende tekst som forklarer feltet */
|
|
@@ -38,5 +40,5 @@ type FileUploadProps = DropzoneOptions & {
|
|
|
38
40
|
removeFileButtonDescription?: string;
|
|
39
41
|
[key: string]: any;
|
|
40
42
|
};
|
|
41
|
-
export declare const FileUpload: ({ standbyText, errorText, successText, errorUpload, onDrop, onDelete, accept, files, label, removeFileButtonDescription, style, ...rest }: FileUploadProps) => import("react/jsx-runtime").JSX.Element;
|
|
43
|
+
export declare const FileUpload: ({ standbyText, errorText, successText, errorUpload, onDrop, onDelete, accept, files, label, removeFileButtonDescription, style, validator, ...rest }: FileUploadProps) => import("react/jsx-runtime").JSX.Element;
|
|
42
44
|
export {};
|
package/dist/fileupload.cjs
CHANGED
|
@@ -3,10 +3,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const utils = require("@entur/utils");
|
|
4
4
|
const jsxRuntime = require("react/jsx-runtime");
|
|
5
5
|
const reactDropzone = require("react-dropzone");
|
|
6
|
+
const _attrAccept = require("attr-accept");
|
|
6
7
|
const classNames = require("classnames");
|
|
7
8
|
const icons = require("@entur/icons");
|
|
8
9
|
const button = require("@entur/button");
|
|
9
10
|
const typography = require("@entur/typography");
|
|
11
|
+
const attrAccept = typeof _attrAccept === "function" ? _attrAccept : _attrAccept.default;
|
|
10
12
|
const FileUpload = ({
|
|
11
13
|
standbyText = "Dra fil eller klikk for å laste opp",
|
|
12
14
|
errorText = "Feil ved opplasting av fil",
|
|
@@ -19,34 +21,55 @@ const FileUpload = ({
|
|
|
19
21
|
label,
|
|
20
22
|
removeFileButtonDescription = "Fjern fil",
|
|
21
23
|
style,
|
|
24
|
+
validator,
|
|
22
25
|
...rest
|
|
23
26
|
}) => {
|
|
27
|
+
const acceptList = toAcceptList(accept);
|
|
28
|
+
const validateFile = (file) => {
|
|
29
|
+
const errors = [];
|
|
30
|
+
if (file.type !== "application/x-moz-file" && !attrAccept(file, acceptList)) {
|
|
31
|
+
errors.push(getInvalidTypeError(accept, acceptList));
|
|
32
|
+
}
|
|
33
|
+
const custom = validator?.(file);
|
|
34
|
+
if (custom) errors.push(...Array.isArray(custom) ? custom : [custom]);
|
|
35
|
+
return errors.length > 0 ? errors : null;
|
|
36
|
+
};
|
|
24
37
|
const { getRootProps, getInputProps, isDragActive, isDragReject } = reactDropzone.useDropzone({
|
|
25
38
|
onDrop,
|
|
26
|
-
|
|
39
|
+
validator: validateFile,
|
|
27
40
|
...rest
|
|
28
41
|
});
|
|
29
42
|
const success = files.length > 0;
|
|
30
43
|
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "eds-file-upload__wrapper", ...style, children: [
|
|
31
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
45
|
+
"div",
|
|
46
|
+
{
|
|
47
|
+
className: "eds-file-upload__input",
|
|
48
|
+
...getRootProps({ role: "button" }),
|
|
49
|
+
children: [
|
|
50
|
+
label && /* @__PURE__ */ jsxRuntime.jsx(typography.Label, { style: { display: "flex" }, children: label }),
|
|
51
|
+
/* @__PURE__ */ jsxRuntime.jsx("input", { ...getInputProps({ accept: acceptList.join(",") }) }),
|
|
52
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
53
|
+
"span",
|
|
39
54
|
{
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
className: classNames(
|
|
56
|
+
"eds-file-upload__dropzone",
|
|
57
|
+
{
|
|
58
|
+
"eds-file-upload__dropzone--success": success
|
|
59
|
+
},
|
|
60
|
+
{ "eds-file-upload__dropzone--active": isDragActive },
|
|
61
|
+
// react-dropzone 14 keeps isDragReject after drop; only show it while dragging
|
|
62
|
+
{
|
|
63
|
+
"eds-file-upload__dropzone--reject": isDragActive && isDragReject
|
|
64
|
+
},
|
|
65
|
+
{ "eds-file-upload__dropzone--error": errorUpload }
|
|
66
|
+
),
|
|
67
|
+
children: success ? successText : errorUpload ? errorText : standbyText
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
),
|
|
50
73
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "eds-file-upload__file-list", children: files.map((file, index) => /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "eds-file-upload__file-name", children: [
|
|
51
74
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
52
75
|
icons.FileIcon,
|
|
@@ -73,6 +96,24 @@ const FileUpload = ({
|
|
|
73
96
|
] }, index)) })
|
|
74
97
|
] });
|
|
75
98
|
};
|
|
99
|
+
function toAcceptList(accept) {
|
|
100
|
+
if (!accept) return [];
|
|
101
|
+
if (typeof accept === "string") return accept.split(",");
|
|
102
|
+
if (Array.isArray(accept)) return accept;
|
|
103
|
+
return Object.entries(accept).flatMap(([mimeType, exts]) => [
|
|
104
|
+
mimeType,
|
|
105
|
+
...exts
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
function getInvalidTypeError(accept, acceptList) {
|
|
109
|
+
let suffix = `one of ${acceptList.join(", ")}`;
|
|
110
|
+
if (typeof accept === "string") suffix = accept;
|
|
111
|
+
else if (acceptList.length === 1) suffix = acceptList[0];
|
|
112
|
+
return {
|
|
113
|
+
code: reactDropzone.ErrorCode.FileInvalidType,
|
|
114
|
+
message: `File type must be ${suffix}`
|
|
115
|
+
};
|
|
116
|
+
}
|
|
76
117
|
function convertSizeToHuman(size) {
|
|
77
118
|
if (size < 1e3) {
|
|
78
119
|
return size + "bytes;";
|
package/dist/fileupload.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileupload.cjs","sources":["../src/FileUpload.tsx","../src/index.tsx"],"sourcesContent":["import {\n DropEvent,\n DropzoneOptions,\n FileRejection,\n useDropzone,\n} from 'react-dropzone';\nimport classNames from 'classnames';\n\nimport { DeleteIcon, FileIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport { Label } from '@entur/typography';\n\nimport './FileUpload.scss';\n\ntype FileUploadProps = DropzoneOptions & {\n /** Tekst som vises ved fullført opplasting\n * @default \"Opplasting fullført\"\n */\n successText?: string;\n /** Tekst som vises om opplasting feilet\n * @default \"Feil ved opplasting av fil\"\n */\n errorText?: string;\n /** Tekst som vises før man laster opp noe\n * @default \"Dra fil eller klikk for å laste opp\"\n */\n standbyText?: string;\n /** Boolean for hvis opplastingen feiler.\n * @default false\n */\n errorUpload?: boolean;\n /** Callback for når en fil legges til */\n onDrop?<T extends File>(\n acceptedFiles: T[],\n fileRejections: FileRejection[],\n event: DropEvent,\n ): void;\n /** Callback for når en fil slettes fra lista */\n onDelete?: (file: File) => void;\n /**
|
|
1
|
+
{"version":3,"file":"fileupload.cjs","sources":["../src/FileUpload.tsx","../src/index.tsx"],"sourcesContent":["import {\n Accept,\n DropEvent,\n DropzoneOptions,\n ErrorCode,\n FileError,\n FileRejection,\n useDropzone,\n} from 'react-dropzone';\nimport _attrAccept from 'attr-accept';\nimport classNames from 'classnames';\n\nimport { DeleteIcon, FileIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport { Label } from '@entur/typography';\n\nimport './FileUpload.scss';\n\n// CJS/ESM interop, same as react-dropzone does internally\nconst attrAccept: (file: File, accept: string | string[]) => boolean =\n typeof _attrAccept === 'function'\n ? _attrAccept\n : (_attrAccept as { default: typeof _attrAccept }).default;\n\ntype FileUploadProps = Omit<DropzoneOptions, 'accept'> & {\n /** Tekst som vises ved fullført opplasting\n * @default \"Opplasting fullført\"\n */\n successText?: string;\n /** Tekst som vises om opplasting feilet\n * @default \"Feil ved opplasting av fil\"\n */\n errorText?: string;\n /** Tekst som vises før man laster opp noe\n * @default \"Dra fil eller klikk for å laste opp\"\n */\n standbyText?: string;\n /** Boolean for hvis opplastingen feiler.\n * @default false\n */\n errorUpload?: boolean;\n /** Callback for når en fil legges til */\n onDrop?<T extends File>(\n acceptedFiles: T[],\n fileRejections: FileRejection[],\n event: DropEvent,\n ): void;\n /** Callback for når en fil slettes fra lista */\n onDelete?: (file: File) => void;\n /** Hvilke filtyper som skal aksepteres, som MIME-typer og/eller filendelser.\n * F.eks. `\".csv\"`, `['.json', 'application/json']` eller `{ 'application/json': ['.json'] }`\n */\n accept?: string | string[] | Accept;\n /** Filene som er aktive i komponenten */\n files: File[];\n /** Beskrivende tekst som forklarer feltet */\n label?: string;\n /** Mulighet for å laste opp flere filer */\n multiple?: boolean;\n /**Minste filstørrelse */\n minSize?: number;\n /**Største filstørrelse */\n maxSize?: number;\n /**Tekst som leses opp av skjermleser på søppelbøtte-ikonet\n * @default \"Fjern fil\"\n */\n removeFileButtonDescription?: string;\n [key: string]: any;\n};\n\nexport const FileUpload = ({\n standbyText = 'Dra fil eller klikk for å laste opp',\n errorText = 'Feil ved opplasting av fil',\n successText = 'Opplasting fullført',\n errorUpload = false,\n onDrop,\n onDelete = file => console.log(file),\n accept = '',\n files = [],\n label,\n removeFileButtonDescription = 'Fjern fil',\n style,\n validator,\n ...rest\n}: FileUploadProps) => {\n // Validate file type ourselves: react-dropzone's own `accept` warns on extension-only values\n const acceptList = toAcceptList(accept);\n const validateFile = (file: File): FileError | FileError[] | null => {\n const errors: FileError[] = [];\n if (\n file.type !== 'application/x-moz-file' &&\n !attrAccept(file, acceptList)\n ) {\n errors.push(getInvalidTypeError(accept, acceptList));\n }\n const custom = validator?.(file);\n if (custom) errors.push(...(Array.isArray(custom) ? custom : [custom]));\n return errors.length > 0 ? errors : null;\n };\n\n const { getRootProps, getInputProps, isDragActive, isDragReject } =\n useDropzone({\n onDrop,\n validator: validateFile,\n ...rest,\n });\n\n const success = files.length > 0;\n\n return (\n <div className=\"eds-file-upload__wrapper\" {...style}>\n <div\n className=\"eds-file-upload__input\"\n {...getRootProps({ role: 'button' })}\n >\n {label && <Label style={{ display: 'flex' }}>{label}</Label>}\n <input {...getInputProps({ accept: acceptList.join(',') })} />\n <span\n className={classNames(\n 'eds-file-upload__dropzone',\n {\n 'eds-file-upload__dropzone--success': success,\n },\n { 'eds-file-upload__dropzone--active': isDragActive },\n // react-dropzone 14 keeps isDragReject after drop; only show it while dragging\n {\n 'eds-file-upload__dropzone--reject': isDragActive && isDragReject,\n },\n { 'eds-file-upload__dropzone--error': errorUpload },\n )}\n >\n {success ? successText : errorUpload ? errorText : standbyText}\n </span>\n </div>\n <div className=\"eds-file-upload__file-list\">\n {files.map((file, index) => (\n <div className=\"eds-file-upload__file-name\" key={index}>\n <FileIcon\n aria-label=\"File icon\"\n className=\"eds-file-upload__file-name-icon\"\n />\n <span className=\"eds-field-upload__file-name-path\">\n {file.name} - {convertSizeToHuman(file.size)}{' '}\n </span>\n <IconButton\n onClick={() => onDelete(file)}\n type=\"button\"\n aria-label={`${removeFileButtonDescription}, ${file.name}`}\n >\n <DeleteIcon />\n </IconButton>\n </div>\n ))}\n </div>\n </div>\n );\n};\n\nfunction toAcceptList(accept: FileUploadProps['accept']): string[] {\n if (!accept) return [];\n if (typeof accept === 'string') return accept.split(',');\n if (Array.isArray(accept)) return accept;\n return Object.entries(accept).flatMap(([mimeType, exts]) => [\n mimeType,\n ...exts,\n ]);\n}\n\n// Same code and message as react-dropzone 11\nfunction getInvalidTypeError(\n accept: FileUploadProps['accept'],\n acceptList: string[],\n): FileError {\n let suffix = `one of ${acceptList.join(', ')}`;\n if (typeof accept === 'string') suffix = accept;\n else if (acceptList.length === 1) suffix = acceptList[0];\n return {\n code: ErrorCode.FileInvalidType,\n message: `File type must be ${suffix}`,\n };\n}\n\nfunction convertSizeToHuman(size: number) {\n if (size < 1000) {\n return size + 'bytes;';\n } else if (size < 1000000) {\n return `${size / 1000} KB`;\n } else {\n return `${(size / 1000000).toPrecision(4)} MB`;\n }\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('fileupload');\n\nexport * from './FileUpload';\n"],"names":["useDropzone","jsxs","Label","jsx","FileIcon","IconButton","DeleteIcon","ErrorCode","warnAboutMissingStyles"],"mappings":";;;;;;;;;;AAmBA,MAAM,aACJ,OAAO,gBAAgB,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QAAQ,CAAC,CAAC,UAAU,IAAI,MAAM;AAAA,IAC1D;AAAA,IACA,GAAG;AAAA,EAAA,CACJ;AACH;AAGA,SAAS,oBACP,QACA,YACW;AACX,MAAI,SAAS,UAAU,WAAW,KAAK,IAAI,CAAC;AAC5C,MAAI,OAAO,WAAW,SAAU,UAAS;AAAA,WAChC,WAAW,WAAW,EAAG,UAAS,WAAW,CAAC;AACvD,SAAO;AAAA,IACL,MAAMC,cAAAA,UAAU;AAAA,IAChB,SAAS,qBAAqB,MAAM;AAAA,EAAA;AAExC;AAEA,SAAS,mBAAmB,MAAc;AACxC,MAAI,OAAO,KAAM;AACf,WAAO,OAAO;AAAA,EAChB,WAAW,OAAO,KAAS;AACzB,WAAO,GAAG,OAAO,GAAI;AAAA,EACvB,OAAO;AACL,WAAO,IAAI,OAAO,KAAS,YAAY,CAAC,CAAC;AAAA,EAC3C;AACF;AC3LAC,MAAAA,uBAAuB,YAAY;;"}
|
package/dist/fileupload.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { warnAboutMissingStyles } from "@entur/utils";
|
|
2
2
|
import { jsxs, jsx } from "react/jsx-runtime";
|
|
3
|
-
import { useDropzone } from "react-dropzone";
|
|
3
|
+
import { useDropzone, ErrorCode } from "react-dropzone";
|
|
4
|
+
import _attrAccept from "attr-accept";
|
|
4
5
|
import classNames from "classnames";
|
|
5
6
|
import { FileIcon, DeleteIcon } from "@entur/icons";
|
|
6
7
|
import { IconButton } from "@entur/button";
|
|
7
8
|
import { Label } from "@entur/typography";
|
|
9
|
+
const attrAccept = typeof _attrAccept === "function" ? _attrAccept : _attrAccept.default;
|
|
8
10
|
const FileUpload = ({
|
|
9
11
|
standbyText = "Dra fil eller klikk for å laste opp",
|
|
10
12
|
errorText = "Feil ved opplasting av fil",
|
|
@@ -17,34 +19,55 @@ const FileUpload = ({
|
|
|
17
19
|
label,
|
|
18
20
|
removeFileButtonDescription = "Fjern fil",
|
|
19
21
|
style,
|
|
22
|
+
validator,
|
|
20
23
|
...rest
|
|
21
24
|
}) => {
|
|
25
|
+
const acceptList = toAcceptList(accept);
|
|
26
|
+
const validateFile = (file) => {
|
|
27
|
+
const errors = [];
|
|
28
|
+
if (file.type !== "application/x-moz-file" && !attrAccept(file, acceptList)) {
|
|
29
|
+
errors.push(getInvalidTypeError(accept, acceptList));
|
|
30
|
+
}
|
|
31
|
+
const custom = validator?.(file);
|
|
32
|
+
if (custom) errors.push(...Array.isArray(custom) ? custom : [custom]);
|
|
33
|
+
return errors.length > 0 ? errors : null;
|
|
34
|
+
};
|
|
22
35
|
const { getRootProps, getInputProps, isDragActive, isDragReject } = useDropzone({
|
|
23
36
|
onDrop,
|
|
24
|
-
|
|
37
|
+
validator: validateFile,
|
|
25
38
|
...rest
|
|
26
39
|
});
|
|
27
40
|
const success = files.length > 0;
|
|
28
41
|
return /* @__PURE__ */ jsxs("div", { className: "eds-file-upload__wrapper", ...style, children: [
|
|
29
|
-
/* @__PURE__ */ jsxs(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
/* @__PURE__ */ jsxs(
|
|
43
|
+
"div",
|
|
44
|
+
{
|
|
45
|
+
className: "eds-file-upload__input",
|
|
46
|
+
...getRootProps({ role: "button" }),
|
|
47
|
+
children: [
|
|
48
|
+
label && /* @__PURE__ */ jsx(Label, { style: { display: "flex" }, children: label }),
|
|
49
|
+
/* @__PURE__ */ jsx("input", { ...getInputProps({ accept: acceptList.join(",") }) }),
|
|
50
|
+
/* @__PURE__ */ jsx(
|
|
51
|
+
"span",
|
|
37
52
|
{
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
className: classNames(
|
|
54
|
+
"eds-file-upload__dropzone",
|
|
55
|
+
{
|
|
56
|
+
"eds-file-upload__dropzone--success": success
|
|
57
|
+
},
|
|
58
|
+
{ "eds-file-upload__dropzone--active": isDragActive },
|
|
59
|
+
// react-dropzone 14 keeps isDragReject after drop; only show it while dragging
|
|
60
|
+
{
|
|
61
|
+
"eds-file-upload__dropzone--reject": isDragActive && isDragReject
|
|
62
|
+
},
|
|
63
|
+
{ "eds-file-upload__dropzone--error": errorUpload }
|
|
64
|
+
),
|
|
65
|
+
children: success ? successText : errorUpload ? errorText : standbyText
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
),
|
|
48
71
|
/* @__PURE__ */ jsx("div", { className: "eds-file-upload__file-list", children: files.map((file, index) => /* @__PURE__ */ jsxs("div", { className: "eds-file-upload__file-name", children: [
|
|
49
72
|
/* @__PURE__ */ jsx(
|
|
50
73
|
FileIcon,
|
|
@@ -71,6 +94,24 @@ const FileUpload = ({
|
|
|
71
94
|
] }, index)) })
|
|
72
95
|
] });
|
|
73
96
|
};
|
|
97
|
+
function toAcceptList(accept) {
|
|
98
|
+
if (!accept) return [];
|
|
99
|
+
if (typeof accept === "string") return accept.split(",");
|
|
100
|
+
if (Array.isArray(accept)) return accept;
|
|
101
|
+
return Object.entries(accept).flatMap(([mimeType, exts]) => [
|
|
102
|
+
mimeType,
|
|
103
|
+
...exts
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
function getInvalidTypeError(accept, acceptList) {
|
|
107
|
+
let suffix = `one of ${acceptList.join(", ")}`;
|
|
108
|
+
if (typeof accept === "string") suffix = accept;
|
|
109
|
+
else if (acceptList.length === 1) suffix = acceptList[0];
|
|
110
|
+
return {
|
|
111
|
+
code: ErrorCode.FileInvalidType,
|
|
112
|
+
message: `File type must be ${suffix}`
|
|
113
|
+
};
|
|
114
|
+
}
|
|
74
115
|
function convertSizeToHuman(size) {
|
|
75
116
|
if (size < 1e3) {
|
|
76
117
|
return size + "bytes;";
|
package/dist/fileupload.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileupload.mjs","sources":["../src/FileUpload.tsx","../src/index.tsx"],"sourcesContent":["import {\n DropEvent,\n DropzoneOptions,\n FileRejection,\n useDropzone,\n} from 'react-dropzone';\nimport classNames from 'classnames';\n\nimport { DeleteIcon, FileIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport { Label } from '@entur/typography';\n\nimport './FileUpload.scss';\n\ntype FileUploadProps = DropzoneOptions & {\n /** Tekst som vises ved fullført opplasting\n * @default \"Opplasting fullført\"\n */\n successText?: string;\n /** Tekst som vises om opplasting feilet\n * @default \"Feil ved opplasting av fil\"\n */\n errorText?: string;\n /** Tekst som vises før man laster opp noe\n * @default \"Dra fil eller klikk for å laste opp\"\n */\n standbyText?: string;\n /** Boolean for hvis opplastingen feiler.\n * @default false\n */\n errorUpload?: boolean;\n /** Callback for når en fil legges til */\n onDrop?<T extends File>(\n acceptedFiles: T[],\n fileRejections: FileRejection[],\n event: DropEvent,\n ): void;\n /** Callback for når en fil slettes fra lista */\n onDelete?: (file: File) => void;\n /**
|
|
1
|
+
{"version":3,"file":"fileupload.mjs","sources":["../src/FileUpload.tsx","../src/index.tsx"],"sourcesContent":["import {\n Accept,\n DropEvent,\n DropzoneOptions,\n ErrorCode,\n FileError,\n FileRejection,\n useDropzone,\n} from 'react-dropzone';\nimport _attrAccept from 'attr-accept';\nimport classNames from 'classnames';\n\nimport { DeleteIcon, FileIcon } from '@entur/icons';\nimport { IconButton } from '@entur/button';\nimport { Label } from '@entur/typography';\n\nimport './FileUpload.scss';\n\n// CJS/ESM interop, same as react-dropzone does internally\nconst attrAccept: (file: File, accept: string | string[]) => boolean =\n typeof _attrAccept === 'function'\n ? _attrAccept\n : (_attrAccept as { default: typeof _attrAccept }).default;\n\ntype FileUploadProps = Omit<DropzoneOptions, 'accept'> & {\n /** Tekst som vises ved fullført opplasting\n * @default \"Opplasting fullført\"\n */\n successText?: string;\n /** Tekst som vises om opplasting feilet\n * @default \"Feil ved opplasting av fil\"\n */\n errorText?: string;\n /** Tekst som vises før man laster opp noe\n * @default \"Dra fil eller klikk for å laste opp\"\n */\n standbyText?: string;\n /** Boolean for hvis opplastingen feiler.\n * @default false\n */\n errorUpload?: boolean;\n /** Callback for når en fil legges til */\n onDrop?<T extends File>(\n acceptedFiles: T[],\n fileRejections: FileRejection[],\n event: DropEvent,\n ): void;\n /** Callback for når en fil slettes fra lista */\n onDelete?: (file: File) => void;\n /** Hvilke filtyper som skal aksepteres, som MIME-typer og/eller filendelser.\n * F.eks. `\".csv\"`, `['.json', 'application/json']` eller `{ 'application/json': ['.json'] }`\n */\n accept?: string | string[] | Accept;\n /** Filene som er aktive i komponenten */\n files: File[];\n /** Beskrivende tekst som forklarer feltet */\n label?: string;\n /** Mulighet for å laste opp flere filer */\n multiple?: boolean;\n /**Minste filstørrelse */\n minSize?: number;\n /**Største filstørrelse */\n maxSize?: number;\n /**Tekst som leses opp av skjermleser på søppelbøtte-ikonet\n * @default \"Fjern fil\"\n */\n removeFileButtonDescription?: string;\n [key: string]: any;\n};\n\nexport const FileUpload = ({\n standbyText = 'Dra fil eller klikk for å laste opp',\n errorText = 'Feil ved opplasting av fil',\n successText = 'Opplasting fullført',\n errorUpload = false,\n onDrop,\n onDelete = file => console.log(file),\n accept = '',\n files = [],\n label,\n removeFileButtonDescription = 'Fjern fil',\n style,\n validator,\n ...rest\n}: FileUploadProps) => {\n // Validate file type ourselves: react-dropzone's own `accept` warns on extension-only values\n const acceptList = toAcceptList(accept);\n const validateFile = (file: File): FileError | FileError[] | null => {\n const errors: FileError[] = [];\n if (\n file.type !== 'application/x-moz-file' &&\n !attrAccept(file, acceptList)\n ) {\n errors.push(getInvalidTypeError(accept, acceptList));\n }\n const custom = validator?.(file);\n if (custom) errors.push(...(Array.isArray(custom) ? custom : [custom]));\n return errors.length > 0 ? errors : null;\n };\n\n const { getRootProps, getInputProps, isDragActive, isDragReject } =\n useDropzone({\n onDrop,\n validator: validateFile,\n ...rest,\n });\n\n const success = files.length > 0;\n\n return (\n <div className=\"eds-file-upload__wrapper\" {...style}>\n <div\n className=\"eds-file-upload__input\"\n {...getRootProps({ role: 'button' })}\n >\n {label && <Label style={{ display: 'flex' }}>{label}</Label>}\n <input {...getInputProps({ accept: acceptList.join(',') })} />\n <span\n className={classNames(\n 'eds-file-upload__dropzone',\n {\n 'eds-file-upload__dropzone--success': success,\n },\n { 'eds-file-upload__dropzone--active': isDragActive },\n // react-dropzone 14 keeps isDragReject after drop; only show it while dragging\n {\n 'eds-file-upload__dropzone--reject': isDragActive && isDragReject,\n },\n { 'eds-file-upload__dropzone--error': errorUpload },\n )}\n >\n {success ? successText : errorUpload ? errorText : standbyText}\n </span>\n </div>\n <div className=\"eds-file-upload__file-list\">\n {files.map((file, index) => (\n <div className=\"eds-file-upload__file-name\" key={index}>\n <FileIcon\n aria-label=\"File icon\"\n className=\"eds-file-upload__file-name-icon\"\n />\n <span className=\"eds-field-upload__file-name-path\">\n {file.name} - {convertSizeToHuman(file.size)}{' '}\n </span>\n <IconButton\n onClick={() => onDelete(file)}\n type=\"button\"\n aria-label={`${removeFileButtonDescription}, ${file.name}`}\n >\n <DeleteIcon />\n </IconButton>\n </div>\n ))}\n </div>\n </div>\n );\n};\n\nfunction toAcceptList(accept: FileUploadProps['accept']): string[] {\n if (!accept) return [];\n if (typeof accept === 'string') return accept.split(',');\n if (Array.isArray(accept)) return accept;\n return Object.entries(accept).flatMap(([mimeType, exts]) => [\n mimeType,\n ...exts,\n ]);\n}\n\n// Same code and message as react-dropzone 11\nfunction getInvalidTypeError(\n accept: FileUploadProps['accept'],\n acceptList: string[],\n): FileError {\n let suffix = `one of ${acceptList.join(', ')}`;\n if (typeof accept === 'string') suffix = accept;\n else if (acceptList.length === 1) suffix = acceptList[0];\n return {\n code: ErrorCode.FileInvalidType,\n message: `File type must be ${suffix}`,\n };\n}\n\nfunction convertSizeToHuman(size: number) {\n if (size < 1000) {\n return size + 'bytes;';\n } else if (size < 1000000) {\n return `${size / 1000} KB`;\n } else {\n return `${(size / 1000000).toPrecision(4)} MB`;\n }\n}\n","import { warnAboutMissingStyles } from '@entur/utils';\nimport './index.scss';\n\nwarnAboutMissingStyles('fileupload');\n\nexport * from './FileUpload';\n"],"names":[],"mappings":";;;;;;;;AAmBA,MAAM,aACJ,OAAO,gBAAgB,aACnB,cACC,YAAgD;AAgDhD,MAAM,aAAa,CAAC;AAAA,EACzB,cAAc;AAAA,EACd,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,cAAc;AAAA,EACd;AAAA,EACA,WAAW,CAAA,SAAQ,QAAQ,IAAI,IAAI;AAAA,EACnC,SAAS;AAAA,EACT,QAAQ,CAAA;AAAA,EACR;AAAA,EACA,8BAA8B;AAAA,EAC9B;AAAA,EACA;AAAA,EACA,GAAG;AACL,MAAuB;AAErB,QAAM,aAAa,aAAa,MAAM;AACtC,QAAM,eAAe,CAAC,SAA+C;AACnE,UAAM,SAAsB,CAAA;AAC5B,QACE,KAAK,SAAS,4BACd,CAAC,WAAW,MAAM,UAAU,GAC5B;AACA,aAAO,KAAK,oBAAoB,QAAQ,UAAU,CAAC;AAAA,IACrD;AACA,UAAM,SAAS,YAAY,IAAI;AAC/B,QAAI,OAAQ,QAAO,KAAK,GAAI,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,MAAM,CAAE;AACtE,WAAO,OAAO,SAAS,IAAI,SAAS;AAAA,EACtC;AAEA,QAAM,EAAE,cAAc,eAAe,cAAc,aAAA,IACjD,YAAY;AAAA,IACV;AAAA,IACA,WAAW;AAAA,IACX,GAAG;AAAA,EAAA,CACJ;AAEH,QAAM,UAAU,MAAM,SAAS;AAE/B,SACE,qBAAC,OAAA,EAAI,WAAU,4BAA4B,GAAG,OAC5C,UAAA;AAAA,IAAA;AAAA,MAAC;AAAA,MAAA;AAAA,QACC,WAAU;AAAA,QACT,GAAG,aAAa,EAAE,MAAM,UAAU;AAAA,QAElC,UAAA;AAAA,UAAA,6BAAU,OAAA,EA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GAAG;AACvD,MAAI,MAAM,QAAQ,MAAM,EAAG,QAAO;AAClC,SAAO,OAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC,UAAU,IAAI,MAAM;AAAA,IAC1D;AAAA,IACA,GAAG;AAAA,EAAA,CACJ;AACH;AAGA,SAAS,oBACP,QACA,YACW;AACX,MAAI,SAAS,UAAU,WAAW,KAAK,IAAI,CAAC;AAC5C,MAAI,OAAO,WAAW,SAAU,UAAS;AAAA,WAChC,WAAW,WAAW,EAAG,UAAS,WAAW,CAAC;AACvD,SAAO;AAAA,IACL,MAAM,UAAU;AAAA,IAChB,SAAS,qBAAqB,MAAM;AAAA,EAAA;AAExC;AAEA,SAAS,mBAAmB,MAAc;AACxC,MAAI,OAAO,KAAM;AACf,WAAO,OAAO;AAAA,EAChB,WAAW,OAAO,KAAS;AACzB,WAAO,GAAG,OAAO,GAAI;AAAA,EACvB,OAAO;AACL,WAAO,IAAI,OAAO,KAAS,YAAY,CAAC,CAAC;AAAA,EAC3C;AACF;AC3LA,uBAAuB,YAAY;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entur/fileupload",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "EUPL-1.2",
|
|
5
5
|
"main": "dist/fileupload.cjs",
|
|
6
6
|
"module": "dist/fileupload.mjs",
|
|
@@ -19,21 +19,23 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"start": "vite build --watch",
|
|
21
21
|
"build": "vite build",
|
|
22
|
-
"test": "jest"
|
|
22
|
+
"test": "jest",
|
|
23
|
+
"typecheck": "tsc -p tsconfig.json --noEmit --rootDir ../.."
|
|
23
24
|
},
|
|
24
25
|
"peerDependencies": {
|
|
25
26
|
"react": ">=18.0.0",
|
|
26
27
|
"react-dom": ">=18.0.0"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@entur/a11y": "^0.4.
|
|
30
|
-
"@entur/button": "^5.2.
|
|
31
|
-
"@entur/icons": "^10.1.
|
|
32
|
-
"@entur/tokens": "^4.
|
|
33
|
-
"@entur/typography": "^3.2.
|
|
34
|
-
"@entur/utils": "^0.
|
|
30
|
+
"@entur/a11y": "^0.4.2",
|
|
31
|
+
"@entur/button": "^5.2.2",
|
|
32
|
+
"@entur/icons": "^10.1.2",
|
|
33
|
+
"@entur/tokens": "^4.4.0",
|
|
34
|
+
"@entur/typography": "^3.2.1",
|
|
35
|
+
"@entur/utils": "^0.16.0",
|
|
36
|
+
"attr-accept": "^2.2.5",
|
|
35
37
|
"classnames": "^2.5.1",
|
|
36
|
-
"react-dropzone": "^
|
|
38
|
+
"react-dropzone": "^14.4.1"
|
|
37
39
|
},
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"@testing-library/jest-dom": "^5.17.0",
|
|
@@ -60,5 +62,5 @@
|
|
|
60
62
|
"./dist/styles.no-base-tokens.css": "./dist/styles.no-base-tokens.css",
|
|
61
63
|
"./package.json": "./package.json"
|
|
62
64
|
},
|
|
63
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "6bd3e1062ff4ee94e96ff26b8f961dd4e57c33e1"
|
|
64
66
|
}
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|