Add MultiSelectVirtual enhance MultiSelect using Tanstack Virtual#60
Open
dennis-emstone wants to merge 2 commits intosersavan:mainfrom
Open
Add MultiSelectVirtual enhance MultiSelect using Tanstack Virtual#60dennis-emstone wants to merge 2 commits intosersavan:mainfrom
dennis-emstone wants to merge 2 commits intosersavan:mainfrom
Conversation
Contributor
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Author
|
I tested by modifying Following is test code. "use client";
import { useEffect, useState } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { toast } from "sonner";
import Link from "next/link";
import { cn } from "@/lib/utils";
import { Button, buttonVariants } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Icons } from "@/components/icons";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import {
PageActions,
PageHeader,
PageHeaderDescription,
PageHeaderHeading,
} from "@/components/page-header";
import { MultiSelectVirtual } from "@/components/multi-select-virtual";
const FormSchema = z.object({
frameworks: z
.array(z.string().min(1))
.min(1)
.nonempty("Please select at least one framework."),
});
export default function Home() {
const [frameworksList, setFrameworksList] = useState<any[] | null>(null);
useEffect(() => {
if (frameworksList) return;
const sampleData = [
{
value: "next.js",
label: "Next.js",
icon: Icons.dog,
},
{
value: "sveltekit",
label: "SvelteKit",
icon: Icons.cat,
},
{
value: "nuxt.js",
label: "Nuxt.js",
icon: Icons.turtle,
},
{
value: "remix",
label: "Remix",
icon: Icons.rabbit,
},
{
value: "astro",
label: "Astro",
icon: Icons.fish,
},
];
const sampleDataLength = sampleData.length;
const testDataSize = 5000;
let newFrameworksList = [];
for (let i = 0; i < testDataSize; i++) {
newFrameworksList.push({
value: `${sampleData[i % sampleDataLength].value} ${i}`,
label: `${sampleData[i % sampleDataLength].label} ${i}`,
icon: sampleData[i % sampleDataLength].icon,
});
}
setFrameworksList(newFrameworksList);
}, []);
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
frameworks: [],
},
});
function onSubmit(data: z.infer<typeof FormSchema>) {
toast(
`You have selected following frameworks: ${data.frameworks.join(", ")}.`
);
}
return (
<main className="flex min-h-screen:calc(100vh - 3rem) flex-col items-center justify-start space-y-3 p-3">
<PageHeader>
<PageHeaderHeading>Multi select component</PageHeaderHeading>
<PageHeaderDescription>assembled with shadcn/ui</PageHeaderDescription>
<PageActions>
<Link
target="_blank"
rel="noreferrer"
href="https://github.com/sersavan/shadcn-multi-select-component"
className={cn(buttonVariants({ variant: "outline" }))}
>
<Icons.gitHub className="mr-2 h-4 w-4" />
GitHub
</Link>
</PageActions>
</PageHeader>
<Card className="w-full max-w-xl p-5">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="frameworks"
render={({ field }) => (
<FormItem>
<FormLabel>Frameworks</FormLabel>
<FormControl>
<MultiSelectVirtual
options={frameworksList ?? []}
onValueChange={field.onChange}
defaultValue={field.value}
placeholder="Select options"
variant="inverted"
animation={2}
maxCount={3}
/>
</FormControl>
<FormDescription>
Choose the frameworks you are interested in.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button variant="default" type="submit" className="w-full">
Submit
</Button>
</form>
</Form>
</Card>
</main>
);
} |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I experienced performance issues when using MultiSelect with a large list.
To improve performance, I integrated TanStack Virtual and created a MultiSelectVirtual component.
I'm submitting a PR with this enhancement.