Skip to content

Commit

Permalink
feat: update sortable again
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Jul 7, 2024
1 parent 01848c3 commit 67d9329
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 129 deletions.
83 changes: 44 additions & 39 deletions src/app/_components/hook-form-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import { useFieldArray, useForm } from "react-hook-form"
import { z } from "zod"

import { Button } from "@/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Skeleton } from "@/components/ui/skeleton"
import {
Sortable,
SortableDragHandle,
Expand Down Expand Up @@ -53,34 +59,47 @@ export function HookFormDemo() {
})

return (
<div className="rounded-lg border p-6">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="flex w-full max-w-4xl flex-col gap-4"
>
<div className="space-y-1">
<h4>Flip tricks</h4>
<p className="text-[0.8rem] text-muted-foreground">
Add your favorite flip tricks
</p>
<Card>
<div className="flex flex-col items-center gap-4 sm:flex-row">
<CardHeader className="w-full flex-col gap-4 space-y-0 sm:flex-row">
<div className="flex flex-1 flex-col gap-1.5">
<CardTitle>Vertical sorting</CardTitle>
<CardDescription>
Sort items in the vertical direction.
</CardDescription>
</div>
<div className="space-y-2">
<Button
type="button"
variant="outline"
size="sm"
className="w-fit"
onClick={() => append({ name: "", spin: "" })}
>
Add trick
</Button>
</CardHeader>
</div>
<CardContent>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="flex w-full flex-col gap-4"
>
<Sortable
value={fields}
onMove={({ activeIndex, overIndex }) =>
move(activeIndex, overIndex)
}
overlay={
<div className="grid grid-cols-[0.5fr,1fr,auto,auto] items-center gap-2">
<Skeleton className="h-8 w-full rounded-sm" />
<Skeleton className="h-8 w-full rounded-sm" />
<Skeleton className="size-8 shrink-0 rounded-sm" />
<Skeleton className="size-8 shrink-0 rounded-sm" />
<div className="h-8 w-full rounded-sm bg-primary/10" />
<div className="h-8 w-full rounded-sm bg-primary/10" />
<div className="size-8 shrink-0 rounded-sm bg-primary/10" />
<div className="size-8 shrink-0 rounded-sm bg-primary/10" />
</div>
}
>
<div className="w-full space-y-2">
<div className="flex w-full flex-col gap-2">
{fields.map((field, index) => (
<SortableItem key={field.id} value={field.id} asChild>
<div className="grid grid-cols-[0.5fr,1fr,auto,auto] items-center gap-2">
Expand Down Expand Up @@ -109,7 +128,7 @@ export function HookFormDemo() {
<SortableDragHandle
variant="outline"
size="icon"
className="size-8 shrink-0"
className="size-8 shrink-0 cursor-grab"
>
<DragHandleDots2Icon
className="size-4"
Expand All @@ -134,26 +153,12 @@ export function HookFormDemo() {
))}
</div>
</Sortable>
<Button
type="button"
variant="outline"
size="sm"
className="w-fit"
onClick={() =>
append({
name: "",
spin: "",
})
}
>
Add trick
<Button size="sm" className="w-fit">
Submit
</Button>
</div>
<Button type="submit" className="w-fit">
Submit
</Button>
</form>
</Form>
</div>
</form>
</Form>
</CardContent>
</Card>
)
}
51 changes: 0 additions & 51 deletions src/app/_components/items.tsx

This file was deleted.

53 changes: 53 additions & 0 deletions src/app/_components/mixed-axis-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use client"

import * as React from "react"
import { closestCorners } from "@dnd-kit/core"

import { dataConfig } from "@/config/data"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { Sortable, SortableItem } from "@/components/ui/sortable"

export function MixedAxisDemo() {
const [specialTricks, setSpecialTricks] = React.useState(
dataConfig.speicalTricks
)

return (
<Card>
<div className="flex flex-col items-center gap-4 sm:flex-row">
<CardHeader>
<CardTitle>Mixed sorting</CardTitle>
<CardDescription>Sort items in both directions.</CardDescription>
</CardHeader>
</div>
<CardContent>
<Sortable
orientation="mixed"
collisionDetection={closestCorners}
value={specialTricks}
onValueChange={setSpecialTricks}
overlay={<div className="size-full rounded-md bg-primary/10" />}
>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-4">
{specialTricks.map((item) => (
<SortableItem key={item.id} value={item.id} asTrigger asChild>
<Card className="flex aspect-video items-center justify-center rounded-md bg-secondary hover:bg-accent">
<CardHeader className="items-center">
<CardTitle>{item.name}</CardTitle>
<CardDescription>{item.points} points</CardDescription>
</CardHeader>
</Card>
</SortableItem>
))}
</div>
</Sortable>
</CardContent>
</Card>
)
}
3 changes: 1 addition & 2 deletions src/app/data-table/_components/data-table-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Input } from "@/components/ui/input"
import { Skeleton } from "@/components/ui/skeleton"
import {
Sortable,
SortableDragHandle,
Expand Down Expand Up @@ -288,7 +287,7 @@ export function DataTableDemo() {
<Table>
<TableBody>
<TableRow>
<Skeleton className="h-12 w-full" />
<div className="h-12 w-full bg-accent/10" />
</TableRow>
</TableBody>
</Table>
Expand Down
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Shell } from "@/components/shell"

import { HookFormDemo } from "./_components/hook-form-demo"
import { Items } from "./_components/items"
import { MixedAxisDemo } from "./_components/mixed-axis-demo"

export default function IndexPage() {
return (
<Shell>
<HookFormDemo />
<Items />
<MixedAxisDemo />
</Shell>
)
}
2 changes: 1 addition & 1 deletion src/components/layouts/site-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function SiteHeader() {
</span>
</Link>
<nav className="flex w-full items-center gap-4 text-sm">
<NavItem href="/">Hook form</NavItem>
<NavItem href="/">Overview</NavItem>
<NavItem href="/data-table">Data table</NavItem>
</nav>
<nav className="flex flex-1 items-center md:justify-end">
Expand Down
76 changes: 76 additions & 0 deletions src/components/ui/card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as React from "react"

import { cn } from "@/lib/utils"

const Card = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"rounded-xl border bg-card text-card-foreground shadow",
className
)}
{...props}
/>
))
Card.displayName = "Card"

const CardHeader = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-1.5 p-6", className)}
{...props}
/>
))
CardHeader.displayName = "CardHeader"

const CardTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn("font-semibold leading-none tracking-tight", className)}
{...props}
/>
))
CardTitle.displayName = "CardTitle"

const CardDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<p
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
CardDescription.displayName = "CardDescription"

const CardContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
))
CardContent.displayName = "CardContent"

const CardFooter = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex items-center p-6 pt-0", className)}
{...props}
/>
))
CardFooter.displayName = "CardFooter"

export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }
Loading

0 comments on commit 67d9329

Please sign in to comment.