-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
fix(checkbox, slide-toggle): forward required attribute to input. #1137
Changes from all commits
ae6acdc
77a5d92
2cc90ec
0e430ea
a8b724a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ describe('MdSlideToggle', () => { | |
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [MdSlideToggleModule.forRoot(), FormsModule], | ||
declarations: [SlideToggleTestApp], | ||
declarations: [SlideToggleTestApp, SlideToggleFormsTestApp], | ||
}); | ||
|
||
TestBed.compileComponents(); | ||
|
@@ -318,6 +318,18 @@ describe('MdSlideToggle', () => { | |
expect(slideToggleElement.classList).toContain('md-slide-toggle-focused'); | ||
}); | ||
|
||
it('should forward the required attribute', () => { | ||
testComponent.isRequired = true; | ||
fixture.detectChanges(); | ||
|
||
expect(inputElement.required).toBe(true); | ||
|
||
testComponent.isRequired = false; | ||
fixture.detectChanges(); | ||
|
||
expect(inputElement.required).toBe(false); | ||
}); | ||
|
||
}); | ||
|
||
describe('custom template', () => { | ||
|
@@ -331,6 +343,55 @@ describe('MdSlideToggle', () => { | |
})); | ||
}); | ||
|
||
describe('with forms', () => { | ||
|
||
let fixture: ComponentFixture<any>; | ||
let testComponent: SlideToggleFormsTestApp; | ||
let buttonElement: HTMLButtonElement; | ||
let labelElement: HTMLLabelElement; | ||
let inputElement: HTMLInputElement; | ||
|
||
// This initialization is async() because it needs to wait for ngModel to set the initial value. | ||
beforeEach(async(() => { | ||
fixture = TestBed.createComponent(SlideToggleFormsTestApp); | ||
|
||
testComponent = fixture.debugElement.componentInstance; | ||
|
||
fixture.detectChanges(); | ||
|
||
buttonElement = fixture.debugElement.query(By.css('button')).nativeElement; | ||
labelElement = fixture.debugElement.query(By.css('label')).nativeElement; | ||
inputElement = fixture.debugElement.query(By.css('input')).nativeElement; | ||
})); | ||
|
||
it('should prevent the form from submit when being required', () => { | ||
|
||
if ('reportValidity' in inputElement === false) { | ||
// If the browser does not report the validity then the tests will break. | ||
// e.g Safari 8 on Mobile. | ||
return; | ||
} | ||
|
||
testComponent.isRequired = true; | ||
|
||
fixture.detectChanges(); | ||
|
||
buttonElement.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(testComponent.isSubmitted).toBe(false); | ||
|
||
testComponent.isRequired = false; | ||
fixture.detectChanges(); | ||
|
||
buttonElement.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(testComponent.isSubmitted).toBe(true); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
/** | ||
|
@@ -347,16 +408,25 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void | |
@Component({ | ||
selector: 'slide-toggle-test-app', | ||
template: ` | ||
<md-slide-toggle [(ngModel)]="slideModel" [disabled]="isDisabled" [color]="slideColor" | ||
[id]="slideId" [checked]="slideChecked" [name]="slideName" | ||
[ariaLabel]="slideLabel" [ariaLabelledby]="slideLabelledBy" | ||
(change)="onSlideChange($event)" | ||
<md-slide-toggle [(ngModel)]="slideModel" | ||
[required]="isRequired" | ||
[disabled]="isDisabled" | ||
[color]="slideColor" | ||
[id]="slideId" | ||
[checked]="slideChecked" | ||
[name]="slideName" | ||
[ariaLabel]="slideLabel" | ||
[ariaLabelledby]="slideLabelledBy" | ||
(change)="onSlideChange($event)" | ||
(click)="onSlideClick($event)"> | ||
|
||
<span>Test Slide Toggle</span> | ||
|
||
</md-slide-toggle>`, | ||
}) | ||
class SlideToggleTestApp { | ||
isDisabled: boolean = false; | ||
isRequired: boolean = false; | ||
slideModel: boolean = false; | ||
slideChecked: boolean = false; | ||
slideColor: string; | ||
|
@@ -371,3 +441,17 @@ class SlideToggleTestApp { | |
this.lastEvent = event; | ||
} | ||
} | ||
|
||
|
||
@Component({ | ||
selector: 'slide-toggle-forms-test-app', | ||
template: ` | ||
<form (ngSubmit)="isSubmitted = true"> | ||
<md-slide-toggle name="slide" ngModel [required]="isRequired">Required</md-slide-toggle> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell, this is necessary to register the element in the forms.
Otherwise I get errors without it. |
||
<button type="submit"></button> | ||
</form>` | ||
}) | ||
class SlideToggleFormsTestApp { | ||
isSubmitted: boolean = false; | ||
isRequired: boolean = false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transform: translateX(-50%)
?Also, it would be cool if we had a screenshot of what this looked like with and without this modification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually going to upload some screenshots, but this Safari thing distracted me too much 😄
Also we already use an absolute position here, and I want the
50%
to be relative to the inner container.