@pepperi-addons/ngx-lib 0.4.2-beta.317 → 0.4.2-beta.319

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -899,24 +899,43 @@ class PepInputAutoWidthDirective {
899
899
  this.el = el;
900
900
  this.includePadding = true;
901
901
  this.input = null;
902
+ this.lastValue = null;
903
+ this.onBlurHandler = () => this.setWidthUsingText();
904
+ this.onKeyupHandler = () => this.setWidthUsingText();
902
905
  }
903
906
  ngAfterViewInit() {
904
- if (this.el.nativeElement.tagName.toLowerCase() === 'input') {
905
- this.input = this.el;
907
+ if (this.el.nativeElement?.tagName?.toLowerCase() === 'input') {
908
+ this.input = this.el.nativeElement;
906
909
  }
907
910
  else {
908
- const list = this.el.nativeElement.querySelectorAll('input');
909
- this.input = list.length > 0 ? list[0] : null;
911
+ const list = this.el.nativeElement?.querySelectorAll
912
+ ? this.el.nativeElement.querySelectorAll('input')
913
+ : null;
914
+ this.input = list && list.length > 0 ? list[0] : null;
910
915
  }
911
916
  if (this.input) {
912
- this.input.addEventListener('blur', this.setWidthUsingText.bind(this));
913
- this.input.addEventListener('keyup', this.setWidthUsingText.bind(this));
917
+ this.input.addEventListener('blur', this.onBlurHandler);
918
+ this.input.addEventListener('keyup', this.onKeyupHandler);
914
919
  }
915
920
  this.setWidthUsingText();
916
921
  }
922
+ ngDoCheck() {
923
+ if (!this.input) {
924
+ return;
925
+ }
926
+ // Keep the width in sync when value is updated programmatically (Angular bindings),
927
+ // not only on user key events.
928
+ if (this.input.value !== this.lastValue) {
929
+ this.setWidthUsingText();
930
+ }
931
+ }
917
932
  ngOnDestroy() {
918
- this.input.removeEventListener('blur', this.setWidthUsingText.bind(this));
919
- this.input.removeEventListener('keyup', this.setWidthUsingText.bind(this));
933
+ if (!this.input) {
934
+ return;
935
+ }
936
+ this.input.removeEventListener('blur', this.onBlurHandler);
937
+ this.input.removeEventListener('keyup', this.onKeyupHandler);
938
+ this.input = null;
920
939
  }
921
940
  get paddingWidth() {
922
941
  return this.includePadding
@@ -933,6 +952,7 @@ class PepInputAutoWidthDirective {
933
952
  if (this.input) {
934
953
  const text = this.input.value;
935
954
  this.setWidth(this.textWidth(text) + this.paddingWidth);
955
+ this.lastValue = text;
936
956
  }
937
957
  }
938
958
  _sumPropertyValues(properties) {