Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module: input-item): fix custom keyboard input value and defaultV… #330

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 16 additions & 14 deletions components/input-item/custom-input/custom-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ export class CustomInput implements OnInit, OnDestroy {
private _setFocus: boolean = false;
private _preventKeyboard: boolean;
private _moneyKeyboardAlign: string;
private _fontColor:string;
private _fontColor: string;

@Input()
get value(): string {
return this._value;
}
set value(v: string) {
if (typeof v === undefined || v === null) {
if (typeof v === 'undefined' || v === null) {
this._value = '';
} else if (this._maxLength !== undefined && this._maxLength >= 0) {
this._value = v.substr(0, this._maxLength);
this._value = v.toString().substr(0, this._maxLength);
} else {
this._value = v;
this._value = v.toString();
}
}
@Input()
set defaultValue(value: string) {
this._defaultValue = value;
this._value = this._defaultValue;
if (!this._value) {
this._value = this._defaultValue.toString();
}
}
@Input()
set maxLength(value: number) {
Expand Down Expand Up @@ -122,7 +124,7 @@ export class CustomInput implements OnInit, OnDestroy {
setTimeout(() => {
this.addBlurListener();
}, 50);
};
}

doBlur = ev => {
const value = this._value;
Expand Down Expand Up @@ -170,22 +172,22 @@ export class CustomInput implements OnInit, OnDestroy {
CustomInputService.hideKeyboard();
}
this.setFakeInputCls();
};
}

removeBlurListener = () => {
document.removeEventListener('click', this.doBlur, false);
};
}

addBlurListener = () => {
document.addEventListener('click', this.doBlur, false);
};
}

onInputBlur = value => {
this.focus = false;
this.setFakeInputCls();
this.onBlur.emit(this._value);
CustomInputService.hideKeyboard();
};
}

onInputFocus = () => {
this.onFocus.emit(this._value);
Expand All @@ -195,19 +197,19 @@ export class CustomInput implements OnInit, OnDestroy {
setTimeout(() => {
CustomInputService.showKeyboard();
}, 100);
};
}

setFakeInputCls = () => {
this.fakeInputCls = {
[`fake-input`]: true,
['fake-input-disabled']: this._disabled,
['focus']: this.focus
};
};
}

setContainerCls = () => {
this.clsFakeContainerLeft = this._moneyKeyboardAlign === 'left';
};
}

onKeyboardClick = keyboardItemValue => {
let valueAfterChange;
Expand Down Expand Up @@ -240,7 +242,7 @@ export class CustomInput implements OnInit, OnDestroy {
this._ngZone.run(() => {
this._value = valueAfterChange;
});
};
}

ngOnInit() {
this._preventKeyboard = this._disabled || !this._editable;
Expand Down
18 changes: 18 additions & 0 deletions components/input-item/input-item.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ describe('InputComponent', () => {
expect(fakeInput.innerText).toBe('23', 'type is money');
});

it('should value override defaultValue work', () => {
component.value = 'test';
component.defaultValue = 'default test';
fixture.detectChanges();
inputEle = inputItem.nativeElement.querySelector('input');
expect(inputEle.value).toBe('test', 'type is text');

component.type = 'money';
fixture.detectChanges();
component.value = '23';
component.defaultValue = '26';
fixture.detectChanges();
const fakeInput = inputItem.nativeElement.querySelector('.fake-input');
expect(fakeInput.innerText).toBe('23', 'type is money');
});

it('should input chinese character work', fakeAsync(() => {
const inputModelEle = inputModel.nativeElement.querySelector('input');
inputModelEle.value = '哈哈';
Expand All @@ -89,13 +105,15 @@ describe('InputComponent', () => {
}));

it('should defaultValue work', () => {
component.value = null;
component.defaultValue = 'test';
fixture.detectChanges();
inputEle = inputItem.nativeElement.querySelector('input');
expect(inputEle.value).toBe('test', 'type is text');

component.type = 'money';
fixture.detectChanges();
component.value = '';
component.defaultValue = '23';
fixture.detectChanges();
const fakeInput = inputItem.nativeElement.querySelector('.fake-input');
Expand Down
6 changes: 4 additions & 2 deletions components/input-item/input-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class InputItem implements OnInit, OnChanges, ControlValueAccessor {
return this._value;
}
set value(v: string) {
if (typeof v === undefined || v === null) {
if (typeof v === 'undefined' || v === null) {
this._value = '';
} else {
this._value = v;
Expand All @@ -99,7 +99,9 @@ export class InputItem implements OnInit, OnChanges, ControlValueAccessor {
}
set defaultValue(value: string) {
this._defaultValue = value;
this._value = this._defaultValue;
if (!this._value) {
this._value = this._defaultValue;
}
}
@Input()
get placeholder(): string {
Expand Down
2 changes: 1 addition & 1 deletion components/textarea-item/textarea-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class TextareaItem implements OnInit, AfterContentChecked, ControlValueAc


writeValue(value: any): void {
if (typeof value === undefined || value === null) {
if (typeof value === 'undefined' || value === null) {
this._value = '';
} else {
this._value = value;
Expand Down