{"id":16099634,"url":"https://github.com/lppedd/ngx-sub-form-test","last_synced_at":"2025-04-05T23:29:52.792Z","repository":{"id":104057279,"uuid":"176493137","full_name":"lppedd/ngx-sub-form-test","owner":"lppedd","description":"Temporary repo","archived":false,"fork":false,"pushed_at":"2019-03-19T11:12:38.000Z","size":169,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T05:56:31.771Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lppedd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-19T11:05:23.000Z","updated_at":"2022-02-22T09:01:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"dcb46e22-4c36-4ee9-b35b-caa6cc5e31b1","html_url":"https://github.com/lppedd/ngx-sub-form-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lppedd%2Fngx-sub-form-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lppedd%2Fngx-sub-form-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lppedd%2Fngx-sub-form-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lppedd%2Fngx-sub-form-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lppedd","download_url":"https://codeload.github.com/lppedd/ngx-sub-form-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247415783,"owners_count":20935383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T18:43:50.223Z","updated_at":"2025-04-05T23:29:52.762Z","avatar_url":"https://github.com/lppedd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NgxSubForm\n\n![ngx-sub-form logo](https://user-images.githubusercontent.com/4950209/53812385-45f48900-3f53-11e9-8687-b57cd335f26e.png)\n\nUtility library for breaking down a form into multiple components.  \nWorks well with polymorphic data structures.\n\n`ngx-sub-form` is here to help you avoid passing your `formGroup` as inputs and tackle down the boilerplate of creating a custom [`ControlValueAccessor`](https://angular.io/api/forms/ControlValueAccessor).\n\n## Install\n\nInstall the [npm-package](https://www.npmjs.com/package/ngx-sub-form):\n\n## Usage\n\n_Before we get started with how to use the library and give some examples, a complete demo is available on [this repo](https://github.com/cloudnc/ngx-sub-form), within the `src` folder.  \nDemo is built around a concept of galactic sales. You can sell either Droids (Protocol, Medical, Astromech, Assassin) or Vehicles (Spaceship, Speeder). This will also be used for the following examples_.\n\n### First component level\n\nWithin the component where the form will be handled, we have to define the top level structure of the form, with _each\npolymorphic type having it's own form control_\n\n```ts\npublic listingForm: FormGroup = new FormGroup({\n  droidListing: new FormControl(null, Validators.required),\n  vehicleListing: new FormControl(null, Validators.required),\n  listingType: new FormControl(null, Validators.required)\n});\n```\n\nand give access to our `enum` from the component:\n\n```ts\npublic ListingType = ListingType;\n```\n\nJust as a sidenote here, here's the `ListingType` enum:\n\n```ts\nexport enum ListingType {\n  VEHICLE = 'Vehicle',\n  DROID = 'Droid',\n}\n```\n\nThen, within the `.component.html` we create a `select` tag to choose between the 2 types:\n\n```html\n\u003cselect formControlName=\"listingType\"\u003e\n  \u003coption *ngFor=\"let listingType of ListingType | keyvalue\" [value]=\"listingType.value\"\u003e\n    {{ listingType.value }}\n  \u003c/option\u003e\n\u003c/select\u003e\n```\n\nNow we need to create, based on the listing type, either a `DroidListingComponent` or a `VehicleListingComponent`:\n\n```html\n\u003cform [formGroup]=\"listingForm\"\u003e\n  \u003cdiv [ngSwitch]=\"listingForm.get(formControlNames.listingType).value\"\u003e\n    \u003capp-droid-listing *ngSwitchCase=\"ListingType.DROID\" formControlName=\"droidListing\"\u003e\u003c/app-droid-listing\u003e\n    \u003capp-vehicle-listing *ngSwitchCase=\"ListingType.VEHICLE\" formControlName=\"vehicleListing\"\u003e\u003c/app-vehicle-listing\u003e\n  \u003c/div\u003e\n\n  \u003cbutton mat-raised-button (click)=\"upsertListing(listingForm.value)\" [disabled]=\"listingForm.invalid\"\u003e\n    Upsert\n  \u003c/button\u003e\n\u003c/form\u003e\n```\n\nOne thing to notice above:\n\n- `formControlName=\"droidListing\"` our sub form component **IS** a custom `ControlValueAccessor` and let us bind our component to a `formControlName` as we would with an input.\n\n### Second component level\n\nThis is where `ngx-sub-form` is becoming useful. All you have to do is:\n\nAdd required providers using the utility function `subformComponentProviders`:\n\n```diff\n+import { subformComponentProviders } from 'ngx-sub-form';\n\n@Component({\n  selector: 'app-vehicle-listing',\n  templateUrl: './vehicle-listing.component.html',\n  styleUrls: ['./vehicle-listing.component.scss'],\n+ providers: subformComponentProviders(VehicleListingComponent),\n})\nexport class VehicleListingComponent {}\n```\n\nMake your original class extend `NgxSubFormComponent` _or_ `NgxSubFormRemapComponent` if you need to remap the data (see below):\n\n```diff\n-import { subformComponentProviders } from 'ngx-sub-form';\n+import { subformComponentProviders, NgxSubFormComponent } from 'ngx-sub-form';\n\n-export class VehicleListingComponent {}\n+export class VehicleListingComponent extends NgxSubFormComponent {}\n```\n\nDefine the controls of your form:\n\n```ts\nprotected formControls: Controls\u003cVehicleListing\u003e = {\n  id: new FormControl(this.uuidService.generate(), Validators.required),\n  price: new FormControl(null, Validators.required),\n};\n\n```\n\n_Simplified from the original example into src folder to keep the example as minimal as possible._\n\nAs you know, [Angular reactive forms are not strongly typed](https://github.com/angular/angular/issues/13721). We're providing an interface (`Controls\u003cT\u003e`) to at least set the correct names within the form (but it will not help you when using `form.get('...').value`). It is still very useful and when making a refactor if your data structure changes and do not match the form structure Typescript compilation will fail.\n\nThe NgxFormComponent base class automatically extracts your form control names and exposes them as a public member on `formControlNames`\n\nThen within the `.html`, you can reference them like so:\n\n```html\n\u003cfieldset [formGroup]=\"formGroup\" class=\"container\"\u003e\n  \u003cinput type=\"text\" placeholder=\"ID\" [formControlName]=\"formControlNames.id\" /\u003e\n\n  \u003cinput type=\"number\" placeholder=\"Price\" [formControlName]=\"formControlNames.price\" /\u003e\n\u003c/fieldset\u003e\n```\n\n### Remapping Data\n\nIt is a frequent pattern to have the data that you're trying to modify in a format that is incovenient to the angular\nforms structural constraints. For this reason, ngx-form-component offers a separate extended class `NgxSubFormRemapComponent`\nwhich will require you to define two interfaces - one to model the data going in to the form (which will be applied\ninternally as `form.setValue()`), and the other to describe the interface of the value that will be set on the form.\n\n**You're always better off making your data structure better suit Angular forms, than abusing forms to fit your data pattern**\n\nFor a complete example of this see `https://github.com/cloudnc/ngx-sub-form/blob/master/src/app/main/listing/vehicle-listing/vehicle-product.component.ts` (repeated below):\n\n```ts\ninterface OneVehicleForm {\n  speeder: Speeder;\n  spaceship: Spaceship;\n  vehicleType: VehicleType;\n}\n\n@Component({\n  selector: 'app-vehicle-product',\n  templateUrl: './vehicle-product.component.html',\n  styleUrls: ['./vehicle-product.component.scss'],\n  providers: subformComponentProviders(VehicleProductComponent),\n})\nexport class VehicleProductComponent extends NgxSubFormRemapComponent\u003cOneVehicle, OneVehicleForm\u003e {\n  protected formControls: Controls\u003cOneVehicleForm\u003e = {\n    speeder: new FormControl(null),\n    spaceship: new FormControl(null),\n    vehicleType: new FormControl(null, { validators: [Validators.required] }),\n  };\n\n  public VehicleType = VehicleType;\n\n  protected transformToFormGroup(obj: OneVehicle): OneVehicleForm {\n    return {\n      speeder: obj.vehicleType === VehicleType.SPEEDER ? obj : null,\n      spaceship: obj.vehicleType === VehicleType.SPACESHIP ? obj : null,\n      vehicleType: obj.vehicleType,\n    };\n  }\n\n  protected transformFromFormGroup(formValue: OneVehicleForm): OneVehicle {\n    switch (formValue.vehicleType) {\n      case VehicleType.SPEEDER:\n        return formValue.speeder;\n      case VehicleType.SPACESHIP:\n        return formValue.spaceship;\n    }\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flppedd%2Fngx-sub-form-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flppedd%2Fngx-sub-form-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flppedd%2Fngx-sub-form-test/lists"}