Skip to content

Commit

Permalink
Fix event target (#391)
Browse files Browse the repository at this point in the history
* Fix event target
  • Loading branch information
roderickhsiao committed Aug 4, 2021
1 parent c0d3acb commit 5964d86
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-i13n",
"description": "React I13n provides a performant and scalable solution to application instrumentation.",
"version": "3.0.0-alpha.7",
"version": "3.0.0-alpha.8",
"main": "index.js",
"module": "index.es.js",
"repository": {
Expand Down
12 changes: 6 additions & 6 deletions src/libs/clickHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright 2015 - Present, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
const isLeftClickEvent = e => e.button === 0;
const isModifiedEvent = e => !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
const isLeftClickEvent = (e) => e.button === 0;
const isModifiedEvent = (e) => !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);

const getLinkTarget = (target, props) => props.target || (target?.target) || '_self';
const isNewWindow = (target, props) => getLinkTarget(target, props) === '_blank';

const isLink = target => target.tagName === 'A';
const isLink = (target) => target.tagName === 'A';
const isButtonLike = (target) => {
const { tagName, type } = target;
if (tagName === 'BUTTON') {
Expand Down Expand Up @@ -37,7 +37,7 @@ const isFormSubmit = (target) => {
// if it's a
// 1. button
// 2. input with submit or button type
if (isButtonLike(target)) {
if (isButtonLike(target) && target.type === 'submit') {
return true;
}
return false;
Expand All @@ -49,7 +49,7 @@ const isFormSubmit = (target) => {
* @method ClickHandler
*/
const clickHandler = (e, options = {}) => {
const target = e.target || e.srcElement;
const target = e.currentTarget;
const isForm = isFormSubmit(target);

let isRedirectLink = isDefaultRedirectLink(target);
Expand Down Expand Up @@ -79,7 +79,7 @@ const clickHandler = (e, options = {}) => {
if (
(!isDefaultRedirectLink(target))
|| (isLink(target) && (!href || (href && href[0] === '#')))
|| (isButtonLike(target) && !target.form)
|| (isButtonLike(target) && !isForm)
) {
isRedirectLink = false;
isPreventDefault = false;
Expand Down
23 changes: 12 additions & 11 deletions src/libs/tests/clickHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('clickHandler', () => {
};

mockClickEvent = {
target: {},
currentTarget: {},
button: 0,
preventDefault: jest.fn()
};
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('clickHandler', () => {
done();
};

mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A',
href: 'https://foobar.com'
};
Expand All @@ -88,8 +88,9 @@ describe('clickHandler', () => {
it('should run click handler correctly if target is an button', (done) => {
const executedActions = [];

mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'BUTTON',
type: 'submit',
form: {
submit() {
executedActions.push('submit');
Expand All @@ -111,7 +112,7 @@ describe('clickHandler', () => {

it('should run click handler correctly if target is input with submit', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'INPUT',
type: 'submit',
form: {
Expand All @@ -135,7 +136,7 @@ describe('clickHandler', () => {

it('should not follow it if follow is set to false', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A',
href: 'https://foobar.com'
};
Expand All @@ -154,7 +155,7 @@ describe('clickHandler', () => {

it('should follow it while follow is set to true', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A',
href: 'https://foobar.com'
};
Expand All @@ -178,7 +179,7 @@ describe('clickHandler', () => {

it('should simply execute event without prevent default and redirection if the link is #', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A'
};
mockClickEvent.preventDefault = function () {
Expand All @@ -196,7 +197,7 @@ describe('clickHandler', () => {

it('should simply execute event without prevent default and redirection is a modified click', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A'
};
mockClickEvent.metaKey = true;
Expand All @@ -215,7 +216,7 @@ describe('clickHandler', () => {

it('should simply execute event without prevent default and redirection if props.target=_blank', (done) => {
const executedActions = [];
mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'SPAN'
};
mockClickEvent.preventDefault = function () {
Expand All @@ -237,7 +238,7 @@ describe('clickHandler', () => {
executedActions.push('assign');
};

mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A',
href: 'foo'
};
Expand All @@ -262,7 +263,7 @@ describe('clickHandler', () => {
executedActions.push('assign');
};

mockClickEvent.target = {
mockClickEvent.currentTarget = {
tagName: 'A',
href: 'foo'
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/isUndefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

const isUndefined = data => typeof data === 'undefined';
const isUndefined = (data) => typeof data === 'undefined';

export default isUndefined;

0 comments on commit 5964d86

Please sign in to comment.