@omniumretail/component-library 1.0.41 → 1.0.42
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/bundle.js +764 -7
- package/dist/types/components/{RangePickerTag/RangePickerTag.stories.d.ts → ImageUpload/ImageUpload.stories.d.ts} +1 -2
- package/dist/types/components/ImageUpload/imageUtils/index.d.ts +2 -0
- package/dist/types/components/ImageUpload/index.d.ts +2 -0
- package/dist/types/components/Upload/Upload.stories.d.ts +4 -0
- package/dist/types/components/Upload/index.d.ts +7 -0
- package/dist/types/components/index.d.ts +1 -0
- package/package.json +4 -1
- package/src/components/Upload/Upload.stories.tsx +45 -0
- package/src/components/Upload/index.tsx +93 -0
- package/src/components/index.tsx +1 -0
- package/dist/types/components/RangePickerTag/index.d.ts +0 -6
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { UploadFile, UploadProps } from 'antd/es/upload/interface';
|
|
2
|
+
interface UploadPropsExtended extends UploadProps {
|
|
3
|
+
onImageChange?: (file: UploadFile | null) => void;
|
|
4
|
+
initialFileList?: UploadFile[];
|
|
5
|
+
}
|
|
6
|
+
export declare const Upload: (props: UploadPropsExtended) => JSX.Element;
|
|
7
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omniumretail/component-library",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.42",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/bundle.js",
|
|
6
6
|
"typings": "./dist/types/index",
|
|
@@ -18,11 +18,13 @@
|
|
|
18
18
|
"@types/react-beautiful-dnd": "^13.1.3",
|
|
19
19
|
"@types/react-dom": "^18.0.9",
|
|
20
20
|
"antd": "^5.1.2",
|
|
21
|
+
"antd-img-crop": "^4.10.2",
|
|
21
22
|
"babel-jest": "^27.4.2",
|
|
22
23
|
"babel-loader": "^8.2.3",
|
|
23
24
|
"babel-plugin-named-asset-import": "^0.3.8",
|
|
24
25
|
"babel-preset-react-app": "^10.0.1",
|
|
25
26
|
"bfj": "^7.0.2",
|
|
27
|
+
"browser-image-compression": "^2.0.2",
|
|
26
28
|
"browserslist": "^4.18.1",
|
|
27
29
|
"camelcase": "^6.2.1",
|
|
28
30
|
"case-sensitive-paths-webpack-plugin": "^2.4.0",
|
|
@@ -59,6 +61,7 @@
|
|
|
59
61
|
"react-beautiful-dnd": "^13.1.1",
|
|
60
62
|
"react-dev-utils": "^12.0.1",
|
|
61
63
|
"react-dom": "^18.2.0",
|
|
64
|
+
"react-easy-crop": "^4.7.4",
|
|
62
65
|
"react-i18next": "^12.1.4",
|
|
63
66
|
"react-refresh": "^0.11.0",
|
|
64
67
|
"resolve": "^1.20.0",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Meta, Story } from "@storybook/react";
|
|
2
|
+
import { Form, UploadFile } from "antd";
|
|
3
|
+
import { Button } from "components/Button";
|
|
4
|
+
import { Upload } from '.';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
title: 'Upload',
|
|
8
|
+
component: Upload,
|
|
9
|
+
} as Meta;
|
|
10
|
+
|
|
11
|
+
const Template: Story<any> = (args) => {
|
|
12
|
+
const [form] = Form.useForm();
|
|
13
|
+
|
|
14
|
+
const handleSubmit = (values: any) => {
|
|
15
|
+
console.log('Form values:', values);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const handleImageChange = (file: UploadFile | null) => {
|
|
19
|
+
form.setFieldsValue({ image: file });
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const initialValue = {
|
|
23
|
+
uid: '-1',
|
|
24
|
+
name: 'image.png',
|
|
25
|
+
status: 'done',
|
|
26
|
+
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
|
|
27
|
+
}
|
|
28
|
+
return (
|
|
29
|
+
<Form form={form} onFinish={handleSubmit}>
|
|
30
|
+
<Form.Item name="image" valuePropName="file" initialValue={initialValue}>
|
|
31
|
+
<Upload {...args} onImageChange={handleImageChange} initialFileList={[initialValue]}></Upload>
|
|
32
|
+
</Form.Item>
|
|
33
|
+
<Form.Item>
|
|
34
|
+
<Button type="primary" htmlType="submit">
|
|
35
|
+
Submit
|
|
36
|
+
</Button>
|
|
37
|
+
</Form.Item>
|
|
38
|
+
</Form>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
export const Primary = Template.bind({});
|
|
44
|
+
Primary.args = {
|
|
45
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Upload as UploadAntd } from 'antd';
|
|
2
|
+
import ImgCrop from 'antd-img-crop';
|
|
3
|
+
import type { RcFile, UploadFile, UploadProps } from 'antd/es/upload/interface';
|
|
4
|
+
import React, { useState } from 'react';
|
|
5
|
+
import imageCompression from 'browser-image-compression';
|
|
6
|
+
|
|
7
|
+
interface UploadPropsExtended extends UploadProps {
|
|
8
|
+
onImageChange?: (file: UploadFile | null) => void;
|
|
9
|
+
initialFileList?: UploadFile[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const Upload = (props: UploadPropsExtended) => {
|
|
13
|
+
const {
|
|
14
|
+
onImageChange,
|
|
15
|
+
initialFileList
|
|
16
|
+
} = props;
|
|
17
|
+
const [fileList, setFileList] = useState<UploadFile[]>(initialFileList || []);
|
|
18
|
+
console.log(fileList);
|
|
19
|
+
|
|
20
|
+
const compressImage = async (file: RcFile) => {
|
|
21
|
+
const options = {
|
|
22
|
+
maxSizeMB: 1,
|
|
23
|
+
maxWidthOrHeight: 1920,
|
|
24
|
+
useWebWorker: true,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const compressedFile = await imageCompression(file, options);
|
|
29
|
+
return compressedFile;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('Image compression failed:', error);
|
|
32
|
+
return file;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const beforeUpload: UploadProps['beforeUpload'] = async (file: RcFile) => {
|
|
37
|
+
const compressedFile = await compressImage(file);
|
|
38
|
+
|
|
39
|
+
const newFile: UploadFile = {
|
|
40
|
+
uid: file.uid,
|
|
41
|
+
name: file.name,
|
|
42
|
+
type: file.type,
|
|
43
|
+
size: compressedFile.size,
|
|
44
|
+
originFileObj: compressedFile as any,
|
|
45
|
+
status: 'done',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
setFileList((prevFileList) => [...prevFileList, newFile]);
|
|
49
|
+
if (onImageChange) {
|
|
50
|
+
onImageChange(newFile);
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const onChange: UploadProps['onChange'] = ({ file, fileList: newFileList }) => {
|
|
56
|
+
if (file.status === 'removed') {
|
|
57
|
+
setFileList(newFileList);
|
|
58
|
+
if (onImageChange) {
|
|
59
|
+
onImageChange(newFileList[0] || null);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const onPreview = async (file: UploadFile) => {
|
|
65
|
+
let src = file.url as string;
|
|
66
|
+
if (!src) {
|
|
67
|
+
src = await new Promise((resolve) => {
|
|
68
|
+
const reader = new FileReader();
|
|
69
|
+
reader.readAsDataURL(file.originFileObj as RcFile);
|
|
70
|
+
reader.onload = () => resolve(reader.result as string);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const image = new Image();
|
|
74
|
+
image.src = src;
|
|
75
|
+
const imgWindow = window.open(src);
|
|
76
|
+
imgWindow?.document.write(image.outerHTML);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<ImgCrop rotationSlider>
|
|
81
|
+
<UploadAntd
|
|
82
|
+
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
|
|
83
|
+
listType="picture-card"
|
|
84
|
+
fileList={fileList}
|
|
85
|
+
beforeUpload={beforeUpload}
|
|
86
|
+
onChange={onChange}
|
|
87
|
+
onPreview={onPreview}
|
|
88
|
+
>
|
|
89
|
+
{fileList.length < 1 && '+ Upload'}
|
|
90
|
+
</UploadAntd>
|
|
91
|
+
</ImgCrop>
|
|
92
|
+
);
|
|
93
|
+
};
|
package/src/components/index.tsx
CHANGED