5.11.0 • Published 6 months ago

@jaspero/ng-helpers v5.11.0

Weekly downloads
358
License
-
Repository
github
Last release
6 months ago

semantic-release CircleCI NPM Version

@jaspero/ng-helpers

A collection of useful components, directives and pipes for Angular applications.

Every item (component, directive and pipe) is published in a separate module, making it easy to just import modules your application will use and not increase bundle size unnecessarily.

Installation

To install this library, run:

$ npm install --save @jaspero/ng-helpers

Then import any Module you need. For example if you need the ClickOutsideDirective import the ClickOutsideModule.

Components

Directives

ClickOutsideDirective

This directive listens for emitted events on the window object and emits if the event doesn't contain the target element.

Example

<div (jpClickOutside)="doSomething()"></div>

Use Cases

  • Closing modals, dropdowns, mobile navigations when clicked outside.
  • Toggling off one item when another is clicked (accordion)

Outputs

namedescription
jpClickOutsideEmits when current element isn't contained in the event

Inputs

nametypedefaultdescription
clickOutsideEventTypestring'click'event to listen for
clickOutsideBlockbooleanfalseif true jpClickOutside doesn't emit

FormTouchOnHoverDirective

This directive requires a FormGroup or FormArray then on mouseenter loops over all controls and marks them touched.

Example

<form [formGroup]="someForm">
  <input type="text" formControlName="someControl">

  <!--We wrapp the button so that we get mouseover event's even when the submit is disabled-->
  <div jpFormTouchOnHover>
    <button type="submit" [disabled]="someForm.invalid">Save</button>
  </div>
</form>

Use Cases

  • This directive is particularly useful when you want to provide information on why the submit button is disabled. Since hovering over it will trigger any validation on the form.

Outputs

namedescription
jpFormTouchOnHoverEmits when controls finish looping and every element was touched

Inputs

nametypedefaultdescription
jpFormTouchedFormGroup or FormArraynullset of controls to loop over

StopPropagationDirective

Listens for the emitted event on the target element and simply forwards it along and calls event.stopPropagation().

Example

  <button (jpStopPropagation)="doSomething()">Click</button>

Use Cases

  • When ever you need to stopPropagation on an event, you can use this directive rather then passing the event along

Outputs

namedescription
jpStopPropagationEmits the original event after calling stopPropagation

Inputs

nametypedefaultdescription
preventDefaultbooleanfalseshould event.preventDefault() also get called.
stopPropagationEventTypestring'click'what event to listen for
conditionboolean or (event) => booleanundefineda condition to check before calling event.stopPropagation()

DebounceChangeDirective

Listens for the emitted event on the target element and simply forwards it along after debounceTime.

Example

  <input type="text" (jpDebounceChange)="doSomething()" />

Use Cases

  • When ever you need to emit events with a delay

Outputs

namedescription
jpDebounceChangeemits original event after debounceTime

Inputs

nametypedefaultdescription
debounceTimenumber500value to pass to the debounceTime pipe
debounceChangeEventTypestring'keyup'what event to listen for
emitOnlyOnChangebooleanfalseonly emit event if the value changes

TrackByFieldDirective

Example

<div *ngFor="let item of items; jpTrackByField:'test'"></div>
@NgModule({
  imports: [TrackByFieldModule.defaultKey()]
})
export class Module {}

LoadClickDirective

Listens for the emitted click event on the target element and add loading class.

Example

  <button [jpLoadClick]="save()">Submit</button>

Use Cases

  • For preventing double click on the submit button.

Inputs

nametypedefaultdescription
jpLoadClickObservablenull
loadClickClassstring'loading'
loadClickStopPropagationbooleanfalseShould stopPropagation be called.
loadClickEventTypestring'click'
loadClickPreventDefaultbooleanfalseShould preventDefault be called.
disableAttributebooleantrueShould the disabled attribute be attached to the element.

Pipes

EnumPipe

A very simple pipe that returns an array of {key: number, value: string} objects from an enum.

Example

  <select>
    <option *ngFor="let item of someEnum | jpEnum" [value]="item.key">
      {{item.value}}
    </option>
  </select>

Use Cases

  • It's most commonly used to easily iterate over an enum in a select

Input Value

valuetypedescription
valueenumSupports any enum value. Provide it in typescript someEnum = SomeEnum to iterate over in html.

Parameters

No parameters for EnumPipe

SanitizePipe

Simplifies using of DomSanitizer. The pipe accepts any value and then tries to sanitize it to the desired format.

Example

<div [innerHtml]="unsanitizedHtml | jpSanitize:'html'"></div>

Use Cases

  • Rendering raw html, styles...

Input Value

valuetypedescription
valuestringAccepts any unsanitized string and runs it through DomSanitizer

Parameters

paramtypedefaultdescription
typehtml or style or script or url or resourceUrlhtmlType of entry value. This determines what DomSanitizer method get's used

TimePassedPipe

This pipe takes a date as input and returns the elapsed time since that date as a number in the desired format.

Example

<div>
 {{someDate | jpTimePassed:null:timePassedType.Minute}} minutes ago
</div>

Use Cases

  • Displaying elapsed time

Input Value

valuetypedescription
valueDateany date

Parameters

paramtypedefaultdescription
dateTwoDatecurrent dateThis is the ending date in the interval. It defaults to the current date.
typeTimePassedTypeTimePassedType.MinuteIn what time format should the elapsed time be returned in.

EnumKeyFormatPipe

Example

<div *ngFor="let item of someObject | jpArrayFromObject">
  <span>KEY: {{item.key}}</span>
  <span>VALUE: {{item.value}}</span>
</div>

Use Cases

  • This pipe is useful when ever you need to iterate an object in your template

Input Value

valuetypedescription
valueobjectany object

Parameters

No parameters for ArrayFromObjectPipe

Decorators

OnChange

A decorator for change detection on properties

Example

export class AppComponent {
  @OnChange<string>((value, simpleChange) => {
      console.log(`Title is changed to: ${value}`);
  })
  @Input()
  title: string;
}

JpFunction

Decorator for methods used by LoadClickDirective. Wraps them in function and expects return of type Observable.

Parameters

valuetypedescription
takenumberWhen JpFunction is called programmatically by default append take(1) to Observable. Pass 0 to skip take operator.

Example

@JpFunction()
waitFor(milliseconds: number = 1000) {
  return of(true).pipe(
    delay(milliseconds)
  );
}
@JpFunction({take: 2})
count() {
  return interval(10).pipe(
    tap((index) => {
      console.log(index);
    })
  );
}

ngOnInit() {
  // Triggers console.log for 0 and 1
  this.count();
}

License

MIT © Jaspero Ltd

5.11.0

6 months ago

5.10.0

1 year ago

5.5.0

2 years ago

5.9.0

1 year ago

5.4.0

2 years ago

5.3.0

2 years ago

5.2.0

2 years ago

5.1.0

3 years ago

5.0.0

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.0

3 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.2

4 years ago

3.4.0

4 years ago

3.3.0

4 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago