Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2017 02:14
Show Gist options
  • Save anonymous/f777590cc1d216751f29a46a3e6d52c4 to your computer and use it in GitHub Desktop.
Save anonymous/f777590cc1d216751f29a46a3e6d52c4 to your computer and use it in GitHub Desktop.
React Calculator
<div id="wrapper">
<div id="app"></div>
</div>
const PointTarget = ReactPoint.PointTarget
class AutoScalingText extends React.Component {
state = {
scale: 1
};
componentDidUpdate() {
const { scale } = this.state
const node = this.node
const parentNode = node.parentNode
const availableWidth = parentNode.offsetWidth
const actualWidth = node.offsetWidth
const actualScale = availableWidth / actualWidth
if (scale === actualScale)
return
if (actualScale < 1) {
this.setState({ scale: actualScale })
} else if (scale < 1) {
this.setState({ scale: 1 })
}
}
render() {
const { scale } = this.state
return (
<div
className="auto-scaling-text"
style={{ transform: `scale(${scale},${scale})` }}
ref={node => this.node = node}
>{this.props.children}</div>
)
}
}
class CalculatorDisplay extends React.Component {
render() {
const { value, ...props } = this.props
const language = navigator.language || 'en-US'
let formattedValue = parseFloat(value).toLocaleString(language, {
useGrouping: true,
maximumFractionDigits: 6
})
// Add back missing .0 in e.g. 12.0
const match = value.match(/\.\d*?(0*)$/)
if (match)
formattedValue += (/[1-9]/).test(match[0]) ? match[1] : match[0]
return (
<div {...props} className="calculator-display">
<AutoScalingText>{formattedValue}</AutoScalingText>
</div>
)
}
}
class CalculatorKey extends React.Component {
render() {
const { onPress, className, ...props } = this.props
return (
<PointTarget onPoint={onPress}>
<button className={`calculator-key ${className}`} {...props}/>
</PointTarget>
)
}
}
const CalculatorOperations = {
'/': (prevValue, nextValue) => prevValue / nextValue,
'*': (prevValue, nextValue) => prevValue * nextValue,
'+': (prevValue, nextValue) => prevValue + nextValue,
'-': (prevValue, nextValue) => prevValue - nextValue,
'=': (prevValue, nextValue) => nextValue
}
class Calculator extends React.Component {
state = {
value: null,
displayValue: '0',
operator: null,
waitingForOperand: false
};
clearAll() {
this.setState({
value: null,
displayValue: '0',
operator: null,
waitingForOperand: false
})
}
clearDisplay() {
this.setState({
displayValue: '0'
})
}
clearLastChar() {
const { displayValue } = this.state
this.setState({
displayValue: displayValue.substring(0, displayValue.length - 1) || '0'
})
}
toggleSign() {
const { displayValue } = this.state
const newValue = parseFloat(displayValue) * -1
this.setState({
displayValue: String(newValue)
})
}
inputPercent() {
const { displayValue } = this.state
const currentValue = parseFloat(displayValue)
if (currentValue === 0)
return
const fixedDigits = displayValue.replace(/^-?\d*\.?/, '')
const newValue = parseFloat(displayValue) / 100
this.setState({
displayValue: String(newValue.toFixed(fixedDigits.length + 2))
})
}
inputDot() {
const { displayValue } = this.state
if (!(/\./).test(displayValue)) {
this.setState({
displayValue: displayValue + '.',
waitingForOperand: false
})
}
}
inputDigit(digit) {
const { displayValue, waitingForOperand } = this.state
if (waitingForOperand) {
this.setState({
displayValue: String(digit),
waitingForOperand: false
})
} else {
this.setState({
displayValue: displayValue === '0' ? String(digit) : displayValue + digit
})
}
}
performOperation(nextOperator) {
const { value, displayValue, operator } = this.state
const inputValue = parseFloat(displayValue)
if (value == null) {
this.setState({
value: inputValue
})
} else if (operator) {
const currentValue = value || 0
const newValue = CalculatorOperations[operator](currentValue, inputValue)
this.setState({
value: newValue,
displayValue: String(newValue)
})
}
this.setState({
waitingForOperand: true,
operator: nextOperator
})
}
handleKeyDown = (event) => {
let { key } = event
if (key === 'Enter')
key = '='
if ((/\d/).test(key)) {
event.preventDefault()
this.inputDigit(parseInt(key, 10))
} else if (key in CalculatorOperations) {
event.preventDefault()
this.performOperation(key)
} else if (key === '.') {
event.preventDefault()
this.inputDot()
} else if (key === '%') {
event.preventDefault()
this.inputPercent()
} else if (key === 'Backspace') {
event.preventDefault()
this.clearLastChar()
} else if (key === 'Clear') {
event.preventDefault()
if (this.state.displayValue !== '0') {
this.clearDisplay()
} else {
this.clearAll()
}
}
};
componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown)
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown)
}
render() {
const { displayValue } = this.state
const clearDisplay = displayValue !== '0'
const clearText = clearDisplay ? 'C' : 'AC'
return (
<div className="calculator">
<CalculatorDisplay value={displayValue}/>
<div className="calculator-keypad">
<div className="input-keys">
<div className="function-keys">
<CalculatorKey className="key-clear" onPress={() => clearDisplay ? this.clearDisplay() : this.clearAll()}>{clearText}</CalculatorKey>
<CalculatorKey className="key-sign" onPress={() => this.toggleSign()}>±</CalculatorKey>
<CalculatorKey className="key-percent" onPress={() => this.inputPercent()}>%</CalculatorKey>
</div>
<div className="digit-keys">
<CalculatorKey className="key-0" onPress={() => this.inputDigit(0)}>0</CalculatorKey>
<CalculatorKey className="key-dot" onPress={() => this.inputDot()}>●</CalculatorKey>
<CalculatorKey className="key-1" onPress={() => this.inputDigit(1)}>1</CalculatorKey>
<CalculatorKey className="key-2" onPress={() => this.inputDigit(2)}>2</CalculatorKey>
<CalculatorKey className="key-3" onPress={() => this.inputDigit(3)}>3</CalculatorKey>
<CalculatorKey className="key-4" onPress={() => this.inputDigit(4)}>4</CalculatorKey>
<CalculatorKey className="key-5" onPress={() => this.inputDigit(5)}>5</CalculatorKey>
<CalculatorKey className="key-6" onPress={() => this.inputDigit(6)}>6</CalculatorKey>
<CalculatorKey className="key-7" onPress={() => this.inputDigit(7)}>7</CalculatorKey>
<CalculatorKey className="key-8" onPress={() => this.inputDigit(8)}>8</CalculatorKey>
<CalculatorKey className="key-9" onPress={() => this.inputDigit(9)}>9</CalculatorKey>
</div>
</div>
<div className="operator-keys">
<CalculatorKey className="key-divide" onPress={() => this.performOperation('/')}>÷</CalculatorKey>
<CalculatorKey className="key-multiply" onPress={() => this.performOperation('*')}>×</CalculatorKey>
<CalculatorKey className="key-subtract" onPress={() => this.performOperation('-')}>−</CalculatorKey>
<CalculatorKey className="key-add" onPress={() => this.performOperation('+')}>+</CalculatorKey>
<CalculatorKey className="key-equals" onPress={() => this.performOperation('=')}>=</CalculatorKey>
</div>
</div>
</div>
)
}
}
ReactDOM.render(
<Calculator/>,
document.getElementById('app')
)
<script src="https://npmcdn.com/react@15.2.1/dist/react.min.js"></script>
<script src="https://npmcdn.com/react-dom@15.2.1/dist/react-dom.min.js"></script>
<script src="https://npmcdn.com/number-to-locale-string@1.0.1/polyfill.number.toLocaleString.js"></script>
<script src="https://npmcdn.com/react-point@2.0.0/umd/react-point.min.js"></script>
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
margin: 0;
font: 100 14px 'Roboto';
}
button {
display: block;
background: none;
border: none;
padding: 0;
font-family: inherit;
user-select: none;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
button:active {
box-shadow: inset 0px 0px 80px 0px rgba(0,0,0,0.25);
}
#wrapper {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#app {
width: 320px;
height: 520px;
position: relative;
}
.calculator {
width: 100%;
height: 100%;
background: black;
display: flex;
flex-direction: column;
}
#wrapper .calculator {
box-shadow: 0px 0px 20px 0px #aaa;
}
.calculator-display {
color: white;
background: #1c191c;
line-height: 130px;
font-size: 6em;
flex: 1;
}
.auto-scaling-text {
display: inline-block;
}
.calculator-display .auto-scaling-text {
padding: 0 30px;
position: absolute;
right: 0;
transform-origin: right;
}
.calculator-keypad {
height: 400px;
display: flex;
}
.calculator .input-keys {
width: 240px;
}
.calculator .function-keys {
display: flex;
}
.calculator .digit-keys {
background: #e0e0e7;
display: flex;
flex-direction: row;
flex-wrap: wrap-reverse;
}
.calculator-key {
width: 80px;
height: 80px;
border-top: 1px solid #777;
border-right: 1px solid #666;
text-align: center;
line-height: 80px;
}
.calculator .function-keys .calculator-key {
font-size: 2em;
}
.calculator .function-keys .key-multiply {
line-height: 50px;
}
.calculator .digit-keys .calculator-key {
font-size: 2.25em;
}
.calculator .digit-keys .key-0 {
width: 160px;
text-align: left;
padding-left: 32px;
}
.calculator .digit-keys .key-dot {
padding-top: 1em;
font-size: 0.75em;
}
.calculator .operator-keys .calculator-key {
color: white;
border-right: 0;
font-size: 3em;
}
.calculator .function-keys {
background: linear-gradient(to bottom, rgba(202,202,204,1) 0%, rgba(196,194,204,1) 100%);
}
.calculator .operator-keys {
background: linear-gradient(to bottom, rgba(252,156,23,1) 0%, rgba(247,126,27,1) 100%);
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment