diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index dc21dfbb3..8a8f96166 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -10,6 +10,7 @@ ### Fixes - Fix `n-alert` `contentTextColor` and `titleTextColor` type theme variable not working, closes [#1495](https://github.com/TuSimple/naive-ui/issues/1495). +- Fix `n-button` `keyboard` prop does not work, closes [#1508](https://github.com/TuSimple/naive-ui/issues/1508). - Fix `n-upload` instance misses `openOpenFileDialog` method. ### i18n diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index b93066237..315747055 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -10,6 +10,7 @@ ### Fixes - 修复 `n-alert` `contentTextColor` 和 `titleTextColor` 的类型主题变量不起作用,关闭 [#1495](https://github.com/TuSimple/naive-ui/issues/1495) +- 修复 `n-button` `keyboard` 属性不起作用,关闭 [#1508](https://github.com/TuSimple/naive-ui/issues/1508) - 修复 `n-upload` 实例缺少 `openOpenFileDialog` 方法 ### i18n diff --git a/src/button/src/Button.tsx b/src/button/src/Button.tsx index 2a444165b..49ef470f4 100644 --- a/src/button/src/Button.tsx +++ b/src/button/src/Button.tsx @@ -153,8 +153,10 @@ const Button = defineComponent({ switch (e.code) { case 'Enter': case 'NumpadEnter': - if (!props.keyboard) return - e.preventDefault() + if (!props.keyboard) { + e.preventDefault() + return + } enterPressedRef.value = true } } diff --git a/src/button/tests/Button.spec.tsx b/src/button/tests/Button.spec.tsx index 38e5d08de..802954fbb 100644 --- a/src/button/tests/Button.spec.tsx +++ b/src/button/tests/Button.spec.tsx @@ -18,6 +18,21 @@ describe('n-button', () => { await inst.trigger('click') expect(onClick).toHaveBeenCalled() }) + it('keyboard', async () => { + const onClick = jest.fn() + const inst = mount(NButton, { + props: { + keyboard: false, + onClick + } + }) + await inst.trigger('click') + expect(onClick).toBeCalledTimes(1) + await inst.trigger('keydown.space') + expect(onClick).toBeCalledTimes(1) + await inst.trigger('keydown.enter') + expect(onClick).toBeCalledTimes(1) + }) it('disabled', async () => { const onClick = jest.fn() const inst = mount(NButton, {