/* argus-disable unPkgSensitiveInfo */
import React, { useState } from 'react';
import FileCard from '../fileCard';
import { Button, Upload, Toast, Tag } from '@douyinfe/semi-ui/index';
import { withField, Form } from '../../form/index';
import { IconPlus, IconFile, IconUpload, IconEyeOpened, IconDownload, IconDelete } from '@douyinfe/semi-icons';
export default {
title: 'Upload'
}
let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
// let action = 'https://127.0.0.1:3000/upload/';
// action = 'https://jsonplaceholder.typicode.com/posts/';
let actionFail = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
let apiNotExist = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
let commonProps = {
action: action,
onSuccess: (...args) => {
console.log('onSuccess');
Toast.success('success');
console.log(args);
},
onProcess: (...args) => {
console.log('onProcess');
console.log(args);
},
onError: (...args) => {
Toast.error('onError');
console.log('onError');
console.log(args);
},
onExceed: (...args) => {
Toast.warning('onExceed');
console.log('onExceed');
console.log(args);
},
onSizeError: (...args) => {
Toast.warning('onSizeError');
console.log('onSizeError');
console.log(args);
},
onChange: (...args) => {
console.log('onChange');
console.log(args);
},
onPreviewClick: fileItem => {
let url = fileItem.url;
console.log(fileItem);
window.open(url);
}
};
export const BasicUsage = () => (
<>
} theme="light">
点击上传
} theme="light">
点击上传(可替换)
一个404的接口}
>
} theme="light">
upload接口 404
一个503的接口}
>
} theme="light">
upload接口503
一个跨域不能请求的接口}
>
} theme="light">
upload接口跨域了
>
);
BasicUsage.story = {
name: 'basic usage',
};
let data = {
role: 'ies',
time: new Date().getTime(),
};
let headers = {
'x-tt-semi-test': 'semi-upload',
};
const getPromptDemo = () => {
const action = '//semi.design/api/upload';
const getPrompt = (pos, isListType) => {
let basicStyle = {
display: 'flex',
alignItems: 'center',
color: 'grey',
height: isListType ? '100%' : 32,
};
let marginStyle = {
left: { marginRight: 10 },
right: { marginLeft: 10 },
};
let style = { ...basicStyle, ...marginStyle[pos] };
return
请上传资格认证材料
;
};
const button = (
} theme="light">
点击上传
);
const positions = ['right', 'left', 'bottom'];
return (
<>
{positions.map((pos, index) => (
<>
{index ? (
) : null}
{button}
>
))}
{positions.map((pos, index) => (
<>
{index ? (
) : null}
>
))}
>
);
};
export const Prompt = () => getPromptDemo();
Prompt.story = {
name: 'prompt',
};
export const CustomDataAndHeaders = () => (
<>
直接返回object
} theme="light">
点击上传
通过函数直接返回object
data} headers={() => headers}>
} theme="light">
点击上传
>
);
CustomDataAndHeaders.story = {
name: 'custom data and headers',
};
let imageOnly = 'image/*';
let videoOnly = 'video/*';
export const Accept = () => (
<>
} theme="light">
点击上传image
} theme="light">
点击上传video
>
);
Accept.story = {
name: 'accept',
};
export const Multiple = () => (
<>
} theme="light">
点击上传
>
);
Multiple.story = {
name: 'multiple',
};
const defaultFileList = [
{
uid: '1',
name: 'jiafang1.jpeg',
status: 'success',
size: '130kb',
url:
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
},
{
uid: '2',
name: 'jiafang2.jpeg',
status: 'uploadFail',
size: '222kb',
url:
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
},
{
uid: '3',
name: 'jiafang3.jpeg',
status: 'uploading',
percent: 50,
size: '222kb',
url:
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
},
{
uid: '4',
name: 'jiafang3.jpeg',
status: 'validateFail',
validateMessage: '文件过大',
size: '222kb',
url:
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
},
{
uid: '5',
name: 'jiafang4.jpeg',
status: 'validating',
validateMessage: '校验中',
size: '222kb',
url:
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
},
];
export const DefaultFileList = () => (
<>
} theme="light">
点击上传
>
);
DefaultFileList.story = {
name: 'defaultFileList',
};
class ControlledUpload extends React.Component {
constructor(props) {
super(props);
this.state = {
fileList: defaultFileList,
};
this.onChange = this.onChange.bind(this);
}
onChange({ fileList, currentFile, event }) {
console.log('onChange');
console.log(fileList);
console.log(currentFile);
this.setState({ fileList: fileList });
}
render() {
return (
} theme="light">
点击上传
);
}
}
export const ControlledFileList = () => ;
ControlledFileList.story = {
name: 'controlled fileList',
};
let kb1 = 1024 * 1024;
let kb2 = kb1 * 2;
let mb1 = kb1 * 1024;
export const MaxSizeAndMinSize = () => (
<>
Toast.error(`${file.name} size invalid`)}
>
} theme="light">
点击上传(最小200kB,最大1MB)
>
);
MaxSizeAndMinSize.story = {
name: 'maxSize and minSize',
};
export const PictureListType = () => (
<>
>
);
PictureListType.story = {
name: 'picture listType',
};
export const PictureListTypeWithDefaultFileList = () => (
<>
}
defaultFileList={defaultFileList}
>
>
);
PictureListTypeWithDefaultFileList.story = {
name: 'picture listType with default file list',
};
class ManulUploadDemo extends React.Component {
constructor() {
super();
this.state = {
fileList: [],
};
this.onFileChange = this.onFileChange.bind(this);
this.onRemove = this.onRemove.bind(this);
this.manulUpload = this.manulUpload.bind(this);
this.uploadRef = React.createRef();
}
onFileChange(file) {
let newFileList = [...this.state.fileList, ...file];
this.setState({ fileList: newFileList });
}
onRemove(file) {
let { fileList } = this.state;
fileList.filter(item => item.uid !== file.uid);
this.setState({ fileList });
}
manulUpload() {
this.uploadRef.current.upload(this.state.fileList);
}
render() {
const { fileList } = this.state;
return (
console.log(...v)}
onError={(...v) => console.log(...v)}
>
} theme="light">
选择文件
} theme="light" onClick={this.manulUpload}>
开始上传
);
}
}
export const ManulUpload = () => ;
ManulUpload.story = {
name: 'manul upload',
};
export const Disabled = () => (
<>
} theme="light" disabled>
点击上传
>
);
Disabled.story = {
name: 'disabled',
};
export const PreviewFile = () => (
<>
}>
} theme="light">
点击上传
>
);
PreviewFile.story = {
name: 'previewFile',
};
const CustomRenderFileDemo = () => {
const render = file => {
return {file.name}
;
};
return (
<>
renderFileItem demo:
} theme="light">
点击上传
itemWidth demo:
} theme="light">
点击上传
>
);
};
export const RenderFileItem = () => ;
RenderFileItem.story = {
name: 'renderFileItem',
};
export const CustomLimit = () => (
<>
Toast.error('超出限定数量')}
>
} theme="light">
点击上传, uploadtrigger = custom
Toast.error('超出限定数量')}>
} theme="light">
点击上传1
Toast.error('超出限定数量')}>
} theme="light">
点击上传2
>
);
CustomLimit.story = {
name: 'custom limit',
};
export const Draggable = () => (
<>
自定义拖拽取内容及结构
>
);
Draggable.story = {
name: 'draggable',
};
export const _FileCard = () => (
<>
console.log(v)}
>
console.log(v)}
>
>
);
_FileCard.story = {
name: 'FileCard',
};
class ValidateDemo extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.beforeUpload = this.beforeUpload.bind(this);
this.afterUpload = this.afterUpload.bind(this);
this.transformFile = this.transformFile.bind(this);
this.count = 0;
}
transformFile(fileInstance) {
if (this.count === 0) {
let newFile = new File([fileInstance], 'newFileName', { type: 'image/png' });
return newFile;
} else {
return fileInstance;
}
}
afterUpload({ response, file }) {
// 可以根据业务接口返回,决定当次上传是否成功
if (response.status_code == 200) {
return {
autoRemove: false,
status: 'uploadFail',
validateMessage: '内容不合法',
name: 'RenameByServer.jpg',
};
} else {
return {};
}
}
beforeUpload({ file, fileList }) {
let result;
if (this.count > 1) {
result = {
autoRemove: false,
fileInstance: file.fileInstance,
shouldUpload: true,
};
} else {
result = {
autoRemove: false,
fileInstance: file.fileInstance,
status: 'validateFail',
shouldUpload: false,
};
}
this.count = this.count + 1;
return result;
}
render() {
return (
} theme="light">
点击上传
);
}
}
export const TransformFileBeforeUploadAfterUpload = () => ;
TransformFileBeforeUploadAfterUpload.story = {
name: 'transformFile beforeUpload afterUpload',
};
class AsyncBeforeUploadDemo extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.beforeUpload = this.beforeUpload.bind(this);
this.count = 0;
}
beforeUpload({ file, fileList }) {
let result;
return new Promise((reslove, reject) => {
if (this.count > 1) {
result = {
autoRemove: false,
shouldUpload: true,
};
this.count = this.count + 1;
reslove(result);
} else {
result = {
autoRemove: false,
fileInstance: file.fileInstance,
status: 'validateFail',
shouldUpload: false,
validateMessage: `第${this.count + 1}个注定失败`,
};
this.count = this.count + 1;
reject(result);
}
});
}
render() {
return (
} theme="light">
点击上传
);
}
}
export const AsyncBeforeUpload = () => ;
AsyncBeforeUpload.story = {
name: 'async beforeUpload',
};
class ValidateMessage extends React.Component {
constructor(props) {
super(props);
this.state = {};
// this.transformFile = this.transformFile.bind(this);
}
render() {
return (
} theme="light">
点击上传
);
}
}
export const UploadValidateMessageValidateStatus = () => ;
UploadValidateMessageValidateStatus.story = {
name: 'Upload validateMessage validateStatus',
};
class FormUploadDemo extends React.Component {
constructor(props) {
super(props);
this.state = {};
// this.transformFile = this.transformFile.bind(this);
}
render() {
return (
{
console.log(fileList);
}}
>
} theme="light">
点击上传
{JSON.stringify(formState)}
>
)}
);
}
}
export const FormUpload = () => ;
FormUpload.story = {
name: 'Form.Upload',
};
class CustomRequestDemo extends React.Component {
constructor(props) {
super(props);
this.state = {};
// this.transformFile = this.transformFile.bind(this);
// this.tosClient =
}
render() {
return (
{
let count = 0;
onError();
// let interval = setInterval(() => {
// if (count === 100) {
// clearInterval(interval);
// onSuccess();
// }
// onProgress({ total: 100, loaded: count });
// count += 50;
// }, 1000);
}}
field="files"
label="认证图片上传"
extraText="请上传符合规格的图片"
name="testName"
>
} theme="light">
点击上传
>
)}
);
}
}
export const FormCustomRequest = () => ;
FormCustomRequest.story = {
name: 'Form.CustomRequest',
};
class AutoReplace extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
} theme="light">
点击上传
);
}
}
export const AutoReplaceLimit1 = () => ;
AutoReplaceLimit1.story = {
name: 'AutoReplace limit=1',
};
function DirectoryUpload() {
return (
<>
} theme="light">
点击上传文件夹
} theme="light">
点击上传文件夹并指定图片格式
>
);
}
export const _DirectoryUpload = () => ;
_DirectoryUpload.story = {
name: 'directory upload',
};
function ForbiddenRemove() {
const [fileList, $fileList] = useState(defaultFileList);
return (
<>
{
$fileList(f.fileList);
}}
beforeRemove={(file, currentList) => currentList.length > 2}
beforeClear={list => list.length > 2}
>
} theme="light">
点击上传
>
);
}
export const _ForbiddenRemove = () => ;
_ForbiddenRemove.story = {
name: 'forbidden remove',
};
export const CustomListOperation = () => {
const renderFileOperation = (fileItem)=>{
return
}
return } theme="light">点击上传
}
CustomListOperation.story = {
name: 'custom list operation',
}
export const TestReplaceFunc = () => (
<>
{return fileInstance;}}
>
} theme="light">
点击上传(最小0kB,最大1MB)
{return fileInstance;}}
>
} theme="light">
只接受image点击上传(最小0kB,最大1MB)
{return fileInstance;}}
>
} theme="light">
点击上传(最小200kB,最大1MB)
>
);
TestReplaceFunc.story = {
name: 'test replace func',
};
class InsertUpload extends React.Component {
constructor() {
super();
this.onFileChange = this.onFileChange.bind(this);
this.insert1 = this.insert1.bind(this);
this.insert2 = this.insert2.bind(this);
this.insert3 = this.insert3.bind(this);
this.uploadRef1 = React.createRef();
this.uploadRef2 = React.createRef();
this.uploadRef3 = React.createRef();
this.file = null;
}
onFileChange(file) {
delete file[0].uid;
this.file = file;
}
insert1() {
// test file number limit
this.uploadRef1.current.insert(this.file, 0);
}
insert2() {
this.uploadRef2.current.insert(this.file, 0);
}
insert3() {
// test size limit
this.uploadRef3.current.insert(this.file, 0);
}
render() {
let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
return (
console.log(...v)}
onError={(...v) => console.log(...v)}
onFileChange={this.onFileChange}
maxSize={mb1}
minSize={0}
limit={1}
transformFile={(fileInstance)=>{return fileInstance;}}
>
} theme="light" style={{ marginRight: 8 }}>
选择文件 limit 1
console.log(...v)}
onError={(...v) => console.log(...v)}
onFileChange={this.onFileChange}
maxSize={mb1}
minSize={0}
limit={2}
transformFile={(fileInstance)=>{return fileInstance;}}
>
} theme="light" style={{ marginRight: 8 }}>
选择文件 limit 2
console.log(...v)}
onError={(...v) => console.log(...v)}
onFileChange={this.onFileChange}
maxSize={mb1}
minSize={kb2}
limit={1}
transformFile={(fileInstance)=>{return fileInstance;}}
>
} theme="light" style={{ marginRight: 8 }}>
选择文件 size 限制
} theme="light" onClick={this.insert1}>
插入首项上传1
} theme="light" onClick={this.insert2}>
插入首项上传2
} theme="light" onClick={this.insert3}>
插入首项上传3
);
}
}
export const Insert = () => ;
Insert.story = {
name: 'insert',
};
class ClickToOpenUpload extends React.Component {
constructor() {
super();
this.onClick = this.onClick.bind(this);
this.uploadRef = React.createRef();
}
onClick() {
// open file select dialog
this.uploadRef.current.openFileDialog();
}
render() {
let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
return (
} theme="light" onClick={this.onClick}>
点我打开文件选择窗口
文件列表
console.log(...v)}
onError={(...v) => console.log(...v)}
>
);
}
}
export const ClickToOpenUploadDemo = () => ;
ClickToOpenUploadDemo.story = {
name: 'click to open upload demo',
};