Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Initial commit of component render classes. #8

Merged
merged 20 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0638019
Initial commit of component render classes.
chriscox Oct 20, 2016
e5d96a8
Merge branch 'develop' into feature/components
chriscox Oct 25, 2016
8500f7e
First pass of updates per feedback.
chriscox Oct 25, 2016
dad1c8b
Another pass of updates per feedback.
chriscox Oct 26, 2016
3c4c4fe
Refactors components to controls using Composition rather than Inheri…
chriscox Nov 7, 2016
2a0e894
Finalizes refactor of components to controls using Composition rather…
chriscox Nov 9, 2016
6bc9a3e
Updates constants formatting.
chriscox Nov 9, 2016
db0fa11
Fixes styles.less spacing/tabs.
chriscox Nov 9, 2016
939dbf7
Adds class commenting. Adds missing TextFieldControl. Removes SliderC…
chriscox Nov 9, 2016
f5d12f7
Merge branch 'develop' into feature/components
chriscox Nov 10, 2016
9ff18a0
Cleans up a few comment and strings.
chriscox Nov 10, 2016
2840c31
Updates control interfaces.
chriscox Nov 15, 2016
9bde50b
Refactors variable controls to call a prop callback method when updated.
chriscox Nov 17, 2016
be4a5d8
Work-in-progress.
chriscox Nov 18, 2016
bef5a8e
Now clones variables during update, allowing each control to handle i…
chriscox Nov 21, 2016
5d548ad
Resets package.json.
chriscox Nov 21, 2016
f791971
Merge branch 'develop' into feature/components
chriscox Nov 21, 2016
95e2423
Adds a render.tsx with logic that handles redrawing when variables up…
chriscox Nov 21, 2016
fffa746
Fixes adding callbacks to internal _callbacks
chriscox Nov 21, 2016
e1ea4fc
Minor code cleanup.
chriscox Nov 21, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 3 additions & 48 deletions src/ui/DOMController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,6 +27,7 @@ import * as update from "immutability-helper";
interface ControllerProps {
wrapperElement: HTMLElement;
variables: Array<Variable>;
Copy link
Member

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 but onUpdate internally. What you really want to do is have every call to updateVariable update variables and then re-render with the updated version. That would remove the need for setState/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

shouldComponentUpdate(nextProps) {
  return nextProps.variable !== this.props.variable
}

updateVariable(variable: Variable, selectedValue: any): void;
}

/**
Expand Down Expand Up @@ -86,25 +81,6 @@ export class DOMController extends React.Component<ControllerProps, void> {
this.props.wrapperElement.classList.toggle(CSS.RMX_VISIBLE);
Copy link
Member

Choose a reason for hiding this comment

The 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 />;
  }

Copy link
Member Author

Choose a reason for hiding this comment

The 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 (
Expand All @@ -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();
64 changes: 64 additions & 0 deletions src/ui/render.tsx
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved above updateVariable. Due to hoisting, the code you wrote will work, but it's easier to read if code is declared before it's used.

Copy link
Member Author

Choose a reason for hiding this comment

The 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();
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
entry: {
remixer: './src/core/Remixer.ts',
overlay: './src/ui/DOMController.tsx'
overlay: './src/ui/render.tsx'
},

output: {
Expand Down