I need to use react-select in a shadow-root web-component like in this example.
How can I pass the onChange event from the <Select>component in my react environment to use it in the state:
web component:
import * as React from "react";
import ReactDOM from "react-dom";
// import { useState } from "react";
import Select from "react-select";
import retargetEvents from "react-shadow-dom-retarget-events";
import { CacheProvider } from "@emotion/react";
import createCache from "@emotion/cache";
export class ReactSelect extends HTMLElement {
createMountPoint() {
this.mountPoint = document.createElement("div");
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(this.mountPoint);
}
createCache() {
return createCache({
container: this.shadowRoot,
key: "test",
prepend: false,
});
}
connectedCallback() {
// Step 1: Create Shadow and Mountpoint
this.createMountPoint();
// Step 2: Create emotion Cache
const cache = this.createCache();
// Step 3: Render component with `CacheProvider`
setTimeout(() => {
ReactDOM.render(
<CacheProvider value={cache}>
<Select
isMulti
name="time"
options={timeOptions}
className="basic-multi-select"
classNamePrefix="select"
placeholder={<div>Time</div>}
onChange={(e) => console.log("select:", e)}
/>
</CacheProvider>,
this.mountPoint
);
retargetEvents(this.shadowRoot);
}, 1);
this.shadowRoot.addEventListener("click", function (e) {
console.log(this);
});
}
}
customElements.get("react-select") ||
customElements.define("react-select", ReactSelect);
react:
import { Styled } from "direflow-component";
import PropTypes from "prop-types";
import React, { useEffect, useState } from "react";
import Select from "react-select";
import styles from "./App.css";
import "./ReactSelectElement";
function App(props) {
return (
<Styled styles={styles}>
<react-select onClick={(e) => console.log(e.target)}></react-select>
</Styled>