-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
17 changed files
with
142 additions
and
18 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
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
8 changes: 8 additions & 0 deletions
8
src/app/shared/formly/types/phone-field/phone-field.component.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,8 @@ | ||
<input | ||
type="tel" | ||
[formControl]="formControl" | ||
class="form-control" | ||
[formlyAttributes]="field" | ||
[attr.data-testing-id]="field.key" | ||
[maxlength]="to?.maxlength ? to.maxlength : 20" | ||
/> |
63 changes: 63 additions & 0 deletions
63
src/app/shared/formly/types/phone-field/phone-field.component.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,63 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FormGroup, ReactiveFormsModule } from '@angular/forms'; | ||
import { FormlyModule } from '@ngx-formly/core'; | ||
|
||
import { FormlyTestingComponentsModule } from 'ish-shared/formly/dev/testing/formly-testing-components.module'; | ||
import { FormlyTestingContainerComponent } from 'ish-shared/formly/dev/testing/formly-testing-container/formly-testing-container.component'; | ||
|
||
import { PhoneFieldComponent } from './phone-field.component'; | ||
|
||
describe('Phone Field Component', () => { | ||
let component: FormlyTestingContainerComponent; | ||
let fixture: ComponentFixture<FormlyTestingContainerComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [PhoneFieldComponent], | ||
imports: [ | ||
FormlyModule.forRoot({ | ||
types: [{ name: 'phone', component: PhoneFieldComponent }], | ||
}), | ||
FormlyTestingComponentsModule, | ||
ReactiveFormsModule, | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
const testComponentInputs = { | ||
model: { phone: '' }, | ||
fields: [ | ||
{ | ||
key: 'phone', | ||
type: 'phone', | ||
}, | ||
], | ||
form: new FormGroup({}), | ||
}; | ||
fixture = TestBed.createComponent(FormlyTestingContainerComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
|
||
component.testComponentInputs = testComponentInputs; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
|
||
it('should be rendered after creation', () => { | ||
fixture.detectChanges(); | ||
expect(element.querySelector('ish-phone-field')).toBeTruthy(); | ||
}); | ||
|
||
it('should be invalid if an incorrect phone is entered', () => { | ||
fixture.detectChanges(); | ||
component.form.get('phone').setValue('123456a'); | ||
fixture.detectChanges(); | ||
expect(component.form.get('phone').valid).toBeFalsy(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/app/shared/formly/types/phone-field/phone-field.component.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,26 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { FieldType, FormlyFieldConfig } from '@ngx-formly/core'; | ||
|
||
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators'; | ||
|
||
@Component({ | ||
selector: 'ish-phone-field', | ||
templateUrl: './phone-field.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PhoneFieldComponent extends FieldType { | ||
formControl: FormControl; | ||
|
||
prePopulate(field: FormlyFieldConfig) { | ||
field.validators = field.validators ?? {}; | ||
field.validators.validation = [...(field.validators.validation ?? []), SpecialValidators.phone]; | ||
|
||
field.validation = field.validation ?? {}; | ||
field.validation.messages = { | ||
...field.validation?.messages, | ||
phone: 'form.phone.error.invalid', | ||
required: 'form.phone.error.required', | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.