npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thinknimble/tn-forms-react

v1.0.3

Published

React utils to use with @thinknimble/tn-forms

Downloads

1,288

Readme

Thinknimble React tn-forms utils

Motivation

React has a particular way of handling state. React's state strongly depends on referential equality rather than deep-equality. Thus, working with thinknimble/tn-forms Form classes don't work out of the box as state.

We put together some utils that could come in handy when using tn-forms with React so that we don't have to repeat ourselves whenever we use it.

Core utils

FormProvider

This is a context provider which allows us to use a tn-forms Form class as state in any of the descendant components within this wrapper.

Sample use:

//...
return (
  <FormProvider<TSampleFormInputs> formClass={SampleForm}>
    <DescendantComponent>
  </FormProvider>
)

TSampleFormInputs is the type of the inputs of the SampleForm.

useTnForm

Hook to consume the form state while inside a descendant component of FormProvider

Sample use:

const DescendantComponent = () => {
  // ...
  const {
    form,
    createFormFieldChangeHandler,
    overrideForm,
    setFields,
    validate,
  } = useTnForm<TSampleForm>();
  //..
  return <></>;
};

The generic type in this case is the combination of both TSampleFormInputs and SampleForm. But will allow for most of the returned values from the hook to be properly typed to your current form.

Note this makes the hook to potentially shoot yourself in the foot if you don't provide the right type here. At compile type you will not see errors, but if the form you're using is not the one that matches the type you passed to useTnForm you will get runtime errors or undefined access issues.

createFormFieldChangeHandler allows you to create a handler to perform changes to a form value. To keep immutability safe, you should not try to modify form in any other way that is not through this method. This function accepts an IFormField and returns a handler which can be used as an event handler. Beware that the created handler resembles (v:T)=>void and does not consider react's Event.

IG: Consider a regular input that has React event attached to its onChange callback.

And also take a CustomInput that wraps around an input but already processes the event and expects a (v:string)=>void callback. Then we could skip wrapping around our handler and simply pass it to the onChange prop.

const DescendantComponent = ()=>{
  //...
  const {form, createFormFieldChangeHandler} = useTnForm<TSampleForm>()

  const onMyFieldChange = (e:ChangeEvent<HTMLInputElement>)=>{
    createFormFieldChangeHandler(form.myField)(e.target.value)
  }

  return (
    <input value={form.myField.value} onChange={onMyFieldChange}/>
    <CustomInput value={form.anotherField.value} onChange={createFormFieldChangeHandler(form.anotherField)}/>
  )
}

form is the piece of state you can use then in your form components. This variable will keep track of changes of the form class instance and will rerender the component accordingly as long as you use the createFormFieldChangeHandler to create the onChange event handlers on the form components.