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

docs(solid): Added instructions with the use directive for solid. #292

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

Blankeos
Copy link
Contributor

@Blankeos Blankeos commented May 1, 2024

There was a weird "gotcha" that took me actually a few frustrating hours to figure out because it wasn't reflected in the docs or anything.

I'm not sure if use:form is actually possible because after some experimenting + reading up on how this works: https://docs.solidjs.com/reference/jsx-attributes/use#use.

I still couldn't get it. So I honestly don't know if it's a skill issue or just the docs.

This was the issue I posted yesterday: #291. I think adding this to the readme would save a ton of time.

I found two ways actually:

Fix 1: Defining in a global dts

// vite-env.d.ts (or any *.d.ts file)
declare global {
  declare module "solid-js" {
    namespace JSX {
      interface Directives {
        form: (node: HTMLFormElement) => void;
      }
    }
  }
}

image

Fix 2: Defining in local file where its used.

import { createForm } from "@felte/solid";

declare module "solid-js" {
  namespace JSX {
    interface Directives {
      form: (node: HTMLFormElement) => void;
    }
  }
}
export default function Register() {
  const { form } = createForm({
    onSubmit: async (values) => {
      /** call to an api. */
    },
  });

  return (
    <main class="text-center mx-auto text-gray-700 p-4">
      <h1 class="">Register</h1>

      <form class="form-control" use:form={form}>
      // ...

🙁 Didn't work

I assumed just using use:form would work, but it didn't lol.

image

🙁 Another attempt that didn't work.

This was what the docs showed, and it didn't work. (it's because of the global value)

// vite-env.d.ts (or any *.d.ts file)
declare module "solid-js" {
  namespace JSX {
    interface Directives {
      form: (node: HTMLFormElement) => void;
    }
  }
}

Copy link

vercel bot commented May 1, 2024

@Blankeos is attempting to deploy a commit to the Pablo Alejandro Berganza Campos' projects Team on Vercel.

A member of the Team first needs to authorize it.

@henrychea
Copy link

I've got it working by using a global.d.ts file

/// <reference types="@solidjs/start/env" />

import "solid-js";
declare module "solid-js" {
  namespace JSX {
    interface Directives {
      form: (node: HTMLFormElement) => void;
    }
  }
}

@klm127
Copy link

klm127 commented Aug 22, 2024

Thanks for this, you saved me a lot of time!

I used a slightly modified version to also add onFelteSuccess and onFelteError, and not lose intrinsic form properties.

import "solid-js";

import { JSX as JSXOrig } from "solid-js";

type TForm = JSXOrig.FormHTMLAttributes<HTMLFormElement> & {
  'use:form'?: any;
  onFelteSuccess?: (e: CustomEvent) => void;
  onFelteError?: (e:CustomEvent) => void;
}


declare module "solid-js" {
  namespace JSX {
    interface IntrinsicElements {
      form: TForm;
    }
  }
}

@pablo-abc pablo-abc merged commit 1689e69 into pablo-abc:main Oct 29, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants