upload.stories.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* argus-disable unPkgSensitiveInfo */
  2. import React, { useState } from 'react';
  3. import FileCard from '../fileCard';
  4. import { Button, Upload, Toast, Icon } from '@douyinfe/semi-ui/index';
  5. import { withField, Form } from '../../form/index';
  6. import { IconPlus, IconFile, IconUpload, IconEyeOpened, IconDownload, IconDelete } from '@douyinfe/semi-icons';
  7. export default {
  8. title: 'Upload'
  9. }
  10. let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
  11. // let action = 'https://127.0.0.1:3000/upload/';
  12. // action = 'https://jsonplaceholder.typicode.com/posts/';
  13. let actionFail = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
  14. let apiNotExist = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
  15. let commonProps = {
  16. action: action,
  17. onSuccess: (...args) => {
  18. console.log('onSuccess');
  19. Toast.success('success');
  20. console.log(args);
  21. },
  22. onProcess: (...args) => {
  23. console.log('onProcess');
  24. console.log(args);
  25. },
  26. onError: (...args) => {
  27. Toast.error('onError');
  28. console.log('onError');
  29. console.log(args);
  30. },
  31. onExceed: (...args) => {
  32. Toast.warning('onExceed');
  33. console.log('onExceed');
  34. console.log(args);
  35. },
  36. onSizeError: (...args) => {
  37. Toast.warning('onSizeError');
  38. console.log('onSizeError');
  39. console.log(args);
  40. },
  41. onChange: (...args) => {
  42. console.log('onChange');
  43. console.log(args);
  44. },
  45. onPreviewClick: fileItem => {
  46. let url = fileItem.url;
  47. console.log(fileItem);
  48. window.open(url);
  49. }
  50. };
  51. export const BasicUsage = () => (
  52. <>
  53. <Upload {...commonProps}>
  54. <Button icon={<IconUpload />} theme="light">
  55. 点击上传
  56. </Button>
  57. </Upload>
  58. <Upload style={{ marginTop: 20 }} {...commonProps} showReplace>
  59. <Button icon={<IconUpload />} theme="light">
  60. 点击上传(可替换)
  61. </Button>
  62. </Upload>
  63. <Upload
  64. style={{ marginTop: 20 }}
  65. {...commonProps}
  66. action={actionFail}
  67. prompt={<div style={{ display: 'flex', alignItems: 'center' }}>一个404的接口</div>}
  68. >
  69. <Button icon={<IconUpload />} theme="light">
  70. upload接口 404
  71. </Button>
  72. </Upload>
  73. <Upload
  74. style={{ marginTop: 20 }}
  75. {...commonProps}
  76. action={'https://semi.dev.com/api/upload503'}
  77. prompt={<div style={{ display: 'flex', alignItems: 'center' }}>一个503的接口</div>}
  78. >
  79. <Button icon={<IconUpload />} theme="light">
  80. upload接口503
  81. </Button>
  82. </Upload>
  83. <Upload
  84. style={{ marginTop: 20 }}
  85. {...commonProps}
  86. action={apiNotExist}
  87. prompt={<div style={{ display: 'flex', alignItems: 'center' }}>一个跨域不能请求的接口</div>}
  88. >
  89. <Button icon={<IconUpload />} theme="light">
  90. upload接口跨域了
  91. </Button>
  92. </Upload>
  93. </>
  94. );
  95. BasicUsage.story = {
  96. name: 'basic usage',
  97. };
  98. let data = {
  99. role: 'ies',
  100. time: new Date().getTime(),
  101. };
  102. let headers = {
  103. 'x-tt-semi-test': 'semi-upload',
  104. };
  105. const getPromptDemo = () => {
  106. const action = '//semi.design/api/upload';
  107. const getPrompt = (pos, isListType) => {
  108. let basicStyle = {
  109. display: 'flex',
  110. alignItems: 'center',
  111. color: 'grey',
  112. height: isListType ? '100%' : 32,
  113. };
  114. let marginStyle = {
  115. left: { marginRight: 10 },
  116. right: { marginLeft: 10 },
  117. };
  118. let style = { ...basicStyle, ...marginStyle[pos] };
  119. return <div style={style}>请上传资格认证材料</div>;
  120. };
  121. const button = (
  122. <Button icon={<IconUpload />} theme="light">
  123. 点击上传
  124. </Button>
  125. );
  126. const positions = ['right', 'left', 'bottom'];
  127. return (
  128. <>
  129. {positions.map((pos, index) => (
  130. <>
  131. {index ? (
  132. <div
  133. style={{
  134. marginBottom: 12,
  135. marginTop: 12,
  136. borderBottom: '1px solid var(--semi-color-border)',
  137. }}
  138. ></div>
  139. ) : null}
  140. <Upload action={action} prompt={getPrompt(pos)} promptPosition={pos}>
  141. {button}
  142. </Upload>
  143. </>
  144. ))}
  145. <div
  146. style={{
  147. marginBottom: 24,
  148. marginTop: 24,
  149. borderBottom: '1px solid var(--semi-color-border)',
  150. }}
  151. ></div>
  152. {positions.map((pos, index) => (
  153. <>
  154. {index ? (
  155. <div
  156. style={{
  157. marginBottom: 12,
  158. marginTop: 12,
  159. borderBottom: '1px solid var(--semi-color-border)',
  160. }}
  161. ></div>
  162. ) : null}
  163. <Upload
  164. action={action}
  165. prompt={getPrompt(pos, true)}
  166. promptPosition={pos}
  167. listType="picture"
  168. defaultFileList={defaultFileList}
  169. >
  170. <React.Fragment>
  171. <IconPlus />
  172. </React.Fragment>
  173. </Upload>
  174. </>
  175. ))}
  176. </>
  177. );
  178. };
  179. export const Prompt = () => getPromptDemo();
  180. Prompt.story = {
  181. name: 'prompt',
  182. };
  183. export const CustomDataAndHeaders = () => (
  184. <>
  185. <h4>直接返回object</h4>
  186. <Upload {...commonProps} data={data} headers={headers}>
  187. <Button icon={<IconUpload />} theme="light">
  188. 点击上传
  189. </Button>
  190. </Upload>
  191. <h4>通过函数直接返回object</h4>
  192. <Upload {...commonProps} data={() => data} headers={() => headers}>
  193. <Button icon={<IconUpload />} theme="light">
  194. 点击上传
  195. </Button>
  196. </Upload>
  197. </>
  198. );
  199. CustomDataAndHeaders.story = {
  200. name: 'custom data and headers',
  201. };
  202. let imageOnly = 'image/*';
  203. let videoOnly = 'video/*';
  204. export const Accept = () => (
  205. <>
  206. <Upload {...commonProps} action={action} accept={imageOnly}>
  207. <Button icon={<IconUpload />} theme="light">
  208. 点击上传image
  209. </Button>
  210. </Upload>
  211. <br />
  212. <br />
  213. <Upload {...commonProps} action={action} accept={videoOnly}>
  214. <Button icon={<IconUpload />} theme="light">
  215. 点击上传video
  216. </Button>
  217. </Upload>
  218. </>
  219. );
  220. Accept.story = {
  221. name: 'accept',
  222. };
  223. export const Multiple = () => (
  224. <>
  225. <Upload {...commonProps} action={action} multiple>
  226. <Button icon={<IconUpload />} theme="light">
  227. 点击上传
  228. </Button>
  229. </Upload>
  230. </>
  231. );
  232. Multiple.story = {
  233. name: 'multiple',
  234. };
  235. const defaultFileList = [
  236. {
  237. uid: '1',
  238. name: 'jiafang1.jpeg',
  239. status: 'success',
  240. size: '130kb',
  241. url:
  242. 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
  243. },
  244. {
  245. uid: '2',
  246. name: 'jiafang2.jpeg',
  247. status: 'uploadFail',
  248. size: '222kb',
  249. url:
  250. 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
  251. },
  252. {
  253. uid: '3',
  254. name: 'jiafang3.jpeg',
  255. status: 'uploading',
  256. percent: 50,
  257. size: '222kb',
  258. url:
  259. 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
  260. },
  261. {
  262. uid: '4',
  263. name: 'jiafang3.jpeg',
  264. status: 'validateFail',
  265. validateMessage: '文件过大',
  266. size: '222kb',
  267. url:
  268. 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
  269. },
  270. {
  271. uid: '5',
  272. name: 'jiafang4.jpeg',
  273. status: 'validating',
  274. validateMessage: '校验中',
  275. size: '222kb',
  276. url:
  277. 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg',
  278. },
  279. ];
  280. export const DefaultFileList = () => (
  281. <>
  282. <Upload {...commonProps} action={action} defaultFileList={defaultFileList}>
  283. <Button icon={<IconUpload />} theme="light">
  284. 点击上传
  285. </Button>
  286. </Upload>
  287. </>
  288. );
  289. DefaultFileList.story = {
  290. name: 'defaultFileList',
  291. };
  292. class ControlledUpload extends React.Component {
  293. constructor(props) {
  294. super(props);
  295. this.state = {
  296. fileList: defaultFileList,
  297. };
  298. this.onChange = this.onChange.bind(this);
  299. }
  300. onChange({ fileList, currentFile, event }) {
  301. console.log('onChange');
  302. console.log(fileList);
  303. console.log(currentFile);
  304. this.setState({ fileList: fileList });
  305. }
  306. render() {
  307. return (
  308. <Upload
  309. {...commonProps}
  310. action={action}
  311. onChange={this.onChange}
  312. fileList={this.state.fileList}
  313. showRetry={false}
  314. >
  315. <Button icon={<IconUpload />} theme="light">
  316. 点击上传
  317. </Button>
  318. </Upload>
  319. );
  320. }
  321. }
  322. export const ControlledFileList = () => <ControlledUpload></ControlledUpload>;
  323. ControlledFileList.story = {
  324. name: 'controlled fileList',
  325. };
  326. let kb1 = 1024 * 1024;
  327. let kb2 = kb1 * 2;
  328. let mb1 = kb1 * 1024;
  329. export const MaxSizeAndMinSize = () => (
  330. <>
  331. <Upload
  332. {...commonProps}
  333. action={action}
  334. maxSize={mb1}
  335. minSize={kb2}
  336. onSizeError={(file, fileList) => Toast.error(`${file.name} size invalid`)}
  337. >
  338. <Button icon={<IconUpload />} theme="light">
  339. 点击上传(最小200kB,最大1MB)
  340. </Button>
  341. </Upload>
  342. </>
  343. );
  344. MaxSizeAndMinSize.story = {
  345. name: 'maxSize and minSize',
  346. };
  347. export const PictureListType = () => (
  348. <>
  349. <Upload
  350. {...commonProps}
  351. showReplace
  352. action={action}
  353. listType="picture"
  354. accept="image/*"
  355. multiple
  356. >
  357. <React.Fragment>
  358. <IconPlus size="extra-large" />
  359. </React.Fragment>
  360. </Upload>
  361. </>
  362. );
  363. PictureListType.story = {
  364. name: 'picture listType',
  365. };
  366. export const PictureListTypeWithDefaultFileList = () => (
  367. <>
  368. <Upload
  369. {...commonProps}
  370. showReplace={false}
  371. action={action}
  372. listType="picture"
  373. accept="image/*"
  374. renderPicPreviewIcon={()=><IconEyeOpened style={{color: 'var(--semi-color-white)',fontSize: 24}} />}
  375. defaultFileList={defaultFileList}
  376. >
  377. <React.Fragment>
  378. <IconPlus size="extra-large" />
  379. </React.Fragment>
  380. </Upload>
  381. </>
  382. );
  383. PictureListTypeWithDefaultFileList.story = {
  384. name: 'picture listType with default file list',
  385. };
  386. class ManulUploadDemo extends React.Component {
  387. constructor() {
  388. super();
  389. this.state = {
  390. fileList: [],
  391. };
  392. this.onFileChange = this.onFileChange.bind(this);
  393. this.onRemove = this.onRemove.bind(this);
  394. this.manulUpload = this.manulUpload.bind(this);
  395. this.uploadRef = React.createRef();
  396. }
  397. onFileChange(file) {
  398. let newFileList = [...this.state.fileList, ...file];
  399. this.setState({ fileList: newFileList });
  400. }
  401. onRemove(file) {
  402. let { fileList } = this.state;
  403. fileList.filter(item => item.uid !== file.uid);
  404. this.setState({ fileList });
  405. }
  406. manulUpload() {
  407. this.uploadRef.current.upload(this.state.fileList);
  408. }
  409. render() {
  410. const { fileList } = this.state;
  411. return (
  412. <div>
  413. <Upload
  414. {...commonProps}
  415. accept="image/gif, image/png, image/jpeg, image/bmp, image/webp"
  416. action={action}
  417. uploadTrigger="custom"
  418. ref={this.uploadRef}
  419. onSuccess={(...v) => console.log(...v)}
  420. onError={(...v) => console.log(...v)}
  421. >
  422. <Button icon={<IconPlus />} theme="light">
  423. 选择文件
  424. </Button>
  425. </Upload>
  426. <Button icon={<IconUpload />} theme="light" onClick={this.manulUpload}>
  427. 开始上传
  428. </Button>
  429. </div>
  430. );
  431. }
  432. }
  433. export const ManulUpload = () => <ManulUploadDemo />;
  434. ManulUpload.story = {
  435. name: 'manul upload',
  436. };
  437. export const Disabled = () => (
  438. <>
  439. <Upload {...commonProps} action={action} disabled defaultFileList={defaultFileList}>
  440. <Button icon={<IconUpload />} theme="light" disabled>
  441. 点击上传
  442. </Button>
  443. </Upload>
  444. </>
  445. );
  446. Disabled.story = {
  447. name: 'disabled',
  448. };
  449. export const PreviewFile = () => (
  450. <>
  451. <Upload {...commonProps} action={action} previewFile={file => <IconFile size="large" />}>
  452. <Button icon={<IconUpload />} theme="light">
  453. 点击上传
  454. </Button>
  455. </Upload>
  456. </>
  457. );
  458. PreviewFile.story = {
  459. name: 'previewFile',
  460. };
  461. const CustomRenderFileDemo = () => {
  462. const render = file => {
  463. return <div style={{ padding: 8, width: 80, height: 32 }}>{file.name}</div>;
  464. };
  465. return (
  466. <>
  467. <h5>renderFileItem demo:</h5>
  468. <Upload {...commonProps} action={action} renderFileItem={render}>
  469. <Button icon={<IconUpload />} theme="light">
  470. 点击上传
  471. </Button>
  472. </Upload>
  473. <h5>itemWidth demo:</h5>
  474. <Upload
  475. {...commonProps}
  476. action={action}
  477. itemStyle={{ width: 150 }}
  478. style={{ width: 500 }}
  479. multiple
  480. >
  481. <Button icon={<IconUpload />} theme="light">
  482. 点击上传
  483. </Button>
  484. </Upload>
  485. </>
  486. );
  487. };
  488. export const RenderFileItem = () => <CustomRenderFileDemo />;
  489. RenderFileItem.story = {
  490. name: 'renderFileItem',
  491. };
  492. export const CustomLimit = () => (
  493. <>
  494. <Upload
  495. {...commonProps}
  496. action={action}
  497. limit={1}
  498. uploadTrigger="custom"
  499. onExceed={() => Toast.error('超出限定数量')}
  500. >
  501. <Button icon={<IconUpload />} theme="light">
  502. 点击上传, uploadtrigger = custom
  503. </Button>
  504. </Upload>
  505. <Upload {...commonProps} action={action} limit={1} onExceed={() => Toast.error('超出限定数量')}>
  506. <Button icon={<IconUpload />} theme="light">
  507. 点击上传1
  508. </Button>
  509. </Upload>
  510. <Upload {...commonProps} action={action} limit={2} onExceed={() => Toast.error('超出限定数量')}>
  511. <Button icon={<IconUpload />} theme="light">
  512. 点击上传2
  513. </Button>
  514. </Upload>
  515. </>
  516. );
  517. CustomLimit.story = {
  518. name: 'custom limit',
  519. };
  520. export const Draggable = () => (
  521. <>
  522. <Upload
  523. {...commonProps}
  524. draggable={true}
  525. // disabled
  526. accept="application/image/*,.md"
  527. dragMainText={'点击上传文件或拖拽文件到这里'}
  528. dragSubText="支持的文件类型:.jpg、.pdf"
  529. ></Upload>
  530. <Upload
  531. {...commonProps}
  532. style={{ marginTop: 10, height: 300 }}
  533. // directory
  534. draggable={true}
  535. dragMainText={'点击上传文件或拖拽文件到这里'}
  536. ></Upload>
  537. <Upload style={{ marginTop: 10, height: 300 }} action={action} draggable={true}>
  538. <div>
  539. <IconUpload size="extra-large" />
  540. 自定义拖拽取内容及结构
  541. </div>
  542. </Upload>
  543. </>
  544. );
  545. Draggable.story = {
  546. name: 'draggable',
  547. };
  548. export const _FileCard = () => (
  549. <>
  550. <FileCard
  551. name="semi"
  552. suffix="jpg"
  553. percent={50}
  554. showRetry
  555. onRetry={v => console.log(v)}
  556. ></FileCard>
  557. <FileCard
  558. name="invalid"
  559. suffix="jpg"
  560. size={12321}
  561. validateStatus="error"
  562. showRetry
  563. onRetry={v => console.log(v)}
  564. ></FileCard>
  565. <FileCard name="semi.jpg" percent={50} size={10022}></FileCard>
  566. </>
  567. );
  568. _FileCard.story = {
  569. name: 'FileCard',
  570. };
  571. class ValidateDemo extends React.Component {
  572. constructor(props) {
  573. super(props);
  574. this.state = {};
  575. this.beforeUpload = this.beforeUpload.bind(this);
  576. this.afterUpload = this.afterUpload.bind(this);
  577. this.transformFile = this.transformFile.bind(this);
  578. this.count = 0;
  579. }
  580. transformFile(fileInstance) {
  581. if (this.count === 0) {
  582. let newFile = new File([fileInstance], 'newFileName', { type: 'image/png' });
  583. return newFile;
  584. } else {
  585. return fileInstance;
  586. }
  587. }
  588. afterUpload({ response, file }) {
  589. // 可以根据业务接口返回,决定当次上传是否成功
  590. if (response.status_code == 200) {
  591. return {
  592. autoRemove: false,
  593. status: 'uploadFail',
  594. validateMessage: '内容不合法',
  595. name: 'RenameByServer.jpg',
  596. };
  597. } else {
  598. return {};
  599. }
  600. }
  601. beforeUpload({ file, fileList }) {
  602. let result;
  603. if (this.count > 1) {
  604. result = {
  605. autoRemove: false,
  606. fileInstance: file.fileInstance,
  607. shouldUpload: true,
  608. };
  609. } else {
  610. result = {
  611. autoRemove: false,
  612. fileInstance: file.fileInstance,
  613. status: 'validateFail',
  614. shouldUpload: false,
  615. };
  616. }
  617. this.count = this.count + 1;
  618. return result;
  619. }
  620. render() {
  621. return (
  622. <Upload
  623. {...commonProps}
  624. action={action}
  625. transformFile={this.transformFile}
  626. // beforeUpload={this.beforeUpload}
  627. afterUpload={this.afterUpload}
  628. >
  629. <Button icon={<IconUpload />} theme="light">
  630. 点击上传
  631. </Button>
  632. </Upload>
  633. );
  634. }
  635. }
  636. export const TransformFileBeforeUploadAfterUpload = () => <ValidateDemo />;
  637. TransformFileBeforeUploadAfterUpload.story = {
  638. name: 'transformFile beforeUpload afterUpload',
  639. };
  640. class AsyncBeforeUploadDemo extends React.Component {
  641. constructor(props) {
  642. super(props);
  643. this.state = {};
  644. this.beforeUpload = this.beforeUpload.bind(this);
  645. this.count = 0;
  646. }
  647. beforeUpload({ file, fileList }) {
  648. let result;
  649. return new Promise((reslove, reject) => {
  650. if (this.count > 1) {
  651. result = {
  652. autoRemove: false,
  653. shouldUpload: true,
  654. };
  655. this.count = this.count + 1;
  656. reslove(result);
  657. } else {
  658. result = {
  659. autoRemove: false,
  660. fileInstance: file.fileInstance,
  661. status: 'validateFail',
  662. shouldUpload: false,
  663. validateMessage: `第${this.count + 1}个注定失败`,
  664. };
  665. this.count = this.count + 1;
  666. reject(result);
  667. }
  668. });
  669. }
  670. render() {
  671. return (
  672. <Upload {...commonProps} action={action} beforeUpload={this.beforeUpload}>
  673. <Button icon={<IconUpload />} theme="light">
  674. 点击上传
  675. </Button>
  676. </Upload>
  677. );
  678. }
  679. }
  680. export const AsyncBeforeUpload = () => <AsyncBeforeUploadDemo />;
  681. AsyncBeforeUpload.story = {
  682. name: 'async beforeUpload',
  683. };
  684. class ValidateMessage extends React.Component {
  685. constructor(props) {
  686. super(props);
  687. this.state = {};
  688. // this.transformFile = this.transformFile.bind(this);
  689. }
  690. render() {
  691. return (
  692. <Upload {...commonProps} action={action}>
  693. <Button icon={<IconUpload />} theme="light">
  694. 点击上传
  695. </Button>
  696. </Upload>
  697. );
  698. }
  699. }
  700. export const UploadValidateMessageValidateStatus = () => <ValidateMessage />;
  701. UploadValidateMessageValidateStatus.story = {
  702. name: 'Upload validateMessage validateStatus',
  703. };
  704. class FormUploadDemo extends React.Component {
  705. constructor(props) {
  706. super(props);
  707. this.state = {};
  708. // this.transformFile = this.transformFile.bind(this);
  709. }
  710. render() {
  711. return (
  712. <Form labelPosition="left">
  713. {({ formState }) => (
  714. <>
  715. <Form.Upload
  716. {...commonProps}
  717. action={action}
  718. field="files"
  719. label="认证图片上传"
  720. extraText="请上传符合规格的图片"
  721. name="testName"
  722. uploadTrigger="custom"
  723. onChange={({ fileList, currentFile }) => {
  724. console.log(fileList);
  725. }}
  726. >
  727. <Button icon={<IconUpload />} theme="light">
  728. 点击上传
  729. </Button>
  730. </Form.Upload>
  731. <code>{JSON.stringify(formState)}</code>
  732. </>
  733. )}
  734. </Form>
  735. );
  736. }
  737. }
  738. export const FormUpload = () => <FormUploadDemo />;
  739. FormUpload.story = {
  740. name: 'Form.Upload',
  741. };
  742. class CustomRequestDemo extends React.Component {
  743. constructor(props) {
  744. super(props);
  745. this.state = {};
  746. // this.transformFile = this.transformFile.bind(this);
  747. // this.tosClient =
  748. }
  749. render() {
  750. return (
  751. <Form labelPosition="left">
  752. {({ formState }) => (
  753. <>
  754. <Form.Upload
  755. {...commonProps}
  756. action={action}
  757. customRequest={({ onProgress, onSuccess, onError }) => {
  758. let count = 0;
  759. onError();
  760. // let interval = setInterval(() => {
  761. // if (count === 100) {
  762. // clearInterval(interval);
  763. // onSuccess();
  764. // }
  765. // onProgress({ total: 100, loaded: count });
  766. // count += 50;
  767. // }, 1000);
  768. }}
  769. field="files"
  770. label="认证图片上传"
  771. extraText="请上传符合规格的图片"
  772. name="testName"
  773. >
  774. <Button icon={<IconUpload />} theme="light">
  775. 点击上传
  776. </Button>
  777. </Form.Upload>
  778. </>
  779. )}
  780. </Form>
  781. );
  782. }
  783. }
  784. export const FormCustomRequest = () => <CustomRequestDemo />;
  785. FormCustomRequest.story = {
  786. name: 'Form.CustomRequest',
  787. };
  788. class AutoReplace extends React.Component {
  789. constructor(props) {
  790. super(props);
  791. this.state = {};
  792. }
  793. render() {
  794. return (
  795. <Upload {...commonProps} action={action} limit={1} name="testName">
  796. <Button icon={<IconUpload />} theme="light">
  797. 点击上传
  798. </Button>
  799. </Upload>
  800. );
  801. }
  802. }
  803. export const AutoReplaceLimit1 = () => <AutoReplace />;
  804. AutoReplaceLimit1.story = {
  805. name: 'AutoReplace limit=1',
  806. };
  807. function DirectoryUpload() {
  808. return (
  809. <>
  810. <Upload {...commonProps} directory>
  811. <Button icon={<IconUpload />} theme="light">
  812. 点击上传文件夹
  813. </Button>
  814. </Upload>
  815. <Upload {...commonProps} directory accept="image/png">
  816. <Button icon={<IconUpload />} theme="light">
  817. 点击上传文件夹并指定图片格式
  818. </Button>
  819. </Upload>
  820. <Upload
  821. {...commonProps}
  822. style={{ marginTop: 10, height: 300 }}
  823. accept="image/png"
  824. draggable={true}
  825. dragMainText={'拖拽文件夹上传'}
  826. onDrop={console.log}
  827. directory
  828. ></Upload>
  829. </>
  830. );
  831. }
  832. export const _DirectoryUpload = () => <DirectoryUpload />;
  833. _DirectoryUpload.story = {
  834. name: 'directory upload',
  835. };
  836. function ForbiddenRemove() {
  837. const [fileList, $fileList] = useState(defaultFileList);
  838. return (
  839. <>
  840. <Upload
  841. fileList={fileList}
  842. onChange={f => {
  843. $fileList(f.fileList);
  844. }}
  845. beforeRemove={(file, currentList) => currentList.length > 2}
  846. beforeClear={list => list.length > 2}
  847. >
  848. <Button icon={<IconUpload />} theme="light">
  849. 点击上传
  850. </Button>
  851. </Upload>
  852. </>
  853. );
  854. }
  855. export const _ForbiddenRemove = () => <ForbiddenRemove />;
  856. _ForbiddenRemove.story = {
  857. name: 'forbidden remove',
  858. };
  859. export const CustomListOperation = () => {
  860. const renderFileOperation = (fileItem)=>{
  861. return <div style={{display: 'flex',columnGap: 8, padding: '0 8px'}}>
  862. <Button icon={<IconEyeOpened></IconEyeOpened>} type="tertiary" theme="borderless" size="small"></Button>
  863. <Button icon={<IconDownload></IconDownload>} type="tertiary" theme="borderless" size="small"></Button>
  864. <Button onClick={e=>fileItem.onRemove()} icon={<IconDelete></IconDelete>} type="tertiary" theme="borderless" size="small"></Button>
  865. </div>
  866. }
  867. return <Upload defaultFileList={defaultFileList} itemStyle={{width: 300}} renderFileOperation={renderFileOperation}><Button icon={<IconUpload />} theme="light">点击上传</Button></Upload>
  868. }
  869. CustomListOperation.story = {
  870. name: 'custom list operation',
  871. }
  872. export const TestReplaceFunc = () => (
  873. <>
  874. <Upload
  875. {...commonProps}
  876. action={action}
  877. accept=".md,image/*,video/*"
  878. maxSize={mb1}
  879. minSize={0}
  880. transformFile={(fileInstance)=>{return fileInstance;}}
  881. >
  882. <Button icon={<IconUpload />} theme="light">
  883. 点击上传(最小0kB,最大1MB)
  884. </Button>
  885. </Upload>
  886. <Upload
  887. {...commonProps}
  888. action={action}
  889. accept="image/*"
  890. maxSize={mb1}
  891. minSize={0}
  892. transformFile={(fileInstance)=>{return fileInstance;}}
  893. >
  894. <Button icon={<IconUpload />} theme="light">
  895. 只接受image点击上传(最小0kB,最大1MB)
  896. </Button>
  897. </Upload>
  898. <Upload
  899. {...commonProps}
  900. action={action}
  901. accept=".md,image/*,video/*"
  902. maxSize={mb1}
  903. minSize={kb2}
  904. transformFile={(fileInstance)=>{return fileInstance;}}
  905. >
  906. <Button icon={<IconUpload />} theme="light">
  907. 点击上传(最小200kB,最大1MB)
  908. </Button>
  909. </Upload>
  910. </>
  911. );
  912. TestReplaceFunc.story = {
  913. name: 'test replace func',
  914. };
  915. class InsertUpload extends React.Component {
  916. constructor() {
  917. super();
  918. this.onFileChange = this.onFileChange.bind(this);
  919. this.insert1 = this.insert1.bind(this);
  920. this.insert2 = this.insert2.bind(this);
  921. this.insert3 = this.insert3.bind(this);
  922. this.uploadRef1 = React.createRef();
  923. this.uploadRef2 = React.createRef();
  924. this.uploadRef3 = React.createRef();
  925. this.file = null;
  926. }
  927. onFileChange(file) {
  928. delete file[0].uid;
  929. this.file = file;
  930. }
  931. insert1() {
  932. // test file number limit
  933. this.uploadRef1.current.insert(this.file, 0);
  934. }
  935. insert2() {
  936. this.uploadRef2.current.insert(this.file, 0);
  937. }
  938. insert3() {
  939. // test size limit
  940. this.uploadRef3.current.insert(this.file, 0);
  941. }
  942. render() {
  943. let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
  944. return (
  945. <div>
  946. <Upload
  947. action={action}
  948. ref={this.uploadRef1}
  949. accept=".md,image/*,video/*"
  950. onSuccess={(...v) => console.log(...v)}
  951. onError={(...v) => console.log(...v)}
  952. onFileChange={this.onFileChange}
  953. maxSize={mb1}
  954. minSize={0}
  955. limit={1}
  956. transformFile={(fileInstance)=>{return fileInstance;}}
  957. >
  958. <Button icon={<IconPlus />} theme="light" style={{ marginRight: 8 }}>
  959. 选择文件 limit 1
  960. </Button>
  961. </Upload>
  962. <Upload
  963. action={action}
  964. ref={this.uploadRef2}
  965. accept=".md,image/*,video/*"
  966. onSuccess={(...v) => console.log(...v)}
  967. onError={(...v) => console.log(...v)}
  968. onFileChange={this.onFileChange}
  969. maxSize={mb1}
  970. minSize={0}
  971. limit={2}
  972. transformFile={(fileInstance)=>{return fileInstance;}}
  973. >
  974. <Button icon={<IconPlus />} theme="light" style={{ marginRight: 8 }}>
  975. 选择文件 limit 2
  976. </Button>
  977. </Upload>
  978. <Upload
  979. {...commonProps}
  980. action={action}
  981. ref={this.uploadRef3}
  982. accept=".md,image/*,video/*"
  983. onSuccess={(...v) => console.log(...v)}
  984. onError={(...v) => console.log(...v)}
  985. onFileChange={this.onFileChange}
  986. maxSize={mb1}
  987. minSize={kb2}
  988. limit={1}
  989. transformFile={(fileInstance)=>{return fileInstance;}}
  990. >
  991. <Button icon={<IconPlus />} theme="light" style={{ marginRight: 8 }}>
  992. 选择文件 size 限制
  993. </Button>
  994. </Upload>
  995. <Button icon={<IconUpload />} theme="light" onClick={this.insert1}>
  996. 插入首项上传1
  997. </Button>
  998. <Button icon={<IconUpload />} theme="light" onClick={this.insert2}>
  999. 插入首项上传2
  1000. </Button>
  1001. <Button icon={<IconUpload />} theme="light" onClick={this.insert3}>
  1002. 插入首项上传3
  1003. </Button>
  1004. </div>
  1005. );
  1006. }
  1007. }
  1008. export const Insert = () => <InsertUpload></InsertUpload>;
  1009. Insert.story = {
  1010. name: 'insert',
  1011. };