-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Angular - Abp form field component #23971
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
Draft
erdemcaygor
wants to merge
11
commits into
dev
Choose a base branch
from
feat/#23955
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
598f2a1
abp-input component created
erdemcaygor ec2ced0
reactive form implementation
erdemcaygor 086ccb2
refactoring
erdemcaygor 089a97b
refactoring
erdemcaygor 0d442ed
Merge branch 'dev' into feat/#23955
erdemcaygor 31a0c0b
refactoring
erdemcaygor a138889
hint input added
erdemcaygor 2d44ac9
style update
erdemcaygor 747c33d
abp form field component added
erdemcaygor e7ba609
abp form field component added
erdemcaygor c8c8934
refactoring
erdemcaygor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
npm/ng-packs/packages/components/abp-form-field/ng-package.json
This file contains hidden or 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,6 @@ | ||
| { | ||
| "$schema": "../../../node_modules/ng-packagr/ng-entrypoint.schema.json", | ||
| "lib": { | ||
| "entryFile": "src/public-api.ts" | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...ckages/components/abp-form-field/src/lib/abp-form-field/abp-form-field-error.component.ts
This file contains hidden or 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,13 @@ | ||
| import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'abp-form-field-error', | ||
| template: `<div class="invalid-feedback d-block"> | ||
| <ng-content></ng-content> | ||
| </div>`, | ||
| changeDetection: ChangeDetectionStrategy.OnPush | ||
| }) | ||
|
|
||
| export class AbpFormFieldErrorComponent { | ||
|
|
||
| } |
11 changes: 11 additions & 0 deletions
11
...ackages/components/abp-form-field/src/lib/abp-form-field/abp-form-field-hint.component.ts
This file contains hidden or 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,11 @@ | ||
| import { Component, ChangeDetectionStrategy } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'abp-form-field-hint', | ||
| template: `<small class="form-text text-muted"> | ||
| <ng-content></ng-content> | ||
| </small>`, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class AbpFormFieldHintComponent { | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ckages/components/abp-form-field/src/lib/abp-form-field/abp-form-field-label.component.ts
This file contains hidden or 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,11 @@ | ||
| import { ChangeDetectionStrategy, Component, input } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'abp-form-field-label', | ||
| template: `<ng-content></ng-content>`, | ||
| changeDetection: ChangeDetectionStrategy.OnPush | ||
| }) | ||
|
|
||
| export class AbpFormFieldLabelComponent { | ||
| for= input<string>(''); | ||
| } |
8 changes: 8 additions & 0 deletions
8
...s/packages/components/abp-form-field/src/lib/abp-form-field/abp-form-field.component.html
This file contains hidden or 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 @@ | ||
| <div class="form-group"> | ||
| <label class="form-label" [for]="labelComponent()?.for()"> | ||
| <ng-content select="abp-form-field-label" /> | ||
| </label> | ||
| <ng-content select="abp-input" /> | ||
| <ng-content select="abp-form-field-hint" /> | ||
| <ng-content select="abp-validation-error" /> | ||
| </div> |
33 changes: 33 additions & 0 deletions
33
...cks/packages/components/abp-form-field/src/lib/abp-form-field/abp-form-field.component.ts
This file contains hidden or 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,33 @@ | ||
| import { | ||
| Component, | ||
| ChangeDetectionStrategy, | ||
| input, | ||
| HostBinding, | ||
| InjectionToken, | ||
| QueryList, | ||
| ContentChild, | ||
| contentChild, | ||
| } from '@angular/core'; | ||
| import { CommonModule } from '@angular/common'; | ||
| import { AbpFormFieldLabelComponent } from './abp-form-field-label.component'; | ||
|
|
||
| export const ABP_FORM_FIELD = new InjectionToken<AbpFormFieldComponent>('AbpFormFieldComponent'); | ||
|
|
||
| @Component({ | ||
| selector: 'abp-form-field', | ||
| templateUrl: './abp-form-field.component.html', | ||
| imports: [CommonModule], | ||
| exportAs: 'abpFormField', | ||
| providers: [{ provide: ABP_FORM_FIELD, useExisting: AbpFormFieldComponent }], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class AbpFormFieldComponent { | ||
|
|
||
| containerClass = input<string>('mb-3'); | ||
| labelComponent = contentChild(AbpFormFieldLabelComponent); | ||
|
|
||
| @HostBinding('class') | ||
| get hostClasses(): string { | ||
| return `d-block mb-3 ${this.containerClass()}`; | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
npm/ng-packs/packages/components/abp-form-field/src/lib/abp-form-field/index.ts
This file contains hidden or 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 @@ | ||
| export * from './abp-form-field.component'; | ||
| export * from './abp-form-field-hint.component'; | ||
| export * from './abp-form-field-label.component'; |
1 change: 1 addition & 0 deletions
1
npm/ng-packs/packages/components/abp-form-field/src/lib/index.ts
This file contains hidden or 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 @@ | ||
| export * from './abp-form-field'; |
1 change: 1 addition & 0 deletions
1
npm/ng-packs/packages/components/abp-form-field/src/public-api.ts
This file contains hidden or 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 @@ | ||
| export * from './lib'; |
This file contains hidden or 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,6 @@ | ||
| { | ||
| "$schema": "../../../node_modules/ng-packagr/ng-entrypoint.schema.json", | ||
| "lib": { | ||
| "entryFile": "src/public-api.ts" | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
npm/ng-packs/packages/components/abp-input/src/abp-input.component.html
This file contains hidden or 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,35 @@ | ||
| @if(abpFormField) { | ||
| <div [formGroup]="abpInputFormGroup"> | ||
| <input | ||
| [type]="type()" | ||
| [id]="id()" | ||
| class="form-control" | ||
| [placeholder]="placeholder()" | ||
| formControlName="value" | ||
| /> | ||
| </div> | ||
| } @else { | ||
| <div class="form-group" [formGroup]="abpInputFormGroup"> | ||
| @if (label()) { | ||
| <label [for]="id()" class="form-label">{{ label() | abpLocalization }}</label> | ||
| } | ||
| <input | ||
| [type]="type()" | ||
| [id]="id()" | ||
| class="form-control" | ||
| [placeholder]="placeholder()" | ||
| formControlName="value" | ||
| /> | ||
| @if (hint()) { | ||
| <small class="form-text text-muted">{{ hint() | abpLocalization }}</small> | ||
| } | ||
| @if (errors.length > 0) { | ||
| <div class="invalid-feedback d-block"> | ||
| @for (error of errors; track error) { | ||
| <div>{{ error }}</div> | ||
| } | ||
| </div> | ||
| } | ||
| <div></div> | ||
| </div> | ||
| } |
109 changes: 109 additions & 0 deletions
109
npm/ng-packs/packages/components/abp-input/src/abp-input.component.ts
This file contains hidden or 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,109 @@ | ||||||||||||||
| import { | ||||||||||||||
| ChangeDetectionStrategy, | ||||||||||||||
| ChangeDetectorRef, | ||||||||||||||
| Component, | ||||||||||||||
| DestroyRef, | ||||||||||||||
| forwardRef, | ||||||||||||||
| inject, | ||||||||||||||
| OnInit, | ||||||||||||||
| input, | ||||||||||||||
| Injector | ||||||||||||||
| } from '@angular/core'; | ||||||||||||||
| import { | ||||||||||||||
| AbstractControl, | ||||||||||||||
| ControlValueAccessor, | ||||||||||||||
| FormBuilder, | ||||||||||||||
| FormControl, | ||||||||||||||
| FormControlName, | ||||||||||||||
| FormGroup, | ||||||||||||||
| FormGroupDirective, | ||||||||||||||
| NG_VALUE_ACCESSOR, | ||||||||||||||
| NgControl, | ||||||||||||||
| ReactiveFormsModule, | ||||||||||||||
| } from '@angular/forms'; | ||||||||||||||
| import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||||||||||||||
| import { LocalizationPipe } from '@abp/ng.core'; | ||||||||||||||
| import { ABP_FORM_FIELD } from '@abp/ng.components/abp-form-field'; | ||||||||||||||
|
|
||||||||||||||
| const ABP_INPUT_CONTROL_VALUE_ACCESSOR = { | ||||||||||||||
| provide: NG_VALUE_ACCESSOR, | ||||||||||||||
| useExisting: forwardRef(() => AbpInputComponent), | ||||||||||||||
| multi: true, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| @Component({ | ||||||||||||||
| selector: 'abp-input', | ||||||||||||||
| templateUrl: './abp-input.component.html', | ||||||||||||||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||||||||||||||
| imports: [ReactiveFormsModule, LocalizationPipe], | ||||||||||||||
| exportAs: 'abpInput', | ||||||||||||||
| host: { | ||||||||||||||
| class: 'abp-input', | ||||||||||||||
| }, | ||||||||||||||
| providers: [ABP_INPUT_CONTROL_VALUE_ACCESSOR], | ||||||||||||||
| }) | ||||||||||||||
| export class AbpInputComponent implements OnInit, ControlValueAccessor { | ||||||||||||||
| label = input<string>(); | ||||||||||||||
| type = input<'text' | 'number' | 'password'>('text'); | ||||||||||||||
| id = input<string>(''); | ||||||||||||||
| placeholder = input<string>(''); | ||||||||||||||
| hint = input<string>(''); | ||||||||||||||
| control: FormControl; | ||||||||||||||
| readonly formBuilder = inject(FormBuilder); | ||||||||||||||
| readonly changeDetectorRef = inject(ChangeDetectorRef); | ||||||||||||||
| readonly destroyRef = inject(DestroyRef); | ||||||||||||||
| readonly injector = inject(Injector); | ||||||||||||||
| readonly abpFormField = inject(ABP_FORM_FIELD, { optional: true }); | ||||||||||||||
| abpInputFormGroup: FormGroup; | ||||||||||||||
|
|
||||||||||||||
| ngOnInit() { | ||||||||||||||
|
|
||||||||||||||
| const ngControl = this.injector.get(NgControl, null); | ||||||||||||||
| if (ngControl) { | ||||||||||||||
| this.control = this.injector.get(FormGroupDirective).getControl(ngControl as FormControlName); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| this.abpInputFormGroup = this.formBuilder.group({ | ||||||||||||||
| value: [''], | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| this.value.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(val => { | ||||||||||||||
| this.onChange(val); | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| writeValue(value: any): void { | ||||||||||||||
| this.value.setValue(value); | ||||||||||||||
| this.changeDetectorRef.markForCheck(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| registerOnChange(fn: any): void { | ||||||||||||||
| this.onChange = fn; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| registerOnTouched(fn: any): void { | ||||||||||||||
| this.onTouched = fn; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| setDisabledState(isDisabled: boolean): void { | ||||||||||||||
| if (isDisabled) { | ||||||||||||||
| this.value.disable(); | ||||||||||||||
| } else { | ||||||||||||||
| this.value.enable(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| get errors(): string[] { | ||||||||||||||
| if (this.control && this.control.errors) { | ||||||||||||||
| return [] | ||||||||||||||
| } | ||||||||||||||
| return [] | ||||||||||||||
|
Comment on lines
+98
to
+100
|
||||||||||||||
| return [] | |
| } | |
| return [] | |
| return Object.keys(this.control.errors); | |
| } | |
| return []; |
This file contains hidden or 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 @@ | ||
| export * from './abp-input.component'; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[nitpick] Extra blank line after method declaration should be removed for consistency.