khashayar/ng-trim-value-accessor

Apply trimmed value on blur

Closed this issue · 5 comments

Hi,

Unless I am missing the point of this package, I would appreciate being able to automatically trim the value when the input receives blur event.

I tried adding something like:

  @HostListener('blur', ['$event.target.value'])
  applyTrim (val: string) {
    this.writeValue(val.trim());
  };

and it works for me.

The scenario this method handles:
When the field is supposed to be trimmed, we want it to be trimmed when the form is submitted but we also want the trimmed value to be applied in the UI.

Without it: the value only gets trimmed when the form is submitted, but it is not reflected in the UI when we leav the input

With it: the value is also applied to the UI on blur event.

It might of course be useful to provide an attribute parameter to explicitly enable the blur handler per input.

It exactly mimics the AngularJS ng-trim behavior, which works the same. What you are suggesting is nice to have. Do you mind creating a pull request?

The PR should be in your inbox.
The more I look at the selector, the more I think the directive went too global, too greedy. As a consumer of this directive I would prefer to be able to explicitly put the directive onto the <input> and thus have control over my app. Maybe it should be a smal module with .forRoot() method to specify whether app wants it global or explicit. Would it work for directives?

I know what you mean, but the whole idea behind this one was to be as simple as possible and having the same functionality of ng-trim.

I agree that it would be nice to have a directive which is configurable, probably a separate one?