FilePicker文件选择器 - 图1

FilePicker 文件选择器

基本用法

  1. import { Cell, FilePicker, Icon } from 'zarm';
  2. class Demo extends React.Component {
  3. state = {
  4. files: [],
  5. };
  6. onSelect(file) {
  7. console.log(file);
  8. const { files } = this.state;
  9. files.push(file);
  10. this.setState({
  11. files,
  12. });
  13. }
  14. render() {
  15. return (
  16. <div>
  17. {this.state.files.map((item, index) => <Cell key={+index}>{item.fileName}</Cell>)}
  18. <div className="file-picker-wrapper">
  19. <FilePicker
  20. className="file-picker-btn"
  21. onChange={selected => this.onSelect(selected)}
  22. >
  23. <Icon type="add" size="lg" />
  24. </FilePicker>
  25. </div>
  26. </div>
  27. )
  28. }
  29. }
  30. ReactDOM.render(<Demo />, mountNode);

多选模式

  1. import { Cell, FilePicker, Icon, Toast, Badge } from 'zarm';
  2. const MAX_FILES_COUNT = 5;
  3. const onBeforeSelect = () => {
  4. alert('执行 onBeforeSelect 方法');
  5. }
  6. class Demo extends React.Component {
  7. state = {
  8. files: [],
  9. };
  10. onSelect(selFiles) {
  11. let { files } = this.state;
  12. files = files.concat(selFiles);
  13. if (files.length > MAX_FILES_COUNT) {
  14. Toast.show('最多只能选择5张图片')
  15. return;
  16. }
  17. this.setState({ files });
  18. }
  19. remove(index) {
  20. const { files } = this.state;
  21. files.splice(index, 1);
  22. this.setState({ files });
  23. Toast.show('删除成功');
  24. }
  25. imgRender() {
  26. const { files } = this.state;
  27. return files.map((item, index) => {
  28. return (
  29. <Badge
  30. key={+index}
  31. sup
  32. className="file-picker-item"
  33. shape="circle"
  34. text={<Icon type="wrong-round-fill" size="sm" theme="danger" />}
  35. onClick={() => this.remove(index)}
  36. >
  37. <div className="file-picker-item-img">
  38. <a href={item.thumbnail} target="_blank" rel="noopener noreferrer">
  39. <img src={item.thumbnail} alt="" />
  40. </a>
  41. </div>
  42. </Badge>
  43. );
  44. });
  45. }
  46. render() {
  47. return (
  48. <div>
  49. <div className="file-picker-wrapper">
  50. {this.imgRender()}
  51. {
  52. (this.state.files.length < MAX_FILES_COUNT) && (
  53. <FilePicker
  54. multiple
  55. className="file-picker-btn"
  56. accept="image/*"
  57. onBeforeSelect={onBeforeSelect}
  58. onChange={selected => this.onSelect(selected)}
  59. >
  60. <Icon type="add" size="lg" />
  61. </FilePicker>
  62. )
  63. }
  64. </div>
  65. </div>
  66. )
  67. }
  68. }
  69. ReactDOM.render(<Demo />, mountNode);

禁用状态

  1. import { FilePicker, Icon } from 'zarm';
  2. ReactDOM.render(
  3. <div className="file-picker-wrapper">
  4. <FilePicker className="file-picker-btn" disabled>
  5. <Icon type="add" size="lg" />
  6. </FilePicker>
  7. </div>
  8. , mountNode);

API

属性类型默认值说明
acceptstring-允许上传的附件格式
capturestring-唤起的原生应用,可选值:照相机camera, 摄像机camcorder, 录音microphone
multiplebooleanfalse是否多选
disabledbooleanfalse是否禁用
onBeforeSelect() => boolean() => true选择前触发的事件
onChange(file?: object | object[]) => void-值变化时触发的回调函数