From e51f7c4c7ff49f48ce7ef02889870a75a5ced813 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 28 Jan 2024 12:04:41 +0900 Subject: [PATCH] Handle submit with enter key in accounts New --- renderer/components/accounts/New.tsx | 46 ++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/renderer/components/accounts/New.tsx b/renderer/components/accounts/New.tsx index f754f07f..a5e5dab4 100644 --- a/renderer/components/accounts/New.tsx +++ b/renderer/components/accounts/New.tsx @@ -1,5 +1,5 @@ import generator, { MegalodonInterface, OAuth, detector } from 'megalodon' -import { useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import { db } from '@/db' import { FormattedMessage, useIntl } from 'react-intl' import { Alert, Button, Dialog, DialogBody, DialogHeader, Input, Spinner, Typography } from '@material-tailwind/react' @@ -18,6 +18,8 @@ export default function New(props: NewProps) { const [loading, setLoading] = useState(false) const { formatMessage } = useIntl() + const domainFormRef = useRef(null) + const authorizeFormRef = useRef(null) const close = () => { setSNS(null) @@ -58,6 +60,22 @@ export default function New(props: NewProps) { global.ipc.invoke('open-browser', appData.url) } + const handleDomainSubmit = useCallback( + (e: SubmitEvent) => { + e.preventDefault() + checkDomain() + }, + [checkDomain] + ) + + useEffect(() => { + domainFormRef.current?.addEventListener('submit', handleDomainSubmit) + + return () => { + domainFormRef.current?.removeEventListener('submit', handleDomainSubmit) + } + }, [props.opened, sns]) + const authorize = async () => { setError('') setLoading(true) @@ -105,6 +123,22 @@ export default function New(props: NewProps) { close() } + const handleAuthorizeSubmit = useCallback( + (e: SubmitEvent) => { + e.preventDefault() + authorize() + }, + [authorize] + ) + + useEffect(() => { + authorizeFormRef.current?.addEventListener('submit', handleAuthorizeSubmit) + + return () => { + authorizeFormRef.current?.removeEventListener('submit', handleAuthorizeSubmit) + } + }, [props.opened, appData]) + return ( <> @@ -117,9 +151,9 @@ export default function New(props: NewProps) { {error} )} -
+
{sns === null ? ( - <> +
@@ -129,11 +163,11 @@ export default function New(props: NewProps) { - + ) : ( <> {appData ? ( - <> +
{appData.session_token ? ( <>
@@ -157,7 +191,7 @@ export default function New(props: NewProps) { - + ) : (