Coder Social home page Coder Social logo

ngx-require-inputs's Introduction

ngx-require-inputs

Decorator for declarative enforcing of Angular component inputs

Install

npm i -d ngx-require-inputs

Basic Usage

import { RequireInputs } from 'ngx-require-inputs';

@RequireInputs<PersonComponent>(['name'])
@Component({
  selector: 'person',
  // config ...
})
export class PersonComponent {
  @Input() public name: string;
}

Calling the component with the following HTML will console.warn that "name" has not been defined

<person></person>

But this will not:

<person [name]="'Giancarlo'"></person

Config

Options interface:

{
    default?: { [k in keyof T]?: any };
    onDoCheck?: boolean;
    severity?: Severity ('throw' | 'log');
    message?: ((arg: string) => string) | string;
}
  • default: object of properties to assign a default value to the options required. This will not log an error.
  • onDoCheck: this will also run the validation on every change, rather than only on ngOnInit. To make this work with AOT, make sure you do define ngDoCheck in your component, even if empty.
  • severity: by default the log will only be a warning, but if set as 'throw' (or Severity.Throw), the runtime will throw an error instead. Useful in dev mode.
  • message: Either a string or a function that takes the property name. By default it is (arg: string) => Property "${arg}" is required.

Example

import { RequireInputs, Severity } from 'ngx-require-inputs';

@RequireInputs<PersonComponent>(['name'], {
  default: {
    name: 'Me'
  },
  onDoCheck: true,
  severity: Severity.Throw,
  message: (arg: string) => `Ehi, ${arg} is required!`
})
@Component({
  selector: 'person'
})
export class PersonComponent {
  @Input() public name: string;

  ngOnInit() {
    console.log('On Init called');
  }

  ngDoCheck() {
    console.log('ngDoCheck called');
  }
}

ngx-require-inputs's People

Contributors

gbuomprisco avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.