mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-15 02:30:58 -05:00
30 lines
750 B
TypeScript
30 lines
750 B
TypeScript
|
import { useCallback, useState, Dispatch, SetStateAction } from 'react'
|
||
|
|
||
|
export default function useSubmittableTextInput(
|
||
|
handleSubmit: (
|
||
|
content: string,
|
||
|
setContent: Dispatch<SetStateAction<string>>
|
||
|
) => void
|
||
|
) {
|
||
|
const [content, setContent] = useState('')
|
||
|
|
||
|
const handleKeyPress = useCallback(
|
||
|
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||
|
if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
|
||
|
e.preventDefault()
|
||
|
handleSubmit(content, setContent)
|
||
|
}
|
||
|
},
|
||
|
[content, handleSubmit]
|
||
|
)
|
||
|
|
||
|
const handleChange = useCallback(
|
||
|
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||
|
setContent(e.target.value)
|
||
|
},
|
||
|
[]
|
||
|
)
|
||
|
|
||
|
return { handleChange, handleKeyPress, content }
|
||
|
}
|