Here is my select form:
<form ref={skuSelectRef}>
<label htmlFor="size" />
<select
id="size"
value={selectedSKU}
onChange={() => changeSKU(size.options[size.selectedIndex].value)}
>
<option value="default">
{outOfStock ? 'OUT OF STOCK' : 'SELECT SIZE'}
</option>
{availableSizes.map((size, i) => (
<option key={i} value={size}>
{size}
</option>
))}
</select>
</form>
The button that when I click I would like for the select form above to open:
<div className="a2cbtn">
<button onClick={onAddToCartClick} id="a2cbtn">
<span>ADD TO CART</span>
</button>
</div>
Both of the above components are in my AddToCart subcomponent. Here is a snippet of the button click handler in the parent component where I am trying to make the select form open:
if (this.state.selectedSKU === 'default') {
this.skuSelectRef.current.click();
console.log(this.skuSelectRef);
console.log('clicked');
}
It seems that the ref is properly created because I am getting {current: form}
logged to the console and the click is working as I'm getting "clicked" logged to the console as well. However the select form is not opening!
At first I tried having the ref in my select element instead of form but then when I clicked the button it gave me an error saying that the function click doesn't exist so I moved the ref to the form element enclosing it and now I am not getting that error but it is still not opening.
I have tried this.skuSelectRef.current.focus()
and this.skuSelectRef.current.select()
as well to no avail.
Any ideas?