mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve CustomRender
handle click outside
This commit is contained in:
parent
504f1556d5
commit
76a1a317f6
@ -60,6 +60,38 @@ const generateItems = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CustomRender = (props) => {
|
||||||
|
const handleClickOutside = (event) => {
|
||||||
|
if (props.sidebarRef.current && !props.sidebarRef.current.contains(event.target)) {
|
||||||
|
props.closeRender()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
document.addEventListener("mousedown", handleClickOutside)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("mousedown", handleClickOutside)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return <div className="render_content_wrapper">
|
||||||
|
<div className="render_content_header">
|
||||||
|
{
|
||||||
|
props.customRenderTitle ?? null
|
||||||
|
}
|
||||||
|
<Button
|
||||||
|
onClick={props.closeRender}
|
||||||
|
>
|
||||||
|
Close
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="render_content">
|
||||||
|
{props.children}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
export default class Sidebar extends React.Component {
|
export default class Sidebar extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
@ -94,6 +126,8 @@ export default class Sidebar extends React.Component {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sidebarRef = React.createRef()
|
||||||
|
|
||||||
collapseDebounce = null
|
collapseDebounce = null
|
||||||
|
|
||||||
componentDidMount = async () => {
|
componentDidMount = async () => {
|
||||||
@ -104,12 +138,12 @@ export default class Sidebar extends React.Component {
|
|||||||
}, 100)
|
}, 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
setRender = (render, options = {}) => {
|
setRender = async (render, options = {}) => {
|
||||||
if (!typeof render === "function") {
|
if (!typeof render === "function") {
|
||||||
throw new Error("Render is required to be a function")
|
throw new Error("Render is required to be a function")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({
|
await this.setState({
|
||||||
customRenderTitle: <div className="render_content_header_title">
|
customRenderTitle: <div className="render_content_header_title">
|
||||||
{
|
{
|
||||||
options.icon && createIconRender(options.icon)
|
options.icon && createIconRender(options.icon)
|
||||||
@ -258,25 +292,16 @@ export default class Sidebar extends React.Component {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
ref={this.sidebarRef}
|
||||||
>
|
>
|
||||||
|
|
||||||
{
|
{
|
||||||
this.state.customRender &&
|
this.state.customRender && <CustomRender
|
||||||
<div className="render_content_wrapper">
|
customRenderTitle={this.state.customRenderTitle}
|
||||||
<div className="render_content_header">
|
closeRender={this.closeRender}
|
||||||
{
|
sidebarRef={this.sidebarRef}
|
||||||
this.state.customRenderTitle ?? null
|
>
|
||||||
}
|
{this.state.customRender}
|
||||||
<Button
|
</CustomRender>
|
||||||
onClick={this.closeRender}
|
|
||||||
>
|
|
||||||
Close
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
<div className="render_content">
|
|
||||||
{this.state.customRender}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user