From 84a05cbd18d0bda34aaff69a8c88e80c0fbd577a Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Fri, 22 Jun 2018 17:01:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20HeaderSearch=20=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=AF=B9=20enter=20=E9=94=AE?= =?UTF-8?q?=E7=9A=84=E9=98=B2=E6=8A=96=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 删除无用 timeout 代码 2. 利用 lodash 中的 debounce 函数增加防抖处理 3. 添加 gitignore 规则,排除 VSC 插件生成的 .history 文件夹 close #1703 --- .gitignore | 3 +++ src/components/HeaderSearch/index.js | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 59a03667..1da27e1b 100755 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ yarn.lock package-lock.json *bak jsconfig.json + +# visual studio code +.history \ No newline at end of file diff --git a/src/components/HeaderSearch/index.js b/src/components/HeaderSearch/index.js index 855e7ac1..a08ad7f4 100644 --- a/src/components/HeaderSearch/index.js +++ b/src/components/HeaderSearch/index.js @@ -2,6 +2,8 @@ import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { Input, Icon, AutoComplete } from 'antd'; import classNames from 'classnames'; +import Debounce from 'lodash-decorators/debounce'; +import Bind from 'lodash-decorators/bind'; import styles from './index.less'; export default class HeaderSearch extends PureComponent { @@ -29,14 +31,9 @@ export default class HeaderSearch extends PureComponent { searchMode: this.props.defaultOpen, value: '', }; - componentWillUnmount() { - clearTimeout(this.timeout); - } onKeyDown = e => { if (e.key === 'Enter') { - this.timeout = setTimeout(() => { - this.props.onPressEnter(this.state.value); // Fix duplicate onPressEnter - }, 0); + this.debouncePressEnter(); } }; onChange = value => { @@ -45,6 +42,15 @@ export default class HeaderSearch extends PureComponent { this.props.onChange(); } }; + // NOTE: 不能小于500,如果长按某键,第一次触发auto repeat的间隔是500ms,小于500会导致触发2次 + @Bind() + @Debounce(500, { + leading: true, + trailing: false, + }) + debouncePressEnter() { + this.props.onPressEnter(this.state.value); + } enterSearchMode = () => { this.setState({ searchMode: true }, () => { if (this.state.searchMode) { -- GitLab