Skip to content
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

feat: 🎸 form builder + components shared library #74

Closed
Closed
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
36 changes: 36 additions & 0 deletions libs/shared/components/.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": "cli",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "cli",
"style": "kebab-case"
}
]
},
"extends": [
"plugin:@nrwl/nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
]
},
{
"files": ["*.html"],
"extends": ["plugin:@nrwl/nx/angular-template"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/shared/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# shared-components
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recreate the lib as "dynamic-form" outside of shared (shared is for things that are also used in the daemon)
im also suggesting dynamic-form instead of components because this lib will also have custom fields, validators, etc


This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test shared-components` to execute the unit tests.
22 changes: 22 additions & 0 deletions libs/shared/components/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'shared-components',
preset: '../../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
coverageDirectory: '../../../coverage/libs/shared/components',
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
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',
],
};
7 changes: 7 additions & 0 deletions libs/shared/components/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/libs/shared/components",
"lib": {
"entryFile": "src/index.ts"
}
}
12 changes: 12 additions & 0 deletions libs/shared/components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@angular-cli-gui/shared/components",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^15.1.0",
"@angular/core": "^15.1.0"
},
"dependencies": {
"tslib": "^2.3.0"
},
"sideEffects": false
}
50 changes: 50 additions & 0 deletions libs/shared/components/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "shared-components",
"$schema": "../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "libs/shared/components/src",
"prefix": "cli",
"targets": {
"build": {
"executor": "@nrwl/angular:ng-packagr-lite",
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
"options": {
"project": "libs/shared/components/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/shared/components/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "libs/shared/components/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/shared/components/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/shared/components/**/*.ts",
"libs/shared/components/**/*.html"
]
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions libs/shared/components/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/form-builder/form-builder.component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<formly-form
[model]="formData"
[fields]="formFields"
[options]="formOptions"
[form]="form"
></formly-form>
<button mat-button type="submit">Submit</button>
</form>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { FormBuilderComponent } from './form-builder.component';

describe('FormBuilderComponent', () => {
let component: FormBuilderComponent;
let fixture: ComponentFixture<FormBuilderComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FormBuilderComponent],
}).compileComponents();

fixture = TestBed.createComponent(FormBuilderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CommonModule } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
Output,
} from '@angular/core';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import {
IFormData,
IFormFields,
IFormOptions,
} from '@angular-cli-gui/shared/data';
import { FormlyModule } from '@ngx-formly/core';
import { FormlyMaterialModule } from '@ngx-formly/material';

@Component({
selector: 'cli-form-builder',
standalone: true,
imports: [
CommonModule,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove and import specific parts of it if needed

FormlyModule,
FormlyMaterialModule,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be imported only where you do FormlyModule.forRoot

ReactiveFormsModule,
MatButtonModule,
],
templateUrl: './form-builder.component.html',
styleUrls: ['./form-builder.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FormBuilderComponent {
@Input() formData: IFormData = {};
@Input() formFields: IFormFields = [];
@Input() formOptions: IFormOptions = {};

form = new FormGroup({});

@Output() formSubmit = new EventEmitter<IFormData>();

public onSubmit(): void {
this.formSubmit.emit(this.formData);
}
}
1 change: 1 addition & 0 deletions libs/shared/components/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-preset-angular/setup-jest';
29 changes: 29 additions & 0 deletions libs/shared/components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"extends": "../../../tsconfig.base.json",
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
17 changes: 17 additions & 0 deletions libs/shared/components/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": []
},
"exclude": [
"src/test-setup.ts",
"src/**/*.spec.ts",
"jest.config.ts",
"src/**/*.test.ts"
],
"include": ["src/**/*.ts"]
}
7 changes: 7 additions & 0 deletions libs/shared/components/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {}
}
15 changes: 15 additions & 0 deletions libs/shared/components/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
1 change: 1 addition & 0 deletions libs/shared/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lib/exec-result.interface';
export * from './lib/generate-component-args.interface';
export * from './lib/directory.interface';
export * from './lib/form-builder.interface';
11 changes: 11 additions & 0 deletions libs/shared/data/src/lib/form-builder.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, unless you're making your own interfaces and mappers between these types, I don't see any benefit of this file and IMO we should use formly models directly


export interface IFormData {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the I prefix for interfaces, if its done in other interfaces keep it but if not remove it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me either. The thing is that in a bunch of cases, we need Dto class and exactly the same interface for the FE. So, I had to add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant interface, its equivalent to Record<string, any>

[key: string]: any;
}

export type IFormField = FormlyFieldConfig;

export type IFormFields = Array<IFormField>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant, just do IFormField[] where its needed (prefer to be explicit as its more readable IMO)


export type IFormOptions = FormlyFormOptions;
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"@angular-cli-gui/configuration": ["libs/configuration/src/index.ts"],
"@angular-cli-gui/executors": ["libs/executors/src/index.ts"],
"@angular-cli-gui/generators": ["libs/generators/src/index.ts"],
"@angular-cli-gui/shared/components": [
"libs/shared/components/src/index.ts"
],
"@angular-cli-gui/shared/data": ["libs/shared/data/src/index.ts"],
"@angular-cli-gui/workspace-manager": [
"libs/workspace-manager/src/index.ts"
Expand Down