Skip to content

Commit 6778440

Browse files
authored
feat: add required-marker (#276)
1 parent b85a05b commit 6778440

16 files changed

+54
-76
lines changed

projects/docs/src/app/examples/dialog/dialog-test/dialog-test.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ChangeDetectionStrategy, Component } from '@angular/core';
21
import { ButtonComponent } from '@/ui/button/button.component';
32
import { DialogCloseDirective } from '@/ui/dialog/dialog-close.directive';
43
import { FormFieldComponent } from '@/ui/form-field/form-field.component';
5-
import { LabelComponent } from '@/ui/form-field/label/label.component';
4+
import { LabelComponent } from '@/ui/form-field/label.component';
65
import { InputDirective } from '@/ui/input/input.directive';
6+
import { ChangeDetectionStrategy, Component } from '@angular/core';
77

88
@Component({
99
selector: 'doc-dialog-test',

projects/docs/src/app/examples/form-field/show-case-form-field/show-case-form-field.component.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { Component, effect, inject } from '@angular/core';
2-
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
31
import { ButtonComponent } from '@/ui/button/button.component';
42
import { DialogService } from '@/ui/dialog/dialog.service';
5-
import { ErrorComponent } from '@/ui/form-field/error/error.component';
3+
import { ErrorComponent } from '@/ui/form-field/error.component';
64
import { FormFieldErrorRegistry } from '@/ui/form-field/form-field-error.registry';
75
import { FormFieldComponent } from '@/ui/form-field/form-field.component';
8-
import { LabelComponent } from '@/ui/form-field/label/label.component';
6+
import { LabelComponent } from '@/ui/form-field/label.component';
97
import { InputDirective } from '@/ui/input/input.directive';
108
import { OptionComponent } from '@/ui/select/option.component';
119
import { SelectComponent } from '@/ui/select/select.component';
10+
import { Component, inject } from '@angular/core';
11+
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
1212

1313
@Component({
1414
selector: 'doc-show-case-form-field',
@@ -56,11 +56,6 @@ export class ShowCaseFormFieldComponent {
5656
min: (error) =>
5757
`Min value is ${error.min}, but your value is ${error.actual}`,
5858
});
59-
60-
effect(() => {
61-
const cont = this.group.controls.lastName;
62-
console.log(cont.errors?.['required']);
63-
});
6459
}
6560

6661
register() {

projects/docs/src/app/examples/toast/show-case-toast/show-case-toast.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ButtonComponent } from '@/ui/button/button.component';
22
import { FormFieldComponent } from '@/ui/form-field/form-field.component';
3-
import { LabelComponent } from '@/ui/form-field/label/label.component';
3+
import { LabelComponent } from '@/ui/form-field/label.component';
44
import { OptionComponent } from '@/ui/select/option.component';
55
import { SelectComponent } from '@/ui/select/select.component';
66
import {

projects/docs/src/app/features/form-field-page/form-field-page.component.ts

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export class FormFieldPageComponent {
7575
description:
7676
'List of errors to ignore. This can be useful when you want to display most of the errors with error group, but handle specific errors with app-error and provide customized error messages',
7777
},
78+
{
79+
name: 'hideRequiredMarker',
80+
type: 'boolean',
81+
default: 'false',
82+
description: 'Hides the required marker',
83+
},
7884
],
7985
},
8086
{

projects/docs/src/tree-structure.json

+16-48
Original file line numberDiff line numberDiff line change
@@ -533,30 +533,14 @@
533533
"name": "form-field",
534534
"files": [
535535
{
536-
"path": "form-field/error",
537-
"name": "error",
538-
"files": [
539-
{
540-
"path": "form-field/error/error.component.css",
541-
"name": "error.component.css",
542-
"language": "css"
543-
},
544-
{
545-
"path": "form-field/error/error.component.html",
546-
"name": "error.component.html",
547-
"language": "html"
548-
},
549-
{
550-
"path": "form-field/error/error.component.spec.ts",
551-
"name": "error.component.spec.ts",
552-
"language": "spec.ts"
553-
},
554-
{
555-
"path": "form-field/error/error.component.ts",
556-
"name": "error.component.ts",
557-
"language": "ts"
558-
}
559-
]
536+
"path": "form-field/error.component.spec.ts",
537+
"name": "error.component.spec.ts",
538+
"language": "spec.ts"
539+
},
540+
{
541+
"path": "form-field/error.component.ts",
542+
"name": "error.component.ts",
543+
"language": "ts"
560544
},
561545
{
562546
"path": "form-field/form-field-error.registry.spec.ts",
@@ -589,30 +573,14 @@
589573
"language": "ts"
590574
},
591575
{
592-
"path": "form-field/label",
593-
"name": "label",
594-
"files": [
595-
{
596-
"path": "form-field/label/label.component.css",
597-
"name": "label.component.css",
598-
"language": "css"
599-
},
600-
{
601-
"path": "form-field/label/label.component.html",
602-
"name": "label.component.html",
603-
"language": "html"
604-
},
605-
{
606-
"path": "form-field/label/label.component.spec.ts",
607-
"name": "label.component.spec.ts",
608-
"language": "spec.ts"
609-
},
610-
{
611-
"path": "form-field/label/label.component.ts",
612-
"name": "label.component.ts",
613-
"language": "ts"
614-
}
615-
]
576+
"path": "form-field/label.component.spec.ts",
577+
"name": "label.component.spec.ts",
578+
"language": "spec.ts"
579+
},
580+
{
581+
"path": "form-field/label.component.ts",
582+
"name": "label.component.ts",
583+
"language": "ts"
616584
}
617585
]
618586
},

projects/ngverse/src/lib/form-field/error/error.component.ts projects/ngverse/src/lib/form-field/error.component.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
33

44
@Component({
55
selector: 'app-error',
6-
imports: [],
7-
templateUrl: './error.component.html',
8-
styleUrl: './error.component.css',
6+
template: '<ng-content></ng-content>',
97
changeDetection: ChangeDetectionStrategy.OnPush,
108
animations: [
119
trigger('toggle', [
@@ -21,6 +19,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
2119
],
2220
host: {
2321
'[@toggle]': 'true',
22+
class: 'text-sm text-danger',
2423
},
2524
})
2625
export class ErrorComponent {}

projects/ngverse/src/lib/form-field/error/error.component.css

-4
This file was deleted.

projects/ngverse/src/lib/form-field/error/error.component.html

-1
This file was deleted.

projects/ngverse/src/lib/form-field/form-field.component.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<div class="relative mb-1.5 flex flex-col gap-1.5 pb-5">
22
<!-- eslint-disable-next-line @angular-eslint/template/label-has-associated-control -->
33
<label class="flex flex-col gap-1.5">
4-
<ng-content select="app-label"></ng-content>
4+
<div class="flex items-center gap-1">
5+
<ng-content select="app-label"></ng-content>
6+
@if (!hideRequiredMarker() && hasRequiredValidation()) {
7+
<span class="text-danger">*</span>
8+
}
9+
</div>
510
<ng-content></ng-content>
611
</label>
712

projects/ngverse/src/lib/form-field/form-field.component.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
} from '@angular/core';
99
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
1010
import { provideNoopAnimations } from '@angular/platform-browser/animations';
11-
import { ErrorComponent } from './error/error.component';
11+
import { ErrorComponent } from './error.component';
1212
import { FormFieldErrorRegistry } from './form-field-error.registry';
1313
import { FormFieldComponent } from './form-field.component';
14-
import { LabelComponent } from './label/label.component';
14+
import { LabelComponent } from './label.component';
1515

1616
describe('FormFieldComponent', () => {
1717
let component: FormFieldTestComponent;

projects/ngverse/src/lib/form-field/form-field.component.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import {
1515
NgControl,
1616
StatusChangeEvent,
1717
TouchedChangeEvent,
18+
Validators,
1819
} from '@angular/forms';
1920
import { filter, merge, Subscription } from 'rxjs';
20-
import { ErrorComponent } from './error/error.component';
21+
import { ErrorComponent } from './error.component';
2122
import { FormFieldErrorRegistry } from './form-field-error.registry';
2223

2324
@Component({
@@ -30,6 +31,7 @@ import { FormFieldErrorRegistry } from './form-field-error.registry';
3031
export class FormFieldComponent implements OnDestroy {
3132
silentErrors = input<string[] | undefined>();
3233
showErrors = input<boolean>(true);
34+
hideRequiredMarker = input<boolean>(false);
3335

3436
errors = signal<string[]>([]);
3537
invalid = signal(false);
@@ -38,6 +40,17 @@ export class FormFieldComponent implements OnDestroy {
3840
() => !this.showErrors() || this.errors().length === 0
3941
);
4042

43+
hasRequiredValidation = computed(() => {
44+
const control = this.ngControl()?.control;
45+
if (control) {
46+
return (
47+
control.hasValidator(Validators.required) ||
48+
control.hasValidator(Validators.requiredTrue)
49+
);
50+
}
51+
return false;
52+
});
53+
4154
private ngControl = contentChild(NgControl);
4255
private sub: Subscription | undefined;
4356
private errorRegistry = inject(FormFieldErrorRegistry);

projects/ngverse/src/lib/form-field/label/label.component.ts projects/ngverse/src/lib/form-field/label.component.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-label',
5-
imports: [],
6-
templateUrl: './label.component.html',
7-
styleUrl: './label.component.css',
5+
template: '<ng-content></ng-content>',
86
changeDetection: ChangeDetectionStrategy.OnPush,
97
})
108
export class LabelComponent {}

projects/ngverse/src/lib/form-field/label/label.component.css

Whitespace-only changes.

projects/ngverse/src/lib/form-field/label/label.component.html

-1
This file was deleted.

0 commit comments

Comments
 (0)