Custom Field Event w/ Data?

Is it possible for a custom field to trigger an event along with some data to use in populating other fields. For example, I have a 'title" field that I'm building that will search the Shopify API for a product title, but once the user selects the title, I'd also like that custom field to send the price and sku for the product up to the "updated" closure or something similar so that I can fill the price and sku fields in the form. Is this possible?
5 Replies
awcodes
awcodes2y ago
Check out closure customization and dependent fields in the docs.
bahamagician
bahamagicianOP2y ago
Hey, thanks. I've tried to use the afterStateUpdated hook with a closure but it's not quite working. In my API call, I'm getting the title, the sku and the price. The custom field I built will only be responsible for the title but I want to send the sku and the price from the API call to the other two fields. It seems that the only data I can send via the closure is the state for the field which in this case would be the title. Is there a way to send the price and sku data to that closure?
toeknee
toeknee2y ago
You need to use the $set('sku', $apiData->sku) within the closure
bahamagician
bahamagicianOP2y ago
How do I send that $apiData array to the closure? From the custom field?
toeknee
toeknee2y ago
Can you provide all your code? IF you are calling the field within the form, simpling calling the Closure $get, $set should let you store the data with $set on that statePath. Example:
myfield::make()
->afterStateUpdated(function($state, Closure $get, $set) {
// Fetch the data
$data = myApiCall
$set('sku', $data->sku');
})
myfield::make()
->afterStateUpdated(function($state, Closure $get, $set) {
// Fetch the data
$data = myApiCall
$set('sku', $data->sku');
})

Did you find this page helpful?