Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. export class Converter extends React.Component {
  2. constructor(props)
  3. {
  4. super(props);
  5.  
  6. const units = [];
  7. const leftValue = 0;
  8. const rightValue = 0;
  9. const rounding = 5;
  10. const leftUnit = "kN";
  11. const rightUnit = "kN";
  12. const combinable = true;
  13.  
  14. this.state = { leftValue, rightValue, rounding, leftUnit, rightUnit, units, combinable }
  15. }
  16.  
  17. render() {
  18. let me = this;
  19.  
  20. function isValidNumber(s)
  21. {
  22. return !!/^\d+(\.\d*)?$/.exec(s);
  23. }
  24.  
  25. function isValidInteger(s)
  26. {
  27. return ((!!/^\d+$/.exec(s) && s <= 12) || s == "");
  28. }
  29.  
  30. console.log(`render(): this.state.rounding = ${this.state.rounding}`);
  31.  
  32. const canConvert = (isValidNumber(this.state.leftValue) && isValidInteger(this.state.rounding));
  33.  
  34. return (
  35. <View>
  36. <View>
  37. <View style={[styles.inline, styles.center]}>
  38. <TextInput style={[{width: 100}, styles.margins]} value={"" + this.state.leftValue} onChangeText={(s) => this.setState({leftValue: s})} keyboardType='numeric'/>
  39. <Text>{this.state.leftUnit}</Text>
  40. </View>
  41. <View style={[styles.inline, styles.center]}>
  42. <TextInput style={[{width: 100}, styles.margins]} value={"" + this.state.rounding} onChangeText={(s) => this.setState({rounding: s})} keyboardType='numeric'/>
  43. <Text>Decimals (0-12)</Text>
  44. </View>
  45. <Picker selectedValue={this.state.leftUnit} onValueChange={(itemValue, itemIndex) => { this.setState({leftUnit: itemValue})} }>
  46. {Object.keys(this.state.units).map((key) => {
  47. return (<Picker.Item key={key} label={key} value={key}/>)
  48. })}
  49. </Picker>
  50. <Picker style={styles.margins} selectedValue={this.state.rightUnit} onValueChange={(itemValue, itemIndex) => this.setState({rightUnit: itemValue})}>
  51. {Object.keys(this.state.units).map((key) => {
  52. return (<Picker.Item key={key} label={key} value={key}/>)
  53. })}
  54. </Picker>
  55. <View style={styles.center}>
  56. <Text style={styles.margins} >{this.state.rightValue} {this.state.rightUnit}</Text>
  57. </View>
  58. <Button title="Convert" onPress={() => this.convert()} disabled={!canConvert} />
  59. </View>
  60. </View>
  61. );
  62. }
  63.  
  64. convert() {
  65. const { factor: fromValFactor } = this.state.units[this.state.leftUnit];
  66. const { factor: toValFactor } = this.state.units[this.state.rightUnit];
  67. const endValue = this.state.leftValue * fromValFactor / toValFactor;
  68. if (this.state.rounding == null) {
  69. this.setState({rightValue: ((this.state.leftValue * fromValFactor) / toValFactor)});
  70. } else {
  71. this.setState({rightValue: ((this.state.leftValue * fromValFactor) / toValFactor).toFixed(this.state.rounding)});
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement