-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.tsx
214 lines (198 loc) · 5.32 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import React from 'react'
import { connect } from '@formily/react-schema-renderer'
import { toArr, isArr, isEqual, mapStyledProps } from '../shared'
import { Button, Upload as NextUpload } from '@alifd/next'
import { UploadProps, CardProps } from '@alifd/next/types/upload'
const { Card: UploadCard, Dragger: UploadDragger } = NextUpload
const exts = [
{
ext: /\.docx?/i,
icon: '//img.alicdn.com/tfs/TB1n8jfr1uSBuNjy1XcXXcYjFXa-200-200.png'
},
{
ext: /\.pptx?/i,
icon: '//img.alicdn.com/tfs/TB1ItgWr_tYBeNjy1XdXXXXyVXa-200-200.png'
},
{
ext: /\.jpe?g/i,
icon: '//img.alicdn.com/tfs/TB1wrT5r9BYBeNjy0FeXXbnmFXa-200-200.png'
},
{
ext: /pdf/i,
icon: '//img.alicdn.com/tfs/TB1GwD8r9BYBeNjy0FeXXbnmFXa-200-200.png'
},
{
ext: /\.png/i,
icon: '//img.alicdn.com/tfs/TB1BHT5r9BYBeNjy0FeXXbnmFXa-200-200.png'
},
{
ext: /\.eps/i,
icon: '//img.alicdn.com/tfs/TB1G_iGrVOWBuNjy0FiXXXFxVXa-200-200.png'
},
{
ext: /\.ai/i,
icon: '//img.alicdn.com/tfs/TB1B2cVr_tYBeNjy1XdXXXXyVXa-200-200.png'
},
{
ext: /\.gif/i,
icon: '//img.alicdn.com/tfs/TB1DTiGrVOWBuNjy0FiXXXFxVXa-200-200.png'
},
{
ext: /\.svg/i,
icon: '//img.alicdn.com/tfs/TB1uUm9rY9YBuNjy0FgXXcxcXXa-200-200.png'
},
{
ext: /\.xlsx?/i,
icon: '//img.alicdn.com/tfs/TB1any1r1OSBuNjy0FdXXbDnVXa-200-200.png'
},
{
ext: /\.psd?/i,
icon: '//img.alicdn.com/tfs/TB1_nu1r1OSBuNjy0FdXXbDnVXa-200-200.png'
},
{
ext: /\.(wav|aif|aiff|au|mp1|mp2|mp3|ra|rm|ram|mid|rmi)/i,
icon: '//img.alicdn.com/tfs/TB1jPvwr49YBuNjy0FfXXXIsVXa-200-200.png'
},
{
ext: /\.(avi|wmv|mpg|mpeg|vob|dat|3gp|mp4|mkv|rm|rmvb|mov|flv)/i,
icon: '//img.alicdn.com/tfs/TB1FrT5r9BYBeNjy0FeXXbnmFXa-200-200.png'
},
{
ext: /\.(zip|rar|arj|z|gz|iso|jar|ace|tar|uue|dmg|pkg|lzh|cab)/i,
icon: '//img.alicdn.com/tfs/TB10jmfr29TBuNjy0FcXXbeiFXa-200-200.png'
},
{
ext: /\..+/i,
icon: '//img.alicdn.com/tfs/TB10.R4r3mTBuNjy1XbXXaMrVXa-200-200.png'
}
]
const testOpts = (ext, options) => {
if (options && isArr(options.include)) {
return options.include.some(url => ext.test(url))
}
if (options && isArr(options.exclude)) {
return !options.exclude.some(url => ext.test(url))
}
return true
}
const getImageByUrl = (url, options) => {
for (let i = 0; i < exts.length; i++) {
if (exts[i].ext.test(url) && testOpts(exts[i].ext, options)) {
return exts[i].icon
}
}
return url
}
const normalizeFileList = fileList => {
if (fileList && fileList.length) {
return fileList.map(file => {
return {
name: file.name,
downloadURL: file.downloadURL || file.imgURL,
...file.response,
imgURL: getImageByUrl(file.imgURL, {
exclude: ['.png', '.jpg', '.jpeg', '.gif']
})
}
})
}
return []
}
const shallowClone = val =>
isArr(val) ? [...val] : typeof val === 'object' ? { ...val } : val
export interface IUploaderProps extends UploadProps {
locale: { [name: string]: any }
}
export const Upload = connect({
getProps: mapStyledProps
})(
class Uploader extends React.Component<IUploaderProps> {
static defaultProps = {
action:
'https://www.easy-mock.com/mock/5b713974309d0d7d107a74a3/alifd/upload',
listType: 'text',
multiple: true
}
state = {
value: toArr(this.props.value)
}
onChangeHandler = (fileList?: any) => {
const { onChange } = this.props
fileList = toArr(fileList)
if (
fileList.every(
file => file.state === 'done' || file.imgURL || file.downloadURL
)
) {
fileList = normalizeFileList(fileList)
this.setState(
{
value: fileList
},
() => {
onChange(fileList.length > 0 ? fileList : undefined)
}
)
} else {
this.setState({
value: fileList
})
}
}
componentDidUpdate(preProps) {
if (this.props.value && !isEqual(this.props.value, preProps.value)) {
this.setState({
value: this.props.value
})
}
}
render() {
const {
listType,
locale,
onChange,
value,
children,
...others
} = this.props
if (listType.indexOf('card') > -1) {
return (
<UploadCard
{...(others as CardProps)}
value={shallowClone(this.state.value)}
onChange={this.onChangeHandler}
listType="card"
/>
)
}
if (listType.indexOf('dragger') > -1) {
return (
// @ts-ignore 感觉是 next 那边对于 Dragger 的 props 定义错了
<UploadDragger
{...others}
value={shallowClone(this.state.value)}
onChange={this.onChangeHandler}
listType={listType.indexOf('image') > -1 ? 'image' : 'text'}
/>
)
}
return (
<NextUpload
{...others}
value={shallowClone(this.state.value)}
onChange={this.onChangeHandler}
listType={listType}
>
{children ? (
children
) : (
<Button style={{ margin: '0 0 10px' }}>
{(locale && locale.uploadText) || '上传文件'}
</Button>
)}
</NextUpload>
)
}
}
)
export default Upload