Skip to content

Commit

Permalink
Merge pull request #32 from ckeditor/t/31
Browse files Browse the repository at this point in the history
Other: Added reset button to the form sample. Closes #31.
  • Loading branch information
pomek authored Aug 28, 2018
2 parents 9abe65d + 48793a4 commit 0d973eb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app/demo-form/demo-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h3>User profile form</h3>
<p *ngIf="description && description.dirty" class="alert">Description is "dirty".</p>
<p *ngIf="description && description.touched" class="alert">Description has been "touched".</p>

<p><button type="reset" (click)="reset()">Reset form</button></p>
<p><button type="submit">Submit this form</button> <em>(Open the console first)</em></p>
</form>

Expand Down
15 changes: 14 additions & 1 deletion src/app/demo-form/demo-form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe( 'DemoFormComponent', () => {
it( 'should log the model to the console when user submits the form', () => {
const spy = spyOn( console, 'log' );

const submitButton: HTMLButtonElement = fixture.debugElement.query( By.css( 'button' ) ).nativeElement;
const submitButton: HTMLButtonElement = fixture.debugElement.query( By.css( 'button[type=submit]' ) ).nativeElement;
submitButton.click();

expect( spy ).toHaveBeenCalledTimes( 1 );
Expand Down Expand Up @@ -63,4 +63,17 @@ describe( 'DemoFormComponent', () => {
done();
} );
} );

it( 'should reset form after clicking the reset button', ( done: Function ) => {
setTimeout( () => {
const resetButton: HTMLButtonElement = fixture.debugElement.query( By.css( 'button[type=reset]' ) ).nativeElement;
resetButton.click();

fixture.detectChanges();

expect( component.formDataPreview ).toEqual( '{"name":null,"surname":null,"description":"<p>&nbsp;</p>"}' );

done();
} );
} );
} );
4 changes: 4 additions & 0 deletions src/app/demo-form/demo-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class DemoFormComponent implements AfterViewInit {
console.log( 'Form submit, model', this.model );
}

reset() {
this.demoForm!.reset();
}

get description() {
return this.demoForm!.controls.description;
}
Expand Down
8 changes: 5 additions & 3 deletions src/ckeditor/ckeditor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ export class CKEditorComponent implements AfterViewInit, OnDestroy, ControlValue

// Implementing the ControlValueAccessor interface (only when binding to ngModel).
writeValue( value: string | null ): void {
// If a null was received, the textarea content needs to be empty
if ( value == null ) {
// This method is called with the `null` value when the form resets.
// A component's responsibility is to restore to the initial state.
if ( value === null ) {
value = '';
}
// If already initialized

// If already initialized.
if ( this.editorInstance ) {
this.editorInstance.setData( value );
}
Expand Down

0 comments on commit 0d973eb

Please sign in to comment.