Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

improve dynamicLists typing #1848

Conversation

sylvhama
Copy link
Contributor

You can test with this condesandbox.
I am using function overloading to define two scenarios:

  1. we have a classic useForm (like all existing ones) that does not use dynamicLists;
  2. we can a useForm relying dynamicLists.

Result is:

  • No more default value in our hook;
  • In App.tsx: No need to use ! or to check if dynamicLists is defined.

In the future we could do the same to also turn fields optional 🤔

@sylvhama sylvhama requested review from a team, attila-berki and oluwatimio and removed request for a team April 20, 2021 18:03
makeCleanAfterSubmit,
}: FormWithDynamicListsInput<T, D>): FormWithDynamicLists<T, D>;

export function useForm<T extends FieldBag, D extends DynamicListBag>({
Copy link
Contributor

@oluwatimio oluwatimio Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering, would you have to pass in a type for the second part of the generic for every useForm call that already passes in a generic type for the form?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, as you can see in the codesandbox (App.tsx), I made two form and I don't need to pass the generic types.

Copy link
Contributor

@oluwatimio oluwatimio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good! Nice 💯 . Thanks for digging into it 🙂 . I am a bit concerned about modifying the useForm this way but it's looking good in the sandbox!

I also noticed all the tests in form.test.tsx are breaking. The full CI does not seem to be running in this PR for some reason but you can try it locally. I can fix the tests in my PR so dont worry about it.

@sylvhama
Copy link
Contributor Author

Aaaaah I thought it was all green LOL nice catch! I will run tests locally and see what's going on.

@oluwatimio
Copy link
Contributor

oluwatimio commented Apr 21, 2021

LOL nice catch! I will run tests locally a

Its all good! We just have to change the useForm call to something like this

    const {
      submit,
      submitting,
      dirty,
      reset,
      makeClean,
      submitErrors,
      dynamicLists,
    } = useForm({
      fields: {title, description, defaultVariant, variants},
      dynamicLists: {
        dynamicVariants: useDynamicList(data.variants, variantsEmptyFactory),
      },
      onSubmit: onSubmit as any,
      makeCleanAfterSubmit,
    });

    const {
      dynamicVariants: {addItem, fields: dynamicListFields},
    } = dynamicLists;

and maybe remove the enable dynamic list prop in productForm and then remove the default test

Copy link
Contributor

@attila-berki attila-berki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯🤯🤯

@@ -73,6 +79,9 @@ describe('useDynamicList', () => {
});

it('will throw an error if new position is out of index range', () => {
const consoleErrorMock = jest.spyOn(console, 'error');
consoleErrorMock.mockImplementation();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid
Screen Shot 2021-04-21 at 10 37 03 AM

@@ -203,6 +212,66 @@ describe('useDynamicList', () => {
expect(wrapper).toContainReactComponent(TextField);
});
});

describe('dirty dynamic list', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far there was no dirty related test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm 🤔 @sylvhama . There are dirty tests in the baselist since that is where it is originally implemented

Copy link
Contributor

@oluwatimio oluwatimio Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I dont think we will need to have it in the dynamiclist test since dynamic list just returns what the baselist is returning

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same case for reset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But useList test don't cover cases where we add or remove an item. Plus it does not hurt to make sure nothing break the default behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I dont meanuseList, I meant useBaseList. Because I noticed in this PR some tests like value changes are being re-created. In that case, could we just write the tests that cover the adding and removing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but addItem, moveItem, removeItem can only be tested here since they are added by dynamiclist.ts. Also we want to make sure there is no local logic within dynamiclist.ts that could break the dirty logic 🤔


import {submitSuccess, submitFail} from '..';

describe('useForm with dynamic list', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those tests are inspired by origin useForm test.

makeClean and makeCleanAfterSubmit seem broken with dynamic lists 🤔

waitForSubmit,
} from './utilities';

import {submitSuccess, submitFail} from '..';

describe('useForm', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the code removed here has been moved to utilities.

import {SimpleProduct, Variant} from './types';
import {TextField} from './TextField';

export function FormWithDynamicVariantList({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new component to test dynamic lists.

import {SimpleProduct} from './types';
import {TextField} from './TextField';

export function ProductForm({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing changed here, I just moved the code.

@sylvhama sylvhama merged commit 6f19a13 into dynamic-lists/add-reset-and-dirty-state Apr 21, 2021
@sylvhama sylvhama deleted the react-form-dynamic-lists-typing branch April 21, 2021 19:33
oluwatimio pushed a commit that referenced this pull request Apr 21, 2021
* improve dynamicLists typing

* revert to initial tests

* mock console.error to clean logs

* add dirty tests for dynamic lists

* update form tests

* remove useless file
oluwatimio added a commit that referenced this pull request Apr 21, 2021
test baselist

Update baselist.test.tsx

lint fix and additional tests

update tests

Update CHANGELOG.md

Update README.md

Update CHANGELOG.md

add dynamic list to the form

update comments

Update form.ts

add check for input since theres a possibility of adding undefined input dynamicListFields

Update baselist.ts

Update baselist.ts

added tests to form

Update form.test.tsx

fixed reset recreating on each render

Update listdirty.ts

Update README.md

Update README.md

Update README.md

added DynamicListBag

Update packages/react-form/README.md

Co-authored-by: Sylvain Hamann <[email protected]>

Update packages/react-form/README.md

Co-authored-by: Sylvain Hamann <[email protected]>

Update README.md

Update baselist.ts

docs

used lazy ref

fix docs

more improvement to docs

return default bag on undefined dynamic list

Update form.test.tsx

rebase gone bad fix

improve dynamicLists typing (#1848)

* improve dynamicLists typing

* revert to initial tests

* mock console.error to clean logs

* add dirty tests for dynamic lists

* update form tests

* remove useless file

Update dynamiclist.test.tsx
oluwatimio added a commit that referenced this pull request Apr 21, 2021
test baselist

Update baselist.test.tsx

lint fix and additional tests

update tests

Update CHANGELOG.md

Update README.md

Update CHANGELOG.md

add dynamic list to the form

update comments

Update form.ts

add check for input since theres a possibility of adding undefined input dynamicListFields

Update baselist.ts

Update baselist.ts

added tests to form

Update form.test.tsx

fixed reset recreating on each render

Update listdirty.ts

Update README.md

Update README.md

Update README.md

added DynamicListBag

Update packages/react-form/README.md

Co-authored-by: Sylvain Hamann <[email protected]>

Update packages/react-form/README.md

Co-authored-by: Sylvain Hamann <[email protected]>

Update README.md

Update baselist.ts

docs

used lazy ref

fix docs

more improvement to docs

return default bag on undefined dynamic list

Update form.test.tsx

rebase gone bad fix

improve dynamicLists typing (#1848)

* improve dynamicLists typing

* revert to initial tests

* mock console.error to clean logs

* add dirty tests for dynamic lists

* update form tests

* remove useless file

Update dynamiclist.test.tsx
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants