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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a Fields component through Form children #119

Open
danielweinmann opened this issue Dec 5, 2022 · 1 comment
Open

Pass a Fields component through Form children #119

danielweinmann opened this issue Dec 5, 2022 · 1 comment

Comments

@danielweinmann
Copy link
Contributor

This component would:

  • Render all fields in the schema precisely as if we hadn't passed children to the form. This is useful because sometimes we just want to customize the errors, the button, or the overall form layout. But not the fields. In this situation, we're currently forced to render all fields manually.
  • If we pass children to it, we only pass the Fields we want to customize, but it will render all form fields in the same order as the schema. This will allow us to customize only the fields we want and rely on the convention for the rest.

This should not change the current Form children render logic. It will only add this option to make our forms even DRYer.

@darlantc
Copy link

This would be awesome!

I just wrote this form code to hide one field based on the value of the other. I would love to focus only on it and leave the schema doing the ordering thing to me for the rest of them.

{({ Errors, Field, watch }) => {
  return (
    <>
      {getFieldsToRender().map((name) => (
        <Field key={name} name={name} />
      ))}
      <Errors />
    </>
  );

  function getFieldsToRender() {
    const paymentSourceValue = watch("paymentSource");
    const isCreditCard = paymentSourceValue?.startsWith("creditCard");

    return [
      "accountId",
      "id",
      "type",
      "value",
      "isConfirmed",
      "date",
      "description",
      "categoryId",
      "paymentSource",
      isCreditCard && "creditCardInvoiceSlug",
      "isIgnored",
      "observation",
      "tags",
    ].filter(Boolean);
  }
}}

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

No branches or pull requests

2 participants