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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃П feature: disable prop for useForm #10496

Merged
merged 24 commits into from
Oct 7, 2023
Merged

馃П feature: disable prop for useForm #10496

merged 24 commits into from
Oct 7, 2023

Conversation

bluebill1049
Copy link
Member

@bluebill1049 bluebill1049 commented Jun 4, 2023

disabled prop

  • new disabled props for useForm
  • allow user to disable the entire form with all associated inputs
useForm({
  disabled: true
})

const App = () => {
  const [disabled, setDisabled] = useState(false);
  const { register, handleSubmit, control } = useForm({
    disabled,
  });

  return (
    <form
      onSubmit={handleSubmit(async () => {
        setDisabled(true);
        await sleep(100);
        setDisabled(false);
      })}
    >
      <input
        type={'checkbox'}
        {...register('checkbox')}
        data-testid={'checkbox'}
      />
      <input type={'radio'} {...register('radio')} data-testid={'radio'} />
      <input type={'range'} {...register('range')} data-testid={'range'} />
      <select {...register('select')} data-testid={'select'} />
      <textarea {...register('textarea')} data-testid={'textarea'} />

      <Controller control={control} render={({ field }) => <input disabled={field.disabled}  />} name="test" />

      <button>Submit</button>
    </form>
  );
}

@codesandbox
Copy link

codesandbox bot commented Jun 4, 2023

This branch is running in CodeSandbox. Use the links below to review this PR faster.


CodeSandbox logoCodeSandbox logo聽 Open in CodeSandbox Web Editor | VS Code | VS Code Insiders | Preview

@github-actions
Copy link
Contributor

github-actions bot commented Jun 4, 2023

Size Change: +159 B (+1%)

Total Size: 20.1 kB

Filename Size Change
dist/index.cjs.js 10 kB +83 B (+1%)
dist/index.umd.js 10.1 kB +76 B (+1%)

compressed-size-action

@bluebill1049 bluebill1049 changed the title feature: disable prop for useForm feature: disable prop for useForm Jun 4, 2023
@bluebill1049 bluebill1049 marked this pull request as ready for review June 4, 2023 11:47
@bluebill1049 bluebill1049 changed the title feature: disable prop for useForm 馃П feature: disable prop for useForm Jun 25, 2023
@glekner
Copy link
Contributor

glekner commented Jun 28, 2023

cc @bengry

@stale
Copy link

stale bot commented Aug 10, 2023

Thank you for your contributions! This Pull Request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Best, RHF Team 鉂わ笍

@stale stale bot added the stale label Aug 10, 2023
@vimtor
Copy link

vimtor commented Sep 15, 2023

I don't know if this is related, but components using fields like this:

<Controller
  control={control}
  render={({ field }) => (
    <TextInput
      disabled={true}
      {...field}
    />
  )}
/>

Stopped being disabled in the latest version ^7.46.1.

@bluebill1049
Copy link
Member Author

I don't know if this is related, but components using fields like this:

<Controller
  control={control}
  render={({ field }) => (
    <TextInput
      disabled={true}
      {...field}
    />
  )}
/>

Stopped being disabled in the latest version ^7.46.1.

<Controller
  control={control}
  render={({ field }) => (
    <TextInput
      {...field}
      disabled={true}
    />
  )}
/>

should fix this.

@vimtor
Copy link

vimtor commented Sep 15, 2023

@bluebill1049 I know 馃槀馃槀

I posted it here just in case it was unintentional. I would expect that if the disabled prop is not specified in the useForm hook, disabled should be undefined when passed to the component.

@bluebill1049
Copy link
Member Author

I posted it here just in case it was unintentional. I would expect that if the disabled prop is not specified in the useForm hook, disabled should be undefined when passed to the component.

I am pretty sure it returns undefined, could you verify it?

@vimtor
Copy link

vimtor commented Sep 16, 2023

I am pretty sure it returns undefined, could you verify it?

Well, my apologies since I thought that doing something like this in JavaScript resulted in disabled being true:

{
  disabled: true,
  disabled: undefined,
}

When in reality the disabled prop should not be passed altogether. I guess instead of making it undefined you could exclude it from the field props to not override any existing props?

I don't know, sorry for the confusion.

@bluebill1049
Copy link
Member Author

should be patched in the next release.

@bluebill1049 bluebill1049 merged commit 0d565fc into master Oct 7, 2023
7 checks passed
@bluebill1049 bluebill1049 deleted the form-disabled branch October 7, 2023 00:28
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.

None yet

3 participants