figbertmath

[RADIOACTIVE] miscellaneous math programs in website form
git clone git://git.figbert.com/figbertmath.git
Log | Files | Refs | README

simpleCalc.js (3256B)


      1 import React from 'react';
      2 import PropTypes from 'prop-types';
      3 import { CalcOut } from "../lowLevel/calcOut";
      4 import { Pad } from "../lowLevel/pad";
      5 
      6 const buttonValues = [
      7     ['AC', 'DEL', '%', '÷'],
      8     ['7', '8', '9', '×'],
      9     ['4', '5', '6', '–'],
     10     ['1', '2', '3', '+'],
     11     ['…', '0', '.', '=']
     12 ];
     13 
     14 export class SimpleCalc extends React.Component {
     15     constructor(props) {
     16         super(props);
     17         this.state = {
     18             output: 0,
     19             input: '',
     20             justSolved: false
     21         };
     22         this.addDigit = this.addDigit.bind(this);
     23         this.clear = this.clear.bind(this);
     24         this.delete = this.delete.bind(this);
     25         this.equate = this.equate.bind(this);
     26         this.onButtonPress = this.onButtonPress.bind(this);
     27     }
     28 
     29     addDigit(buttonVal) {
     30         if (this.state.justSolved) {
     31             this.setState({
     32                 output: 0,
     33                 input: '',
     34                 justSolved: false
     35             });
     36         }
     37         let value;
     38         switch (buttonVal) {
     39             case '÷':
     40                 value = '/';
     41                 break;
     42             case '–':
     43                 value = '-';
     44                 break;
     45             case '×':
     46                 value = '*';
     47                 break;
     48             default:
     49                 value = buttonVal;
     50         }
     51         const newInput = this.state.input + value;
     52         this.setState({
     53             input: newInput
     54         });
     55     }
     56 
     57     clear() {
     58         this.setState({
     59             output: 0,
     60             input: ''
     61         });
     62     }
     63 
     64     delete() {
     65         this.setState({
     66             input: this.state.input.slice(0,-1)
     67         });
     68     }
     69 
     70     equate() {
     71         if (!this.state.justSolved) {
     72             let final;
     73             if (!isNaN(Number(this.state.input.slice(-1)))) {
     74                 // eslint-disable-next-line
     75                 final = eval(this.state.input);
     76                 if (final == null || isNaN(final)) {
     77                     final = 'ERROR';
     78                 }
     79             } else {
     80                 final = 'ERROR';
     81             }
     82             this.setState({
     83                 output: final,
     84                 input: '',
     85                 justSolved: true
     86             });
     87         }
     88     }
     89 
     90     onButtonPress(object) {
     91         let value = object.target.value;
     92         switch (value) {
     93             case 'AC':
     94                 this.clear();
     95                 break;
     96             case 'DEL':
     97                 this.delete();
     98                 break;
     99             case '=':
    100                 this.equate();
    101                 break;
    102             case undefined:
    103                 break;
    104             default:
    105                 this.addDigit(value);
    106         }
    107     }
    108 
    109     render() {
    110         return (
    111             <div>
    112                 <CalcOut
    113                     mode={this.props.mode}
    114                     output={this.state.output}
    115                     input={this.state.input}
    116                 />
    117                 <Pad
    118                     buttonValues={buttonValues}
    119                     onClick={this.onButtonPress}
    120                     onModeClick={this.props.onModeChange}
    121                     type={'small'}
    122                 />
    123             </div>
    124         );
    125     }
    126 }
    127 SimpleCalc.propTypes = {
    128     mode: PropTypes.string.isRequired,
    129     onModeChange: PropTypes.func
    130 };