Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor to fix field array bugs

Tim Wilsona060817c73b6
diff --git a/apps/web/ui/partners/design/edit-list.tsx b/apps/web/ui/partners/design/edit-list.tsx
index acf479bf11..a5b8ff53d4 100644
--- a/apps/web/ui/partners/design/edit-list.tsx
+++ b/apps/web/ui/partners/design/edit-list.tsx
@@ -2,7 +2,12 @@
import { Button, GripDotsVertical, Plus2, Trash } from "@dub/ui";
import { cn } from "@dub/utils";
-import { motion, Reorder, useDragControls } from "framer-motion";
+import {
+ AnimatePresence,
+ motion,
+ Reorder,
+ useDragControls,
+} from "framer-motion";
import {
createContext,
Dispatch,
@@ -45,10 +50,11 @@ export function EditList({
axis="y"
values={values}
onReorder={onReorder}
+ layoutScroll
className="flex flex-col gap-2"
>
<EditListContext.Provider value={{ expandedValue, setExpandedValue }}>
- {children}
+ <AnimatePresence>{children}</AnimatePresence>
</EditListContext.Provider>
</Reorder.Group>
<Button
@@ -81,6 +87,7 @@ export function EditListItem({
return (
<Reorder.Item
+ key={value}
value={value}
dragListener={false}
dragControls={controls}
diff --git a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
index e40ed86c0b..54606de968 100644
--- a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
+++ b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
@@ -1,17 +1,15 @@
"use client";
import { programLanderAccordionBlockSchema } from "@/lib/zod/schemas/program-lander";
-import { Button, Modal, useMediaQuery, useScrollProgress } from "@dub/ui";
-import { Dispatch, SetStateAction, useId, useRef } from "react";
import {
- FieldArrayWithId,
- FormProvider,
- useFieldArray,
- UseFieldArrayRemove,
- useForm,
- useFormContext,
- useWatch,
-} from "react-hook-form";
+ AnimatedSizeContainer,
+ Button,
+ Modal,
+ useMediaQuery,
+ useScrollProgress,
+} from "@dub/ui";
+import { Dispatch, SetStateAction, useId, useRef } from "react";
+import { FormProvider, useForm } from "react-hook-form";
import { v4 as uuid } from "uuid";
import { z } from "zod";
import { EditList, EditListItem } from "../edit-list";
@@ -59,13 +57,9 @@ function AccordionBlockModalInner({
},
});
- const { handleSubmit, register, control } = form;
+ const { handleSubmit, register, watch, setValue } = form;
- const { fields, append, remove, replace } = useFieldArray({
- control,
- keyName: "rhfId",
- name: "items",
- });
+ const fields = watch("items");
const scrollRef = useRef<HTMLDivElement>(null);
const { scrollProgress, updateScrollProgress } = useScrollProgress(scrollRef);
@@ -111,32 +105,98 @@ function AccordionBlockModalInner({
onScroll={updateScrollProgress}
className="scrollbar-hide relative max-h-[calc(100vh-300px)] overflow-y-auto"
>
- <EditList
- values={fields.map(({ id }) => id)}
- onAdd={() => {
- const id = uuid();
- append({
- id,
- title: `Item ${fields.length + 1}`,
- content: "",
- });
-
- return id;
- }}
- onReorder={(updated) =>
- replace(updated.map((id) => fields.find((f) => f.id === id)!))
- }
+ <AnimatedSizeContainer
+ //height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
>
- {fields.map((field, index) => (
- <AccordionItem
- key={field.id}
- field={field}
- totalFields={fields.length}
- remove={remove}
- index={index}
- />
- ))}
- </EditList>
+ <EditList
+ values={fields.map(({ id }) => id)}
+ onAdd={() => {
+ const id = uuid();
+
+ setValue(
+ "items",
+ [
+ ...fields,
+ {
+ id,
+ title: `Item ${fields.length + 1}`,
+ content: "",
+ },
+ ],
+ { shouldDirty: true },
+ );
+
+ return id;
+ }}
+ onReorder={(updated) =>
+ setValue(
+ "items",
+ updated.map((id) => fields.find((f) => f.id === id)!),
+ { shouldDirty: true },
+ )
+ }
+ >
+ {fields.map((field, index) => (
+ <EditListItem
+ key={field.id}
+ value={field.id}
+ title={field.title || "Item"}
+ onRemove={
+ fields.length > 1
+ ? () =>
+ setValue(
+ "items",
+ fields.filter(({ id }) => id !== field.id),
+ { shouldDirty: true },
+ )
+ : undefined
+ }
+ >
+ <div className="flex flex-col gap-6">
+ {/* Title */}
+ <div>
+ <label
+ htmlFor={`${id}-${field.id}-title`}
+ className="flex items-center gap-2 text-sm font-medium text-neutral-700"
+ >
+ Title
+ </label>
+ <div className="mt-2 rounded-md shadow-sm">
+ <input
+ id={`${id}-${field.id}-title`}
+ type="text"
+ placeholder="Title"
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register(`items.${index}.title`)}
+ />
+ </div>
+ </div>
+
+ {/* Content */}
+ <div>
+ <label
+ htmlFor={`${id}-${field.id}-content`}
+ className="flex items-center gap-2 text-sm font-medium text-neutral-700"
+ >
+ Content
+ </label>
+ <div className="mt-2 rounded-md shadow-sm">
+ <textarea
+ id={`${id}-${field.id}-content`}
+ rows={3}
+ maxLength={1000}
+ placeholder="Start typing"
+ className="block max-h-32 min-h-16 w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register(`items.${index}.content`)}
+ />
+ </div>
+ </div>
+ </div>
+ </EditListItem>
+ ))}
+ </EditList>
+ </AnimatedSizeContainer>
{/* Bottom scroll fade */}
<div
@@ -165,76 +225,6 @@ function AccordionBlockModalInner({
);
}
-function AccordionItem({
- field,
- totalFields,
- remove,
- index,
-}: {
- field: FieldArrayWithId<AccordionBlockData, "items", "rhfId">;
- totalFields: number;
- remove: UseFieldArrayRemove;
- index: number;
-}) {
- const id = useId();
- const { register, control } = useFormContext<AccordionBlockData>();
-
- const title = useWatch({
- control,
- name: `items.${index}.title`,
- });
-
- return (
- <EditListItem
- key={field.id}
- value={field.id}
- title={title || "Item"}
- onRemove={totalFields > 1 ? () => remove(index) : undefined}
- >
- <div className="flex flex-col gap-6">
- {/* Title */}
- <div>
- <label
- htmlFor={`${id}-${field.id}-title`}
- className="flex items-center gap-2 text-sm font-medium text-neutral-700"
- >
- Title
- </label>
- <div className="mt-2 rounded-md shadow-sm">
- <input
- id={`${id}-${field.id}-title`}
- type="text"
- placeholder="Title"
- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register(`items.${index}.title`)}
- />
- </div>
- </div>
-
- {/* Content */}
- <div>
- <label
- htmlFor={`${id}-${field.id}-content`}
- className="flex items-center gap-2 text-sm font-medium text-neutral-700"
- >
- Content
- </label>
- <div className="mt-2 rounded-md shadow-sm">
- <textarea
- id={`${id}-${field.id}-content`}
- rows={3}
- maxLength={1000}
- placeholder="Start typing"
- className="block max-h-32 min-h-16 w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register(`items.${index}.content`)}
- />
- </div>
- </div>
- </div>
- </EditListItem>
- );
-}
-
export function AccordionBlockThumbnail() {
return (
<svg