-
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.
fix: findLast is not available for all browsers (WIP) (#211)
Co-authored-by: Adam Howard <[email protected]>
- Loading branch information
1 parent
e538d70
commit a94e0f4
Showing
3 changed files
with
21 additions
and
13 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 |
---|---|---|
@@ -1,23 +1,27 @@ | ||
import { useRef, type RefObject } from 'react' | ||
import { useRef, type RefObject } from "react"; | ||
|
||
export function useEnterSubmit(): { | ||
formRef: RefObject<HTMLFormElement> | ||
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | ||
formRef: RefObject<HTMLFormElement>; | ||
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void; | ||
} { | ||
const formRef = useRef<HTMLFormElement>(null) | ||
const formRef = useRef<HTMLFormElement>(null); | ||
|
||
const handleKeyDown = ( | ||
event: React.KeyboardEvent<HTMLTextAreaElement> | ||
event: React.KeyboardEvent<HTMLTextAreaElement>, | ||
): void => { | ||
if ( | ||
event.key === 'Enter' && | ||
event.key === "Enter" && | ||
!event.shiftKey && | ||
!event.nativeEvent.isComposing | ||
) { | ||
formRef.current?.requestSubmit() | ||
event.preventDefault() | ||
try { | ||
formRef.current?.requestSubmit(); | ||
} catch (error) { | ||
console.error("Failed to submit form:", error); | ||
} | ||
event.preventDefault(); | ||
} | ||
} | ||
}; | ||
|
||
return { formRef, onKeyDown: handleKeyDown } | ||
return { formRef, onKeyDown: 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