felte icon indicating copy to clipboard operation
felte copied to clipboard

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

Open Blankeos opened this issue 9 months ago • 3 comments

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: https://github.com/pablo-abc/felte/issues/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;
    }
  }
}

Blankeos avatar May 01 '24 18:05 Blankeos

@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.

vercel[bot] avatar May 01 '24 18:05 vercel[bot]

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;
    }
  }
}

henrychea avatar Jul 04 '24 04:07 henrychea

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;
    }
  }
}

klm127 avatar Aug 22 '24 13:08 klm127