fix(InputNumber): 解决组件InputNumber无法blur时校验的问题

This commit is contained in:
Eager 2018-09-16 00:07:18 +08:00
parent 634c34e9a0
commit 42589ae36a

View file

@ -34,7 +34,7 @@
</div> </div>
</template> </template>
<script> <script>
import { oneOf } from '../../utils/assist'; import { oneOf, findComponentUpward } from '../../utils/assist';
import Emitter from '../../mixins/emitter'; import Emitter from '../../mixins/emitter';
const prefixCls = 'ivu-input-number'; const prefixCls = 'ivu-input-number';
@ -254,16 +254,16 @@
setValue (val) { setValue (val) {
// step precision // step precision
if (val && !isNaN(this.precision)) val = Number(Number(val).toFixed(this.precision)); if (val && !isNaN(this.precision)) val = Number(Number(val).toFixed(this.precision));
const {min, max} = this; const {min, max} = this;
if (val!==null) { if (val!==null) {
if (val > max) { if (val > max) {
val = max; val = max;
} else if (val < min) { } else if (val < min) {
val = min; val = min;
} }
} }
this.$nextTick(() => { this.$nextTick(() => {
this.currentValue = val; this.currentValue = val;
this.$emit('input', val); this.$emit('input', val);
@ -278,6 +278,9 @@
blur () { blur () {
this.focused = false; this.focused = false;
this.$emit('on-blur'); this.$emit('on-blur');
if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
this.dispatch('FormItem', 'on-form-blur', this.currentValue);
}
}, },
keyDown (e) { keyDown (e) {
if (e.keyCode === 38) { if (e.keyCode === 38) {
@ -289,20 +292,20 @@
} }
}, },
change (event) { change (event) {
if (event.type == 'input' && !this.activeChange) return; if (event.type == 'input' && !this.activeChange) return;
let val = event.target.value.trim(); let val = event.target.value.trim();
if (this.parser) { if (this.parser) {
val = this.parser(val); val = this.parser(val);
} }
const isEmptyString = val.length === 0; const isEmptyString = val.length === 0;
if(isEmptyString){ if(isEmptyString){
this.setValue(null); this.setValue(null);
return; return;
} }
if (event.type == 'input' && val.match(/^\-?\.?$|\.$/)) return; // prevent fire early if decimal. If no more input the change event will fire later if (event.type == 'input' && val.match(/^\-?\.?$|\.$/)) return; // prevent fire early if decimal. If no more input the change event will fire later
val = Number(val); val = Number(val);
if (!isNaN(val)) { if (!isNaN(val)) {