-
Notifications
You must be signed in to change notification settings - Fork 30
Initial commit of component render classes. #8
Changes from 2 commits
0638019
e5d96a8
8500f7e
dad1c8b
3c4c4fe
2a0e894
6bc9a3e
db0fa11
939dbf7
f5d12f7
9ff18a0
2840c31
9bde50b
be4a5d8
bef5a8e
5d548ad
f791971
95e2423
fffa746
e1ea4fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,11 @@ | |
* under the License. | ||
*/ | ||
|
||
import "./overlay/styles.less"; | ||
import * as React from "react"; | ||
import * as ReactDOM from "react-dom"; | ||
import { KeyCode, KeyEvent, CSS } from "../lib/Constants"; | ||
import { Messaging } from "../lib/Messaging"; | ||
import { Overlay } from "./overlay/Overlay"; | ||
import { Variable, VariableCallback } from "../core/variables/Variable"; | ||
import { remixer } from "../core/Remixer"; | ||
|
||
import * as update from "immutability-helper"; | ||
// import { newContext } from "immutability-helper"; | ||
import { Variable } from "../core/variables/Variable"; | ||
|
||
/** | ||
* Interface for the properties assigned to the DOMController component. | ||
|
@@ -33,6 +27,7 @@ import * as update from "immutability-helper"; | |
interface ControllerProps { | ||
wrapperElement: HTMLElement; | ||
variables: Array<Variable>; | ||
updateVariable(variable: Variable, selectedValue: any): void; | ||
} | ||
|
||
/** | ||
|
@@ -86,25 +81,6 @@ export class DOMController extends React.Component<ControllerProps, void> { | |
this.props.wrapperElement.classList.toggle(CSS.RMX_VISIBLE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like I've asked this in every review and haven't seen any comments/changes. Maybe the comments got lost in the GitHub UI? Why are you mutating the DOM of an element this component doesn't control? Couldn't this just be: toggleVisibility() {
this.setState({
isVisible: !this.props.isVisible,
}})
}
render() {
const {
isVisible,
variables,
updateVariable,
} = this.props;
if (this.props.isVisible) {
return (
// your DOM tree
)
} else {
return <div />;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we chatted about this. Its part of #12. Just never made that change. |
||
} | ||
|
||
/** | ||
* Handles all control updates by setting a new selected value for the | ||
* variable. | ||
* | ||
* To maintain immutability for React, lets first clone the variable, | ||
* update its selected value, then set it back to the variables array. | ||
* Doing so allows each control to handle its own `shouldComponentUpdate` | ||
* method to determine if it should be re-rendered. | ||
* | ||
* @param {Variable} variable The variable to update. | ||
* @param {any} selectedValue The new selected value. | ||
*/ | ||
updateVariable = (variable: Variable, selectedValue: any): void => { | ||
const index = variables.indexOf(variable); | ||
let clonedVariable = variable.clone(); | ||
clonedVariable.selectedValue = selectedValue; | ||
variables[index] = clonedVariable; | ||
} | ||
|
||
/** @override */ | ||
render() { | ||
return ( | ||
|
@@ -115,31 +91,10 @@ export class DOMController extends React.Component<ControllerProps, void> { | |
<div className="mdl-card__supporting-text mdl-card__actions mdl-card--border"> | ||
<Overlay | ||
variables={this.props.variables} | ||
updateVariable={this.updateVariable} | ||
updateVariable={this.props.updateVariable} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
let variables = remixer.attachedInstance.variablesArray; | ||
const overlayWrapper = document.getElementById(CSS.RMX_OVERLAY_WRAPPER); | ||
|
||
/** Renders the DOMController component to the overlay wrapper element. */ | ||
function redraw(): void { | ||
ReactDOM.render( | ||
<DOMController | ||
wrapperElement={overlayWrapper} | ||
variables={variables} | ||
/>, | ||
overlayWrapper | ||
); | ||
} | ||
|
||
// Add `redraw()` as a callback when selected value changes on a variable. | ||
variables.forEach(variable => { | ||
variable.addCallback(redraw); | ||
}); | ||
|
||
redraw(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** @license | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import "./overlay/styles.less"; | ||
import * as React from "react"; | ||
import * as ReactDOM from "react-dom"; | ||
import { CSS } from "../lib/Constants"; | ||
import { Variable } from "../core/variables/Variable"; | ||
import { DOMController } from "./DOMController"; | ||
import { remixer } from "../core/Remixer"; | ||
|
||
/** | ||
* Handles all control updates by setting a new selected value for the | ||
* variable. | ||
* | ||
* To maintain immutability for React, lets first clone the variable, | ||
* update its selected value, then set it back to the variables array. | ||
* Doing so allows each control to handle its own `shouldComponentUpdate` | ||
* method to determine if it should be re-rendered. | ||
* | ||
* @param {Variable} variable The variable to update. | ||
* @param {any} selectedValue The new selected value. | ||
*/ | ||
function updateVariable(variable: Variable, selectedValue: any): void { | ||
const index = variables.indexOf(variable); | ||
let clonedVariable = variable.clone(); | ||
clonedVariable.selectedValue = selectedValue; | ||
variables[index] = clonedVariable; | ||
} | ||
|
||
let variables = remixer.attachedInstance.variablesArray; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. Made a quick cleanup pass. |
||
const overlayWrapper = document.getElementById(CSS.RMX_OVERLAY_WRAPPER); | ||
|
||
/** Renders the DOMController component to the overlay wrapper element. */ | ||
function redraw(): void { | ||
ReactDOM.render( | ||
<DOMController | ||
wrapperElement={overlayWrapper} | ||
variables={variables} | ||
updateVariable={updateVariable} | ||
/>, | ||
overlayWrapper | ||
); | ||
} | ||
|
||
// Add `redraw()` as a callback when selected value changes on a variable. | ||
variables.forEach(variable => { | ||
variable.addCallback(redraw); | ||
}); | ||
|
||
redraw(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weird that you are specifying
variables
externally butonUpdate
internally. What you really want to do is have every call toupdateVariable
updatevariables
and then re-render with the updated version. That would remove the need forsetState
/forceUpdate
inside your controls and let them be pure functions.The idiomatic way to do that would be to have
variables
(and its values) be immutable, so each control could say