inserting a new object into items #779
-
I'm trying to add a new object to the items array. The allowNew option doesn't work for me because my items are objects with multiple properties, not just strings. I pop up a react-bootstrap Modal dialog, enter the item data, add the item to the items array, and set the selected item. Nothing shows up in my typeahead. What is the best approach to do this. --thanks, Mike |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Hey @flyfisher1952 can you please post some code samples? |
Beta Was this translation helpful? Give feedback.
-
Hi, I got a codesandbox site working, at least well enough to demonstrate the problem. The REST API isn't there so It doesn't pull the data correctly, but the popup works and should still be able to add a new element to the (empty) items list for the typeahead and have it display in the typeahead. I'm not sure that the internal link works for others since this is the first time I've used codesandbox. Here is the link: https://fqlkzu-3000.csb.app/PostReport The link in the browser itself is: The "Water" typeahead is the one I'm working on. Click the plus icon and the pop-up will appear. Enter a string and click "Ok". At that point, the addWaterOk callback runs. That is where is should add the new water object to the items list and set it as the selectedWater. I've messed around with this for days now and tried a bunch of approaches. I currently think the async onSearch function is probably stepping on the list and I need to create a state variable that gets added in getWaters to the list each time it is called if the newWater object exists. My username on codesandbox is the same as my GitHub username since I'm logging in to codesandbox with my GitHub credentials. I really appreciate your help on this. thanks, Mike |
Beta Was this translation helpful? Give feedback.
So I had to use a kludge via useRef. Adding the following line to the handleAddWater function did the trick.
I replaced the line that forces the typeahead to have focus since adding the new water accomplishes the typeahead's purpose. I can now increase the complexity of the water object.
thanks.