some improvements on component renders

This commit is contained in:
SrGooglo 2025-03-13 23:39:01 +00:00
parent 08ba20b275
commit 1c5c213b96
2 changed files with 308 additions and 293 deletions

View File

@ -56,20 +56,24 @@ export default class DefaultWindowRender extends React.Component {
this.setState({ title }) this.setState({ title })
} }
updateDimensions = (dimensions = { updateDimensions = (
dimensions = {
width: 0, width: 0,
height: 0, height: 0,
}) => { },
) => {
this.ref.current?.updateSize({ this.ref.current?.updateSize({
width: dimensions.width, width: dimensions.width,
height: dimensions.height, height: dimensions.height,
}) })
} }
updatePosition = (position = { updatePosition = (
position = {
x: 0, x: 0,
y: 0, y: 0,
}) => { },
) => {
this.ref.current?.updatePosition({ this.ref.current?.updatePosition({
x: position.x, x: position.x,
y: position.y, y: position.y,
@ -86,9 +90,12 @@ export default class DefaultWindowRender extends React.Component {
const windowHeight = dimensions.height ?? 600 const windowHeight = dimensions.height ?? 600
const windowWidth = dimensions.width ?? 400 const windowWidth = dimensions.width ?? 400
const y = window.innerHeight / 2 - windowHeight / 2
const x = window.innerWidth / 2 - windowWidth / 2
return { return {
x: window.innerWidth / 2 - windowWidth / 2, x: x,
y: window.innerHeight / 2 - windowHeight / 2, y: y,
} }
} }
@ -112,8 +119,14 @@ export default class DefaultWindowRender extends React.Component {
if (Array.isArray(actions)) { if (Array.isArray(actions)) {
return actions.map((action) => { return actions.map((action) => {
return ( return (
<div key={action.key} onClick={action.onClick} {...action.props}> <div
{React.isValidElement(action.render) ? action.render : React.createElement(action.render)} key={action.key}
onClick={action.onClick}
{...action.props}
>
{React.isValidElement(action.render)
? action.render
: React.createElement(action.render)}
</div> </div>
) )
}) })
@ -133,13 +146,13 @@ export default class DefaultWindowRender extends React.Component {
dimensions: this.state.dimensions, dimensions: this.state.dimensions,
} }
return <WindowContext.Provider value={ctx}> return (
{ <WindowContext.Provider value={ctx}>
React.isValidElement(this.props.children) {React.isValidElement(this.props.children)
? React.cloneElement(this.props.children, ctx) ? React.cloneElement(this.props.children, ctx)
: React.createElement(this.props.children, ctx) : React.createElement(this.props.children, ctx)}
}
</WindowContext.Provider> </WindowContext.Provider>
)
} }
render() { render() {
@ -149,30 +162,21 @@ export default class DefaultWindowRender extends React.Component {
return null return null
} }
return <Rnd return (
<Rnd
ref={this.ref} ref={this.ref}
default={{ default={{
...position, ...position,
...dimensions, ...dimensions,
}} }}
minWidth={ minWidth={this.props.minWidth}
this.props.minWidth minHeight={this.props.minHeight}
} enableResizing={this.props.enableResizing ?? true}
minHeight={ disableDragging={this.props.disableDragging ?? false}
this.props.minHeight
}
enableResizing={
this.props.enableResizing ?? true
}
disableDragging={
this.props.disableDragging ?? false
}
dragHandleClassName={ dragHandleClassName={
this.props.dragHandleClassName ?? "window_topbar" this.props.dragHandleClassName ?? "window_topbar"
} }
bounds={ //bounds={this.props.bounds ?? "#root"}
this.props.bounds ?? "#root"
}
className="window_wrapper" className="window_wrapper"
> >
<div className="window_topbar"> <div className="window_topbar">
@ -181,16 +185,15 @@ export default class DefaultWindowRender extends React.Component {
</div> </div>
<div className="window_body"> <div className="window_body">
{ {this.state.renderError && (
this.state.renderError && <div className="render_error"> <div className="render_error">
<h1>Render Error</h1> <h1>Render Error</h1>
<code>{this.state.renderError.message}</code> <code>{this.state.renderError.message}</code>
</div> </div>
} )}
{ {!this.state.renderError && this.getComponentRender()}
!this.state.renderError && this.getComponentRender()
}
</div> </div>
</Rnd> </Rnd>
)
} }
} }

View File

@ -59,7 +59,7 @@ export default class WindowManager extends Core {
onClose = null, onClose = null,
createOrUpdate = false, createOrUpdate = false,
closeOnClickOutside = false, closeOnClickOutside = false,
} = {} } = {},
) { ) {
let element = document.createElement("div") let element = document.createElement("div")
let node = null let node = null
@ -70,7 +70,9 @@ export default class WindowManager extends Core {
if (this.root.querySelector(`#${id}`) && !createOrUpdate) { if (this.root.querySelector(`#${id}`) && !createOrUpdate) {
const newId = `${id}_${Date.now()}` const newId = `${id}_${Date.now()}`
this.console.warn(`Window ${id} already exist, overwritting id to ${newId}.\nYou can use {createOrUpdate = true} option to force refresh render of window`) this.console.warn(
`Window ${id} already exist, overwritting id to ${newId}.\nYou can use {createOrUpdate = true} option to force refresh render of window`,
)
id = newId id = newId
} }
@ -115,16 +117,26 @@ export default class WindowManager extends Core {
// if useFrame is true, wrap the fragment with a DefaultWindow component // if useFrame is true, wrap the fragment with a DefaultWindow component
if (useFrame) { if (useFrame) {
fragment = <DefaultWindow> fragment = <DefaultWindow>{fragment}</DefaultWindow>
{fragment}
</DefaultWindow>
} }
node.render(React.cloneElement(fragment, { if (React.isValidElement(fragment)) {
node.render(
React.cloneElement(fragment, {
close: () => { close: () => {
this.close(id, onClose) this.close(id, onClose)
},
}),
)
} else {
node.render(
React.createElement(fragment, {
close: () => {
this.close(id, onClose)
},
}),
)
} }
}))
return { return {
element, element,