Skip to content

fix(input): prefix copied input id #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/components/input/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[attr.aria-disabled]="ariaDisabled"
[attr.aria-required]="ariaRequired"
[attr.aria-invalid]="ariaInvalid"
[id]="id"
[id]="inputId"
[disabled]="disabled"
[required]="required"
[attr.maxlength]="maxLength"
Expand All @@ -22,7 +22,7 @@
[(ngModel)]="value">

<label class="md-input-placeholder"
[attr.for]="id"
[attr.for]="inputId"
[class.md-empty]="empty"
[class.md-focused]="focused"
[class.md-float]="floatingPlaceholder"
Expand Down
23 changes: 23 additions & 0 deletions src/components/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export function main() {
});
}));

it('copies a prefixed id attribute to the inner input', injectAsync([], () => {
return builder.createAsync(MdInputIdTestController)
.then((fixture) => {
fixture.detectChanges();
let el: HTMLInputElement = fixture.debugElement.query(By.css('input')).nativeElement;
expect(el.getAttribute('id')).toEqual('md-input-id');
});
}));

it('validates there\'s only one hint label per side', injectAsync([], () => {
return builder.createAsync(MdInputInvalidHintTestController)
.then((fixture: ComponentFixture) => {
Expand Down Expand Up @@ -179,6 +188,11 @@ export function main() {
expect(el).not.toBeNull();
expect(el.nativeElement.textContent).toMatch('Other placeholder');
expect(el.nativeElement.textContent).not.toMatch(/\*/g);

let input = fixture.debugElement.query(By.css('input'));
expect(input.nativeElement.getAttribute('id')).toContain('md-input-');
expect(el.nativeElement.getAttribute('for'))
.toEqual(input.nativeElement.getAttribute('id'));
})();
});
}));
Expand Down Expand Up @@ -351,3 +365,12 @@ class MdInputAriaTestController {
ariaLabel: string = 'label';
ariaDisabled: boolean = true;
}

@Component({
selector: 'test-input-controller',
template: `
<md-input id="id"></md-input>
`,
directives: [MdInput]
})
class MdInputIdTestController {}
5 changes: 4 additions & 1 deletion src/components/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class MdHint {
})
export class MdInput implements ControlValueAccessor, AfterContentInit, OnChanges {
private _focused: boolean = false;
private _inputId: string = '';
private _value: any = '';

/** Callback registered via registerOnTouched (ControlValueAccessor) */
Expand All @@ -121,6 +122,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange

/** Readonly properties. */
get focused() { return this._focused; }
get inputId() { return this._inputId; }
get empty() { return this._value == null || this._value === ''; }
get characterCount(): number {
return this.empty ? 0 : ('' + this._value).length;
Expand All @@ -134,7 +136,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
@Input() @BooleanFieldValue() disabled: boolean = false;
@Input() @BooleanFieldValue() floatingPlaceholder: boolean = true;
@Input() hintLabel: string = '';
@Input() id: string = `md-input-${nextUniqueId++}`;
@Input() id: string = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change here should actually be:

  1. Rename id to inputId
  2. Remove the @Input() decorator, since the component itself doesn't ever use the id from the host element.

@Input() maxLength: number = -1;
@Input() placeholder: string;
@Input() @BooleanFieldValue() required: boolean = false;
Expand Down Expand Up @@ -184,6 +186,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
}

ngAfterContentInit() {
this._inputId = `${'md-input'}-${this.id || nextUniqueId++}`;
this._validateConstraints();

// Trigger validation when the hint children change.
Expand Down