tycho-components 0.12.0 → 0.12.2
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/AppDropzone/AppDropzone.d.ts +1 -1
- package/dist/AppDropzone/AppDropzone.js +5 -28
- package/dist/AppDropzone/AppDropzoneBody.d.ts +13 -5
- package/dist/AppDropzone/AppDropzoneBody.js +40 -5
- package/dist/AppDropzone/index.d.ts +2 -2
- package/dist/AppForm/AppForm.js +2 -2
- package/dist/AppForm/AppFormField.d.ts +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,36 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { useRef } from 'react';
|
|
3
3
|
import { useTranslation } from 'react-i18next';
|
|
4
4
|
import AppModal from '../AppModal';
|
|
5
|
-
import CommonContext from '../configs/CommonContext';
|
|
6
|
-
import { toastLoading } from '../configs/store/actions';
|
|
7
|
-
import { useMessageUtils } from '../configs/useMessageUtils';
|
|
8
5
|
import AppDropzoneBody from './AppDropzoneBody';
|
|
9
|
-
import UploadService from './UploadService';
|
|
10
6
|
export default function AppDropzone({ folder, onClose, onSuccess, onError, accept, onDrop, keepName, title, alternativeButton, }) {
|
|
11
7
|
const { t } = useTranslation('upload');
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const upload = async () => {
|
|
16
|
-
if (!file || state.toastLoading)
|
|
17
|
-
return;
|
|
18
|
-
dispatch(toastLoading(true));
|
|
19
|
-
const data = new FormData();
|
|
20
|
-
data.append('file', file);
|
|
21
|
-
data.append('folder', folder);
|
|
22
|
-
keepName && data.append('keepOriginalName', 'true');
|
|
23
|
-
UploadService.execute(data)
|
|
24
|
-
.then((r) => {
|
|
25
|
-
onSuccess(r.data);
|
|
26
|
-
})
|
|
27
|
-
.catch((err) => {
|
|
28
|
-
dispatchError({ err: 'error.uploading.image', t, key: 'upload' });
|
|
29
|
-
onError && onError(err);
|
|
30
|
-
})
|
|
31
|
-
.finally(() => {
|
|
32
|
-
dispatch(toastLoading(false));
|
|
33
|
-
});
|
|
8
|
+
const bodyRef = useRef(null);
|
|
9
|
+
const handleConfirm = () => {
|
|
10
|
+
bodyRef.current?.upload();
|
|
34
11
|
};
|
|
35
|
-
return (_jsx(AppModal, { title: title || t('modal.title'), className: "modal-upload", close: onClose, confirm:
|
|
12
|
+
return (_jsx(AppModal, { title: title || t('modal.title'), className: "modal-upload", close: onClose, confirm: handleConfirm, alternativeButton: alternativeButton, children: _jsx(AppDropzoneBody, { ref: bodyRef, folder: folder, onSuccess: onSuccess, onError: onError, accept: accept, onDrop: onDrop, keepName: keepName, showConfirmButton: false }) }));
|
|
36
13
|
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
import { UploadedFile } from './UploadedFile';
|
|
1
2
|
import './style.scss';
|
|
2
|
-
type
|
|
3
|
+
export type AppDropzoneBodyProps = {
|
|
4
|
+
folder: string;
|
|
5
|
+
onSuccess: (response: UploadedFile) => void;
|
|
6
|
+
onError?: (err: any) => void;
|
|
3
7
|
accept: Record<string, string[]>;
|
|
4
|
-
file?: File;
|
|
5
|
-
onFileChange: (file: File | undefined) => void;
|
|
6
8
|
onDrop?: () => void;
|
|
9
|
+
keepName?: boolean;
|
|
10
|
+
/** When false, the confirm button is hidden (e.g. when used inside modal with its own confirm) */
|
|
11
|
+
showConfirmButton?: boolean;
|
|
7
12
|
};
|
|
8
|
-
export
|
|
9
|
-
|
|
13
|
+
export type AppDropzoneBodyRef = {
|
|
14
|
+
upload: () => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
declare const AppDropzoneBody: import("react").ForwardRefExoticComponent<AppDropzoneBodyProps & import("react").RefAttributes<AppDropzoneBodyRef>>;
|
|
17
|
+
export default AppDropzoneBody;
|
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect, useState } from 'react';
|
|
2
|
+
import { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState, } from 'react';
|
|
3
3
|
import Dropzone from 'react-dropzone';
|
|
4
4
|
import { useTranslation } from 'react-i18next';
|
|
5
|
+
import { Button } from 'tycho-storybook';
|
|
6
|
+
import CommonContext from '../configs/CommonContext';
|
|
7
|
+
import { toastLoading } from '../configs/store/actions';
|
|
8
|
+
import { useMessageUtils } from '../configs/useMessageUtils';
|
|
9
|
+
import UploadService from './UploadService';
|
|
5
10
|
import './style.scss';
|
|
6
|
-
|
|
11
|
+
function AppDropzoneBodyInner({ folder, onSuccess, onError, accept, onDrop, keepName, showConfirmButton = true, }, ref) {
|
|
7
12
|
const { t } = useTranslation('upload');
|
|
13
|
+
const { state, dispatch } = useContext(CommonContext);
|
|
14
|
+
const { dispatchError } = useMessageUtils();
|
|
15
|
+
const [file, setFile] = useState();
|
|
8
16
|
const [preview, setPreview] = useState();
|
|
17
|
+
const fileRef = useRef();
|
|
18
|
+
fileRef.current = file;
|
|
9
19
|
const isImageFile = (f) => {
|
|
10
20
|
return f.type?.startsWith('image/') ?? false;
|
|
11
21
|
};
|
|
@@ -21,12 +31,37 @@ export default function AppDropzoneBody({ accept, file, onFileChange, onDrop, })
|
|
|
21
31
|
}
|
|
22
32
|
setPreview(undefined);
|
|
23
33
|
}, [file]);
|
|
34
|
+
const upload = async () => {
|
|
35
|
+
const currentFile = fileRef.current;
|
|
36
|
+
if (!currentFile || state.toastLoading)
|
|
37
|
+
return;
|
|
38
|
+
dispatch(toastLoading(true));
|
|
39
|
+
const data = new FormData();
|
|
40
|
+
data.append('file', currentFile);
|
|
41
|
+
data.append('folder', folder);
|
|
42
|
+
keepName && data.append('keepOriginalName', 'true');
|
|
43
|
+
UploadService.execute(data)
|
|
44
|
+
.then((r) => {
|
|
45
|
+
onSuccess(r.data);
|
|
46
|
+
})
|
|
47
|
+
.catch((err) => {
|
|
48
|
+
dispatchError({ err: 'error.uploading.image', t, key: 'upload' });
|
|
49
|
+
onError?.(err);
|
|
50
|
+
})
|
|
51
|
+
.finally(() => {
|
|
52
|
+
dispatch(toastLoading(false));
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
useImperativeHandle(ref, () => ({
|
|
56
|
+
upload,
|
|
57
|
+
}));
|
|
24
58
|
const handleDrop = (files) => {
|
|
25
59
|
if (files.length === 0)
|
|
26
60
|
return;
|
|
27
|
-
|
|
28
|
-
onFileChange(droppedFile);
|
|
61
|
+
setFile(files[0]);
|
|
29
62
|
onDrop?.();
|
|
30
63
|
};
|
|
31
|
-
return (_jsxs("div", { className: "dropzone-container", children: [!file && (_jsx(Dropzone, { onDrop: (acceptedFiles) => handleDrop(acceptedFiles), accept: accept, maxFiles: 1, children: ({ getRootProps, getInputProps }) => (_jsxs("div", { ...getRootProps(), className: "dropzone", children: [_jsx("input", { ...getInputProps() }), _jsx("span", { children: t('label.dropzone') })] })) })), file && preview && _jsx("img", { className: "preview", src: preview, alt: "" }), file && !preview && (_jsxs("div", { className: "uploaded-message", children: [_jsx("b", { children: t('label.uploaded.file') }), _jsx("span", { children: file.name }), _jsx("b", { children: t('label.confirm') })] }))] }));
|
|
64
|
+
return (_jsxs("div", { className: "dropzone-container", children: [!file && (_jsx(Dropzone, { onDrop: (acceptedFiles) => handleDrop(acceptedFiles), accept: accept, maxFiles: 1, children: ({ getRootProps, getInputProps }) => (_jsxs("div", { ...getRootProps(), className: "dropzone", children: [_jsx("input", { ...getInputProps() }), _jsx("span", { children: t('label.dropzone') })] })) })), file && preview && _jsx("img", { className: "preview", src: preview, alt: "" }), file && !preview && (_jsxs("div", { className: "uploaded-message", children: [_jsx("b", { children: t('label.uploaded.file') }), _jsx("span", { children: file.name }), _jsx("b", { children: t('label.confirm') })] })), file && showConfirmButton && (_jsx(Button, { onClick: upload, text: t('common:button.confirm') }))] }));
|
|
32
65
|
}
|
|
66
|
+
const AppDropzoneBody = forwardRef(AppDropzoneBodyInner);
|
|
67
|
+
export default AppDropzoneBody;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import AppDropzone from './AppDropzone';
|
|
2
|
-
import AppDropzoneBody from './AppDropzoneBody';
|
|
2
|
+
import AppDropzoneBody, { type AppDropzoneBodyProps, type AppDropzoneBodyRef } from './AppDropzoneBody';
|
|
3
3
|
export default AppDropzone;
|
|
4
|
-
export { AppDropzoneBody };
|
|
4
|
+
export { AppDropzoneBody, type AppDropzoneBodyProps, type AppDropzoneBodyRef };
|
package/dist/AppForm/AppForm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { CheckboxField, DatePickerField, SelectField, SwitchField, TextField } from 'tycho-storybook';
|
|
2
|
+
import { CheckboxField, DatePickerField, SelectField, SwitchField, TextField, } from 'tycho-storybook';
|
|
3
3
|
import './style.scss';
|
|
4
4
|
const convertList = (values, labelAttr, valueAttr) => {
|
|
5
5
|
if (!values)
|
|
@@ -23,7 +23,7 @@ export default function AppForm({ fields, form, prefix, onChangeFunctions = {},
|
|
|
23
23
|
const handleChange = getOnChangeFn(field.attr);
|
|
24
24
|
if (field.type === 'select') {
|
|
25
25
|
const selectOptions = getOptions(field.attr) || field.options || [];
|
|
26
|
-
return (_jsx(SelectField, { attr: name, label: field.name, createdForm: form, options: selectOptions, disabled: field.disabled, required: field.required, onChange: (value) => handleChange?.(value) }, name));
|
|
26
|
+
return (_jsx(SelectField, { attr: name, label: field.name, createdForm: form, options: selectOptions, disabled: field.disabled, required: field.required, onChange: (value) => handleChange?.(value), multiple: field.multiple }, name));
|
|
27
27
|
}
|
|
28
28
|
if (field.type === 'check') {
|
|
29
29
|
return (_jsx(CheckboxField, { attr: name, label: field.name, createdForm: form, disabled: field.disabled }, name));
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { default as AppAnalytics } from './AppAnalytics';
|
|
|
2
2
|
export { default as AppCard } from './AppCard/AppCard';
|
|
3
3
|
export { default as AppClipboard } from './AppClipboard';
|
|
4
4
|
export { default as AppColorpicker } from './AppColorpicker';
|
|
5
|
-
export { default as AppDropzone, AppDropzoneBody } from './AppDropzone';
|
|
5
|
+
export { default as AppDropzone, AppDropzoneBody, type AppDropzoneBodyProps, type AppDropzoneBodyRef, } from './AppDropzone';
|
|
6
6
|
export type { UploadedFile } from './AppDropzone/UploadedFile';
|
|
7
7
|
export { default as AppEditable } from './AppEditable';
|
|
8
8
|
export type { AppEditableField } from './AppEditable/AppEditableField';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export { default as AppAnalytics } from './AppAnalytics';
|
|
|
2
2
|
export { default as AppCard } from './AppCard/AppCard';
|
|
3
3
|
export { default as AppClipboard } from './AppClipboard';
|
|
4
4
|
export { default as AppColorpicker } from './AppColorpicker';
|
|
5
|
-
export { default as AppDropzone, AppDropzoneBody } from './AppDropzone';
|
|
5
|
+
export { default as AppDropzone, AppDropzoneBody, } from './AppDropzone';
|
|
6
6
|
export { default as AppEditable } from './AppEditable';
|
|
7
7
|
export { validateFormField } from './AppEditable/FormField';
|
|
8
8
|
export { default as AppForm } from './AppForm/AppForm';
|