diff --git a/package.json b/package.json index 441168d..d677051 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "build": "tsc", "lint": "eslint ./src --ext .js,.jsx,.ts,.tsx", + "test": "vitest", "prepare": "npm run build", "prepublishOnly": "npm test && npm run lint", "preversion": "npm run lint", diff --git a/src/index.ts b/src/index.ts index 3ea2162..5f0eb2d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,6 +33,14 @@ interface BarcodeScannerComposableExports { stopListening: () => void; } +/** + * Used as type for the listener configuration. + */ +interface ScannedBarcodeOptions { + timeout?: number; + isPreventDefault?: boolean; +} + /** * Keyboard constant values. */ @@ -47,8 +55,9 @@ const keyboard = { /** * The event listener timeout configuration. */ -const config = { +const config:ScannedBarcodeOptions = { timeout: 100, + isPreventDefault: false, }; /** @@ -58,13 +67,13 @@ const config = { * listen to these events and store and return the data that was read. Each character in a barcode value * is a separate event, followed by an Enter to indicate the end of the stream. */ -export default function useBarcodeDetector(): BarcodeScannerComposableExports { +export default function useBarcodeDetector(options: ScannedBarcodeOptions = {}): BarcodeScannerComposableExports { let barcodeScannerInterval: NodeJS.Timeout | null = null; let listeningActive: boolean = false; let onScanCallback: BarcodeScannerListenerCallback | undefined; const barcode = ref(''); - + config.isPreventDefault = options?.isPreventDefault ?? config.isPreventDefault /** * Acts as a factory method for creating a new barcode data object. * This object contains the value the barcode and a timestamp for when it was scanned. @@ -99,6 +108,9 @@ export default function useBarcodeDetector(): BarcodeScannerComposableExports { onScanCallback( createScannedBarcodeData(barcode.value), ); + if(config.isPreventDefault){ + event.preventDefault() + } } barcode.value = '';