row.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { Component } from "react";
  2. import {
  3. StyleSheet,
  4. Switch,
  5. Text,
  6. TextInput,
  7. TouchableOpacity,
  8. View
  9. } from "react-native";
  10. const styles = StyleSheet.create({
  11. complete: {
  12. textDecorationLine: "line-through"
  13. },
  14. container: {
  15. alignItems: "flex-start",
  16. flexDirection: "row",
  17. justifyContent: "space-between",
  18. padding: 10
  19. },
  20. destroy: {
  21. color: "#cc9a9a",
  22. fontSize: 20
  23. },
  24. done: {
  25. borderColor: "#7be290",
  26. borderRadius: 5,
  27. borderWidth: 1,
  28. padding: 7
  29. },
  30. doneText: {
  31. color: "#4d4d4d",
  32. fontSize: 20
  33. },
  34. input: {
  35. color: "#4d4d4d",
  36. flex: 1,
  37. fontSize: 24,
  38. height: 100,
  39. marginLeft: 16,
  40. padding: 0
  41. },
  42. text: {
  43. color: "#4d4d4d",
  44. fontSize: 24
  45. },
  46. textWrap: {
  47. flex: 1,
  48. marginHorizontal: 10
  49. }
  50. });
  51. class Row extends Component {
  52. render() {
  53. // console.log("Row.render", this.props);
  54. const { complete } = this.props;
  55. const textComponent = (
  56. <TouchableOpacity style={styles.textWrap} onLongPress={() => this.props.onToggleEdit(true)}>
  57. <Text style={[styles.text, complete && styles.complete]}>{this.props.text}</Text>
  58. </TouchableOpacity>
  59. );
  60. const removeButton = (
  61. <TouchableOpacity onPress={this.props.onRemove}>
  62. <Text style={styles.destroy}>X</Text>
  63. </TouchableOpacity>
  64. );
  65. const editingComponent = (
  66. <View style={styles.textWrap}>
  67. <TextInput
  68. autoFocus
  69. multiline
  70. onChangeText={this.props.onUpdate}
  71. style={styles.input}
  72. value={this.props.text}
  73. />
  74. </View>
  75. );
  76. const doneButton = (
  77. <TouchableOpacity
  78. onPress={() => this.props.onToggleEdit(false)}
  79. style={styles.done}
  80. >
  81. <Text style={styles.doneText}>Save</Text>
  82. </TouchableOpacity>
  83. );
  84. return (
  85. <View style={styles.container}>
  86. <Switch
  87. value={complete}
  88. onValueChange={this.props.onComplete}
  89. />
  90. {this.props.editing ? editingComponent : textComponent}
  91. {this.props.editing ? doneButton : removeButton}
  92. </View>
  93. );
  94. }
  95. }
  96. Row.propTypes = {
  97. complete: React.PropTypes.bool,
  98. editing: React.PropTypes.bool,
  99. onComplete: React.PropTypes.func,
  100. onRemove: React.PropTypes.func,
  101. onToggleEdit: React.PropTypes.func,
  102. onUpdate: React.PropTypes.func,
  103. text: React.PropTypes.string
  104. };
  105. export default Row;