Skip to content

Commit

Permalink
fix: attach onScan only inside the ScanInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan Bansal committed Sep 13, 2024
1 parent 37d2bf3 commit 3dbe5cd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
15 changes: 5 additions & 10 deletions beam/src/components/ScanInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { inject, onMounted, onUnmounted } from 'vue'
import onScan from 'onscan.js'
import { onMounted, onUnmounted } from 'vue'
const props = defineProps<{
handler: (barcode: string, qty: number) => void
scanHandler: (barcode: string, qty: number) => void
}>()
const onScan = inject<any>('onScan')
onMounted(() => {
onScan?.setOptions(window, {
onScan: (barcode: string, qty: number) => {
props.handler(barcode, qty)
},
})
onScan.attachTo(window, { onScan: props.scanHandler })
})
onUnmounted(() => {
onScan?.detachFrom(window)
onScan.detachFrom(window)
})
</script>
9 changes: 0 additions & 9 deletions beam/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */

import onScan from 'onscan.js'
import { App } from 'vue'

import ActionFooter from '@/components/ActionFooter.vue'
Expand All @@ -23,14 +22,6 @@ import 'themes/beam.css'
* @public
*/
function install(app: App /* options */) {
if (onScan.isAttachedTo(window)) {
// there isn't an easy way to fetch the existing attached instance, so re-attach
onScan.detachFrom(window)
}

const scanHandler = onScan.attachTo(window)
app.provide('onScan', scanHandler)

app.component('ActionFooter', ActionFooter)
app.component('BeamModal', BeamModal)
app.component('BeamModalOutlet', BeamModalOutlet)
Expand Down
2 changes: 1 addition & 1 deletion examples/beam/default.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ListView :items="items" @scrollbottom="loadMoreItems" />
<ActionFooter @click="handlePrimaryAction">Done</ActionFooter>
<ScanInput :handler="incrementItemCount" />
<ScanInput :scanHandler="incrementItemCount" />
<BeamModalOutlet @confirmmodal="confirmModal" @closemodal="closeModal"></BeamModalOutlet>
</Variant>
</Story>
Expand Down

0 comments on commit 3dbe5cd

Please sign in to comment.