Skip to content

Commit

Permalink
fix(module: input-item): fix custom keyboard input value and defaultV… (
Browse files Browse the repository at this point in the history
  • Loading branch information
nuonuoge authored and fisherspy committed Mar 12, 2019
1 parent ffe4f8d commit a73a7e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
30 changes: 16 additions & 14 deletions components/input-item/custom-input/custom-input.component.ts
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
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
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

0 comments on commit a73a7e8

Please sign in to comment.