Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
fix(text-field): Set character counter in setValue (#4572)
Browse files Browse the repository at this point in the history
(cherry picked from commit bce2e63)
  • Loading branch information
patrickrodee authored and abhiomkar committed Apr 8, 2019
1 parent 60a268f commit 7261fd0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/mdc-textfield/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
// Prevent Safari from moving the caret to the end of the input when the value has not changed.
if (this.getValue() !== value) {
this.getNativeInput_().value = value;
this.setCharacterCounter_(value.length);
}
const isValid = this.isValid();
this.styleValidity_(isValid);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/mdc-textfield/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ test('#setValue does not affect disabled state', () => {
td.verify(mockAdapter.removeClass(cssClasses.INVALID), {times: 1});
});

test('#setValue updates character counter when present', () => {
const {foundation, mockAdapter, characterCounter} = setupTest({useCharacterCounter: true});
const nativeInput = {
type: 'text',
value: '',
maxLength: 4,
validity: {
valid: true,
},
};
td.when(mockAdapter.getNativeInput()).thenReturn(nativeInput);

foundation.setValue('ok');
td.verify(characterCounter.setCounterValue(2, 4), {times: 1});
});

test('#isValid for native validation', () => {
const {foundation, nativeInput} = setupValueTest({value: '', optIsValid: true});
assert.isOk(foundation.isValid());
Expand Down

0 comments on commit 7261fd0

Please sign in to comment.