Skip to content
This repository has been archived by the owner on Apr 28, 2018. It is now read-only.

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
My first version wasn't usable.
I've rewritten the code to be able to make use of the function.
  • Loading branch information
danieleloscozzese committed Apr 28, 2017
1 parent 5b8f3c6 commit ffb9281
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
31 changes: 24 additions & 7 deletions lib/jack.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
"use strict";
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.preventSubmission = preventSubmission;
function preventSubmission(submission, callback) {
submission.preventDefault();
if (callback) {
callback();
exports.default = preventFormSubmission;


/**
* Sets up a form with an event listener on the submit event that prevents
* submission.
*/
function preventFormSubmission(formId, callback) {
var form = document.getElementById(formId);

if (form === null) {
throw new Error('The form with id ' + formId + ' was not found on the page.');
} else {
return;
var submitHandler = function submitHandler(submission) {
submission.preventDefault();

if (callback) {
callback();
} else {
return;
}
};

form.addEventListener('submit', submitHandler, { capture: true, passive: false, once: false });
}
}
25 changes: 20 additions & 5 deletions lib/jack.js.flow
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// @flow

export function preventSubmission(submission: Event, callback?: () => void): void {
submission.preventDefault();
if (callback) {
callback();
/**
* Sets up a form with an event listener on the submit event that prevents
* submission.
*/
export default function preventFormSubmission(formId: string, callback?: () => void): void {
const form = document.getElementById(formId);

if (form === null) {
throw new Error(`The form with id ${formId} was not found on the page.`);
} else {
return;
const submitHandler = function(submission: Event) {
submission.preventDefault();

if (callback) {
callback();
} else {
return;
}
}

form.addEventListener('submit', submitHandler, {capture: true, passive: false, once: false });
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "customary",
"version": "1.0.2",
"version": "2.0.0",
"description": "Prevents form submission after native validation has taken place.",
"main": "lib/jack.js",
"scripts": {
Expand Down
25 changes: 20 additions & 5 deletions src/jack.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// @flow

export function preventSubmission(submission: Event, callback?: () => void): void {
submission.preventDefault();
if (callback) {
callback();
/**
* Sets up a form with an event listener on the submit event that prevents
* submission.
*/
export default function preventFormSubmission(formId: string, callback?: () => void): void {
const form = document.getElementById(formId);

if (form === null) {
throw new Error(`The form with id ${formId} was not found on the page.`);
} else {
return;
const submitHandler = function(submission: Event) {
submission.preventDefault();

if (callback) {
callback();
} else {
return;
}
}

form.addEventListener('submit', submitHandler, {capture: true, passive: false, once: false });
}
}

0 comments on commit ffb9281

Please sign in to comment.