From 5b968d1b867ac0e50a5cb072cf76d0a8d0423709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E6=9D=AA?= Date: Thu, 3 Dec 2020 14:19:55 +0800 Subject: [PATCH] feat: add keyCodes.ts --- src/common/keyCodes.ts | 3 +++ src/components/input/input.tsx | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 src/common/keyCodes.ts diff --git a/src/common/keyCodes.ts b/src/common/keyCodes.ts new file mode 100644 index 000000000..72f6709ad --- /dev/null +++ b/src/common/keyCodes.ts @@ -0,0 +1,3 @@ +export const enum KeyCodes { + ENTER = "Enter" +} diff --git a/src/components/input/input.tsx b/src/components/input/input.tsx index 17f8d2749..d5855b27b 100644 --- a/src/components/input/input.tsx +++ b/src/components/input/input.tsx @@ -1,6 +1,7 @@ import './style.scss'; import * as React from 'react'; import { classNames, prefixClaName } from 'mo/common/className'; +import { KeyCodes } from 'mo/common/keyCodes' import TextArea from './TextArea'; @@ -16,11 +17,11 @@ export interface InputProps { value?: string; readonly defaultValue?: string; readonly className?: string; - readonly onFocus?: (e: React.FocusEvent) => void; - readonly onBlur?: (e: React.FocusEvent) => void; - readonly onPressEnter?: React.KeyboardEventHandler; - readonly onKeyDown?: (e: React.KeyboardEvent) => void; - readonly onChange?: (e: any) => void; + onFocus?: (e: React.FocusEvent) => void; + onBlur?: (e: React.FocusEvent) => void; + onPressEnter?: React.KeyboardEventHandler; + onKeyDown?: (e: React.KeyboardEvent) => void; + onChange?: (e: React.ChangeEvent) => void; } export const inputClassName = prefixClaName('input'); @@ -65,7 +66,6 @@ export interface InputState { } class Input extends React.Component { - // static Search: typeof Search; static TextArea: typeof TextArea; static defaultProps = { @@ -108,15 +108,15 @@ class Input extends React.Component { } handleChange = (e: React.ChangeEvent) => { + const { onChange } = this.props this.setValue(e.target.value); - resolveOnChange(this.input, e, this.props.onChange); + resolveOnChange(this.input, e, onChange); }; handleKeyDown = (e: React.KeyboardEvent) => { const { onPressEnter, onKeyDown } = this.props; - // todo - if (e.keyCode === 13 && onPressEnter) { - onPressEnter(e); + if (e.key === KeyCodes.ENTER) { + onPressEnter?.(e); } onKeyDown?.(e); };