npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-mm

v18.0.0

Published

Developer toolkit for Angular applications

Downloads

83

Readme

codecov

I develop many applications and notice a lot of repeating patterns that I decided to move to a separate library. You can see a lot of connection to PrimeNG as I mainly use it in my Angular applications. If you see potential to be used with another library, don't hesitate to use it. If you find a bug or have an idea for improvement, you can create an issue with a good description.

Versioning

x.y.z

  • x - Angular version
  • y - Generally add new feature(s). It can be breaking changes
  • z - Fixes

Example

|Angular|ngx-mm| Comments | |---|---|-------------------------------------| |14.x|14.x.x| ... | |15.x|15.x.x| ... | |15.x|15.x.2| Fix to suppor | |15.x|15.2.x| Breaking changes or add new feature |

Installation

npm install ngx-mm --save

Components

Forms

Getting started

Import MMFormsModule to your AppModule

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MMFormsModule.forRoot({
      // confgiuration
    }, {
      // providers
    }),
  ],
  providers: [

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now you can use in your forms:

app.component.html

<form [formGroup]="myForm">
  <mm-form-field formControlName="username" label="Username">
    <ng-template mmFormControl let-control>
      <input [formControl]="control">
    </ng-template>
  </mm-form-field>
</form>

app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  myForm: FormGroup;

  constructor(private formBuilder: FormBuilder) {
    this.myForm = this.formBuilder.group({
      username: [{value: null, disabled: false}, Validators.compose([
        Validators.required,
        Validators.minLength(3),
        Validators.maxLength(5)
      ])]
    })
  }
}

Configuration

Create your own implementation of MMErrorMessageResolver.

export class CustomMessageResolver implements MMErrorMessageResolver {
  resolveErrorMessage(key: string, value?: any, formControlName?: string | number | null): string {
    switch (key) {
      case 'required':
        return 'This field is required';
      case 'minlength':
        return `The min length of ${value.requiredLength} characters is not reached, you typed in ${value.actualLength} characters`;
      case 'maxlength':
        return `The max length of ${value.requiredLength} characters is reached, you typed in ${value.actualLength} characters`;
      default:
        return 'This field is invalid'
    }
  }
}

Provide your implementation in global configuration. Additionally, you can also configure the default style, class, and behaviors in the global configuration.

The full list of configurations can be found below

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MMFormsModule.forRoot({
      fieldClass: 'field',
      errorClass: 'text-red-900',
      showMandatory: true,
      mandatorySymbol: '#',
      invalidOnTouch: true,
      invalidOnDirty: false,
    }, {
      errorMessageResolver: CustomMessageResolver
    }),
  ],
  providers: [

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Override global configuration

You have many options for overriding the global configuration:

  • by providers
  • by input
  • by template
By provider

Wherever you can provide providers (module, component, routes) you can override the forms configuration.

...

providers: [
  {
    provide: MM_FORMS_CONFIG,
    useValue: {}
  },
  {
    provide: MMErrorMessageResolver,
    useClass: CustomMessageResolver
  }
]

...
By input

Every possible configuration can be override by component Input (all possible inputs are below).

...

<mm-form-field formControlName="field"
               label="Username"
               class="col-12"
               [showMandatory]="false"
               mandatorySymbol="#" labelStyleClass="text-red-500"
               [invalidOnTouch]="true">
  <ng-template mmFormControl let-control>
    <input pInputText
           [formControl]="control"
           class="w-full"
           [class.ng-dirty]="control?.touched && control?.invalid"
           [class.ng-invalid]="control?.touched && control?.invalid">
  </ng-template>
</mm-form-field>

...
By template

If providers and inputs are not enough, you can use templates to modify the component (all possible templates are below).

...

<mm-form-field formControlName="field"
               label="Username"
               class="col-12"
               [showMandatory]="false"
               mandatorySymbol="#" labelStyleClass="text-red-500"
               [invalidOnTouch]="true">
  <ng-template mmFormControl let-control>
    <input pInputText
           [formControl]="control"
           class="w-full"
           [class.ng-dirty]="control?.touched && control?.invalid"
           [class.ng-invalid]="control?.touched && control?.invalid">
  </ng-template>
  <ng-template mmMmFormLabel let-label>
    MyLabel is {{label}}
  </ng-template>
</mm-form-field>

...

APIs

Inputs (properties)

| Name | Type | Default | Description | |-----------------------|---------|--------|---------------------------------------------------------------------------------------------------| | label | string | '' | Form field label | | id | string | '' | Unique identifier of form field ("for" use case) | | helper | string | '' | Help text for form field | | invalidOnTouch | boolean | true | Show invalid state when form field is touched (use invalidOnTouch or invalidOnDirty not both) | | invalidOnDirty | boolean | false | Show invalid state when form field is dirty (use invalidOnTouch or invalidOnDirty not both) | | reverseLabelControl | boolean | false | Reverse order of label and control form | | style | string | '' | Inline style of field | | styleClass | string | 'field' | Style class of field | | labelStyle | string | '' | Inline style of label | | labelStyleClass | string | '' | Style class of label | | helperStyle | string | '' | Inline style of helper | | helperStyleClass | string | '' | Style class of helper | | errorStyle | string | '' | Inline style of error | | errorStyleClass | string | '' | Style class of error | | mandatorySymbol | string | * | Symbol of mandatory | | mandatoryStyle | string | '' | Inline style of mandatory | | mandatoryClass | string | '' | Style class of mandatory | | showMandatroy | boolean | true | When present show mandatory symbol if form contorol has required validator |

Templates
mmFormControl

Template displaying form control. Required

<ng-template mmFormControl let-control></ng-template>

Context

| Name | Type | Implicit | |---------|-----------------|----------| | control | AbstractControl | true |

mmFormLabel

Template displaying form label.

<ng-template mmFormLabel let-label></ng-template>

Context

| Name | Type | Implicit | |-----------------|---------|----------| | label | string | true | | id | string | false | | mandatory | boolean | false | | showMandatory | boolean | false | | mandatorySymbol | string | false |

mmFormMandatory

Template displaying form mandatory.

<ng-template mmFormMandatory let-mandatory></ng-template>

Context

| Name | Type | Implicit | |-----------------|---------|----------| | mandatory | boolean | true | | showMandatory | boolean | false | | mandatorySymbol | string | false |

mmFormHelper

Template displaying form helper.

<ng-template mmFormHelper let-helper></ng-template>

Context

| Name | Type | Implicit | |-----------------|---------|----------| | helper | string | true |

mmFormError

Template displaying errors

<ng-template mmFormError></ng-template>

Context

| Name | Type | Implicit | |-------------|--------|----------| | message | string | true | | key | string | false | | value | string | false | | controlName | string | false |

Types

MMConfig

export interface MMFormsConfig {
  // Field
  fieldClass?: string;
  style?: string;
  reverseLabelControl?: boolean;

  // Label
  labelClass?: string;
  labelStyle?: string;

  // Helper
  helperClass?: string;
  helperStyle?: string;

  // Error
  errorClass?: string;
  errorStyle?: string;

  // Mandatory
  mandatoryClass?: string;
  mandatoryStyle?: string;
  mandatorySymbol?: string;
  showMandatory?: boolean;

  // Validation
  invalidOnTouch?: boolean;
  invalidOnDirty?: boolean;
}

MMErrorMessageResolver

export abstract class MMErrorMessageResolver {

  public abstract resolveErrorMessage(key: string, value?: any, formControlName?: string | null | number): string;

}