Skip to content

HOA-307 ng forms #31

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/node_modules
npm-debug.log
yarn-error.log
**/node_modules

# IDEs and editors
.idea/
Expand Down
36 changes: 36 additions & 0 deletions apps/ng-forms-example-app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "app",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "app",
"style": "kebab-case"
}
]
},
"extends": [
"plugin:@nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
]
},
{
"files": ["*.html"],
"extends": ["plugin:@nx/angular-template"],
"rules": {}
}
]
}
22 changes: 22 additions & 0 deletions apps/ng-forms-example-app/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'ng-forms-example-app',
preset: '../../jest.preset.cjs',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../coverage/apps/ng-forms-example-app',
transform: {
'^.+\\.(ts|mjs|js|html)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
],
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
};
98 changes: 98 additions & 0 deletions apps/ng-forms-example-app/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"name": "ng-forms-example-app",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"prefix": "app",
"sourceRoot": "apps/ng-forms-example-app/src",
"tags": [],
"targets": {
"build": {
"executor": "@angular-devkit/build-angular:browser",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/apps/ng-forms-example-app",
"index": "apps/ng-forms-example-app/src/index.html",
"main": "apps/ng-forms-example-app/src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "apps/ng-forms-example-app/tsconfig.app.json",
"assets": [
"apps/ng-forms-example-app/src/favicon.ico",
"apps/ng-forms-example-app/src/assets"
],
"styles": [
"apps/ng-forms-example-app/src/styles.scss",
"@angular/material/prebuilt-themes/indigo-pink.css"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"executor": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "ng-forms-example-app:build:production"
},
"development": {
"browserTarget": "ng-forms-example-app:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"executor": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "ng-forms-example-app:build"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"apps/ng-forms-example-app/**/*.ts",
"apps/ng-forms-example-app/**/*.html"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/ng-forms-example-app/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
}
}
5 changes: 5 additions & 0 deletions apps/ng-forms-example-app/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="container">
<mat-card>
<app-user-form class="form"></app-user-form>
</mat-card>
</div>
12 changes: 12 additions & 0 deletions apps/ng-forms-example-app/src/app/app.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
height: auto;
margin: 0;
}

.form {
padding: 15px;
}
13 changes: 13 additions & 0 deletions apps/ng-forms-example-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from '@angular/core';
import { ControlComponent } from './control/control.component';
import { UserFormComponent } from './user-form/user-form.component';
import { MatCardModule } from '@angular/material/card';

@Component({
standalone: true,
imports: [ControlComponent, UserFormComponent, MatCardModule],
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {}
6 changes: 6 additions & 0 deletions apps/ng-forms-example-app/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

export const appConfig: ApplicationConfig = {
providers: [importProvidersFrom(BrowserAnimationsModule)],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<mat-form-field>
<mat-label>{{ label }}</mat-label>
<input matInput [formControl]="control" />
</mat-form-field>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:host {
display: flex;
flex-direction: column;
align-items: flex-start;
}
40 changes: 40 additions & 0 deletions apps/ng-forms-example-app/src/app/control/control.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component, forwardRef, Input, OnInit } from '@angular/core';
import { FormControlComponent } from '@house-of-angular/ng-forms';
import {
ControlValueAccessor,
NG_VALUE_ACCESSOR,
ReactiveFormsModule,
} from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import {
MAT_FORM_FIELD_DEFAULT_OPTIONS,
MatFormFieldModule,
} from '@angular/material/form-field';

@Component({
selector: 'app-control',
standalone: true,
imports: [ReactiveFormsModule, MatFormFieldModule, MatInputModule],
templateUrl: './control.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ControlComponent),
multi: true,
},
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: { appearance: 'outline' },
},
],
styleUrls: ['./control.component.scss'],
})
export class ControlComponent
extends FormControlComponent<string>
implements OnInit, ControlValueAccessor
{
@Input({ required: true }) label!: string;
registerOnChange: (value: string) => void = () => '';
registerOnTouched: () => void = () => '';
writeValue: () => void = () => '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div [formGroup]="form">
<app-control label="City" [formControl]="cityControl"></app-control>
<app-control label="Street" [formControl]="streetControl"></app-control>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
// eslint-disable-next-line @nrwl/nx/enforce-module-boundaries
import { FormGroupComponent } from '../../../../../../projects/ng-forms/src/lib/ng-forms/form-group/form-group.directive';
import { ControlComponent } from '../../control/control.component';
// eslint-disable-next-line @nrwl/nx/enforce-module-boundaries
import { NullableFormControl } from '../../../../../../projects/ng-forms/src/lib/ng-forms/types';

export interface AddressForm {
city: string;
street: string;
}

@Component({
selector: 'app-address-form',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, ControlComponent, FormsModule],
templateUrl: './address-form.component.html',
styleUrls: ['./address-form.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AddressFormComponent extends FormGroupComponent<AddressForm> {
get cityControl() {
return this.form.controls['city'] as NullableFormControl<string>;
}

get streetControl() {
return this.form.controls['street'] as NullableFormControl<string>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<form [formGroup]="userForm">
<app-control label="Name" [formControl]="nameControl"></app-control>
<app-control label="Surname" formControlName="surname"></app-control>
<app-address-form [formGroup]="addressGroup"></app-address-form>
</form>
Empty file.
22 changes: 22 additions & 0 deletions apps/ng-forms-example-app/src/app/user-form/user-form.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Component} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {ControlComponent} from '../control/control.component';
import {AddressForm, AddressFormComponent,} from './address-form/address-form.component';
import {FormGroupComponent} from "@house-of-angular/ng-forms";

export interface UserDetailsForm {
name: string;
surname?: string;
address: AddressForm;
}

@Component({
selector: 'app-user-form',
standalone: true,
imports: [ReactiveFormsModule, ControlComponent, AddressFormComponent],
templateUrl: './user-form.component.html',
styleUrls: ['./user-form.component.scss'],
})
export class UserFormComponent
extends FormGroupComponent<UserDetailsForm>
{}
Empty file.
Binary file added apps/ng-forms-example-app/src/favicon.ico
Binary file not shown.
13 changes: 13 additions & 0 deletions apps/ng-forms-example-app/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>ng-forms-example-app</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>
7 changes: 7 additions & 0 deletions apps/ng-forms-example-app/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);
1 change: 1 addition & 0 deletions apps/ng-forms-example-app/src/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */
8 changes: 8 additions & 0 deletions apps/ng-forms-example-app/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
};
import 'jest-preset-angular/setup-jest';
10 changes: 10 additions & 0 deletions apps/ng-forms-example-app/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
}
7 changes: 7 additions & 0 deletions apps/ng-forms-example-app/tsconfig.editor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"types": ["jest", "node"]
}
}
Loading