@quansitech/antd-admin 1.0.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.
Files changed (38) hide show
  1. package/components/Column/Cascader.tsx +79 -0
  2. package/components/Column/File.tsx +168 -0
  3. package/components/Column/Image.tsx +77 -0
  4. package/components/Column/Readonly/Cascader.tsx +51 -0
  5. package/components/Column/Readonly/File.tsx +54 -0
  6. package/components/Column/Readonly/Image.tsx +78 -0
  7. package/components/Column/Readonly/Option.tsx +58 -0
  8. package/components/Column/Readonly/types.d.ts +9 -0
  9. package/components/Column/Ueditor.tsx +314 -0
  10. package/components/Column/types.d.ts +29 -0
  11. package/components/Form/Action/Button.tsx +125 -0
  12. package/components/Form/Action/types.d.ts +5 -0
  13. package/components/Form/Actions.tsx +35 -0
  14. package/components/Form.tsx +171 -0
  15. package/components/FormContext.ts +8 -0
  16. package/components/Layout.tsx +237 -0
  17. package/components/LayoutContext.ts +26 -0
  18. package/components/ModalContext.ts +16 -0
  19. package/components/Table/Action/Button.tsx +89 -0
  20. package/components/Table/Action/StartEditable.tsx +59 -0
  21. package/components/Table/Action/types.d.ts +7 -0
  22. package/components/Table/Option/Link.tsx +68 -0
  23. package/components/Table/Option/types.d.ts +5 -0
  24. package/components/Table/ToolbarActions.tsx +39 -0
  25. package/components/Table.scss +7 -0
  26. package/components/Table.tsx +279 -0
  27. package/components/TableContext.ts +14 -0
  28. package/components/Tabs.tsx +72 -0
  29. package/components/types.d.ts +0 -0
  30. package/lib/container.ts +82 -0
  31. package/lib/customRule.ts +10 -0
  32. package/lib/global.ts +11 -0
  33. package/lib/helpers.tsx +150 -0
  34. package/lib/http.ts +74 -0
  35. package/lib/schemaHandler.ts +122 -0
  36. package/lib/upload.ts +177 -0
  37. package/package.json +35 -0
  38. package/readme.md +128 -0
