chore: Use react-hook-form in group, API key, Gitea and GitLab forms (#14837)

* chore: Use react-hook-form in group, API key, Gitea and GitLab forms

* Rely on native validation in Gitea and GitLab connect forms

* Sync Input character count for uncontrolled fields on mount
This commit is contained in:
Tom Moor authored and GitHub committed 2026-12-04 22:18:11 -05:00
1 parent 9fd3254a8d
commit fea8ab65a7
6 files changed
+204 -244

No files matched your search

@@ -1,4 +1,5 @@
import * as React from "react";
import { useForm } from "react-hook-form";
import { useTranslation, Trans } from "react-i18next";
import styled from "styled-components";
import { toast } from "sonner";
@@ -13,6 +14,12 @@ import useStores from "~/hooks/useStores";
import { client } from "~/utils/ApiClient";
import { redirectTo } from "~/utils/urls";
interface FormData {
url: string;
clientId: string;
clientSecret: string;
}
/**
* Dialog that presents the user with options to connect to GitLab Cloud
* or a self-managed GitLab instance.
@@ -23,9 +30,9 @@ function GitLabConnectDialog() {
const [showCustomForm, setShowCustomForm] = React.useState(
!env.GITLAB_CLIENT_ID
);
const [customUrl, setCustomUrl] = React.useState("");
const [clientId, setClientId] = React.useState("");
const [clientSecret, setClientSecret] = React.useState("");
const { register, handleSubmit } = useForm<FormData>({
defaultValues: { url: "", clientId: "", clientSecret: "" },
});
const [saving, setSaving] = React.useState(false);
const handleConnect = React.useCallback(
@@ -53,21 +60,19 @@ function GitLabConnectDialog() {
}, [handleConnect]);
const handleConnectCustom = React.useCallback(
async (ev: React.FormEvent) => {
ev.preventDefault();
const url = customUrl.trim().replace(/\/+$/, "");
async (data: FormData) => {
await handleConnect({
url,
clientId: clientId.trim() || undefined,
clientSecret: clientSecret.trim() || undefined,
url: data.url.trim().replace(/\/+$/, ""),
clientId: data.clientId.trim() || undefined,
clientSecret: data.clientSecret.trim() || undefined,
});
},
[customUrl, clientId, clientSecret, handleConnect]
[handleConnect]
);
if (showCustomForm) {
return (
<form onSubmit={handleConnectCustom}>
<form onSubmit={handleSubmit(handleConnectCustom)}>
<Flex column gap={12}>
<Text as="p" type="secondary">
<Trans>Enter the details for your GitLab instance.</Trans>
@@ -75,8 +80,7 @@ function GitLabConnectDialog() {
<Input
label={t("GitLab URL")}
placeholder="https://gitlab.example.com"
value={customUrl}
onChange={(ev) => setCustomUrl(ev.currentTarget.value)}
{...register("url")}
pattern="https://.*"
title={t("URL must start with https")}
required
@@ -85,15 +89,13 @@ function GitLabConnectDialog() {
<Input
label={t("Client ID")}
placeholder={t("OAuth application ID")}
value={clientId}
onChange={(ev) => setClientId(ev.currentTarget.value)}
{...register("clientId")}
required
/>
<Input
label={t("Client secret")}
placeholder={t("OAuth application secret")}
value={clientSecret}
onChange={(ev) => setClientSecret(ev.currentTarget.value)}
{...register("clientSecret")}
type="password"
required
/>
@@ -107,15 +109,7 @@ function GitLabConnectDialog() {
{t("Back")}
</Button>
)}
<Button
type="submit"
disabled={
!customUrl.trim() ||
!clientId.trim() ||
!clientSecret.trim() ||
saving
}
>
<Button type="submit" disabled={saving}>
{saving ? `${t("Connecting")}…` : t("Connect")}
</Button>
</Flex>