Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Start work on spot-form-field

Benjamin Bädorfc5313bee4145
diff --git a/frontend/src/app/spot/components/form-field/form-binding.directive.ts b/frontend/src/app/spot/components/form-field/form-binding.directive.ts
new file mode 100644
index 00000000000..1120c707b14
--- /dev/null
+++ b/frontend/src/app/spot/components/form-field/form-binding.directive.ts
@@ -0,0 +1,24 @@
+import { Directive, forwardRef, Input } from '@angular/core';
+import {
+ FormArray, FormControl, FormGroup, NgControl,
+} from '@angular/forms';
+
+export const formControlBinding:any = {
+ provide: NgControl,
+ useExisting: forwardRef(() => SpotFormBindingDirective),
+};
+
+@Directive({
+ selector: '[spotFormBinding]',
+ providers: [formControlBinding],
+ exportAs: 'ngForm',
+})
+export class SpotFormBindingDirective extends NgControl {
+ @Input('spotFormBinding') form!:FormControl|FormGroup|FormArray;
+
+ get control():FormControl|FormGroup|FormArray {
+ return this.form;
+ }
+
+ viewToModelUpdate():void {}
+}
diff --git a/frontend/src/app/spot/components/form-field/form-field.component.html b/frontend/src/app/spot/components/form-field/form-field.component.html
new file mode 100644
index 00000000000..e8cb875d7d2
--- /dev/null
+++ b/frontend/src/app/spot/components/form-field/form-field.component.html
@@ -0,0 +1,48 @@
+<ng-container *ngIf="!hidden">
+ <label class="spot-form-field--label-wrap">
+ <div class="spot-form-field--label">
+ <span
+ *ngIf="showErrorMessage"
+ class="Hidden for sighted"
+ >Invalid</span>
+ {{ label }}
+ <span *ngIf="required" class="spot-form-field--label-indicator">*</span>
+ <attribute-help-text
+ [attribute]="helpTextAttribute"
+ [attributeScope]="helpTextAttributeScope"
+ ></attribute-help-text>
+ </div>
+
+ <ng-container *ngIf="!noWrapLabel">
+ <ng-container *ngTemplateOutlet="inputTemplate"></ng-container>
+ </ng-container>
+ </label>
+
+ <ng-container *ngIf="noWrapLabel">
+ <ng-container *ngTemplateOutlet="inputTemplate"></ng-container>
+ </ng-container>
+
+ <div
+ class="spot-form-field--description"
+ [id]="descriptionID"
+ >
+ <ng-content select="[slot=description]"></ng-content>
+ </div>
+
+ <div
+ class="spot-form-field--errors"
+ *ngIf="showErrorMessage"
+ [id]="errorsID"
+ >
+ <ng-content select="[slot=errors]"></ng-content>
+ </div>
+</ng-container>
+
+<ng-template #inputTemplate>
+ <div
+ class="spot-form-field--input"
+ [attr.aria-describedby]="describedByID"
+ >
+ <ng-content select="[slot=input]"></ng-content>
+ </div>
+</ng-template>
diff --git a/frontend/src/app/spot/components/form-field/form-field.component.ts b/frontend/src/app/spot/components/form-field/form-field.component.ts
new file mode 100644
index 00000000000..057eab85cdf
--- /dev/null
+++ b/frontend/src/app/spot/components/form-field/form-field.component.ts
@@ -0,0 +1,72 @@
+import {
+ Component, ContentChild, HostBinding, Input, Optional,
+} from '@angular/core';
+import { AbstractControl, FormGroupDirective, NgControl } from '@angular/forms';
+
+@Component({
+ selector: 'spot-form-field',
+ templateUrl: './form-field.component.html',
+})
+export class SpotFormFieldComponent {
+ @HostBinding('class.spot-form-field') className = true;
+
+ @HostBinding('class.spot-form-field_invalid') get errorClassName() {
+ return this.showErrorMessage;
+ }
+
+ @Input() label = '';
+
+ @Input() noWrapLabel = false;
+
+ @Input() required = false;
+
+ @Input() hidden = false;
+
+ @Input() control?:AbstractControl;
+
+ @Input() helpTextAttribute?:string;
+
+ @Input() helpTextAttributeScope?:string;
+
+ @Input() showValidationErrorOn:'change' | 'blur' | 'submit' | 'never' = 'submit';
+
+ @ContentChild(NgControl) ngControl:NgControl;
+
+ internalID = `spot-form-field-${+new Date()}`;
+
+ get errorsID() {
+ return `${this.internalID}-errors`;
+ }
+
+ get descriptionID() {
+ return `${this.internalID}-description`;
+ }
+
+ get describedByID() {
+ return this.showErrorMessage ? this.errorsID : this.descriptionID;
+ }
+
+ get formControl():AbstractControl|undefined|null {
+ return this.ngControl?.control || this.control;
+ }
+
+ get showErrorMessage():boolean {
+ if (!this.formControl) {
+ return false;
+ }
+
+ if (this.showValidationErrorOn === 'submit') {
+ return this.formControl.invalid && this._formGroupDirective?.submitted;
+ } if (this.showValidationErrorOn === 'blur') {
+ return this.formControl.invalid && this.formControl.touched;
+ } if (this.showValidationErrorOn === 'change') {
+ return this.formControl.invalid && this.formControl.dirty;
+ }
+
+ return false;
+ }
+
+ constructor(
+ @Optional() private _formGroupDirective:FormGroupDirective,
+ ) {}
+}
diff --git a/frontend/src/app/spot/components/text-field/text-field.component.ts b/frontend/src/app/spot/components/text-field/text-field.component.ts
index 0778605e1c1..37fb92d8d64 100644
--- a/frontend/src/app/spot/components/text-field/text-field.component.ts
+++ b/frontend/src/app/spot/components/text-field/text-field.component.ts
@@ -38,7 +38,7 @@ export class SpotTextFieldComponent implements ControlValueAccessor {
@Input() name = `spot-text-field-${+(new Date())}`;
- @HostBinding('class.spot-text-field_disabled') @Input() disabled = false;
+ @Input() @HostBinding('class.spot-text-field_disabled') public disabled = false;
@Input() showClearButton = true;
diff --git a/frontend/src/app/spot/spot-docs.component.html b/frontend/src/app/spot/spot-docs.component.html
index 4e50f9b69d5..b64d1dfe4f6 100644
--- a/frontend/src/app/spot/spot-docs.component.html
+++ b/frontend/src/app/spot/spot-docs.component.html
@@ -429,6 +429,35 @@ Currently selected value is {{ toggleValue }}
class="spot-icon spot-icon_bell"
></span>
</spot-text-field>
+
+ <spot-text-field
+ placeholder="Disabled"
+ [disabled]="true"
+ >
+ <span
+ slot="before"
+ class="spot-icon spot-icon_bell"
+ ></span>
+ </spot-text-field>
+</section>
+
+<h1>Form Field</h1>
+
+<section class="space-children">
+ <spot-form-field
+ label="Username"
+ required="true"
+ >
+ <spot-text-field
+ slot="input"
+ [(ngModel)]="usernameValue"
+ >
+ <span
+ slot="before"
+ class="spot-icon spot-icon_user"
+ ></span>
+ </spot-text-field>
+ </spot-form-field>
</section>
<h1>Drop Modal</h1>
diff --git a/frontend/src/app/spot/spot-docs.component.ts b/frontend/src/app/spot/spot-docs.component.ts
index 2dff05bcd39..91877019625 100644
--- a/frontend/src/app/spot/spot-docs.component.ts
+++ b/frontend/src/app/spot/spot-docs.component.ts
@@ -15,6 +15,8 @@ export class SpotDocsComponent {
textFieldValue = 'ngModel value';
+ usernameValue = '';
+
dropModalOpen = false;
dropModalAlignment = 'bottom-left';
diff --git a/frontend/src/app/spot/spot.module.ts b/frontend/src/app/spot/spot.module.ts
index c8ccdd98554..071f1dcfe67 100644
--- a/frontend/src/app/spot/spot.module.ts
+++ b/frontend/src/app/spot/spot.module.ts
@@ -11,6 +11,8 @@ import { SpotTextFieldComponent } from './components/text-field/text-field.compo
import { SpotFilterChipComponent } from './components/filter-chip/filter-chip.component';
import { SpotDropModalComponent } from './components/drop-modal/drop-modal.component';
import { SpotTooltipComponent } from './components/tooltip/tooltip.component';
+import { SpotFormFieldComponent } from './components/form-field/form-field.component';
+import { SpotFormBindingDirective } from './components/form-field/form-binding.directive';
import { SpotDocsComponent } from './spot-docs.component';
@NgModule({
@@ -31,6 +33,8 @@ import { SpotDocsComponent } from './spot-docs.component';
SpotTextFieldComponent,
SpotFilterChipComponent,
SpotDropModalComponent,
+ SpotFormFieldComponent,
+ SpotFormBindingDirective,
SpotTooltipComponent,
],
exports: [
@@ -39,6 +43,8 @@ import { SpotDocsComponent } from './spot-docs.component';
SpotTextFieldComponent,
SpotFilterChipComponent,
SpotDropModalComponent,
+ SpotFormFieldComponent,
+ SpotFormBindingDirective,
SpotTooltipComponent,
],
})
diff --git a/frontend/src/app/spot/styles/sass/components/form-field.children.sass b/frontend/src/app/spot/styles/sass/components/form-field.children.sass
new file mode 100644
index 00000000000..f7d19e1e1f0
--- /dev/null
+++ b/frontend/src/app/spot/styles/sass/components/form-field.children.sass
@@ -0,0 +1,7 @@
+.spot-form-field_invalid
+ .ng-select-container,
+ .spot-input,
+ .spot-option-list--item,
+ .document-editor__editable-container,
+ .ck.ck-toolbar
+ border-color: var(--content-form-error-color)
diff --git a/frontend/src/app/spot/styles/sass/components/form-field.sass b/frontend/src/app/spot/styles/sass/components/form-field.sass
new file mode 100644
index 00000000000..886c3f0f737
--- /dev/null
+++ b/frontend/src/app/spot/styles/sass/components/form-field.sass
@@ -0,0 +1,48 @@
+@import 'form-field.children'
+
+.spot-form-field
+ display: flex
+ flex-direction: column
+ line-height: 1.6rem
+
+ // This is a fix for the formly-forms, which render the wrapper around a non-visible input
+ &:empty
+ display: none
+
+ &--label-wrap
+ display: flex
+ flex-direction: column
+ margin: 0
+
+ &--label
+ display: flex
+ flex-direction: row
+ align-items: center
+ min-height: $spot-spacing-1_5
+ margin-bottom: $spot-spacing-0_25
+
+ @include spot-body-small
+ color: $spot-color-basic-gray-1
+
+ &-indicator
+ color: $spot-color-basic-black
+
+ &_invalid &--label
+ color: var(--content-form-error-color)
+
+ &--description
+ font-size: 12px
+
+ &--errors
+ display: flex
+ flex-direction: column
+
+ &--error
+ color: var(--content-form-error-color)
+ margin-bottom: 1rem
+ font-size: 0.8rem
+
+ &--description,
+ &--errors
+ &:empty
+ display: none
diff --git a/frontend/src/app/spot/styles/sass/components/index.sass b/frontend/src/app/spot/styles/sass/components/index.sass
index 778eada86c6..e9655e54774 100644
--- a/frontend/src/app/spot/styles/sass/components/index.sass
+++ b/frontend/src/app/spot/styles/sass/components/index.sass
@@ -12,3 +12,5 @@
@import './modal'
@import './action-bar'
@import './tooltip'
+@import './form-field'
+@import './form-field.children'
diff --git a/frontend/src/app/spot/styles/sass/components/text-field.sass b/frontend/src/app/spot/styles/sass/components/text-field.sass
index 0a41c8f95a3..ad19a844453 100644
--- a/frontend/src/app/spot/styles/sass/components/text-field.sass
+++ b/frontend/src/app/spot/styles/sass/components/text-field.sass
@@ -18,9 +18,17 @@
color: $spot-color-basic-black
@include spot-focus
+ &:disabled,
&_disabled
+ color: $spot-color-basic-gray-3
border-color: $spot-color-basic-gray-4
+ &:disabled,
+ &_disabled &--input
+ &,
+ &:hover
+ background-color: $spot-color-basic-white
+
&--input
background: transparent
border: 0