index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const { Plugin } = require('@uppy/core')
  2. const toArray = require('@uppy/utils/lib/toArray')
  3. const Translator = require('@uppy/utils/lib/Translator')
  4. const { h } = require('preact')
  5. module.exports = class FileInput extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.id = this.opts.id || 'FileInput'
  9. this.title = 'File Input'
  10. this.type = 'acquirer'
  11. this.defaultLocale = {
  12. strings: {
  13. chooseFiles: 'Choose files'
  14. }
  15. }
  16. // Default options
  17. const defaultOptions = {
  18. target: null,
  19. pretty: true,
  20. inputName: 'files[]'
  21. }
  22. // Merge default options with the ones set by user
  23. this.opts = Object.assign({}, defaultOptions, opts)
  24. // i18n
  25. this.translator = new Translator([ this.defaultLocale, this.uppy.locale, this.opts.locale ])
  26. this.i18n = this.translator.translate.bind(this.translator)
  27. this.i18nArray = this.translator.translateArray.bind(this.translator)
  28. this.render = this.render.bind(this)
  29. this.handleInputChange = this.handleInputChange.bind(this)
  30. this.handleClick = this.handleClick.bind(this)
  31. }
  32. handleInputChange (ev) {
  33. this.uppy.log('[FileInput] Something selected through input...')
  34. const files = toArray(ev.target.files)
  35. files.forEach((file) => {
  36. try {
  37. this.uppy.addFile({
  38. source: this.id,
  39. name: file.name,
  40. type: file.type,
  41. data: file
  42. })
  43. } catch (err) {
  44. // Nothing, restriction errors handled in Core
  45. }
  46. })
  47. }
  48. handleClick (ev) {
  49. this.input.click()
  50. }
  51. render (state) {
  52. /* http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/ */
  53. const hiddenInputStyle = {
  54. width: '0.1px',
  55. height: '0.1px',
  56. opacity: 0,
  57. overflow: 'hidden',
  58. position: 'absolute',
  59. zIndex: -1
  60. }
  61. const restrictions = this.uppy.opts.restrictions
  62. const accept = restrictions.allowedFileTypes ? restrictions.allowedFileTypes.join(',') : null
  63. // empty value="" on file input, so that the input is cleared after a file is selected,
  64. // because Uppy will be handling the upload and so we can select same file
  65. // after removing — otherwise browser thinks it’s already selected
  66. return <div class="uppy-Root uppy-FileInput-container">
  67. <input class="uppy-FileInput-input"
  68. style={this.opts.pretty && hiddenInputStyle}
  69. type="file"
  70. name={this.opts.inputName}
  71. onchange={this.handleInputChange}
  72. multiple={restrictions.maxNumberOfFiles !== 1}
  73. accept={accept}
  74. ref={(input) => { this.input = input }}
  75. value="" />
  76. {this.opts.pretty &&
  77. <button class="uppy-FileInput-btn" type="button" onclick={this.handleClick}>
  78. {this.i18n('chooseFiles')}
  79. </button>
  80. }
  81. </div>
  82. }
  83. install () {
  84. const target = this.opts.target
  85. if (target) {
  86. this.mount(target, this)
  87. }
  88. }
  89. uninstall () {
  90. this.unmount()
  91. }
  92. }