Update react refs

This commit is contained in:
Casper Lamboo 2021-05-19 19:51:34 +02:00
parent 1a5f0fd9d0
commit 31e9ec735f
5 changed files with 44 additions and 21 deletions

View File

@ -69,6 +69,11 @@ class D2Panel extends React.Component {
activeNeedRender = false; activeNeedRender = false;
inactiveNeedRender = false; inactiveNeedRender = false;
constructor(props) {
super(props);
this.canvasContainer = React.createRef();
}
componentWillMount() { componentWillMount() {
// Scene space // Scene space
this.sceneActive = new EventGroup({ this.sceneActive = new EventGroup({
@ -97,12 +102,12 @@ class D2Panel extends React.Component {
} }
componentDidMount() { componentDidMount() {
const { canvasContainer } = this.refs; if (this.canvasContainer.current) {
this.container = canvasContainer; this.container = this.canvasContainer.current;
this.container.appendChild(this.sceneInactive.image);
this.container.appendChild(this.sceneActive.image);
this.container.appendChild(this.sceneInactive.image);
this.container.appendChild(this.sceneActive.image);
}
this.sceneActive.onClick = (event) => event.stopPropagation(); this.sceneActive.onClick = (event) => event.stopPropagation();
} }
@ -218,7 +223,7 @@ class D2Panel extends React.Component {
return ( return (
<div className={classes.container}> <div className={classes.container}>
<ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} /> <ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} />
<div className={classes.canvasContainer} ref="canvasContainer" /> <div className={classes.canvasContainer} ref={this.canvasContainer} />
<InputText screenMatrix={screenMatrix} /> <InputText screenMatrix={screenMatrix} />
</div> </div>
); );

View File

@ -50,6 +50,10 @@ const styles = {
}; };
class D3Panel extends React.Component { class D3Panel extends React.Component {
constructor(props) {
super(props);
this.canvasContainer = React.createRef();
}
static propTypes = { static propTypes = {
state: PropTypes.object.isRequired, state: PropTypes.object.isRequired,
@ -73,10 +77,11 @@ class D3Panel extends React.Component {
} }
componentDidMount() { componentDidMount() {
this.container = this.refs.canvasContainer; if (this.canvasContainer.current) {
this.container.appendChild(this.renderer.domElement); this.container = this.canvasContainer.current;
this.container.appendChild(this.renderer.domElement);
this.renderScene(); // immidiatly render because when THREE.JS inits, a black screen is generated this.renderScene(); // immidiatly render because when THREE.JS inits, a black screen is generated
}
} }
componentWillUnmount() { componentWillUnmount() {
@ -221,7 +226,7 @@ class D3Panel extends React.Component {
return ( return (
<div className={classes.container}> <div className={classes.container}>
<ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} /> <ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} />
<div className={classes.canvasContainer} ref="canvasContainer"/> <div className={classes.canvasContainer} ref={this.canvasContainer}/>
</div> </div>
); );
} }

View File

@ -28,6 +28,11 @@ class DoodlePreview extends React.Component {
pixelRatio: 1 pixelRatio: 1
}; };
constructor(props) {
super(props);
this.canvas = React.createRef();
}
static propTypes = { static propTypes = {
classes: PropTypes.objectOf(PropTypes.string), classes: PropTypes.objectOf(PropTypes.string),
width: PropTypes.number.isRequired, width: PropTypes.number.isRequired,
@ -49,14 +54,12 @@ class DoodlePreview extends React.Component {
if (docData) sketchData = await JSONToSketchData(docData); if (docData) sketchData = await JSONToSketchData(docData);
const { canvas } = this.refs;
const sceneData = await createSceneData(sketchData); const sceneData = await createSceneData(sketchData);
const scene = createScene(sceneData, canvas); const scene = createScene(sceneData, this.canvas.current);
this.setState(scene); this.setState(scene);
this.editorControls = new THREE.EditorControls(scene.camera, canvas); this.editorControls = new THREE.EditorControls(scene.camera, this.canvas.current);
this.editorControls.addEventListener('change', () => rafOnce(scene.render)); this.editorControls.addEventListener('change', () => rafOnce(scene.render));
} }
@ -78,7 +81,7 @@ class DoodlePreview extends React.Component {
return ( return (
<div className={classes.container}> <div className={classes.container}>
<ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} /> <ReactResizeDetector handleWidth handleHeight onResize={this.resizeHandler} />
<canvas className={classes.canvas} ref="canvas" /> <canvas className={classes.canvas} ref={this.canvas} />
</div> </div>
); );
} }

View File

@ -25,6 +25,11 @@ const styles = {
}; };
class InputText extends React.Component { class InputText extends React.Component {
constructor(props) {
super(props);
this.text = React.createRef();
}
static propTypes = { static propTypes = {
state: PropTypes.object.isRequired, state: PropTypes.object.isRequired,
classes: PropTypes.objectOf(PropTypes.string), classes: PropTypes.objectOf(PropTypes.string),
@ -37,7 +42,7 @@ class InputText extends React.Component {
if (!shapeData) return; if (!shapeData) return;
const { changeText } = this.props; const { changeText } = this.props;
const text = this.refs.text.value; const text = this.text.current.value;
changeText(text); changeText(text);
}; };
@ -51,7 +56,7 @@ class InputText extends React.Component {
} }
componentWillUpdate() { componentWillUpdate() {
if (this.refs.text) this.refs.text.focus(); if (this.text.current) this.text.current.focus();
} }
render() { render() {
@ -82,7 +87,7 @@ class InputText extends React.Component {
width: `${width}px` width: `${width}px`
}} }}
value={text} value={text}
ref="text" ref={this.text}
spellCheck="false" spellCheck="false"
autoFocus autoFocus
onChange={this.onInputChange} onChange={this.onInputChange}

View File

@ -8,6 +8,11 @@ import { hexToStyle } from '../utils/colorUtils.js';
// const debug = createDebug('d3d:ui:submenu'); // const debug = createDebug('d3d:ui:submenu');
class SubMenu extends React.Component { class SubMenu extends React.Component {
constructor(props) {
super(props);
this.li = React.createRef();
}
static propTypes = { static propTypes = {
onSelect: PropTypes.func, onSelect: PropTypes.func,
onOpen: PropTypes.func, onOpen: PropTypes.func,
@ -61,7 +66,7 @@ class SubMenu extends React.Component {
const { open, onClose, value } = this.props; const { open, onClose, value } = this.props;
if (open && onClose) { if (open && onClose) {
// was click on this submenu? // was click on this submenu?
const onSubmenu = this.refs.li.contains(event.target); const onSubmenu = this.li.current.contains(event.target);
// debug(`onDocumentClick ${event.type} ${onSubmenu ? 'onSubmenu' : ''}`); // debug(`onDocumentClick ${event.type} ${onSubmenu ? 'onSubmenu' : ''}`);
if (!onSubmenu) onClose({ menuValue: value }); if (!onSubmenu) onClose({ menuValue: value });
} }
@ -79,7 +84,7 @@ class SubMenu extends React.Component {
let className = 'submenu'; let className = 'submenu';
if (open) className += ' open'; if (open) className += ' open';
return ( return (
<li id={id} className={className} ref="li" style={style}> <li id={id} className={className} ref={this.li} style={style}>
<Button <Button
id={`${selectedValue}-menu`} id={`${selectedValue}-menu`}
value={selectedValue} value={selectedValue}