index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React, { Children } from 'react'
  2. import {
  3. StyleSheet,
  4. TouchableOpacity,
  5. View,
  6. TextInput,
  7. Dimensions,
  8. Text,
  9. } from 'react-native'
  10. import PropTypes from 'prop-types'
  11. import Icon from '../Iconfont/Iconfont'
  12. const { width, height } = Dimensions.get('window')
  13. export default class InputText extends React.Component {
  14. constructor(props) {
  15. super(props)
  16. this.state = { text: '' }
  17. }
  18. static propTypes = {
  19. style: PropTypes.object,
  20. textStyle: PropTypes.object,
  21. placeholder: PropTypes.string,
  22. placeholderTextColor: PropTypes.string,
  23. iconColor: PropTypes.string,
  24. textInputBacg: PropTypes.string,
  25. iconName: PropTypes.string,
  26. iconSize: PropTypes.number,
  27. placeholderSize: PropTypes.number,
  28. onchangeFn: PropTypes.func,
  29. onFocusFn: PropTypes.func,
  30. touchInput: PropTypes.func,
  31. autoFocus: PropTypes.bool,
  32. touchBool: PropTypes.bool,
  33. isICon: PropTypes.bool,
  34. keyboardType: PropTypes.string,
  35. isBlurTrue: PropTypes.bool,
  36. defaultValue: PropTypes.string,
  37. }
  38. static defaultProps = {
  39. style: {
  40. height: 28,
  41. width: Dimensions.get('window').width / 1.36,
  42. marginTop: 20,
  43. borderRadius: Dimensions.get('window').width / 2,
  44. alignSelf: 'center',
  45. paddingLeft: 14,
  46. },
  47. textStyle: {
  48. paddingLeft: 10,
  49. height: 32,
  50. fontSize: 13,
  51. padding: 0,
  52. // color: "#FFF"
  53. // fontSize: 11
  54. },
  55. keyboardType: 'default',
  56. placeholder: '请输入商品名称或商品编码',
  57. placeholderTextColor: '#FFF',
  58. placeholderSize: 11,
  59. iconColor: '#FFF',
  60. textInputBacg: '#CCC',
  61. iconName: 'icon-icon-sousuo',
  62. iconSize: 18,
  63. onchangeFn: () => {},
  64. onFocusFn: () => {},
  65. touchInput: () => {},
  66. touchBool: false,
  67. autoFocus: false,
  68. isICon: true,
  69. isBlurTrue: true,
  70. }
  71. setClear() {
  72. this.setState({ text: '' })
  73. }
  74. render() {
  75. if (this.props.touchBool) {
  76. return (
  77. <TouchableOpacity
  78. onPress={() => this.props.touchInput()}
  79. activeOpacity={1}
  80. style={[
  81. this.props.style,
  82. {
  83. backgroundColor: this.props.textInputBacg,
  84. flexDirection: 'row',
  85. },
  86. ]}
  87. >
  88. <Icon
  89. name={this.props.iconName}
  90. size={this.props.iconSize}
  91. color={this.props.iconColor}
  92. style={{ marginTop: 7 }}
  93. />
  94. <Text
  95. style={{
  96. alignSelf: 'center',
  97. fontSize: this.props.placeholderSize,
  98. marginLeft: 10,
  99. color: this.props.placeholderTextColor,
  100. }}
  101. >
  102. {this.props.placeholder}
  103. </Text>
  104. </TouchableOpacity>
  105. )
  106. } else {
  107. return (
  108. <View
  109. style={[
  110. this.props.style,
  111. {
  112. backgroundColor: this.props.textInputBacg,
  113. flexDirection: 'row',
  114. },
  115. ]}
  116. >
  117. {this.props.isICon ? (
  118. <Icon
  119. name={this.props.iconName}
  120. size={this.props.iconSize}
  121. color={this.props.iconColor}
  122. style={{ marginTop: 7 }}
  123. />
  124. ) : null}
  125. <TextInput
  126. style={[
  127. this.props.textStyle,
  128. {
  129. flex: 1,
  130. },
  131. ]}
  132. defaultValue={this.props.defaultValue + ''}
  133. keyboardType={this.props.keyboardType}
  134. placeholder={this.props.placeholder}
  135. placeholderTextColor={this.props.placeholderTextColor}
  136. onChangeText={text => {
  137. this.setState({
  138. text: parseFloat(this.props.maxNum)
  139. ? parseFloat(text) >= parseFloat(this.props.maxNum)
  140. ? this.props.maxNum
  141. : text
  142. : text,
  143. })
  144. }}
  145. value={this.state.text + ''}
  146. defaultValue={this.props.defaultValue}
  147. multiline={false}
  148. onBlur={() => {
  149. this.props.isBlurTrue
  150. ? this.props.onchangeFn(this.state.text)
  151. : {}
  152. }}
  153. onSubmitEditing={() => {
  154. this.props.onchangeFn(this.state.text)
  155. }}
  156. onFocus={() => {
  157. this.props.onFocusFn()
  158. }}
  159. underlineColorAndroid="transparent"
  160. blurOnSubmit={true}
  161. autoFocus={this.props.autoFocus}
  162. autoCorrect={false}
  163. clearButtonMode={'while-editing'}
  164. />
  165. </View>
  166. )
  167. }
  168. }
  169. }