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]: Vee-validate fails when auto importing Form component #172

Closed
Zackery747 opened this issue Nov 14, 2023 · 9 comments
Closed

[Bug]: Vee-validate fails when auto importing Form component #172

Zackery747 opened this issue Nov 14, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@Zackery747
Copy link

Zackery747 commented Nov 14, 2023

Environment

Developement/Production OS: Windows 10 19043.1110
Node version: 16.0.0
Package manager: pnpm@8.6.0
Radix Vue version: 1.0.0
Shadcn Vue version: 1.0.0
Vue version: 3.0.0
Nuxt version: 3.0.0
Nuxt mode: universal
Nuxt target: server
CSS framework: tailwindcss@3.3.3
Client OS: Windows 10 19043.1110
Browser: Chrome 90.0.4430.212

Link to minimal reproduction

https://stackblitz.com/edit/nuxt-starter-bsx2ja?file=nuxt.config.ts

Steps to reproduce

You only have to run the project.

Describe the bug

Cannot destructure property 'componentField' of 'undefined' as it is undefined.

This is caused by Vee-Validate being used in the Form component. It only happens when the Form components are automatically imported. The Form works perfectly if you manually import the Form components.

Expected behavior

No error is being thrown.

Conext & Screenshots (if applicable)

<script setup lang="ts">
import { h } from 'vue';
import { useForm } from 'vee-validate';
import { toTypedSchema } from '@vee-validate/zod';
import * as z from 'zod';

const formSchema = toTypedSchema(
  z.object({
    username: z.string().min(2).max(50),
  })
);

const { handleSubmit } = useForm({
  validationSchema: formSchema,
});

const onSubmit = handleSubmit((values) => {
  toast({
    title: 'You submitted the following values:',
    description: h(
      'pre',
      { class: 'mt-2 w-[340px] rounded-md bg-slate-950 p-4' },
      h('code', { class: 'text-white' }, JSON.stringify(values, null, 2))
    ),
  });
});
</script>

<template>
  <Form class="w-2/3 space-y-6" @submit="onSubmit">
    <FormField v-slot="{ componentField }" name="username">
      <FormItem v-auto-animate>
        <FormLabel>Username</FormLabel>
        <FormControl>
          <Input type="text" placeholder="shadcn" v-bind="componentField" />
        </FormControl>
        <FormDescription> This is your public display name. </FormDescription>
        <FormMessage />
      </FormItem>
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>
@Zackery747 Zackery747 added the bug Something isn't working label Nov 14, 2023
@sadeghbarati
Copy link
Collaborator

sadeghbarati commented Nov 14, 2023

Thanks for the report this because index.ts ignored from auto-import in Nuxt installation

Only .vue files will be auto-imported

And I export vee-validate Form and Field components in index.ts

Will check it

@sadeghbarati
Copy link
Collaborator

@Zackery747

For now install @vee-validate/nuxt

https://vee-validate.logaretm.com/v4/integrations/nuxt/

And change Field auto-import name to FormField

@Zackery747
Copy link
Author

Thanks for the feedback @sadeghbarati! 💪

Just manually importing the form like this also works perfectly for now. 😃

import {
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form'

@zernonia
Copy link
Member

@sadeghbarati Im thinking of instead of importing component based on .vue extension, maybe we can add import based on the components exported in each index.ts file.

Thus would eliminate the issue whereby some component wasn't imported such in this case, as well as Pagination.
WDYT?

@sadeghbarati
Copy link
Collaborator

sadeghbarati commented Nov 16, 2023

@zernonia

That's good idea
I tested scanning only index.ts files, but that does seem not working

https://stackblitz.com/edit/github-xkryhq?file=nuxt.config.ts,.nuxt%2Fcomponents.d.ts


Another Stackblitz using Nuxt modules

https://stackblitz.com/edit/github-xkryhq-egbi1f?file=nuxt.config.ts,.nuxt%2Fcomponents.d.ts,modules%2Fshadcn-vue.ts


I made PR to Nuxt to add prepend (unshift) opt to addComponentsDirs in Nuxt Kit once it's ready in 3.9 we can create a module for shadcn-vue too

But we have to add a warning about not adding radix-vue/nuxt auto-import when the user using shadcn-vue components

@sadeghbarati
Copy link
Collaborator

sadeghbarati commented Nov 20, 2023

/cc @DamianGlowala sry for mentioning, I could not find a solution for this problem so I though it's better to ask a Nuxter

Please first look at these links

Some of the components don't need to be wrapped in another Vue component, for example in Form we just export Form and Field as FormField from vee-validate

The problem is Nuxt can't scan those components that are exported inside index.ts
I know in shadcn-vue Nuxt Configure we mention scanning only .vue extensions but even if I edit that to this 👇 , Nuxt scan still is not working well

dirs.unshift({
  path: '~/components/ui',
  pattern: ['**/index.ts'],
  prefix: 'Ui',
  pathPrefix: false
})

@zernonia
Copy link
Member

Will be fix by #196

@zernonia
Copy link
Member

zernonia commented Dec 1, 2023

You can now use shadcn-nuxt module to autoImport the component correctly.

@orenmizr
Copy link

orenmizr commented Apr 8, 2024

latest nuxt + shadcn from today... copied code from docs -> failed. imported manually - works. @zernonia

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

4 participants