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

Add a way to add disabled prop to all Fields or inputs. #1233

Open
tsiq-jeremy opened this issue Jan 10, 2019 · 14 comments
Open

Add a way to add disabled prop to all Fields or inputs. #1233

tsiq-jeremy opened this issue Jan 10, 2019 · 14 comments

Comments

@tsiq-jeremy
Copy link

馃殌 Feature request

I want to be able to make all Fields/inputs in a form be disabled through the top level Form component. Our goal is that while submitting, we disable the form so the user can not change the fields during this time.

Current Behavior

Currently you would need to pass disabled prop to every input like this:

<Form>
  <Field id="a" name="a" type="text" disabled={isSubmitting}/>
  <Field id="b" name="b" type="text" disabled={isSubmitting}/>
</Form>

Desired Behavior

Instead it would be nice to pass the disabled prop just to the Form and have it be passed down to the Field's.

<Form disabled={isSubmitting}>
  <Field id="a" name="a" type="text"/>
  <Field id="b" name="b" type="text"/>
</Form>

Who does this impact? Who is this for?

This is helpful for anyone who has to regularly prevent users from filling out fields or wanting to toggle a form to be read only.

Additional context

Docs on the disabled attribute.
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/disabled

I have not done research on how this works with other types of inputs such as checkboxes or select.

@jaredpalmer
Copy link
Owner

AFAIK <form disabled> is invalid. This would be quite magical. Need to think about it more.

@jaredpalmer jaredpalmer changed the title Have easy way to add disabled prop to all Fields or inputs. Add a way to add disabled prop to all Fields or inputs. Feb 6, 2019
@jaredpalmer
Copy link
Owner

This can be implemented in user land by building a custom <Form> and using the DOM. The tricky thing is that not all Fields are inputs and not all users use <Field>.

@tsiq-jeremy
Copy link
Author

For a project we built a custom <Form> component that under the hood uses react context to pass the disabled prop to all of the input components. As you mentioned, probably the trickiest parts is normalizing all of the input components so that they take the same disabled prop. For instance react-select takes as prop isDisabled so you would need a mapping that accounts for this.

@stale stale bot added the stale label Apr 11, 2019
@lambert-velir
Copy link

Could disabled={isSubmitting} be the default behavior of <Field>?

@stale stale bot removed the stale label May 8, 2019
@Andreyco
Copy link
Collaborator

Andreyco commented May 8, 2019

The tricky thing is that not all Fields are inputs and not all users use <Field>.

cc @lambert-velir

@lambert-velir
Copy link

disabled is a valid attribute for all form elements, input, textarea, select, which is what the component prop of <Field> expects. https://jaredpalmer.com/formik/docs/api/field#component

If the user passes a custom React component, I would think they would either pass disabled through to the underlying input, etc, or handle it the same way.

@gtournie
Copy link

gtournie commented Jun 18, 2019

Why not using a fieldset at the top level? You can disabled all the fields inside doing just <fieldset disabled>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset#Attributes

@seanf
Copy link

seanf commented Jul 2, 2019

Thanks @gtournie. For anyone else using antd, the inputs won't look disabled, so you might need a bit of CSS, eg this quick hack:

fieldset:disabled input {
  background: #ddd;
  cursor: wait;
}

@hnordt
Copy link

hnordt commented Jul 24, 2019

Here's what I'm doing:

function FormInput(props) {
  return (
    <Field name={props.name}>
      {({ field, form })=> (
        <Input
          {...field}
          disabled={form.isSubmitting || props.disabled}
          error={
            form.touched[field.name]
              ? form.errors[field.name]
              : null
          }
        />
      )}
    </Field>
  );
}

@stale stale bot added the stale label Sep 22, 2019
@onderonur
Copy link

onderonur commented Oct 21, 2019

For material-ui form items, I have created a custom form component with an additional context. Form gets a isDisabled prop and passes it to the context. Each form item inside the form consumes this context and they can also use their own disabled prop. This may not be the perfect solution. But it is simple and maintainable :)

@stale stale bot removed the stale label Oct 21, 2019
@stale stale bot added the stale label Dec 20, 2019
@nitedani
Copy link

nitedani commented Mar 25, 2021

const FormikDisabler = ({ disabled }: { disabled?: boolean }) => {
  const { setSubmitting } = useFormikContext();
  useEffect(() => {
    if (disabled) {
      setSubmitting(true);
    } else {
      setSubmitting(false);
    }
  }, [disabled, setSubmitting]);
  return null;
};

usage:

      <Formik ...>
        <Form>
          <FormikDisabler disabled />
           ...
        </Form>
      </Formik>

@johnrom
Copy link
Collaborator

johnrom commented Mar 25, 2021

I think a plugin api would be the right way to add functionality like this. #3109

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@VladBrok
Copy link

VladBrok commented Jun 1, 2023

@gtournie thanks

Why not using a fieldset at the top level? You can disabled all the fields inside doing just <fieldset disabled>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset#Attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests