anein / angular2-trim-directive

the directive trims whitespaces from the end of an input text value.
MIT License
45 stars 26 forks source link

throw error when enter 1 letter only. with trim="blur" in reactive form #35

Closed zymr-keshav closed 5 years ago

zymr-keshav commented 5 years ago

angular v 6.1.10 ng2-trim-directive v 2.3.0

using in reactive form with trim="blur" options it works fine as expected

  1. trim input after blur
  2. trim if all whitespace and also display error on the required field.

BUT facing one strange issue. when user input only a single letter, it throws the same error as required.

interestingly it works fine when writing 2 letters and removes the later from the input box.

Will you please check this issue?

check this demo . enter 'a' and then enter aa and remove the a

why both are behaving differently?

anein commented 5 years ago

Hey!

Yep. Weird behavior. I think it's safe to assume that the issue lies on line 129, causing the model isn't updated.

zymr-keshav commented 5 years ago

yes, the issue possibly on line number 129 and how to fix that? I have tried to create pull request but failed and also git clone but it failed due to typescript version mismatch.

zymr-keshav commented 5 years ago

this code changes fixed my issue but do not know whether it is applicable to all or not. One thing i did not understand is when we assign const previousValue = this._value; then how these const trimmedValue = this._value.trim(); and previous.trim() can be different?

  private updateValue(event: string, value: string): void {
        const currentValue = this.trim !== '' && event !== this.trim ? value : value.trim();
        const previousValue = this._value;
        let trimmedPreviousValue = '';
        if (Boolean(previousValue)) {
            trimmedPreviousValue = previousValue.toString()trim();
        }

        this.writeValue(currentValue);
        const trimmedValue = this._value.trim();
        if (trimmedValue !== previousValue && (trimmedValue !== '' || trimmedPreviousValue !== '')) {
            this.onChange(this._value);
        }
    }
ghost commented 5 years ago

Pasting is also broken again :(

anein commented 5 years ago

THAT condition, which is responsible for updating the model, wasn't optimized. Done and done. =)

//cc @alexvisenze @zymr-keshav