Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ interface BarcodeScannerComposableExports {
stopListening: () => void;
}

/**
* Used as type for the listener configuration.
*/
interface ScannedBarcodeOptions {
timeout?: number;
isPreventDefault?: boolean;
}

/**
* Keyboard constant values.
*/
Expand All @@ -47,8 +55,9 @@ const keyboard = {
/**
* The event listener timeout configuration.
*/
const config = {
const config:ScannedBarcodeOptions = {
timeout: 100,
isPreventDefault: false,
};

/**
Expand All @@ -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<string>('');

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.
Expand Down Expand Up @@ -99,6 +108,9 @@ export default function useBarcodeDetector(): BarcodeScannerComposableExports {
onScanCallback(
createScannedBarcodeData(barcode.value),
);
if(config.isPreventDefault){
event.preventDefault()
}
}

barcode.value = '';
Expand Down