This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tag): Add tag-add-form for custom forms.
- Loading branch information
Showing
21 changed files
with
695 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
libs/barista-components/tag/src/tag-add/tag-add-form/tag-add-form.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<ng-content></ng-content> |
3 changes: 3 additions & 0 deletions
3
libs/barista-components/tag/src/tag-add/tag-add-form/tag-add-form.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:host { | ||
display: block; | ||
} |
98 changes: 98 additions & 0 deletions
98
libs/barista-components/tag/src/tag-add/tag-add-form/tag-add-form.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { | ||
FormControl, | ||
FormGroup, | ||
FormsModule, | ||
ReactiveFormsModule, | ||
Validators, | ||
} from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { DtTagAddForm } from './tag-add-form'; | ||
|
||
describe('DtTagAddForm', () => { | ||
let component: DtTagAddFormComponent; | ||
let fixture: ComponentFixture<DtTagAddFormComponent>; | ||
let addTagFormInstance: DtTagAddForm; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [DtTagAddForm, DtTagAddFormComponent], | ||
imports: [FormsModule, ReactiveFormsModule], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(DtTagAddFormComponent); | ||
component = fixture.componentInstance; | ||
addTagFormInstance = fixture.debugElement.query(By.directive(DtTagAddForm)) | ||
.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should emit current validity and changes to the validity of a provided form-group', () => { | ||
const validSpy = jest.fn(); | ||
const sub = addTagFormInstance.valid$.subscribe(validSpy); | ||
fixture.detectChanges(); | ||
|
||
expect(validSpy).toHaveBeenCalledTimes(1); | ||
expect(validSpy).toHaveBeenLastCalledWith(false); | ||
|
||
component.keyFormControl.setValue('1234'); | ||
|
||
expect(validSpy).toHaveBeenCalledTimes(2); | ||
expect(validSpy).toHaveBeenLastCalledWith(true); | ||
|
||
component.keyFormControl.setValue('123'); | ||
|
||
expect(validSpy).toHaveBeenCalledTimes(3); | ||
expect(validSpy).toHaveBeenLastCalledWith(false); | ||
|
||
sub.unsubscribe(); | ||
}); | ||
}); | ||
|
||
/** Test component that passes a form-group to dt-tag-add-form. */ | ||
@Component({ | ||
selector: 'dt-test-app', | ||
template: ` | ||
<dt-tag-add-form> | ||
<form [formGroup]="form" class="key-value-form"> | ||
<input | ||
#key | ||
type="text" | ||
aria-label="Tag key" | ||
required | ||
formControlName="key" | ||
/> | ||
</form> | ||
</dt-tag-add-form> | ||
`, | ||
}) | ||
class DtTagAddFormComponent { | ||
readonly keyFormControl = new FormControl('', [ | ||
// tslint:disable-next-line: no-unbound-method | ||
Validators.minLength(4), | ||
]); | ||
|
||
readonly form = new FormGroup({ | ||
key: this.keyFormControl, | ||
}); | ||
} |
59 changes: 59 additions & 0 deletions
59
libs/barista-components/tag/src/tag-add/tag-add-form/tag-add-form.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
Component, | ||
ContentChild, | ||
ContentChildren, | ||
ElementRef, | ||
NgZone, | ||
QueryList, | ||
} from '@angular/core'; | ||
import { FormGroupDirective } from '@angular/forms'; | ||
import { DtInput } from '@dynatrace/barista-components/input'; | ||
import { Observable, of } from 'rxjs'; | ||
import { map, share, startWith, switchMap, take } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'dt-tag-add-form', | ||
templateUrl: './tag-add-form.html', | ||
styleUrls: ['./tag-add-form.scss'], | ||
}) | ||
export class DtTagAddForm { | ||
/** @internal ElementRef of Add Tag Input */ | ||
@ContentChildren(DtInput, { read: ElementRef, descendants: true }) | ||
_inputs: QueryList<ElementRef<HTMLInputElement>>; | ||
|
||
/** @internal The FormGroup of the custom form for adding tags */ | ||
@ContentChild(FormGroupDirective) _form: FormGroupDirective; | ||
|
||
/** Emits whether the form input is valid based on a FormGroupDirective. */ | ||
readonly valid$: Observable<boolean> = this._ngZone.onStable.pipe( | ||
take(1), | ||
switchMap(() => | ||
(this._form?.statusChanges ?? of()).pipe(startWith(undefined)), | ||
), | ||
map(() => this._form.valid ?? true), | ||
share(), | ||
); | ||
|
||
constructor(private readonly _ngZone: NgZone) {} | ||
|
||
/** @internal Resets the provided FormGroup */ | ||
_reset(): void { | ||
this._form?.resetForm(); | ||
} | ||
} |
Oops, something went wrong.