Coder Social home page Coder Social logo

sersavan / shadcn-multi-select-component Goto Github PK

View Code? Open in Web Editor NEW
486.0 4.0 23.0 215 KB

A multi-select component designed with shadcn/ui

Home Page: https://shadcn-multi-select-component.vercel.app

License: MIT License

JavaScript 0.32% CSS 3.06% TypeScript 96.63%
nextjs reactjs shadcn-ui radix-ui multi-select multi-select-container multi-select-dropdown multi-selection multi-selector

shadcn-multi-select-component's Issues

form.reset() method doesn't clear the multiselect component

I tried implementing a clear button for the form in which I'm using the multiselect component.
I used onClick={() => {form.reset();}} for this clear button and the multiselect field is the only one not being cleared.

(By the way thank you for this amazing component).

styling issue

First, thanks you.
As we need to style component according to own design. I am unable to customize style of this compoent.
Here is my code,

 <FormField
            control={form.control}
            name="sizes"
            render={({ field }) => (
              <FormItem>
                {/* <FormLabel>Select size</FormLabel> */}
                <FormControl>
                  <MultiSelectFormField
                    options={sizesList}
                    defaultValue={field.value}
                    onValueChange={field.onChange}
                    placeholder="Select sizes"
                    className="h-30 max-w-[300px] shadow-none"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
      Please help me to customize this component

how can i use your component with react hook form?

i need to something like this.. to have control under your multi select component

<Form {...form}>
              <form
                onSubmit={form.handleSubmit(onHandleSubmit)}
                className="space-y-4"
              >
                <FormField
                  control={form.control}
                  name="authors"
                  render={({ field: { ...field } }) => (
                    <FormItem className="mb-5">
                      <FormLabel>Author</FormLabel>
                      <MultiSelect
                        selected={field.value}
                        options={authorsData}
                        {...field} />
                    </FormItem>
                  )}
                />
                <Button type="submit" className="w-full">
                  Continue
                </Button>
              </form>
            </Form>
            ```

Cannot scroll item on mobile device and desktop

I'm experiencing an issue where I'm unable to use the scroll mouse wheel and scroll touch on a mobile phone. This issue persists across various browsers and devices.

video_2024-07-04_20-49-47.mp4

Warning: Maximum update depth exceeded.

Hello @sersavan !

first of all, thanks for share your component!

im trying to use it without defaultValues, but have useEffect to update state on first render and it is causing a error when
defaultValues doesnt exist, causing error:

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

If i try to pass a empty string, i have empty component on UI as image below.

image

const form = useForm<Example>({
  resolver: zodResolver(exampleSchema),
  defaultValues: {},
});

<Form {...form}>
  <form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-2">
    <FormField
      control={form.control}
      name="example"
      render={({ field }) => (
        <FormItem className="mb-5">
          <FormControl>
            <MultiSelect
              options={[
                { value: 'example', label: 'Example' },
              ]}
              onValueChange={field.onChange}
              defaultValue={field.value}
              placeholder="Example"
              variant="inverted"
              animation={2}
              maxCount={3}
            />
          </FormControl>
          <FormMessage />
        </FormItem>
      )}
    />
  </form>
</Form>
Dependencies

"react": "^18.3.1",
"react-hook-form": "^7.53.0",
"react-dom": "^18.3.1",
"next": "^14.2.7",
"@hookform/resolvers": "^3.9.0",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-radio-group": "^1.2.0",
"@radix-ui/react-scroll-area": "^1.1.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.2",

Popover Menu Content

Hi, I got disabled popover like below from the image. I can't click on anything. It seems CommandItem components from shadcn got applied data-disabled attribute set to false. I have no idea how to solve it in the code, but I manage to remove that attribute from the inspect the items can be clickable again.

Screenshot from 2024-07-10 19-31-02

TypeScript compiler is unable to iterate through the Set

Great work. I managed to resolve the TS error I get with my NextJS [14] project

What you think?

    const toggleOption = (value: string) => {
      if (selectedValuesSet.current.has(value)) {
        selectedValuesSet.current.delete(value);
        setSelectedValues(selectedValues.filter((v) => v !== value));
      } else {
        selectedValuesSet.current.add(value);
        setSelectedValues([...selectedValues, value]);
      }
      onValueChange(Array.from(selectedValuesSet.current));
    };

Issue with selectedValues not updating on mount

While implementing the MultiSelect component using controlled form, I noticed a minor issue in the useEffect hook that can arise during the initial mounting phase. The intention appears to be synchronizing the selectedValues state with the defaultValue prop.

However, there's an error that can prevent this synchronization from happening correctly when the component first mounts.

Currently, if defaultValue is already set when the component mounts and it differs from the initial value of selectedValues (which is an empty array), the useEffect will detect this discrepancy.

However, the subsequent setSelectedValues(selectedValues); maintains the existing selectedValues instead of updating it to the defaultValue.

React.useEffect(() => {
  if (JSON.stringify(selectedValues) !== JSON.stringify(defaultValue)) {
    setSelectedValues(selectedValues);
  }
}, [defaultValue, selectedValues]);

To fix my problem I've simply set the selected values to the defaultValue

React.useEffect(() => {
  if (JSON.stringify(selectedValues) !== JSON.stringify(defaultValue)) {
    setSelectedValues(defaultValue);
  }
}, [defaultValue, selectedValues]);

How to contribute this project?

Hi! I am quite satisfied the work you have done :)
I'd like to say thanks to you first.

Btw, I need some additional functionalities!

My specific use cases are,

  1. I need to disable specific option(s) initially.
  2. In the context of 1. I cannot interact with the disabled option(s) by clicking itself(themselves) or UI elements like 'Clear' button, 'x' icon button, '(Select All)' option item in the list. Even if I click that, there should be no effect on disabled option(s).

I think I am able to implement this functionality in a few hrs. If you don't mind letting me contribute to this functionality, please let me know how to do that!

Overflow hidden when using multiselect in shadcn/dialog

Hey, I just want to let you know there's something wrong with overflow when you use this component in shadcn/dialog. Scroll is not working.

Screenshot 2024-08-27 at 13 27 32

Take care!

[Updated] Nevermind, I haven't noticed modalPopover option before. Please, delete it. Take care!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.