Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 1a852bc

Browse files
devversionThomasBurleson
authored andcommitted
feat(input): add directive to auto select text on input focus
Fixes #6679 Closes #6701
1 parent dfd4ba2 commit 1a852bc

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/components/input/demoBasicUsage/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
<md-input-container class="md-block">
8585
<label>Biography</label>
86-
<textarea ng-model="user.biography" columns="1" md-maxlength="150" rows="5"></textarea>
86+
<textarea ng-model="user.biography" md-maxlength="150" rows="5" md-select-on-focus></textarea>
8787
</md-input-container>
8888

8989

src/components/input/input.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ angular.module('material.components.input', [
1515
.directive('ngMessages', ngMessagesDirective)
1616
.directive('ngMessage', ngMessageDirective)
1717
.directive('ngMessageExp', ngMessageDirective)
18+
.directive('mdSelectOnFocus', mdSelectOnFocusDirective)
1819

1920
.animation('.md-input-invalid', mdInputInvalidMessagesAnimation)
2021
.animation('.md-input-messages-animation', ngMessagesAnimation)
@@ -572,6 +573,29 @@ function placeholderDirective($log) {
572573
}
573574
}
574575

576+
function mdSelectOnFocusDirective() {
577+
578+
return {
579+
restrict: 'A',
580+
link: postLink
581+
};
582+
583+
function postLink(scope, element, attr) {
584+
if (element[0].nodeName !== 'INPUT' && element[0].nodeName !== "TEXTAREA") return;
585+
586+
element.on('focus', onFocus);
587+
588+
scope.$on('$destroy', function() {
589+
element.off('focus', onFocus);
590+
});
591+
592+
function onFocus() {
593+
// Use HTMLInputElement#select to fix firefox select issues
594+
element[0].select();
595+
}
596+
}
597+
}
598+
575599
var visibilityDirectives = ['ngIf', 'ngShow', 'ngHide', 'ngSwitchWhen', 'ngSwitchDefault'];
576600
function ngMessagesDirective() {
577601
return {

src/components/input/input.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,26 @@ describe('md-input-container directive', function() {
402402
expect(el[0].querySelector("[ng-messages]").classList.contains('md-auto-hide')).toBe(false);
403403
}));
404404

405+
it('should select the input value on focus', inject(function() {
406+
var container = setup('md-select-on-focus');
407+
var input = container.find('input');
408+
input.val('Auto Text Select');
409+
410+
document.body.appendChild(container[0]);
411+
412+
expect(isTextSelected(input[0])).toBe(false);
413+
414+
input.triggerHandler('focus');
415+
416+
expect(isTextSelected(input[0])).toBe(true);
417+
418+
document.body.removeChild(container[0]);
419+
420+
function isTextSelected(input) {
421+
return input.selectionStart == 0 && input.selectionEnd == input.value.length
422+
}
423+
}));
424+
405425
describe('Textarea auto-sizing', function() {
406426
var ngElement, element, ngTextarea, textarea, scope, parentElement;
407427

0 commit comments

Comments
 (0)