upload.stories.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bf8647bffab13c38772c9ff94bf91a9d.jpg',
  243. },
  244. {
  245. uid: '2',
  246. name: 'jiafang2.jpeg',
  247. status: 'uploadFail',
  248. size: '222kb',
  249. url:
  250. 'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bf8647bffab13c38772c9ff94bf91a9d.jpg',
  251. },
  252. {
  253. uid: '3',
  254. name: 'jiafang3.jpeg',
  255. status: 'uploading',
  256. percent: 50,
  257. size: '222kb',
  258. url:
  259. 'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bf8647bffab13c38772c9ff94bf91a9d.jpg',
  260. },
  261. {
  262. uid: '4',
  263. name: 'jiafang3.jpeg',
  264. status: 'validateFail',
  265. validateMessage: '文件过大',
  266. size: '222kb',
  267. url:
  268. 'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bf8647bffab13c38772c9ff94bf91a9d.jpg',
  269. },
  270. {
  271. uid: '5',
  272. name: 'jiafang4.jpeg',
  273. status: 'validating',
  274. validateMessage: '校验中',
  275. size: '222kb',
  276. url:
  277. 'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bf8647bffab13c38772c9ff94bf91a9d.jpg',
  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 ControledUpload 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 = () => <ControledUpload></ControledUpload>;
  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. draggable={true}
  534. dragMainText={'点击上传文件或拖拽文件到这里'}
  535. ></Upload>
  536. <Upload style={{ marginTop: 10, height: 300 }} action={action} draggable={true}>
  537. <div>
  538. <IconUpload size="extra-large" />
  539. 自定义拖拽取内容及结构
  540. </div>
  541. </Upload>
  542. </>
  543. );
  544. Draggable.story = {
  545. name: 'draggable',
  546. };
  547. export const _FileCard = () => (
  548. <>
  549. <FileCard
  550. name="semi"
  551. suffix="jpg"
  552. percent={50}
  553. showRetry
  554. onRetry={v => console.log(v)}
  555. ></FileCard>
  556. <FileCard
  557. name="invalid"
  558. suffix="jpg"
  559. size={12321}
  560. validateStatus="error"
  561. showRetry
  562. onRetry={v => console.log(v)}
  563. ></FileCard>
  564. <FileCard name="semi.jpg" percent={50} size={10022}></FileCard>
  565. </>
  566. );
  567. _FileCard.story = {
  568. name: 'FileCard',
  569. };
  570. class ValidateDemo extends React.Component {
  571. constructor(props) {
  572. super(props);
  573. this.state = {};
  574. this.beforeUpload = this.beforeUpload.bind(this);
  575. this.afterUpload = this.afterUpload.bind(this);
  576. this.transformFile = this.transformFile.bind(this);
  577. this.count = 0;
  578. }
  579. transformFile(fileInstance) {
  580. if (this.count === 0) {
  581. let newFile = new File([fileInstance], 'newFileName', { type: 'image/png' });
  582. return newFile;
  583. } else {
  584. return fileInstance;
  585. }
  586. }
  587. afterUpload({ response, file }) {
  588. // 可以根据业务接口返回,决定当次上传是否成功
  589. if (response.status_code == 200) {
  590. return {
  591. autoRemove: false,
  592. status: 'uploadFail',
  593. validateMessage: '内容不合法',
  594. name: 'RenameByServer.jpg',
  595. };
  596. } else {
  597. return {};
  598. }
  599. }
  600. beforeUpload({ file, fileList }) {
  601. let result;
  602. if (this.count > 1) {
  603. result = {
  604. autoRemove: false,
  605. fileInstance: file.fileInstance,
  606. shouldUpload: true,
  607. };
  608. } else {
  609. result = {
  610. autoRemove: false,
  611. fileInstance: file.fileInstance,
  612. status: 'validateFail',
  613. shouldUpload: false,
  614. };
  615. }
  616. this.count = this.count + 1;
  617. return result;
  618. }
  619. render() {
  620. return (
  621. <Upload
  622. {...commonProps}
  623. action={action}
  624. transformFile={this.transformFile}
  625. // beforeUpload={this.beforeUpload}
  626. afterUpload={this.afterUpload}
  627. >
  628. <Button icon={<IconUpload />} theme="light">
  629. 点击上传
  630. </Button>
  631. </Upload>
  632. );
  633. }
  634. }
  635. export const TransformFileBeforeUploadAfterUpload = () => <ValidateDemo />;
  636. TransformFileBeforeUploadAfterUpload.story = {
  637. name: 'transformFile beforeUpload afterUpload',
  638. };
  639. class AsyncBeforeUploadDemo extends React.Component {
  640. constructor(props) {
  641. super(props);
  642. this.state = {};
  643. this.beforeUpload = this.beforeUpload.bind(this);
  644. this.count = 0;
  645. }
  646. beforeUpload({ file, fileList }) {
  647. let result;
  648. return new Promise((reslove, reject) => {
  649. if (this.count > 1) {
  650. result = {
  651. autoRemove: false,
  652. shouldUpload: true,
  653. };
  654. this.count = this.count + 1;
  655. reslove(result);
  656. } else {
  657. result = {
  658. autoRemove: false,
  659. fileInstance: file.fileInstance,
  660. status: 'validateFail',
  661. shouldUpload: false,
  662. validateMessage: `第${this.count + 1}个注定失败`,
  663. };
  664. this.count = this.count + 1;
  665. reject(result);
  666. }
  667. });
  668. }
  669. render() {
  670. return (
  671. <Upload {...commonProps} action={action} beforeUpload={this.beforeUpload}>
  672. <Button icon={<IconUpload />} theme="light">
  673. 点击上传
  674. </Button>
  675. </Upload>
  676. );
  677. }
  678. }
  679. export const AsyncBeforeUpload = () => <AsyncBeforeUploadDemo />;
  680. AsyncBeforeUpload.story = {
  681. name: 'async beforeUpload',
  682. };
  683. class ValidateMessage extends React.Component {
  684. constructor(props) {
  685. super(props);
  686. this.state = {};
  687. // this.transformFile = this.transformFile.bind(this);
  688. }
  689. render() {
  690. return (
  691. <Upload {...commonProps} action={action}>
  692. <Button icon={<IconUpload />} theme="light">
  693. 点击上传
  694. </Button>
  695. </Upload>
  696. );
  697. }
  698. }
  699. export const UploadValidateMessageValidateStatus = () => <ValidateMessage />;
  700. UploadValidateMessageValidateStatus.story = {
  701. name: 'Upload validateMessage validateStatus',
  702. };
  703. class FormUploadDemo extends React.Component {
  704. constructor(props) {
  705. super(props);
  706. this.state = {};
  707. // this.transformFile = this.transformFile.bind(this);
  708. }
  709. render() {
  710. return (
  711. <Form labelPosition="left">
  712. {({ formState }) => (
  713. <>
  714. <Form.Upload
  715. {...commonProps}
  716. action={action}
  717. field="files"
  718. label="认证图片上传"
  719. extraText="请上传符合规格的图片"
  720. name="testName"
  721. uploadTrigger="custom"
  722. onChange={({ fileList, currentFile }) => {
  723. console.log(fileList);
  724. }}
  725. >
  726. <Button icon={<IconUpload />} theme="light">
  727. 点击上传
  728. </Button>
  729. </Form.Upload>
  730. <code>{JSON.stringify(formState)}</code>
  731. </>
  732. )}
  733. </Form>
  734. );
  735. }
  736. }
  737. export const FormUpload = () => <FormUploadDemo />;
  738. FormUpload.story = {
  739. name: 'Form.Upload',
  740. };
  741. class CustomRequestDemo extends React.Component {
  742. constructor(props) {
  743. super(props);
  744. this.state = {};
  745. // this.transformFile = this.transformFile.bind(this);
  746. // this.tosClient =
  747. }
  748. render() {
  749. return (
  750. <Form labelPosition="left">
  751. {({ formState }) => (
  752. <>
  753. <Form.Upload
  754. {...commonProps}
  755. action={action}
  756. customRequest={({ onProgress, onSuccess, onError }) => {
  757. let count = 0;
  758. onError();
  759. // let interval = setInterval(() => {
  760. // if (count === 100) {
  761. // clearInterval(interval);
  762. // onSuccess();
  763. // }
  764. // onProgress({ total: 100, loaded: count });
  765. // count += 50;
  766. // }, 1000);
  767. }}
  768. field="files"
  769. label="认证图片上传"
  770. extraText="请上传符合规格的图片"
  771. name="testName"
  772. >
  773. <Button icon={<IconUpload />} theme="light">
  774. 点击上传
  775. </Button>
  776. </Form.Upload>
  777. </>
  778. )}
  779. </Form>
  780. );
  781. }
  782. }
  783. export const FormCustomRequest = () => <CustomRequestDemo />;
  784. FormCustomRequest.story = {
  785. name: 'Form.CustomRequest',
  786. };
  787. class AutoReplace extends React.Component {
  788. constructor(props) {
  789. super(props);
  790. this.state = {};
  791. }
  792. render() {
  793. return (
  794. <Upload {...commonProps} action={action} limit={1} name="testName">
  795. <Button icon={<IconUpload />} theme="light">
  796. 点击上传
  797. </Button>
  798. </Upload>
  799. );
  800. }
  801. }
  802. export const AutoReplaceLimit1 = () => <AutoReplace />;
  803. AutoReplaceLimit1.story = {
  804. name: 'AutoReplace limit=1',
  805. };
  806. function DirectoryUpload() {
  807. return (
  808. <>
  809. <Upload {...commonProps} directory>
  810. <Button icon={<IconUpload />} theme="light">
  811. 点击上传文件夹
  812. </Button>
  813. </Upload>
  814. <Upload {...commonProps} directory accept="image/png">
  815. <Button icon={<IconUpload />} theme="light">
  816. 点击上传文件夹并指定图片格式
  817. </Button>
  818. </Upload>
  819. <Upload
  820. {...commonProps}
  821. style={{ marginTop: 10, height: 300 }}
  822. accept="image/png"
  823. draggable={true}
  824. dragMainText={'拖拽文件夹上传'}
  825. onDrop={console.log}
  826. directory
  827. ></Upload>
  828. </>
  829. );
  830. }
  831. export const _DirectoryUpload = () => <DirectoryUpload />;
  832. _DirectoryUpload.story = {
  833. name: 'directory upload',
  834. };
  835. function ForbiddenRemove() {
  836. const [fileList, $fileList] = useState(defaultFileList);
  837. return (
  838. <>
  839. <Upload
  840. fileList={fileList}
  841. onChange={f => {
  842. $fileList(f.fileList);
  843. }}
  844. beforeRemove={(file, currentList) => currentList.length > 2}
  845. beforeClear={list => list.length > 2}
  846. >
  847. <Button icon={<IconUpload />} theme="light">
  848. 点击上传
  849. </Button>
  850. </Upload>
  851. </>
  852. );
  853. }
  854. export const _ForbiddenRemove = () => <ForbiddenRemove />;
  855. _ForbiddenRemove.story = {
  856. name: 'forbidden remove',
  857. };
  858. export const CustomListOperation = () => {
  859. const renderFileOperation = (fileItem)=>{
  860. return <div style={{display: 'flex',columnGap: 8, padding: '0 8px'}}>
  861. <Button icon={<IconEyeOpened></IconEyeOpened>} type="tertiary" theme="borderless" size="small"></Button>
  862. <Button icon={<IconDownload></IconDownload>} type="tertiary" theme="borderless" size="small"></Button>
  863. <Button onClick={e=>fileItem.onRemove()} icon={<IconDelete></IconDelete>} type="tertiary" theme="borderless" size="small"></Button>
  864. </div>
  865. }
  866. return <Upload defaultFileList={defaultFileList} itemStyle={{width: 300}} renderFileOperation={renderFileOperation}><Button icon={<IconUpload />} theme="light">点击上传</Button></Upload>
  867. }
  868. CustomListOperation.story = {
  869. name: 'custom list operation',
  870. }