Skip to content

Commit

Permalink
fix(Input): support onPressEnter (#1476)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhang Rui <zhangrui@growingio.com>
  • Loading branch information
Ryan Zhang and Zhang Rui committed Nov 16, 2021
1 parent d28a8c0 commit 4418d99
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/input/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import classNames from 'classnames';
import { InputProps } from './interface';
import usePrefixCls from '../utils/hooks/use-prefix-cls';
Expand All @@ -12,6 +12,8 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
disabled,
className,
placeholder,
onPressEnter,
onKeyPress,
...rest
} = props;

Expand All @@ -35,6 +37,16 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
[prefixCls, customizeSuffix, customizePrefix]
);

const handleKeyPress = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
onPressEnter?.(e);
}
onKeyPress?.(e);
},
[onPressEnter, onKeyPress]
);

const prefixFcCls = useMemo(
() =>
classNames(`${prefixCls}__prefix`, {
Expand All @@ -60,7 +72,16 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
() => (customizeSuffix ? <div className={suffixCls}>{customizeSuffix}</div> : null),
[suffixCls, customizeSuffix]
);
const input = <input {...rest} disabled={disabled} className={inputClass} placeholder={placeholder} ref={ref} />;
const input = (
<input
{...rest}
disabled={disabled}
className={inputClass}
onKeyPress={handleKeyPress}
placeholder={placeholder}
ref={ref}
/>
);

if (!suffix && !prefix) {
return input;
Expand Down
6 changes: 6 additions & 0 deletions src/input/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface BaseInputProps
* input 的后缀图标
*/
suffix?: React.ReactNode;

/**
* 当点击回车键时调用
*/
onPressEnter?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
}

export interface InputProps extends Omit<BaseInputProps, 'onChange'> {
Expand Down Expand Up @@ -136,6 +141,7 @@ export interface InputButtonProps
* 当Input Button的值修改后的方法
*/
onInputChange?: (value: string) => void;

/**
* 点击onClear回调
*
Expand Down
2 changes: 1 addition & 1 deletion src/popover/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface PopoverProps {
/**
被包裹的元素
*/
children: React.ReactElement | string;
children: React.ReactNode;

placement?: Placement;
trigger?: TriggerAction | TriggerAction[];
Expand Down
2 changes: 1 addition & 1 deletion src/tooltip/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export interface TooltipProps extends Omit<PopoverProps, 'content'> {
/**
被包裹的元素
*/
children: React.ReactElement;
children: React.ReactNode;
}

1 comment on commit 4418d99

@vercel
Copy link

@vercel vercel bot commented on 4418d99 Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.