upload.stories.jsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* argus-disable unPkgSensitiveInfo */
  2. import React, { useState } from 'react';
  3. import FileCard from '../fileCard';
  4. import { Button, Upload, Toast, Tag } 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. };
  1012. class ClickToOpenUpload extends React.Component {
  1013. constructor() {
  1014. super();
  1015. this.onClick = this.onClick.bind(this);
  1016. this.uploadRef = React.createRef();
  1017. }
  1018. onClick() {
  1019. // open file select dialog
  1020. this.uploadRef.current.openFileDialog();
  1021. }
  1022. render() {
  1023. let action = 'https://run.mocky.io/v3/d6ac5c9e-4d39-4309-a747-7ed3b5694859';
  1024. return (
  1025. <div>
  1026. <Button icon={<IconUpload />} theme="light" onClick={this.onClick}>
  1027. 点我打开文件选择窗口
  1028. </Button>
  1029. <div style={{ marginTop: 50 }}>
  1030. <Tag color="blue" type="light">文件列表</Tag>
  1031. <Upload
  1032. {...commonProps}
  1033. action={action}
  1034. ref={this.uploadRef}
  1035. onSuccess={(...v) => console.log(...v)}
  1036. onError={(...v) => console.log(...v)}
  1037. >
  1038. </Upload>
  1039. </div>
  1040. </div>
  1041. );
  1042. }
  1043. }
  1044. export const ClickToOpenUploadDemo = () => <ClickToOpenUpload></ClickToOpenUpload>;
  1045. ClickToOpenUploadDemo.story = {
  1046. name: 'click to open upload demo',
  1047. };