Giter VIP home page Giter VIP logo

codemod's Introduction

npm downloads npm npm Discord

Get started | API | Form Builder | FAQs | Examples

Features

Install

npm install react-hook-form

Quickstart

import { useForm } from 'react-hook-form';

function App() {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm();

  return (
    <form onSubmit={handleSubmit((data) => console.log(data))}>
      <input {...register('firstName')} />
      <input {...register('lastName', { required: true })} />
      {errors.lastName && <p>Last name is required.</p>}
      <input {...register('age', { pattern: /\d+/ })} />
      {errors.age && <p>Please enter number for age.</p>}
      <input type="submit" />
    </form>
  );
}

Sponsors

Thanks go to these kind and lovely sponsors!

Past sponsors

Backers

Thanks go to all our backers! [Become a backer].

Contributors

Thanks go to these wonderful people! [Become a contributor].

codemod's People

Contributors

jorisre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

codemod's Issues

No support for context API

react-hook-form V6 provides an standard context API (useFormContext). It should be also covered by codemod.

export const findUseFormDeclarators = (
root: Collection<any>,
j: JSCodeshift
) => (
path: ASTPath<ImportDeclaration>
): Collection<VariableDeclarator> | null => {
/**
* We search for `useForm` in import node
* @example
* import { useForm } from "react-hook-form";
* ^
* */
const useFormImport = path.value.specifiers.find(
(specifier) =>
specifier.type === 'ImportSpecifier' &&
specifier.imported.name === USE_FORM
);
if (!useFormImport) return null;
/**
* Retrieve useForm method name: `useForm` or `useFormCustomName`
* @example
* import { useForm } from "react-hook-form";
* import { useForm: useFormCustomName } from "react-hook-form";
* */
const useForm = useFormImport.local.name;
/**
* We search for all uses of `useForm` or `useFormCustomName`
* @example
* const { ... } = useForm();
* const { ... } = useFormCustomName();
* */
return root.find(j.VariableDeclarator, {
init: { callee: { name: useForm } }
});
};

Not working when name is template literal

Describe the bug
When there was template literal used in name of input codemod only removes ref={register} but doesn't create {...register()} and leaves name

To Reproduce
example input before running codemod:

<input
name={`${key.name}.apply`}
type="checkbox"
ref={register}
label="should apply for this key?"
id={`${key.name}.apply`}
/>

After codemod:

<input
name={`${key.name}.apply`}
type="checkbox"

label="should apply for this key?"
id={`${key.name}.apply`}
/>

Expected behavior

<input
{...register(`${key.name}.apply`)}
type="checkbox"
label="should apply for this key?"
id={`${key.name}.apply`}
/>

No changes, all files with NOC status

Describe the bug
No changes in TS components all files with NOC status.

Example:

import {CustomInput, FormGroup, Label} from 'reactstrap';
// ...
<FormGroup>
            <CustomInput
              type="checkbox"
              name="setCustomFeature"
              id="setCustomFeature"
              label={
                <input
                  type="text"
                  name="reason.custom_feature"
                  className="custom-input-undeline"
                  placeholder="Custom feature"
                  ref={register}
                />
              }
              innerRef={register}/>
          </FormGroup>

Output:

All done. 
Results: 
0 errors
459 unmodified
0 skipped
0 ok

6->7 - didn't create register prop

Describe the bug
In a few of my inputs, either ref prop was simply deleted, or left untouched, with no new props added. Codemod completed with 0 errors.

To Reproduce
On this one ref prop was just deleted, nothing added:

<Input.med
  name={`${type}.containers`}
  type="number"
  ref={register({
    required: true,
    validate: {
      min: (value) =>
        parseFloat(value) >= min ||
        `You must have at least ${min} containers`,
      max: (value) =>
        parseFloat(value) <= max ||
        `You cannot have more than ${max} containers`,
      integer: (value) =>
        Number.isInteger(parseFloat(value)) ||
        'Must be an integer',
    },
  })}
/>

This one was just left as is - nothing deleted, nothing added:

<Input.med
  name={`${type}.memory`}
  type="number"
  disabled={!haEnabled}
  ref={
    !haEnabled
      ? null
      : register({
          validate: {
            min: (value) =>
              parseFloat(value) >= min || `Should be greater than ${min}`,
            max: (value) =>
              parseFloat(value) <= max || `Should be greater than ${max}`,
          },
        })
  }
/>

Expected behavior
Should be replaced with a set of corresponding props

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Node.js 14

npx @hookform/codemod v7/update-register not working for material ui

Describe the bug
A clear and concise description of what the bug is.

The npx @hookform/codemod v7/update-register does not update material UI textfield with
<TextField disabled fullWidth margin="normal" name="name" size="small" type="text" variant="outlined" error={!!errors.name} inputRef={register({ required: true })} />
Only works for
`-

  • <input {...register('example')} />
  • <input {...register('example')} />
  • <input {...register('example')} />
  • <input ref={register({ required: true })} name="example" />
  • <input {...register('example', { required: true })} />
  • <TextInput ref={register({ required: true })} name="example" />
  • <TextInput {...register('example', { required: true })} />`

To Reproduce
Steps to reproduce the behavior

Expected behavior
A clear and concise description of what you expected to happen.

when I run npx @hookform/codemod v7/update-register
it should update matatrial UI textfield input from
<TextField disabled fullWidth margin="normal" name="name" size="small" type="text" variant="outlined" error={!!errors.name} inputRef={register({ required: true })} />
To
<TextField disabled fullWidth margin="normal" size="small" type="text" variant="outlined" error={!!errors.name} {...register("name",{ required: true })} />

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. MacOs, Linux]
  • Node.js [e.g. 12, 14]

Additional context
Add any other context about the problem here.

useTypedController

Describe the solution you'd like
replace such lines:

import { useTypedController } from '@hookform/strictly-typed';
...
const TypedController = useTypedController<DealPlacementPositionValue>({ control });
...
<TypedController ... />
...

with

import { Controller } from 'react-hook-form';
...
<Controller contol={control} render={({field})} ... />
...

Additional context
We can create codemod for TypedController (from @hookform/strictly-typed package). It was working very similar way as a new Controller.

TODO:
Handle new structure of render properties

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.