Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Autofocus first form field #208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
60 changes: 60 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,4 +1209,64 @@ angular.module('stormpath', [
});
}
};
}])

/**
* @ngdoc directive
* @restrict A
* @name stormpath.spAutoFocusForm:spAutoFocusForm
*
* @description
* This directive, when placed on a container element, automatically focuses
* on the first descendant <input> element inside the form.
*
* If the form is rendered synchronously, the directive can be placed on a form
* without any parameters and will focus immediately. However, if the form is
* instead loaded asynchronously, it needs to be passed a parameter that indicates
* when the form has loaded. As soon as the parameter evaluates to a truthy value,
* the autofocus will occur. When evaluation this value, the current scope is used.
*
* When there are no inputs inside the container, nothing happens.
*
* @example
* <pre><code>
* <form id="form-1" sp-auto-focus-form>
* <input id="will-be-focused" type="text" />
* </form>
*
* <form id="form-2" sp-auto-focus="viewModel">
* <div ng-repeat="field in viewModel.fields">
* <!-- First one will be focused once it is rendered -->
* <input name="{{field.name}}" type="{{field.type}}" />
* </div>
* </form>
* </pre></code>
*/
.directive('spAutoFocusForm', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function link(scope, elem, attrs) {
function focusFirstInput() {
var firstInput = angular.element(elem).find('input')[0];

if (firstInput) {
$timeout(function() {
firstInput.focus();
});
}
}

if (attrs.spAutoFocusForm.length) {
scope.$watch(function() {
return scope.$eval(attrs.spAutoFocusForm);
}, function(curr, prev) {
if (curr && !prev) {
focusFirstInput();
}
});
} else {
focusFirstInput();
}
}
};
}]);
2 changes: 1 addition & 1 deletion src/spEmailVerification.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal" ng-show="needsReVerification && !reVerificationSent" ng-submit="submit()">
<form sp-auto-focus-form class="form-horizontal" ng-show="needsReVerification && !reVerificationSent" ng-submit="submit()">

<div class="form-group">
<label for="spEmail" class="col-xs-12 col-sm-4 control-label">Email or Username</label>
Expand Down
2 changes: 1 addition & 1 deletion src/spLoginForm.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div ng-show="!viewModel" class="sp-loading">
Loading...
</div>
<form class="form-horizontal" ng-hide="accepted || !viewModel" ng-submit="submit()">
<form sp-auto-focus-form="viewModel" class="form-horizontal" ng-hide="accepted || !viewModel" ng-submit="submit()">
<div class="form-group" ng-repeat="field in viewModel.form.fields">
<label for="sp-{{field.name}}" class="col-xs-12 col-sm-4 control-label">{{field.label}}</label>
<div class="col-xs-12 col-sm-4">
Expand Down
2 changes: 1 addition & 1 deletion src/spPasswordResetForm.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal" ng-show="verified && !reset" ng-submit="submit()">
<form sp-auto-focus-form class="form-horizontal" ng-show="verified && !reset" ng-submit="submit()">

<div class="form-group">
<label for="spEmail" class="col-xs-12 col-sm-4 control-label">New Password</label>
Expand Down
2 changes: 1 addition & 1 deletion src/spPasswordResetRequestForm.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal" ng-hide="sent" ng-submit="submit()">
<form sp-auto-focus-form class="form-horizontal" ng-hide="sent" ng-submit="submit()">

<div class="form-group">
<label for="spEmail" class="col-xs-12 col-sm-4 control-label">Email or Username</label>
Expand Down
4 changes: 2 additions & 2 deletions src/spRegistrationForm.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<div ng-show="!viewModel" class="sp-loading">
Loading...
</div>
<form class="form-horizontal" ng-hide="!viewModel || (created && !authenticating)" ng-submit="submit()">
<form sp-auto-focus-form="viewModel" class="form-horizontal" ng-hide="!viewModel || (created && !authenticating)" ng-submit="submit()">
<div class="form-group" ng-repeat="field in viewModel.form.fields">
<label for="sp-{{field.name}}" class="col-xs-12 col-sm-4 control-label">{{field.label}}</label>
<div class="col-xs-12 col-sm-4">
Expand All @@ -70,4 +70,4 @@
</div>
</form>
</div>
</div>
</div>