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

[Feature]: Use a deep partial TS type for defaultValues prop #313

Closed
Alex-Yakubovsky opened this issue Aug 7, 2023 · 2 comments · Fixed by #318
Closed

[Feature]: Use a deep partial TS type for defaultValues prop #313

Alex-Yakubovsky opened this issue Aug 7, 2023 · 2 comments · Fixed by #318

Comments

@Alex-Yakubovsky
Copy link
Contributor

What is the new or updated feature that you are suggesting?

For the nested object syntax it would be handy to seed only a subset of the fields. For example, consider a todo app where we want to pre-fill the first todo based off a previous interaction

<ValidatedForm validator={validator} defaultValues={{ todos: [{ task: "<prefilled value>" }] }}> // TS error, task is missing property `dueDate`
  <Input name="todos[0].task" />
  <Input name="todos[0].dueDate" />
</ValidatedForm>

Functionally, it looks like ValidatedForm already works like this. Just the Typescript type need to be updated. As a work around in my codebase, I've been re-exporting the ValidatedForm component with a DeepPartial type for the defaultValues prop.

import { FormProps, ValidatedForm } from "remix-validated-form";

type DeepPartial<T> = T extends object
  ? {
      [P in keyof T]?: DeepPartial<T[P]>;
    }
  : T;

export default ValidatedForm as <DataType, Subaction extends string | undefined>(
  props: Omit<FormProps<DataType, Subaction>, "defaultValues"> & {
    defaultValues?: DeepPartial<FormProps<DataType, Subaction>["defaultValues"]>;
  }
) => JSX.Element;

Why should this feature be included?

  • So that the type matches the implementation
  • Useful for pre-filling more complex forms that use the nested object syntax
@airjp73
Copy link
Owner

airjp73 commented Aug 11, 2023

For some reason I thought I had already done this a long time ago haha. Would you mind creating a PR to update this?

@Alex-Yakubovsky
Copy link
Contributor Author

Happy to, here's the PR: #318

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 a pull request may close this issue.

2 participants