forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(TextArea): add `enterkeyhint` and `onEnterPress` * chore: test case for `teatarea` * feat: `useInputHandleKeyDown` hook * fix: export `useInputHandleKeyDown` in `Input` * chore: move `useInputHandleKeyDown` * chore: adjust code --------- Co-authored-by: 二货机器人 <[email protected]>
- Loading branch information
Showing
4 changed files
with
87 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useIsomorphicLayoutEffect } from 'ahooks' | ||
|
||
interface InputHandleKeyDownType<T> { | ||
onEnterPress?: (e: React.KeyboardEvent<T>) => void | ||
onKeyDown?: (e: React.KeyboardEvent<T>) => void | ||
enterKeyHint?: React.InputHTMLAttributes<HTMLInputElement>['enterKeyHint'] | ||
nativeInputRef: React.RefObject<T> | ||
} | ||
|
||
export default function useInputHandleKeyDown< | ||
T extends HTMLInputElement | HTMLTextAreaElement, | ||
>({ | ||
onEnterPress, | ||
onKeyDown, | ||
nativeInputRef, | ||
enterKeyHint, | ||
}: InputHandleKeyDownType<T>) { | ||
const handleKeydown = (e: React.KeyboardEvent<T>) => { | ||
if (onEnterPress && (e.code === 'Enter' || e.keyCode === 13)) { | ||
onEnterPress(e) | ||
} | ||
onKeyDown?.(e) | ||
} | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const ele = nativeInputRef.current | ||
|
||
if (!enterKeyHint || !ele) return | ||
|
||
ele.setAttribute('enterkeyhint', enterKeyHint) | ||
return () => { | ||
ele.removeAttribute('enterkeyhint') | ||
} | ||
}, [enterKeyHint]) | ||
|
||
return handleKeydown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters