Просмотр исходного кода

don’t use onload, because fileCard is actually always in DOM

@goto-bus-stop could you help a bit here? I’m confused as to how to
remove an event listener when FileCard is closed, given that data
needed for that event listener lives in closure. Now it seems to me
that every time FileCard is open, a new listener is added
Artur Paikin 7 лет назад
Родитель
Сommit
2f0d1b8581
1 измененных файлов с 9 добавлено и 4 удалено
  1. 9 4
      src/plugins/Dashboard/FileCard.js

+ 9 - 4
src/plugins/Dashboard/FileCard.js

@@ -8,14 +8,19 @@ const meta = {}
 
 
 module.exports = function fileCard (props) {
 module.exports = function fileCard (props) {
   file = props.fileCardFor ? props.files[props.fileCardFor] : false
   file = props.fileCardFor ? props.files[props.fileCardFor] : false
-  // const meta = {}
 
 
   const handleEnterKey = (ev) => {
   const handleEnterKey = (ev) => {
-    if (event.keyCode === 13) {
+    if (file && event.keyCode === 13) {
       props.done(meta, file.id)
       props.done(meta, file.id)
     }
     }
   }
   }
 
 
+  if (file) {
+    document.body.addEventListener('keyup', handleEnterKey)
+  } else {
+    document.body.removeEventListener('keyup', handleEnterKey)
+  }
+
   const tempStoreMeta = (ev) => {
   const tempStoreMeta = (ev) => {
     const value = ev.target.value
     const value = ev.target.value
     const name = ev.target.dataset.name
     const name = ev.target.dataset.name
@@ -73,8 +78,8 @@ module.exports = function fileCard (props) {
   </div>`
   </div>`
 
 
   return onload(html`<div>${fileCardEl()}</div>`,
   return onload(html`<div>${fileCardEl()}</div>`,
-    () => document.body.addEventListener('keyup', handleEnterKey),
-    () => document.body.removeEventListener('keyup', handleEnterKey),
+    null,
+    null,
     `uppy${file.id}`
     `uppy${file.id}`
   )
   )
 }
 }