Skip to content

Commit

Permalink
[Forwardport 2.3] Trim username on customer account login page
Browse files Browse the repository at this point in the history
  • Loading branch information
dankhrapiyush committed Jun 6, 2018
1 parent 6110fdd commit c44f1f8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/code/Magento/Customer/view/frontend/templates/form/login.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@
</form>
</div>
</div>

<script type="text/x-magento-init">
{
".field.email": {
"Magento_Customer/js/trim-username": {
"formSelector": "form.form-login"
}
}
}
</script>
65 changes: 65 additions & 0 deletions app/code/Magento/Customer/view/frontend/web/js/trim-username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'jquery'
], function ($) {
'use strict';

$.widget('mage.trimUsername', {
options: {
cache: {},
formSelector: 'form',
emailSelector: 'input[type="email"]'
},

/**
* Widget initialization
* @private
*/
_create: function () {
// We need to look outside the module for backward compatibility, since someone can already use the module.
// @todo Narrow this selector in 2.3 so it doesn't accidentally finds the email field from the
// newsletter email field or any other "email" field.
this.options.cache.email = $(this.options.formSelector).find(this.options.emailSelector);
this._bind();
},

/**
* Event binding, will monitor change, keyup and paste events.
* @private
*/
_bind: function () {
if (this.options.cache.email.length) {
this._on(this.options.cache.email, {
'change': this._trimUsername,
'keyup': this._trimUsername,
'paste': this._trimUsername
});
}
},

/**
* Trim username
* @private
*/
_trimUsername: function () {
var username = this._getUsername().trim();

this.options.cache.email.val(username);
},

/**
* Get username value
* @returns {*}
* @private
*/
_getUsername: function () {
return this.options.cache.email.val();
}
});

return $.mage.trimUsername;
});

0 comments on commit c44f1f8

Please sign in to comment.