Skip to content

Commit

Permalink
Merge pull request #16 from PolymerElements/fix-caret
Browse files Browse the repository at this point in the history
preserve caret position when editing
  • Loading branch information
notwaldorf committed Jun 10, 2015
2 parents ab8bdf3 + 5c42185 commit d3a3104
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
11 changes: 6 additions & 5 deletions gold-phone-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
required$="[[required]]"
bind-value="{{value}}"
name$="[[name]]"
allowed-pattern="[0-9]"
allowed-pattern="[0-9\-]"
autocomplete="tel"
type="tel"
prevent-invalid-input
Expand Down Expand Up @@ -192,13 +192,14 @@

formattedValue += value[i];
}

this.value = formattedValue.trim();
this.updateValueAndPreserveCaret(formattedValue.trim());

// If the character right before the selection is a newly inserted
// dash, we need to advance the selection to maintain the caret position.
if (!previousCharADash && this.value.charAt(start - 1) == '-')
this.$.input.selectionStart++;
if (!previousCharADash && this.value.charAt(start - 1) == '-') {
this.$.input.selectionStart = start + 1;
this.$.input.selectionEnd = start + 1;
}
}

})
Expand Down
22 changes: 11 additions & 11 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@
assert.notEqual(getComputedStyle(error).display, 'none', 'error is not display:none');
});

// TODO: re-enable when PolymerElements/iron-input/issues/24 is fixed.
// test('caret position is preserved', function() {
// var input = fixture('required');
// var ironInput = input.querySelector('input[is="iron-input"]');
// input.value='111-111-1';
// ironInput.selectionStart = (2,2);
// input.value='112-111-11';
//
// assert.equal(ironInput.selectionStart, 2, 'selectionStart is preserved');
// assert.equal(ironInput.selectionEnd, 2, 'selectionEnd is preserved');
// });
test('caret position is preserved', function() {
var input = fixture('required');
var ironInput = Polymer.dom(input.root).querySelector('input[is="iron-input"]');
input.value='111-111-1';
ironInput.selectionStart = 2;
ironInput.selectionEnd = 2;
input._computeValue('112-111-11');

assert.equal(ironInput.selectionStart, 2, 'selectionStart is preserved');
assert.equal(ironInput.selectionEnd, 2, 'selectionEnd is preserved');
});

suite('a11y', function() {

Expand Down

0 comments on commit d3a3104

Please sign in to comment.