CountNum_new.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. Text,
  5. TouchableOpacity,
  6. View,
  7. TextInput,
  8. Image,
  9. } from 'react-native'
  10. import PropTypes from 'prop-types'
  11. import countNumSubRed from './countNumSubRed.png'
  12. import countNumAddRed from './countNumAddRed.png'
  13. import countNumSubGray from './countNumSubGray.png'
  14. export default class CountNum extends Component {
  15. //初始化
  16. constructor(props) {
  17. super(props)
  18. let defaultValue = 1
  19. if (this.props.defaultValue && this.props.defaultValue > 0) {
  20. defaultValue = this.props.defaultValue
  21. }
  22. this.state = {
  23. totalnum: defaultValue,
  24. }
  25. this._callback = this._callback.bind(this)
  26. this.maxValue = props.maxValue
  27. this.minValue = props.minValue
  28. // this.unitScale = props.unitScale
  29. }
  30. static propTypes = {
  31. size: PropTypes.number,
  32. touchSpan: PropTypes.number,
  33. touchDisabledAdd: PropTypes.bool,
  34. touchDisabledSub: PropTypes.bool,
  35. }
  36. static defaultProps = {
  37. size: 25,
  38. touchSpan: 1,
  39. touchDisabledAdd: false,
  40. touchDisabledSub: false,
  41. }
  42. componentWillReceiveProps(nextProps) {
  43. // if ((nextProps.maxValue !== this.props.maxValue) || (nextProps.minValue !== this.props.minValue)) {
  44. this.maxValue = nextProps.maxValue
  45. this.minValue = nextProps.minValue
  46. // this.state.totalnum = nextProps.defaultValue
  47. this.setState({
  48. totalnum: nextProps.defaultValue,
  49. })
  50. // }
  51. }
  52. _callback(totalnum) {
  53. if (this.props.callback) {
  54. this.props.callback(totalnum)
  55. }
  56. }
  57. //处理数量和金额变化
  58. _changeNumAndSum(totalnum) {
  59. this.setState({
  60. totalnum: totalnum,
  61. })
  62. this._callback(totalnum)
  63. }
  64. //数量输入框处理
  65. _getInputContent(InputText) {
  66. // if (!InputText) {
  67. // return this.setState({ totalnum: InputText })
  68. // }
  69. if (InputText == this.state.totalnum) return
  70. if (parseFloat(InputText) > this.maxValue) {
  71. InputText = this.minValue || 1
  72. }
  73. this.state.totalnum = InputText
  74. // this._changeNumAndSum(InputText);
  75. }
  76. //点击加号
  77. _onPressIncrease(totalnum) {
  78. let num =
  79. (totalnum
  80. ? parseFloat(totalnum)
  81. : parseFloat(this.props.defaultValue || 1)) + this.props.touchSpan
  82. if (num > 999999) {
  83. num = 999999
  84. }
  85. if (num > this.maxValue) {
  86. num = this.maxValue
  87. }
  88. this._changeNumAndSum(num)
  89. }
  90. //点击减号
  91. _onPressDecrease(totalnum) {
  92. let num =
  93. (totalnum
  94. ? parseFloat(totalnum)
  95. : parseFloat(this.props.defaultValue || 1)) - this.props.touchSpan
  96. if (this.minValue != null || this.minValue != undefined) {
  97. if (num < this.minValue) {
  98. num = this.minValue
  99. }
  100. } else {
  101. if (num < 1) {
  102. num = 1
  103. }
  104. }
  105. this._changeNumAndSum(num)
  106. }
  107. _handlerNull() {
  108. if (!this.state.totalnum) {
  109. // return this._changeNumAndSum(this.minValue || 1)
  110. return
  111. }
  112. this._changeNumAndSum(this.state.totalnum)
  113. }
  114. render() {
  115. let fontSize
  116. this.props.size >= 30 ? (fontSize = 14) : (fontSize = 12)
  117. if (this.props.isDesign) {
  118. return (
  119. <View
  120. style={[
  121. {
  122. flexDirection: 'row',
  123. // position: "absolute",
  124. height: this.props.size,
  125. width: this.props.size * 3.1,
  126. // height: 25,
  127. // width: this.props.defaultScale ? 110 : 100,
  128. top: this.props.top ? this.props.top : 0,
  129. backgroundColor: '#FFF',
  130. },
  131. this.props.left ? { left: this.props.left } : null,
  132. this.props.right ? { right: this.props.right } : null,
  133. ]}
  134. >
  135. {/* 减号 */}
  136. <TouchableOpacity
  137. disabled={this.props.touchDisabledSub}
  138. onPress={() => this._onPressDecrease(this.state.totalnum)}
  139. style={{
  140. justifyContent: 'center',
  141. height: this.props.size,
  142. width: this.props.size,
  143. // flex: 1,
  144. }}
  145. >
  146. <Image
  147. style={{ width: 20, height: 20 }}
  148. source={
  149. this.props.orderNum && this.props.orderNum > 1
  150. ? countNumSubRed
  151. : countNumSubGray
  152. }
  153. resizeMode="cover"
  154. />
  155. </TouchableOpacity>
  156. {/* 输入框 */}
  157. <View
  158. style={{
  159. flex: 2,
  160. justifyContent: 'center',
  161. }}
  162. >
  163. <TextInput
  164. ref={ip => (this._ip = ip)}
  165. style={{
  166. fontSize: fontSize,
  167. color: '#333333',
  168. padding: 0,
  169. backgroundColor: '#FFF',
  170. textAlign: 'center',
  171. height: 20,
  172. }}
  173. // defaultValue={1.toString()}
  174. // maxLength={6}
  175. underlineColorAndroid="transparent"
  176. keyboardType="numeric"
  177. blurOnSubmit={true}
  178. onBlur={() => this._handlerNull()}
  179. onChangeText={text => this._getInputContent(text)}
  180. //加个空的字符串以便将数字转换成字符串显示
  181. defaultValue={this.state.totalnum + ''}
  182. // value={this.state.totalnum + ''}
  183. editable={this.props.editable}
  184. />
  185. </View>
  186. {/* 加号 */}
  187. <TouchableOpacity
  188. disabled={this.props.touchDisabledAdd}
  189. onPress={() => this._onPressIncrease(this.state.totalnum)}
  190. style={{
  191. justifyContent: 'center',
  192. height: this.props.size,
  193. width: this.props.size,
  194. }}
  195. >
  196. <Image
  197. style={{ width: 20, height: 20 }}
  198. source={countNumAddRed}
  199. resizeMode="cover"
  200. />
  201. </TouchableOpacity>
  202. {this.props.defaultScale ? (
  203. <Text
  204. style={{
  205. textAlign: 'center',
  206. alignSelf: 'center',
  207. fontSize: 12,
  208. color: '#999999',
  209. marginLeft: 4,
  210. }}
  211. >
  212. {this.props.defaultScale}
  213. </Text>
  214. ) : null}
  215. </View>
  216. )
  217. } else {
  218. return (
  219. <View
  220. style={[
  221. {
  222. flexDirection: 'row',
  223. // position: "absolute",
  224. height: this.props.size,
  225. width: this.props.size * 4.2,
  226. // height: 25,
  227. // width: this.props.defaultScale ? 110 : 100,
  228. top: this.props.top ? this.props.top : 0,
  229. backgroundColor: '#FFF',
  230. borderRadius: 3,
  231. borderColor: '#DDDDDD',
  232. borderWidth: 0,
  233. },
  234. this.props.left ? { left: this.props.left } : null,
  235. this.props.right ? { right: this.props.right } : null,
  236. ]}
  237. >
  238. {/* 减号 */}
  239. <TouchableOpacity
  240. disabled={this.props.touchDisabledSub}
  241. onPress={() => this._onPressDecrease(this.state.totalnum)}
  242. style={{
  243. justifyContent: 'center',
  244. height: this.props.size,
  245. width: this.props.size,
  246. backgroundColor: '#FFF',
  247. // flex: 1,
  248. borderWidth: 1,
  249. borderColor: '#DDDDDD',
  250. borderTopLeftRadius: 3,
  251. borderBottomLeftRadius: 3,
  252. }}
  253. >
  254. <Text
  255. style={{
  256. textAlign: 'center',
  257. color: '#666',
  258. fontWeight: 'bold',
  259. fontSize: this.props.fontSize || fontSize,
  260. }}
  261. >
  262. -
  263. </Text>
  264. </TouchableOpacity>
  265. {/* 输入框 */}
  266. <View
  267. style={{
  268. flex: 2,
  269. borderTopWidth: 1,
  270. borderTopColor: '#DDDDDD',
  271. borderBottomWidth: 1,
  272. borderBottomColor: '#DDDDDD',
  273. justifyContent: 'center',
  274. }}
  275. >
  276. <TextInput
  277. ref={ip => (this._ip = ip)}
  278. style={{
  279. fontSize: fontSize,
  280. color: '#333333',
  281. padding: 0,
  282. backgroundColor: '#FFF',
  283. textAlign: 'center',
  284. height: 20,
  285. }}
  286. // defaultValue={1.toString()}
  287. // maxLength={6}
  288. underlineColorAndroid="transparent"
  289. keyboardType="numeric"
  290. blurOnSubmit={true}
  291. onBlur={() => this._handlerNull()}
  292. onChangeText={text => this._getInputContent(text)}
  293. //加个空的字符串以便将数字转换成字符串显示
  294. defaultValue={this.state.totalnum + ''}
  295. // value={this.state.totalnum + ''}
  296. editable={this.props.editable}
  297. />
  298. </View>
  299. {/* 加号 */}
  300. <TouchableOpacity
  301. disabled={this.props.touchDisabledAdd}
  302. onPress={() => this._onPressIncrease(this.state.totalnum)}
  303. style={{
  304. justifyContent: 'center',
  305. height: this.props.size,
  306. width: this.props.size,
  307. backgroundColor: '#FFF',
  308. // flex: 1,
  309. borderWidth: 1,
  310. borderColor: '#DDDDDD',
  311. borderTopRightRadius: 3,
  312. borderBottomRightRadius: 3,
  313. }}
  314. >
  315. <Text
  316. style={{
  317. textAlign: 'center',
  318. color: '#666',
  319. fontWeight: 'bold',
  320. fontSize: this.props.fontSize || fontSize,
  321. }}
  322. >
  323. +
  324. </Text>
  325. </TouchableOpacity>
  326. {this.props.defaultScale ? (
  327. <Text
  328. style={{
  329. textAlign: 'center',
  330. alignSelf: 'center',
  331. fontSize: 12,
  332. color: '#999999',
  333. marginLeft: 4,
  334. }}
  335. >
  336. {this.props.defaultScale}
  337. </Text>
  338. ) : null}
  339. </View>
  340. )
  341. }
  342. }
  343. }