0

I'm using a stepper from the angular material and I'm unsure how best to make a component for each step of this stepper.

Because I have a lot of difficulty in shared forms. I wanted to know the best way to do this.

In the main component of the stepper, get the values of each step and at the end make a post with the 3 forms of the step. As you can see, the code is already getting too big, so the ideal would be to make a component for each step

Componente TS
  
  companyInfo!: FormGroup;
  contactInfo!: FormGroup;
  selectedType: boolean = false;
  
  constructor(public fb: FormBuilder) { }

  ngOnInit(): void {
    this.initCompanyInfo();
    this.initContactInfo();
    console.log(this.companyInfo.get('industry')?.value)
  }
  initCompanyInfo(){
    this.companyInfo = this.fb.group({
      companyName: ['', Validators.required],
      industry: ['', Validators.required],
      country: ['', Validators.required],
    })
  }
  initContactInfo() {
    this.contactInfo = this.fb.group({
      contact: this.fb.array([this.createContact])
    })
  }
  createContact(): FormGroup {
    return this.fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      position: ['', Validators.required],
      email: ['', Validators.required],
      confirmEmail: ['', Validators.required],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required],
      phoneNumber: ['', Validators.required],
    })
  }
<mat-horizontal-stepper [labelPosition]="'bottom'" linear>
  <mat-progress-bar [value]=progressValue></mat-progress-bar>

  <!-- STEP 1 -->

  <mat-step label="COMPANY INFORMATION">
    <mat-card>
      <div class="card-header">
        <p>Company Info</p>
        <button mat-raised-button matStepperNext (click)="additionProgress()">Next</button>
      </div>
      <mat-card-content>
        <form [formGroup]="companyInfo">
            <div class="main-right">
              <div class="header-section">
                <span>STATE</span>
              </div>
              <div class="form">
                <mat-form-field class="width6" appearance="outline">
                  <mat-label>COUNTRY</mat-label>
                  <input matInput formControlName="country">
                  <mat-error></mat-error>
                </mat-form-field>

                <mat-form-field appearance="outline">
                  <mat-label>STATE</mat-label>
                  <input matInput formControlName="state">
                  <mat-error></mat-error>
                </mat-form-field>

                <mat-form-field appearance="outline">
                  <mat-label>CITY</mat-label>
                  <input matInput formControlName="city">
                  <mat-error></mat-error>
                </mat-form-field>

                <mat-form-field class="width6" appearance="outline">
                  <mat-label>ADDRESS</mat-label>
                  <input matInput formControlName="address">
                  <mat-error></mat-error>
                </mat-form-field>

                <mat-form-field class="width8" appearance="outline">
                  <mat-label>ZIPCODE</mat-label>
                  <input matInput type="number" formControlName="zipcode">
                  <mat-error></mat-error>
                </mat-form-field>
              </div>
            </div>
          </div>
          <div class="main-form">
            <div class="main-left">
              <div class="header-section">
                <span>COMPANY</span>
              </div>

              </div>
            </div>
          </div>
        </form>
      </mat-card-content>
    </mat-card>
  </mat-step>
  
  
  <!-- STEP 2 -->
  <mat-step label="CONTACT INFORMATION">
    <p>Opa2</p>
    <button mat-raised-button matStepperPrevious (click)="subtractionProgress()">Previous</button>
    <button mat-raised-button matStepperNext (click)="additionProgress()">Next</button>
  </mat-step>


  <!-- STEP 3 -->
  <mat-step label="GENERAL INFORMATION">
    <p>Opa3</p>
    <button mat-raised-button matStepperPrevious (click)="subtractionProgress()">Previous</button>
  </mat-step>
</mat-horizontal-stepper>

1
  • Just each component has an @Input() that was the "form" Commented Aug 17, 2021 at 6:13

1 Answer 1

1

you can use a SERVICE and BehaviorSubject to share the FormGroup objects among multiple components... check the following demo on stackblitz...

https://stackblitz.com/edit/angular-ivy-cd6bup

demo_app

Sign up to request clarification or add additional context in comments.

3 Comments

you can simply create the formGroup in main.component and pass as input to the components. See your forked stackblitz
of course there are many solutions out there when talking JavaScript !
Man, I love your idea, but when I try to implement it, it's in formGroup Type 'FormGroup | null' is not assignable to type 'FormGroup'. Type 'null' is not assignable to type 'FormGroup'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.