-
Notifications
You must be signed in to change notification settings - Fork 225
[react-form | DynamicList] add reset function and dirty state #1828
Conversation
👋 Thank you for the PR! Please run:
to get the latest changes and fix the failing test, apologies for the inconvenience. |
2c86b17
to
452f2f9
Compare
6b447e7
to
e3e9901
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested locally via yalc
and it does not fix #1830
It seems useForm
is not aware that the dynamic list is dirty. If I add a new item the form remains not dirty. Plus when I press reset I would expect that the list becomes empty again but it does not.
With this commit 8a4cfa7 we can now reset and get the dirty state of the dynamic list from the forms reset and dirty state |
0cd4a61
to
310bf01
Compare
PR now ready for review. Resetting lists in a listbag with the form's reset function should now be possible with dynamic list or any type of list created in the future. Same case for dirty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My biggest concern is the comment about export interface ListBag
.
Otherwise it looks good, I have not checked all the tests since we might update several stuff first.
27987cb
to
8b945c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I noticed that was already there, an a comment about some readability concerns, but otherwise this is looking pretty nice to me! I didn't tophat, but I'm happy to have this API in the library.
const fieldsRef = useRef(fields); | ||
fieldsRef.current = fields; | ||
const fieldsRef = useRef(fieldsWithLists); | ||
fieldsRef.current = fieldsWithLists; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was already here, but a note on the pattern:
This technically works but may stop working in future react updates. We recently removed similar patterns in other hooks in quilt and are considering adding a lint rule. It might be best to find an alternative way of doing this as a result.
Actually, looking at it closer, since we update it literally every render, why can't we just use the value directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh yes I think we can get rid of the ref since we update it, Thanks! Will try that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticing removing the ref causes the makeClean
function to be re-created on every render so I switched to useLazyRef
to avoid mutating refs directly during the render phase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice improvement of the form and the dynamic list 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there must be a way e.g. https://www.typescriptlang.org/docs/handbook/2/conditional-types.html (I am not TS expert, this might not be the right thing to look at). Also another problem comes to my mind, what if I only one to manipulate dynamic lists? then |
I am not sure but I think if we are to use a conditional type, wouldn't we still have to check what type it is before using the dynamic list? I personally don't see this being different to a prop being optional in react and having to check its defined before using it. Same thing for a gql query retuning an optional value and having to check. I am not sure where the problem lies because the user is in control of passing in the dynamic list so if they know it is defined, they can use the non null assertation which is what it was created for. The typescript docs says that here |
We discussed this and we will be returning an empty DynamicList bag when dynamicLists param is undefined. This will have no effect on computation in the form and will remove having to use the assertion operator or checking if dynamic list is defined. |
b42269d
to
572b8a7
Compare
Proposition to avoid default value for |
b1223c7
to
79bb4b9
Compare
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
79bb4b9
to
5101dda
Compare
Description
Fixes #1830 and part of https://github.com/Shopify/core-issues/issues/23975
There is no reset ability for dynamic list or flag to know if the dynamic list is dirty. This means if for example we are using a context bar and click discard, the dynamic list new field will still persist.
This PR adds the ability to reset to the initial list state and to know if the dynamic list is dirty.
It also adds the ability to add multiple dynamic lists
co-authored with @sylvhama
Tophat
Code sandbox here .
Type of change
Checklist