@@ -0,0 +1,122 @@
1
+ import {ProColumnType, ProSchema} from "@ant-design/pro-components";
2
+ import {UploadFile} from "antd";
3
+ import http from "./http";
4
+
5
+ type Handler = (schema: any) => ProSchema | ProColumnType
6
+
7
+ const uploadValidator = (_: unknown, value: UploadFile[]) => {
8
+ return new Promise((resolve, reject) => {
9
+ if (!value) {
10
+ resolve(true)
11
+ return
12
+ }
13
+ for (let i = 0; i < value.length; i++) {
14
+ switch (value[i].status) {
15
+ case 'error':
16
+ reject('存在上传失败文件,请删除失败文件后重新上传')
17
+ return
18
+ case 'uploading':
19
+ reject('文件上传中,请稍后')
20
+ return
21
+ }
22
+ }
23
+ resolve(true)
24
+ })
25
+ }
26
+
27
+ const uploadTransform = (value?: UploadFile[], _name?: string) => {
28
+ if (value instanceof Array) {
29
+ return value.filter(file => file.status === 'done')
30
+ .map((file: UploadFile) => {
31
+ return file.response?.file_id
32
+ }).join(',')
33
+ }
34
+ return value
35
+ }
36
+
37
+ export const commonHandler: Handler = schema => {
38
+ if (schema.valueEnum) {
39
+ schema.valueEnum = new Map(schema.valueEnum)
40
+ }
41
+ return schema
42
+ }
43
+
44
+ export const schemaHandler: Record<string, Handler> = {
45
+ dateTimeRange: schema => {
46
+ if (schema.search !== false) {
47
+ return {
48
+ ...schema,
49
+ search: {
50
+ transform(value) {
51
+ if (value) {
52
+ return value.join(' - ')
53
+ }
54
+ return value
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ return {
61
+ ...schema,
62
+ }
63
+ },
64
+ dateRange: schema => {
65
+ if (schema.search !== false) {
66
+ return {
67
+ ...schema,
68
+ search: {
69
+ transform(value) {
70
+ if (value) {
71
+ return value.join(' - ')
72
+ }
73
+ return value
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ return {
80
+ ...schema,
81
+ }
82
+ },
83
+
84
+ // 上传
85
+ image: schema => {
86
+ schema.formItemProps.rules.push({
87
+ validator: uploadValidator,
88
+ })
89
+
90
+ return {
91
+ ...schema,
92
+ transform: uploadTransform,
93
+ }
94
+ },
95
+ file: schema => {
96
+ schema.formItemProps.rules.push({
97
+ validator: uploadValidator,
98
+ })
99
+
100
+ return {
101
+ ...schema,
102
+ transform: uploadTransform,
103
+ }
104
+ },
105
+
106
+ select(schema) {
107
+ schema.searchOnChange = true
108
+ if (schema.fieldProps?.searchUrl) {
109
+ return {
110
+ ...schema,
111
+ request: async params => {
112
+ const res = await http(schema.fieldProps.searchUrl, {params})
113
+ return res.data.map((item: { value: any, label?: string }) => ({
114
+ label: item.label || item.value,
115
+ value: item.value
116
+ }))
117
+ }
118
+ }
119
+ }
120
+ return schema
121
+ },
122
+ }
package/lib/upload.ts ADDED
@@ -0,0 +1,177 @@
1
+ import http from "./http";
2
+ import {AxiosError} from "axios";
3
+ import {UploadRequestOption} from "rc-upload/lib/interface";
4
+ import {GetProp, message, Upload, UploadFile, UploadProps} from "antd";
5
+ import {md5} from "js-md5"
6
+
7
+ type QsUploadFile = UploadFile & {
8
+ hash_id?: string,
9
+ }
10
+
11
+ export async function customRequest(options: UploadRequestOption & {
12
+ file: QsUploadFile
13
+ }) {
14
+ const policyRes = await http({
15
+ url: options.action,
16
+ method: 'get',
17
+ headers: options.headers,
18
+ fetchOptions: {
19
+ noHandle: true
20
+ },
21
+ params: {
22
+ title: options.file.name,
23
+ hash_id: options.file.hash_id,
24
+ file_type: options.file?.type || '' as string
25
+ }
26
+ })
27
+ if (policyRes.data.status) {
28
+ options.onSuccess && options.onSuccess({
29
+ ...policyRes.data,
30
+ url: policyRes.data.url || policyRes.data.file_url
31
+ })
32
+ return
33
+ }
34
+
35
+ const formData = new FormData();
36
+ let url = ''
37
+ if (policyRes.data.server_url) {
38
+ url = policyRes.data.server_url
39
+ } else if (policyRes.data.url) {
40
+ url = policyRes.data.url
41
+ }
42
+ if (policyRes.data.params) {
43
+ for (const key in policyRes.data.params) {
44
+ formData.append(key, policyRes.data.params[key])
45
+ }
46
+ }
47
+
48
+ formData.append('file', options.file)
49
+ try {
50
+ const res = await http({
51
+ url: url,
52
+ method: 'post',
53
+ data: formData,
54
+ fetchOptions: {
55
+ noHandle: true
56
+ },
57
+ headers: options.headers,
58
+ onUploadProgress(ev) {
59
+ options.onProgress && options.onProgress({
60
+ percent: (ev.progress || 0) * 100
61
+ })
62
+ },
63
+ })
64
+
65
+ if (res.data.info) {
66
+ message.error(res.data.info)
67
+ options.onError && options.onError(new Error(res.data.info), res.data)
68
+ return
69
+ }
70
+
71
+ options.onSuccess && options.onSuccess({
72
+ ...res.data,
73
+ url: res.data.url || res.data.file_url
74
+ })
75
+ } catch (e) {
76
+ if (e instanceof AxiosError) {
77
+ options.onError && options.onError(e, e.response?.data)
78
+ }
79
+ if (e instanceof Error) {
80
+ options.onError && options.onError({
81
+ name: e.name,
82
+ message: e.message,
83
+ method: options.method,
84
+ url: options.action
85
+ })
86
+ }
87
+ throw e
88
+ }
89
+ }
90
+
91
+ export type FileType = Parameters<GetProp<UploadProps, 'beforeUpload'>>[0];
92
+
93
+ export function getBase64(file: FileType): Promise<string> {
94
+ return new Promise((resolve, reject) => {
95
+ const reader = new FileReader();
96
+ reader.readAsDataURL(file);
97
+ reader.onload = () => resolve(reader.result as string);
98
+ reader.onerror = (error) => reject(error);
99
+ })
100
+ }
101
+
102
+ /**
103
+ * 计算文件MD5
104
+ * https://github.com/quansitech/file-md5-wasm/blob/master/js/calc_file_hash.js
105
+ * @param file
106
+ */
107
+ export function calc_file_hash(file: File) {
108
+ const MD5_RANGE_SIZE = 10 * 1024 * 1024;
109
+ const CUT_LIMIT = 40 * 1024 * 1024;
110
+
111
+ function makeMd5Range(fileSize: number) {
112
+ if (CUT_LIMIT > fileSize) {
113
+ return [[0, fileSize]];
114
+ }
115
+
116
+ // MD5_RANGE_SIZE设置为10
117
+ const first = [0.0, MD5_RANGE_SIZE];
118
+ const last = [fileSize - MD5_RANGE_SIZE, fileSize];
119
+
120
+ // 中间段算法
121
+ const rangeMod = fileSize - 3.0 * MD5_RANGE_SIZE;
122
+ const midStart = fileSize % rangeMod;
123
+ const middle = [MD5_RANGE_SIZE + midStart, MD5_RANGE_SIZE + midStart + MD5_RANGE_SIZE];
124
+
125
+ return [first, middle, last];
126
+ }
127
+
128
+ return new Promise<string>(function (resolve, reject) {
129
+ const fileSize = file.size;
130
+ const range = makeMd5Range(fileSize);
131
+
132
+ let index = 0;
133
+ const hash = md5.create();// 创建MD5哈希对象
134
+
135
+ const reader = new FileReader();
136
+
137
+ reader.onload = function (e) {
138
+ // 处理当前分块的读取结果
139
+ const chunkData = e.target?.result;
140
+
141
+ hash.update(chunkData as string); // 更新哈希计算结果
142
+
143
+ if (index < range.length - 1) {
144
+ index++;
145
+ readNextChunk(range, index);
146
+ } else {
147
+ const fileMD5 = hash.hex(); // 计算文件的最终MD5哈希值
148
+
149
+ resolve(fileMD5);
150
+ }
151
+ };
152
+
153
+ reader.onerror = function (e) {
154
+ reject(e);
155
+
156
+ };
157
+
158
+ function readNextChunk(chunkRange: number[][], index: number) {
159
+ const chunk = file.slice(chunkRange[index][0], chunkRange[index][1]);
160
+ reader.readAsArrayBuffer(chunk);
161
+ }
162
+
163
+ readNextChunk(range, index);
164
+ });
165
+
166
+ }
167
+
168
+ export async function beforeUpload(file: File & {
169
+ hash_id?: string,
170
+ }, fileList: UploadFile[], allFileList: UploadFile[]) {
171
+ file.hash_id = await calc_file_hash(file)
172
+ const f = allFileList.filter((item: QsUploadFile) => !!item.hash_id).find((item: QsUploadFile) => item.hash_id === file.hash_id)
173
+ if (f) {
174
+ message.error(file.name + '文件已上传')
175
+ return Upload.LIST_IGNORE
176
+ }
177
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@quansitech/antd-admin",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "files": [
9
+ "components",
10
+ "lib"
11
+ ],
12
+ "dependencies": {
13
+ "@ant-design/pro-components": "^2.7.15",
14
+ "@inertiajs/react": "^1.2.0",
15
+ "antd": "^5.20.5",
16
+ "antd-img-crop": "^4.23.0",
17
+ "axios": "^1.7.7",
18
+ "lodash": "^4.17.21",
19
+ "react": "^18.3.0",
20
+ "react-dom": "^18.3.0",
21
+ "js-md5": "^0.8.3",
22
+ "prettier": "^3.3.3"
23
+ },
24
+ "devDependencies": {
25
+ "@types/lodash": "^4.14.193",
26
+ "@types/react": "^18.3.0",
27
+ "@types/react-dom": "^18.3.0"
28
+ },
29
+ "author": "958691165@qq.com",
30
+ "license": "MIT",
31
+ "description": "qscmf antd-admin前端组件",
32
+ "publishConfig": {
33
+ "access": "public"
34
+ }
35
+ }
package/readme.md ADDED
@@ -0,0 +1,128 @@
1
+ # Qs-antd-admin
2
+
3
+ 该项目作为qs-cmf的后台前端组件库,基于[ant-design-pro](https://procomponents.ant.design/components)
4
+
5
+ ## 安装
6
+
7
+ ```shell
8
+ npm install @quansitech/antd-admin
9
+ ```
10
+
11
+ ## 使用参考
12
+
13
+ ### valueType列表
14
+
15
+ 参考 [ant-design-pro#valueType](https://procomponents.ant.design/components/schema#valuetype-%E5%88%97%E8%A1%A8)
16
+
17
+ ## 自定义组件
18
+
19
+ 对外暴露 [container](./lib/container.ts) 供外部调用
20
+
21
+ ```ts
22
+ import container from "@quansitech/antd-admin/lib/container";
23
+
24
+ container.register('[组件名]', () => import('[组件路径]'));
25
+ ```
26
+
27
+ ### 通用
28
+
29
+ #### 通用Column Schema
30
+
31
+ - 组件名前缀:``` Column. ```
32
+ - 用途:表单项组件(非只读模式)、表格列编辑组件、表格搜索项组件
33
+ - 示例:
34
+
35
+ ```tsx
36
+ // [组件.tsx]
37
+ import {ColumnProps} from "@quansitech/antd-admin/compontents/Column/types";
38
+
39
+ export default function (props: ColumnProps) {
40
+
41
+ return <>
42
+ 组件内容
43
+ </>
44
+ }
45
+
46
+ // [app.tsx]
47
+ import container from "@quansitech/antd-admin/lib/container";
48
+
49
+ container.register('Column.组件名', () => import('[组件路径]'));
50
+ ```
51
+
52
+ - 若要补充组件库,请把组件放``` compontents/Column/ ``` 目录下
53
+
54
+ #### 只读Column Schema
55
+
56
+ - 组件名前缀:``` Column.Readonly. ```
57
+ - 用途:表单项组件(只读模式)、表格列组件
58
+ - 示例:
59
+
60
+ ```tsx
61
+ // [组件.tsx]
62
+ import {ColumnProps} from "@quansitech/antd-admin/compontents/Column/Readonly/types";
63
+
64
+ export default function (props: ColumnProps) {
65
+
66
+ return <>
67
+ 组件内容
68
+ </>
69
+ }
70
+
71
+ // [app.tsx]
72
+ import container from "@quansitech/antd-admin/lib/container";
73
+
74
+ container.register('Column.Readonly.组件名', () => import('[组件路径]'));
75
+ ```
76
+
77
+ - 若要补充组件库,请把组件放``` compontents/Column/Readonly/ ``` 目录下
78
+
79
+ ### 表格Table
80
+
81
+ #### 工具栏操作组件
82
+
83
+ - 组件名前缀:``` Table.Column.Action. ```
84
+ - 示例:
85
+
86
+ ```tsx
87
+ // [组件.tsx]
88
+
89
+ import {TableActionProps} from "@quansitech/antd-admin/compontents/Table/Action/types";
90
+
91
+ export default function (props: TableActionProps) {
92
+ return <Button>{props.title}</Button>
93
+ }
94
+
95
+ // [app.tsx]
96
+
97
+ import container from "@quansitech/antd-admin/lib/container";
98
+
99
+ container.register('Table.Column.Action.组件名', () => import('[组件路径]'));
100
+
101
+ ```
102
+
103
+ - 若要补充组件库,请把组件放``` compontents/Table/Action/ ``` 目录下
104
+
105
+ #### 行操作组件
106
+
107
+ - 组件名前缀:``` Table.Column.Option ```
108
+ - 示例:
109
+
110
+ ```tsx
111
+ // [组件.tsx]
112
+
113
+ import {TableColumnOptionProps} from "@quansitech/antd-admin/compontents/Table/Column/Option/types";
114
+
115
+ export default function (props: TableColumnOptionProps) {
116
+ <a onClick={onClick}>{props.title}</a>
117
+ }
118
+
119
+ // [app.tsx]
120
+
121
+ import container from "@quansitech/antd-admin/lib/container";
122
+
123
+ container.register('Table.Column.Option.组件名', () => import('[组件路径]'));
124
+
125
+ ```
126
+
127
+ - 若要补充组件库,请把组件放``` compontents/Table/Column/Option/ ``` 目录下
128
+