From 88f165edb5c3993f4dba90c3267a1ea037e0869b Mon Sep 17 00:00:00 2001 From: tjz <415800467@qq.com> Date: Sat, 9 Jun 2018 15:48:34 +0800 Subject: [PATCH] fix: input-number autoFocus not work --- components/input-number/__tests__/index.test.js | 4 ++++ components/input-number/index.jsx | 1 + components/vc-input-number/src/index.js | 4 +++- tests/shared/focusTest.js | 7 +++++-- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 components/input-number/__tests__/index.test.js diff --git a/components/input-number/__tests__/index.test.js b/components/input-number/__tests__/index.test.js new file mode 100644 index 0000000000..bb52ccd387 --- /dev/null +++ b/components/input-number/__tests__/index.test.js @@ -0,0 +1,4 @@ +import InputNumber from '..' +import focusTest from '../../../tests/shared/focusTest' + +focusTest(InputNumber) diff --git a/components/input-number/index.jsx b/components/input-number/index.jsx index 1504bf6d03..ab084cd38b 100644 --- a/components/input-number/index.jsx +++ b/components/input-number/index.jsx @@ -25,6 +25,7 @@ export const InputNumberProps = { name: PropTypes.string, id: PropTypes.string, precision: PropTypes.number, + autoFocus: PropTypes.bool, } export default { diff --git a/components/vc-input-number/src/index.js b/components/vc-input-number/src/index.js index f5e8947bf2..a1b4442f0d 100755 --- a/components/vc-input-number/src/index.js +++ b/components/vc-input-number/src/index.js @@ -105,6 +105,9 @@ export default { }, mounted () { this.$nextTick(() => { + if (this.autoFocus && !this.disabled) { + this.focus() + } this.updatedFunc() }) }, @@ -571,7 +574,6 @@ export default { onBlur={this.onBlur} onKeydown={editable ? this.onKeyDown : noop} onKeyup={editable ? this.onKeyUp : noop} - autoFocus={this.autoFocus} maxLength={this.maxLength} readOnly={this.readOnly} disabled={this.disabled} diff --git a/tests/shared/focusTest.js b/tests/shared/focusTest.js index 85c52421fc..2a1d30ca4e 100644 --- a/tests/shared/focusTest.js +++ b/tests/shared/focusTest.js @@ -1,4 +1,5 @@ import { mount } from '@vue/test-utils' +import { asyncExpect } from '../utils' export default function focusTest (Component) { describe('focus and blur', () => { @@ -22,7 +23,7 @@ export default function focusTest (Component) { expect(handleFocus).toBeCalled() }) - it('blur() and onBlur', () => { + it('blur() and onBlur', async () => { const handleBlur = jest.fn() const wrapper = mount({ render (h) { @@ -32,7 +33,9 @@ export default function focusTest (Component) { wrapper.vm.$refs.component.focus() wrapper.vm.$refs.component.blur() jest.runAllTimers() - expect(handleBlur).toBeCalled() + await asyncExpect(() => { + expect(handleBlur).toBeCalled() + }) }) it('autoFocus', (done) => {