CountNum.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. Text,
  5. TouchableOpacity,
  6. View,
  7. TextInput,
  8. } from 'react-native'
  9. import PropTypes from 'prop-types'
  10. export default class CountNum extends Component {
  11. //初始化
  12. constructor(props) {
  13. super(props)
  14. let defaultValue = 1
  15. if (this.props.defaultValue && this.props.defaultValue > 0) {
  16. defaultValue = this.props.defaultValue
  17. }
  18. this.state = {
  19. totalnum: defaultValue,
  20. }
  21. this._callback = this._callback.bind(this)
  22. this.maxValue = props.maxValue
  23. this.minValue = props.minValue
  24. // this.unitScale = props.unitScale
  25. }
  26. static propTypes = {
  27. size: PropTypes.number,
  28. touchSpan: PropTypes.number,
  29. touchDisabledAdd: PropTypes.bool,
  30. touchDisabledSub: PropTypes.bool,
  31. }
  32. static defaultProps = {
  33. size: 25,
  34. touchSpan: 1,
  35. touchDisabledAdd: false,
  36. touchDisabledSub: false,
  37. }
  38. componentWillReceiveProps(nextProps) {
  39. // if ((nextProps.maxValue !== this.props.maxValue) || (nextProps.minValue !== this.props.minValue)) {
  40. this.maxValue = nextProps.maxValue
  41. this.minValue = nextProps.minValue
  42. // this.state.totalnum = nextProps.defaultValue
  43. this.setState({
  44. totalnum: nextProps.defaultValue,
  45. })
  46. // }
  47. }
  48. _callback(totalnum) {
  49. if (this.props.callback) {
  50. this.props.callback(totalnum)
  51. }
  52. }
  53. //处理数量和金额变化
  54. _changeNumAndSum(totalnum) {
  55. this.setState({
  56. totalnum: totalnum,
  57. })
  58. this._callback(totalnum)
  59. }
  60. //数量输入框处理
  61. _getInputContent(InputText) {
  62. // if (!InputText) {
  63. // return this.setState({ totalnum: InputText })
  64. // }
  65. if (parseFloat(InputText) > this.maxValue) {
  66. InputText = this.minValue || 1
  67. }
  68. // this.setState({
  69. // totalnum : InputText
  70. // })
  71. this._changeNumAndSum(InputText);
  72. }
  73. //点击加号
  74. _onPressIncrease(totalnum) {
  75. let num = parseFloat(totalnum) + this.props.touchSpan
  76. if (num > 999999) {
  77. num = 999999
  78. }
  79. if (num > this.maxValue) {
  80. num = this.maxValue
  81. }
  82. this._changeNumAndSum(num)
  83. }
  84. //点击减号
  85. _onPressDecrease(totalnum) {
  86. let num = parseFloat(totalnum) - this.props.touchSpan
  87. if (this.minValue != null || this.minValue != undefined) {
  88. if (num < this.minValue) {
  89. num = this.minValue
  90. }
  91. } else {
  92. if (num < 1) {
  93. num = 1
  94. }
  95. }
  96. this._changeNumAndSum(num)
  97. }
  98. _handlerNull() {
  99. if (!this.state.totalnum) {
  100. return this._changeNumAndSum(this.minValue || 1)
  101. }
  102. this._changeNumAndSum(this.state.totalnum)
  103. }
  104. render() {
  105. let fontSize
  106. this.props.size >= 30 ? (fontSize = 14) : (fontSize = 12)
  107. return (
  108. <View
  109. style={[
  110. {
  111. flexDirection: 'row',
  112. // position: "absolute",
  113. height: this.props.size,
  114. width: this.props.size * 4.2,
  115. // height: 25,
  116. // width: this.props.defaultScale ? 110 : 100,
  117. top: this.props.top ? this.props.top : 0,
  118. backgroundColor: '#FFF',
  119. borderRadius: 3,
  120. borderColor: '#DDDDDD',
  121. borderWidth: 0,
  122. },
  123. this.props.left ? { left: this.props.left } : null,
  124. this.props.right ? { right: this.props.right } : null,
  125. ]}
  126. >
  127. {/* 减号 */}
  128. <TouchableOpacity
  129. disabled={this.props.touchDisabledSub}
  130. onPress={() => this._onPressDecrease(this.state.totalnum)}
  131. style={{
  132. justifyContent: 'center',
  133. height: this.props.size,
  134. width: this.props.size,
  135. backgroundColor: '#FFF',
  136. // flex: 1,
  137. borderWidth: 1,
  138. borderColor: '#DDDDDD',
  139. borderTopLeftRadius: 3,
  140. borderBottomLeftRadius: 3,
  141. }}
  142. >
  143. <Text
  144. style={{
  145. textAlign: 'center',
  146. color: '#666',
  147. fontWeight: 'bold',
  148. fontSize: this.props.fontSize || fontSize,
  149. }}
  150. >
  151. -
  152. </Text>
  153. </TouchableOpacity>
  154. {/* 输入框 */}
  155. <View
  156. style={{
  157. flex: 2,
  158. borderTopWidth: 1,
  159. borderTopColor: '#DDDDDD',
  160. borderBottomWidth: 1,
  161. borderBottomColor: '#DDDDDD',
  162. justifyContent: 'center',
  163. }}
  164. >
  165. <TextInput
  166. ref={ip => (this._ip = ip)}
  167. style={{
  168. fontSize: fontSize,
  169. color: '#333333',
  170. padding: 0,
  171. backgroundColor: '#FFF',
  172. textAlign: 'center',
  173. height: 20,
  174. }}
  175. // defaultValue={1.toString()}
  176. // maxLength={6}
  177. underlineColorAndroid="transparent"
  178. keyboardType="numeric"
  179. blurOnSubmit={true}
  180. onBlur={() => this._handlerNull()}
  181. onChangeText={text => this._getInputContent(text)}
  182. //onChange={this._getInputContent}
  183. //加个空的字符串以便将数字转换成字符串显示
  184. //defaultValue={this.state.totalnum + ''}
  185. defaultValue={this.props.defaultValue?(this.props.defaultValue + ''):1}
  186. //value={this.state.totalnum+""}
  187. editable={this.props.editable}
  188. />
  189. </View>
  190. {/* 加号 */}
  191. <TouchableOpacity
  192. disabled={this.props.touchDisabledAdd}
  193. onPress={() => this._onPressIncrease(this.state.totalnum)}
  194. style={{
  195. justifyContent: 'center',
  196. height: this.props.size,
  197. width: this.props.size,
  198. backgroundColor: '#FFF',
  199. // flex: 1,
  200. borderWidth: 1,
  201. borderColor: '#DDDDDD',
  202. borderTopRightRadius: 3,
  203. borderBottomRightRadius: 3,
  204. }}
  205. >
  206. <Text
  207. style={{
  208. textAlign: 'center',
  209. color: '#666',
  210. fontWeight: 'bold',
  211. fontSize: this.props.fontSize || fontSize,
  212. }}
  213. >
  214. +
  215. </Text>
  216. </TouchableOpacity>
  217. {this.props.defaultScale ? (
  218. <Text
  219. style={{
  220. textAlign: 'center',
  221. alignSelf: 'center',
  222. fontSize: 12,
  223. color: '#999999',
  224. marginLeft: 4,
  225. }}
  226. >
  227. {this.props.defaultScale}
  228. </Text>
  229. ) : null}
  230. </View>
  231. )
  232. }
  233. }