-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSAEM2-29 feat: provide custom keyboard with calculation (#15)
* JSAEM2-29 feat: provide custom keyboard with calculation * JSAEM2-29 fixed: basic request changes from QA * JSAEM 2-29 fixed: applied all request changes * JSAEM2-29 fixing: capital issue * JSAEM2-29 fixed: capital issue * JSAEM 2-29 fixed: naming issues
- Loading branch information
1 parent
b74bd2f
commit c155679
Showing
8 changed files
with
295 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
export const keyboardLayout = [ | ||
[1, 4, 7, 'C'], | ||
[2, 5, 8, '0'], | ||
[3, 6, 9, '.'], | ||
['+', '-', 'Remove', 'Add'], | ||
]; | ||
|
||
const isNumber = (value) => !Number.isNaN(parseFloat(value)); | ||
|
||
const getLastNum = (str) => { | ||
let rst = ''; | ||
for (let iter = str.length - 1; iter >= 0; iter -= 1) { | ||
if (['+', '-'].indexOf(str.charAt(iter)) >= 0) { | ||
break; | ||
} | ||
rst = `${str.charAt(iter)}${rst}`; | ||
} | ||
return rst; | ||
}; | ||
|
||
export const append = (oriStr, newChar) => { | ||
const lastNum = getLastNum(oriStr); | ||
if (newChar === '.') { | ||
if (lastNum.indexOf('.') < 0) { | ||
return `${oriStr}.`; | ||
} | ||
return oriStr; | ||
} | ||
|
||
if (isNumber(newChar) && lastNum.indexOf('.') >= 0 && lastNum.split('.')[1].length === 2) { | ||
return oriStr; | ||
} | ||
|
||
return oriStr + newChar; | ||
}; | ||
|
||
export const removeLast = (oriStr) => oriStr.slice(0, -1); | ||
|
||
export const getResult = (oriStr) => { | ||
const operationArr = []; | ||
let temp = ''; | ||
for (let i = 0; i < oriStr.length; i += 1) { | ||
if (['+', '-'].indexOf(oriStr.charAt(i)) >= 0) { | ||
operationArr.push(temp); | ||
operationArr.push(oriStr.charAt(i)); | ||
temp = ''; | ||
} else { | ||
temp += oriStr.charAt(i); | ||
} | ||
} | ||
|
||
if (temp !== '') { | ||
operationArr.push(temp); | ||
} | ||
|
||
let result = 0; | ||
let sign = 1; | ||
operationArr.forEach((i) => { | ||
if (i === '+') { | ||
sign = 1; | ||
} else if (i === '-') { | ||
sign = -1; | ||
} else { | ||
temp = parseFloat(i); | ||
result += sign * temp; | ||
} | ||
}); | ||
|
||
return result.toFixed(2).toString(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
append, removeLast, keyboardLayout, getResult, | ||
} from './Calculator'; | ||
import styles from './Style'; | ||
import DateSelector from './DateSelector'; | ||
import KeyboardButton from './KeyboardButton'; | ||
|
||
export default function CalculatorKeyboard({ | ||
calculable, | ||
expStr, | ||
createHandler, | ||
transDate, | ||
setTransDate, | ||
onExpressionChange, | ||
}) { | ||
const wrapResult = (exp) => ({ | ||
expression: exp, | ||
result: getResult(exp), | ||
}); | ||
|
||
const pressHandler = (pressedVal) => { | ||
if (!calculable) { | ||
return; | ||
} | ||
|
||
switch (pressedVal) { | ||
case 'Add': | ||
createHandler(); | ||
break; | ||
case 'C': | ||
onExpressionChange(wrapResult('')); | ||
break; | ||
case 'Remove': | ||
onExpressionChange(wrapResult(removeLast(expStr))); | ||
break; | ||
default: | ||
onExpressionChange(wrapResult(append(expStr, pressedVal))); | ||
} | ||
}; | ||
|
||
return ( | ||
<View style={styles.keyboardLayout}> | ||
<DateSelector transDate={transDate} setTransDate={setTransDate} /> | ||
<View style={styles.keyboardRowLayout}> | ||
{keyboardLayout.map((row, idx) => ( | ||
<View key={`row${idx + 1}`}> | ||
{row.map((cell, idx2) => ( | ||
<View key={`cell${idx + 1}-${idx2 + 1}`}> | ||
<KeyboardButton btnVal={cell.toString()} pressHandler={pressHandler} /> | ||
</View> | ||
))} | ||
</View> | ||
))} | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
CalculatorKeyboard.propTypes = { | ||
calculable: PropTypes.bool.isRequired, | ||
expStr: PropTypes.string, | ||
createHandler: PropTypes.func.isRequired, | ||
transDate: PropTypes.number.isRequired, | ||
setTransDate: PropTypes.func.isRequired, | ||
onExpressionChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
CalculatorKeyboard.defaultProps = { | ||
expStr: '', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { Button } from 'react-native-elements'; | ||
import PropTypes from 'prop-types'; | ||
import btnColor from '../Common/Color'; | ||
import styles from './Style'; | ||
|
||
const IconButton = ({ iconName, onPress }) => ( | ||
<Button | ||
type="outline" | ||
icon={{ | ||
name: iconName, | ||
size: 25, | ||
color: btnColor.grey, | ||
}} | ||
buttonStyle={styles.keyboardBtn} | ||
onPress={onPress} | ||
/> | ||
); | ||
|
||
IconButton.propTypes = { | ||
iconName: PropTypes.string.isRequired, | ||
onPress: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default IconButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { Button } from 'react-native-elements'; | ||
import PropTypes from 'prop-types'; | ||
import btnColor from '../Common/Color'; | ||
import IconButton from './IconButton'; | ||
import styles from './Style'; | ||
|
||
const NumberBtn = ({ btnVal, onPress }) => ( | ||
<Button | ||
type="outline" | ||
buttonStyle={styles.keyboardBtn} | ||
title={btnVal.toString()} | ||
titleStyle={{ color: btnColor.grey }} | ||
onPress={onPress} | ||
/> | ||
); | ||
|
||
const KeyboardButton = ({ btnVal, pressHandler }) => { | ||
switch (btnVal) { | ||
case 'Add': | ||
return <IconButton iconName="playlist-add" onPress={() => pressHandler(btnVal)} />; | ||
case 'Remove': | ||
return <IconButton iconName="backspace" onPress={() => pressHandler(btnVal)} />; | ||
default: | ||
return <NumberBtn btnVal={btnVal} onPress={() => pressHandler(btnVal)} />; | ||
} | ||
}; | ||
|
||
NumberBtn.propTypes = { | ||
btnVal: PropTypes.string.isRequired, | ||
onPress: PropTypes.func.isRequired, | ||
}; | ||
|
||
KeyboardButton.propTypes = { | ||
btnVal: PropTypes.string.isRequired, | ||
pressHandler: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default KeyboardButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.