Guest User

Untitled

a guest
Jul 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. var CalcNumButton = React.createClass({
  2. handleClick: function(e) {
  3. e.preventDefault();
  4.  
  5. this.props.onClickButton({clickType: "number", value: this.props.numValue});
  6. },
  7. render: function() {
  8. return (
  9. <div className="calcButton" >
  10. <h2 className="calcLabel" onClick={this.handleClick}>
  11. {this.props.label}
  12. </h2>
  13. </div>
  14. );
  15. }
  16. });
  17.  
  18.  
  19. var CalcOpsButton = React.createClass({
  20. handleClick: function(e) {
  21. e.preventDefault();
  22. this.props.onClickButton({clickType: "operation", operation: this.props.operation});
  23. },
  24. render: function() {
  25. return (
  26. <div className="calcOpsButton" >
  27. <h2 className="calcLabel" onClick={this.handleClick}>
  28. {this.props.operation}
  29. </h2>
  30. </div>
  31. );
  32. }
  33. });
  34.  
  35.  
  36. var CalcScreen = React.createClass({
  37. getInitialState: function () {
  38. return { screenStr: this.props.initScreenStr };
  39. },
  40. handleScreenUpdate: function(value) {
  41. var newState = {};
  42. newState["screenStr"] = value;
  43.  
  44. this.setState(newState);
  45. },
  46. render: function() {
  47. return (
  48. <div className="calcScreen" >
  49. <h2 className="calcScreenDisplay" >
  50. {this.state.screenStr}
  51. </h2>
  52. </div>
  53. );
  54. }
  55. });
  56.  
  57.  
  58. var Calculator = React.createClass({
  59. getInitialState: function () {
  60. return { screenVal: 0, screenStr: "0", lastInput: 0, pendingOperation: '', pendingScreenReset: false };
  61. },
  62. updateScreen: function() {
  63. var screen = this.refs.screen;
  64. screen.handleScreenUpdate(this.state.screenStr)
  65. },
  66. // Will only be called if the state is valid
  67. runOperation: function() {
  68. var newValue = 0;
  69. switch (this.state.pendingOperation) {
  70. case "+":
  71. newValue = this.state.lastInput + this.state.screenVal;
  72. break;
  73. case "-":
  74. newValue = this.state.lastInput - this.state.screenVal;
  75. break;
  76. case "*":
  77. newValue = this.state.lastInput * this.state.screenVal;
  78. break;
  79. case "/":
  80. newValue = this.state.lastInput / this.state.screenVal;
  81. break;
  82. }
  83. this.state.screenVal = newValue;
  84. this.state.screenStr = String(newValue);
  85. this.updateScreen();
  86.  
  87. },
  88. handleClickButton: function(clickData) {
  89.  
  90. if (typeof(clickData.clickType) !== 'undefined') {
  91. if (clickData.clickType == "number") {
  92.  
  93. if (this.state.screenStr == "0" || (this.state.pendingOperation != '' && this.state.pendingScreenReset)) {
  94. this.state.screenStr = String(clickData.value);
  95. this.state.pendingScreenReset = false;
  96. } else {
  97. this.state.screenStr = this.state.screenStr + String(clickData.value);
  98. }
  99. this.state.screenVal = parseInt(this.state.screenStr);
  100. this.updateScreen();
  101. } else if (clickData.clickType == "operation") {
  102.  
  103. if (clickData.operation == "C") {
  104. this.state.lastInput = 0; // UNDEFINED?
  105. this.state.screenVal = 0;
  106. this.state.screenStr = "0";
  107. this.state.pendingOperation = '';
  108. this.updateScreen();
  109.  
  110. } else if (clickData.operation == "="
  111. && this.state.pendingOperation != '') { // ignore if we dont have an operation in waiting
  112.  
  113. // CHECK FOR VALID STATE?
  114.  
  115. this.runOperation();
  116.  
  117. } else {
  118. if (this.state.pendingOperation != '') {
  119. this.runOperation();
  120. }
  121.  
  122. this.state.lastInput = this.state.screenVal;
  123. this.state.pendingScreenReset = true;
  124.  
  125. // WAIT til a num is given like most calculators?
  126. //this.state.screenVal = 0;
  127. //this.state.screenStr = "0";
  128. this.state.pendingOperation = clickData.operation;
  129. this.updateScreen();
  130. }
  131.  
  132. } else {
  133. console.log("ERROR bad clickType: " + clickData.clickType);
  134. }
  135.  
  136. } else {
  137. console.log("ERROR unknown clickType");
  138. }
  139. },
  140. render: function() {
  141. return (
  142. <div className="calculator">
  143. <CalcScreen initScreenStr={this.state.screenStr} ref="screen"/>
  144. <div className="calcRow">
  145. <CalcNumButton label='1' numValue={1} onClickButton={this.handleClickButton}/>
  146. <CalcNumButton label='2' numValue={2} onClickButton={this.handleClickButton}/>
  147. <CalcNumButton label="3" numValue={3} onClickButton={this.handleClickButton}/>
  148. <CalcNumButton label="4" numValue={4} onClickButton={this.handleClickButton}/>
  149. <CalcNumButton label="5" numValue={5} onClickButton={this.handleClickButton}/>
  150. <CalcNumButton label="6" numValue={6} onClickButton={this.handleClickButton}/>
  151. </div>
  152. <div className="calcRow">
  153. <CalcNumButton label='7' numValue={7} onClickButton={this.handleClickButton}/>
  154. <CalcNumButton label='8' numValue={8} onClickButton={this.handleClickButton}/>
  155. <CalcNumButton label="9" numValue={9} onClickButton={this.handleClickButton}/>
  156. <CalcNumButton label="0" numValue={0} onClickButton={this.handleClickButton}/>
  157. <div className="buttonGap"></div>
  158. <CalcOpsButton operation="=" onClickButton={this.handleClickButton}/>
  159.  
  160. </div>
  161. <div className="calcRow">
  162. <CalcOpsButton operation='+' onClickButton={this.handleClickButton}/>
  163. <CalcOpsButton operation='-' onClickButton={this.handleClickButton}/>
  164. <CalcOpsButton operation="*" onClickButton={this.handleClickButton}/>
  165. <CalcOpsButton operation="/" onClickButton={this.handleClickButton}/>
  166. <div className="buttonGap"></div>
  167. <CalcOpsButton operation="C" onClickButton={this.handleClickButton}/>
  168. </div>
  169. </div>
  170. )
  171. }
  172. });
  173.  
  174.  
  175.  
  176. ReactDOM.render(
  177. <Calculator />,
  178. document.getElementById('calculatorContainer')
  179. );
Add Comment
Please, Sign In to add comment