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
New onCreate behavior for Select component creates challenges #2095
Comments
Hi @apoco, inside
|
Hm, your changes don't fix the issue. Maybe you didn't save your sandbox? I understand the documentation, but notice that when you choose the created item that it still shows |
Alright, I get the issue now, you want to get new data in onChange handler. I think we can add an option of returning null that will indicate that onCreate should not call onChange. |
…nd MultiSelect when onCreate function returns null or undefined (#2095)
Fixed in 5.1.7, now when onCreate function returns null or undefined onChange is not called |
What package has an issue
@mantine/core
Describe the bug
I'm working on upgrading from Mantine 4 to 5, and one of the changes introduces challenges. To explain, let's consider this sample component that worked in Mantine 4:
When a user typed a new value, this sequence of events would take place.
value
prop set to the new item, and a newboats
property with the list including the new item.selectItems
would be updated due to the change of theboats
propertylookupById
would be updated due to the change of theboats
propertySelect
is rendered with the newdata
andvalue
With 5.x we have to return a new
SelectItem
in our callback, and theonChange
callback is called immediately afteronCreate
. Even ifonCreate
doesreturn { value: newBoat.id, label: newBoat.name };
at the end, this sequence happens:value
lookupById
because a re-render has not yet happened, soonSelect
is called withundefined
.value
prop set toundefined
and a newboats
property with the list including the new item.So after this sequence the object we just created is not selected because on step 5 the call to
onChange
undoes the selection we performed on step 3.What version of @mantine/hooks page do you have in package.json?
5.1.4
If possible, please include a link to a codesandbox with the reproduced problem
https://codesandbox.io/s/kind-lamport-noc5ez?file=/src/App.tsx
Do you know how to fix the issue
Yes
Are you willing to participate in fixing this issue and create a pull request with the fix
Yes
Possible fix
Thankfully for me there's a workaround for the race condition because I can just assume that if a matching value is not in my lookup
Map
that this create race condition took place, but I wonder if there's a cleaner approach that introduces less surprises.I'm guessing that the reason you require a return value from
onCreate
is for cases where thedata
prop is not fully controlled. Thus, it makes sense that if Mantine is going to manage the state of the new item you'd have to supply it. Similarly, it'd make sense thatonChange
would be called immediately to select that newly injected item.For those of us that are treating this as a controlled component, maybe it'd make sense to allow not returning a value in
onCreate
or support returningfalse
to indicate that we're handling our own insertion and selection logic. Ifundefined
(orfalse
) is returned,onChange
wouldn't be called (this would allow let us support the case where new item creation is explicitly failing), but if you do return a value the current behavior would persist.Another thing that could work is if the
onChange
callback on creation of a new item has a second boolean parameter indicating that it is the selection of a new item.Thoughts?
The text was updated successfully, but these errors were encountered: