AddFiles.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const { localIcon } = require('./icons')
  2. const { h, Component } = require('preact')
  3. class AddFiles extends Component {
  4. constructor (props) {
  5. super(props)
  6. this.triggerFileInputClick = this.triggerFileInputClick.bind(this)
  7. this.handleFileInputChange = this.handleFileInputChange.bind(this)
  8. this.renderPoweredByUppy = this.renderPoweredByUppy.bind(this)
  9. this.renderHiddenFileInput = this.renderHiddenFileInput.bind(this)
  10. this.renderDropPasteBrowseTagline = this.renderDropPasteBrowseTagline.bind(this)
  11. this.renderMyDeviceAcquirer = this.renderMyDeviceAcquirer.bind(this)
  12. this.renderAcquirer = this.renderAcquirer.bind(this)
  13. }
  14. triggerFileInputClick () {
  15. this.fileInput.click()
  16. }
  17. handleFileInputChange (event) {
  18. this.props.handleInputChange(event)
  19. // We clear the input after a file is selected, because otherwise
  20. // change event is not fired in Chrome and Safari when a file
  21. // with the same name is selected.
  22. // ___Why not use value="" on <input/> instead?
  23. // Because if we use that method of clearing the input,
  24. // Chrome will not trigger change if we drop the same file twice (Issue #768).
  25. event.target.value = null
  26. }
  27. renderPoweredByUppy () {
  28. return (
  29. <a
  30. tabindex="-1"
  31. href="https://uppy.io"
  32. rel="noreferrer noopener"
  33. target="_blank"
  34. class="uppy-Dashboard-poweredBy">
  35. {this.props.i18n('poweredBy') + ' '}
  36. <svg aria-hidden="true" class="UppyIcon uppy-Dashboard-poweredByIcon" width="11" height="11" viewBox="0 0 11 11">
  37. <path d="M7.365 10.5l-.01-4.045h2.612L5.5.806l-4.467 5.65h2.604l.01 4.044h3.718z" fill-rule="evenodd" />
  38. </svg>
  39. <span class="uppy-Dashboard-poweredByUppy">Uppy</span>
  40. </a>
  41. )
  42. }
  43. renderHiddenFileInput () {
  44. return (
  45. <input class="uppy-Dashboard-input"
  46. hidden
  47. aria-hidden="true"
  48. tabindex={-1}
  49. type="file"
  50. name="files[]"
  51. multiple={this.props.maxNumberOfFiles !== 1}
  52. onchange={this.handleFileInputChange}
  53. accept={this.props.allowedFileTypes}
  54. ref={(ref) => { this.fileInput = ref }} />
  55. )
  56. }
  57. renderDropPasteBrowseTagline () {
  58. const browse =
  59. <button type="button"
  60. class="uppy-Dashboard-browse"
  61. onclick={this.triggerFileInputClick}>
  62. {this.props.i18n('browse')}
  63. </button>
  64. return (
  65. <div class="uppy-Dashboard-dropFilesTitle">
  66. {this.props.acquirers.length === 0
  67. ? this.props.i18nArray('dropPaste', { browse })
  68. : this.props.i18nArray('dropPasteImport', { browse })
  69. }
  70. </div>
  71. )
  72. }
  73. renderMyDeviceAcquirer () {
  74. return (
  75. <div class="uppy-DashboardTab" role="presentation">
  76. <button type="button"
  77. class="uppy-DashboardTab-btn"
  78. role="tab"
  79. tabindex={0}
  80. onclick={this.triggerFileInputClick}>
  81. {localIcon()}
  82. <div class="uppy-DashboardTab-name">{this.props.i18n('myDevice')}</div>
  83. </button>
  84. </div>
  85. )
  86. }
  87. renderAcquirer (acquirer) {
  88. return (
  89. <div class="uppy-DashboardTab" role="presentation">
  90. <button type="button"
  91. class="uppy-DashboardTab-btn"
  92. role="tab"
  93. tabindex={0}
  94. aria-controls={`uppy-DashboardContent-panel--${acquirer.id}`}
  95. aria-selected={this.props.activePickerPanel.id === acquirer.id}
  96. onclick={() => this.props.showPanel(acquirer.id)}>
  97. {acquirer.icon()}
  98. <div class="uppy-DashboardTab-name">{acquirer.name}</div>
  99. </button>
  100. </div>
  101. )
  102. }
  103. render () {
  104. return (
  105. <div class="uppy-DashboardAddFiles">
  106. {this.renderHiddenFileInput()}
  107. <div class="uppy-DashboardTabs">
  108. {this.renderDropPasteBrowseTagline()}
  109. {
  110. this.props.acquirers.length > 0 &&
  111. <div class="uppy-DashboardTabs-list" role="tablist">
  112. {this.renderMyDeviceAcquirer()}
  113. {this.props.acquirers.map((acquirer) =>
  114. this.renderAcquirer(acquirer)
  115. )}
  116. </div>
  117. }
  118. </div>
  119. <div class="uppy-DashboardAddFiles-info">
  120. { this.props.note && <div class="uppy-Dashboard-note">{this.props.note}</div> }
  121. { this.props.proudlyDisplayPoweredByUppy && this.renderPoweredByUppy(this.props) }
  122. </div>
  123. </div>
  124. )
  125. }
  126. }
  127. module.exports = AddFiles