Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Extract InputWithSaveButton with a width-stable save button (#2926)

Luke Velladd67a1c5f681
diff --git a/apps/web/src/app/[locale]/(space)/settings/general/components/space-settings-form.tsx b/apps/web/src/app/[locale]/(space)/settings/general/components/space-settings-form.tsx
index 8b708c601..cc84656f4 100644
--- a/apps/web/src/app/[locale]/(space)/settings/general/components/space-settings-form.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/general/components/space-settings-form.tsx
@@ -9,15 +9,7 @@ import {
FieldLabel,
FieldTitle,
} from "@rallly/ui/field";
-import {
- InputGroup,
- InputGroupAddon,
- InputGroupButton,
- InputGroupInput,
-} from "@rallly/ui/input-group";
import { toast } from "@rallly/ui/sonner";
-import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";
-import { CheckIcon } from "lucide-react";
import type React from "react";
import type { Control } from "react-hook-form";
import { Controller, useForm } from "react-hook-form";
@@ -27,6 +19,7 @@ import {
ImageUploadControl,
ImageUploadPreview,
} from "@/components/image-upload";
+import { InputWithSaveButton } from "@/components/input-with-save-button";
import {
getSpaceImageUploadUrlAction,
removeSpaceImageAction,
@@ -175,41 +168,17 @@ function SpaceNameField({
name="name"
render={({ field, fieldState }) => (
<>
- <InputGroup className="w-56">
- <InputGroupInput
- {...field}
- id="space-name"
- disabled={disabled}
- placeholder={t("spaceNamePlaceholder", {
- defaultValue: "My Team",
- })}
- aria-invalid={fieldState.invalid}
- />
- {/* Rendered only when there is something to save. A disabled
- button here would trip InputGroup's `has-disabled:` styles
- and grey out the whole field. */}
- {isDirty && !disabled ? (
- <InputGroupAddon align="inline-end">
- <Tooltip>
- <TooltipTrigger
- render={
- <InputGroupButton
- type="submit"
- size="icon-xs"
- loading={isSaving}
- aria-label={t("save", { defaultValue: "Save" })}
- >
- <CheckIcon />
- </InputGroupButton>
- }
- />
- <TooltipContent>
- <Trans i18nKey="save" defaults="Save" />
- </TooltipContent>
- </Tooltip>
- </InputGroupAddon>
- ) : null}
- </InputGroup>
+ <InputWithSaveButton
+ {...field}
+ id="space-name"
+ disabled={disabled}
+ placeholder={t("spaceNamePlaceholder", {
+ defaultValue: "My Team",
+ })}
+ aria-invalid={fieldState.invalid}
+ isDirty={isDirty}
+ isSaving={isSaving}
+ />
{fieldState.invalid ? (
<FieldError className="mt-1.5" errors={[fieldState.error]} />
) : null}
diff --git a/apps/web/src/app/[locale]/control-panel/branding/components/app-name-field.tsx b/apps/web/src/app/[locale]/control-panel/branding/components/app-name-field.tsx
index c601222e6..fb2139b74 100644
--- a/apps/web/src/app/[locale]/control-panel/branding/components/app-name-field.tsx
+++ b/apps/web/src/app/[locale]/control-panel/branding/components/app-name-field.tsx
@@ -2,18 +2,11 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { FieldError } from "@rallly/ui/field";
-import {
- InputGroup,
- InputGroupAddon,
- InputGroupButton,
- InputGroupInput,
-} from "@rallly/ui/input-group";
import { toast } from "@rallly/ui/sonner";
-import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";
-import { CheckIcon } from "lucide-react";
import { Controller, useForm } from "react-hook-form";
+import { InputWithSaveButton } from "@/components/input-with-save-button";
import { brandingSettingsSchema } from "@/features/instance-settings/schema";
-import { Trans, useTranslation } from "@/i18n/client";
+import { useTranslation } from "@/i18n/client";
import { useSafeAction } from "@/lib/safe-action/client";
import { updateBrandingSettingsAction } from "../actions";
@@ -52,38 +45,14 @@ export function AppNameField({
name="appName"
render={({ field, fieldState }) => (
<>
- <InputGroup className="w-56">
- <InputGroupInput
- {...field}
- id="app-name"
- disabled={disabled}
- aria-invalid={fieldState.invalid}
- />
- {/* Rendered only when there is something to save. A disabled
- button here would trip InputGroup's `has-disabled:` styles
- and grey out the whole field. */}
- {form.formState.isDirty && !disabled ? (
- <InputGroupAddon align="inline-end">
- <Tooltip>
- <TooltipTrigger
- render={
- <InputGroupButton
- type="submit"
- size="icon-xs"
- loading={updateBranding.isExecuting}
- aria-label={t("save", { defaultValue: "Save" })}
- >
- <CheckIcon />
- </InputGroupButton>
- }
- />
- <TooltipContent>
- <Trans i18nKey="save" defaults="Save" />
- </TooltipContent>
- </Tooltip>
- </InputGroupAddon>
- ) : null}
- </InputGroup>
+ <InputWithSaveButton
+ {...field}
+ id="app-name"
+ disabled={disabled}
+ aria-invalid={fieldState.invalid}
+ isDirty={form.formState.isDirty}
+ isSaving={updateBranding.isExecuting}
+ />
{fieldState.invalid ? (
<FieldError className="mt-1.5" errors={[fieldState.error]} />
) : null}
diff --git a/apps/web/src/components/input-with-save-button.tsx b/apps/web/src/components/input-with-save-button.tsx
new file mode 100644
index 000000000..efa22add9
--- /dev/null
+++ b/apps/web/src/components/input-with-save-button.tsx
@@ -0,0 +1,63 @@
+"use client";
+
+import { cn } from "@rallly/ui";
+import {
+ InputGroup,
+ InputGroupAddon,
+ InputGroupButton,
+ InputGroupInput,
+} from "@rallly/ui/input-group";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";
+import { CheckIcon } from "lucide-react";
+import type React from "react";
+import { Trans, useTranslation } from "@/i18n/client";
+
+/**
+ * Text input with an inline save button that appears when there are unsaved
+ * changes. Must be rendered inside a form: the save button submits it.
+ */
+export function InputWithSaveButton({
+ isDirty,
+ isSaving,
+ disabled,
+ className,
+ ...inputProps
+}: React.ComponentProps<typeof InputGroupInput> & {
+ isDirty: boolean;
+ isSaving: boolean;
+}) {
+ const { t } = useTranslation();
+ const showSaveButton = isDirty && !disabled;
+
+ return (
+ <InputGroup className={cn("w-56", className)}>
+ <InputGroupInput {...inputProps} disabled={disabled} />
+ {/* Kept mounted so the input width doesn't change when the save button
+ appears; `invisible` also keeps it out of the tab order. It must not
+ be disabled instead — that would trip InputGroup's `has-disabled:`
+ styles and grey out the whole field. */}
+ <InputGroupAddon
+ align="inline-end"
+ className={cn(!showSaveButton && "invisible")}
+ >
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <InputGroupButton
+ type="submit"
+ size="icon-xs"
+ loading={isSaving}
+ aria-label={t("save", { defaultValue: "Save" })}
+ >
+ <CheckIcon />
+ </InputGroupButton>
+ }
+ />
+ <TooltipContent>
+ <Trans i18nKey="save" defaults="Save" />
+ </TooltipContent>
+ </Tooltip>
+ </InputGroupAddon>
+ </InputGroup>
+ );
+}