Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: Carousel refreshes a page when it's inside a form #3930

Closed
2 tasks done
alamenai opened this issue Jun 6, 2024 · 1 comment
Closed
2 tasks done

[bug]: Carousel refreshes a page when it's inside a form #3930

alamenai opened this issue Jun 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@alamenai
Copy link

alamenai commented Jun 6, 2024

Describe the bug

When this Carousel component is placed inside a form element, it causes the entire page to refresh.

Problem Summary:

  1. Component Involved: Carousel from Shadcn UI library.
  2. Context: When the Carousel is placed inside a form.
  3. Observed Behavior: The entire page refreshes.
ba831df9-d410-4952-9e89-bf5edf4e9305.webm

Affected component/components

Carousel

How to reproduce

ba831df9-d410-4952-9e89-bf5edf4e9305.webm

Code

<FormItem className='w-full items-baseline'>
                            <FormControl>
                              <Input
                                id='electricity-annual-consumption'
                                className='h-11'
                                placeholder='Geben Sie Ihren Strombedarf ein'
                                {...field}
                                onChange={(event) => {
                                  const { value } = event.target
                                  if (/^\d*$/.test(value)) {
                                    field.onChange(value)
                                  }
                                }}
                              />
                            </FormControl>
                            <FormMessage />

                            <div>
                              <Carousel
                                opts={{
                                  align: "start",
                                }}
                                className='w-full max-w-sm'
                              >
                                <CarouselContent>
                                  {Array.from({ length: 5 }).map((_, index) => (
                                    <CarouselItem key={index} className='md:basis-1/2 lg:basis-1/3'>
                                      <div className='p-1'>
                                        <Card>
                                          <CardContent className='flex aspect-square items-center justify-center p-6'>
                                            <span className='text-3xl font-semibold'>{index + 1}</span>
                                          </CardContent>
                                        </Card>
                                      </div>
                                    </CarouselItem>
                                  ))}
                                </CarouselContent>
                                <CarouselPrevious />
                                <CarouselNext />
                              </Carousel>
                            </div>
                            <Button
                              disabled={!isElectricityConsumptionValid(parseInt(value, 10))}
                              variant='ghost'
                              className='text-blue-500 hover:text-blue-600 w-full'
                              onClick={() => setIsElectrictyDemandEditable(false)}
                            >
                              Speichern
                            </Button>
                          </FormItem>

Codesandbox/StackBlitz link

https://jam.dev/c/7429742c-aff6-4f54-8438-d1b00e55f069

Logs

No response

System Info

OS: Linux
Browser: Chrome

Before submitting

  • I've made research efforts and searched the documentation
  • I've searched for existing issues
@alamenai alamenai added the bug Something isn't working label Jun 6, 2024
@alamenai
Copy link
Author

alamenai commented Jun 6, 2024

It's resolved by adding onClick={(e) => e.preventDefault()} to the Carousel component.

@alamenai alamenai closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant