index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. baseComponent({
  5. properties: {
  6. prefixCls: {
  7. type: String,
  8. value: 'wux-input',
  9. },
  10. label: {
  11. type: String,
  12. value: '',
  13. },
  14. extra: {
  15. type: String,
  16. value: '',
  17. },
  18. defaultValue: {
  19. type: String,
  20. value: '',
  21. },
  22. value: {
  23. type: String,
  24. value: '',
  25. observer(newVal) {
  26. if (this.data.controlled) {
  27. this.updated(newVal)
  28. }
  29. },
  30. },
  31. controlled: {
  32. type: Boolean,
  33. value: false,
  34. },
  35. type: {
  36. type: String,
  37. value: 'text',
  38. },
  39. password: {
  40. type: Boolean,
  41. value: false,
  42. },
  43. placeholder: {
  44. type: String,
  45. value: '',
  46. },
  47. placeholderStyle: {
  48. type: [String, Object],
  49. value: '',
  50. observer(newVal) {
  51. this.setData({
  52. extStyle: styleToCssString(newVal),
  53. })
  54. },
  55. },
  56. placeholderClass: {
  57. type: String,
  58. value: 'input-placeholder',
  59. },
  60. disabled: {
  61. type: Boolean,
  62. value: false,
  63. },
  64. maxlength: {
  65. type: Number,
  66. value: 140,
  67. },
  68. cursorSpacing: {
  69. type: Number,
  70. value: 11,
  71. },
  72. focus: {
  73. type: Boolean,
  74. value: false,
  75. },
  76. confirmType: {
  77. type: String,
  78. value: 'done',
  79. },
  80. confirmHold: {
  81. type: Boolean,
  82. value: false,
  83. },
  84. cursor: {
  85. type: Number,
  86. value: -1,
  87. },
  88. selectionStart: {
  89. type: Number,
  90. value: -1,
  91. },
  92. selectionEnd: {
  93. type: Number,
  94. value: -1,
  95. },
  96. adjustPosition: {
  97. type: Boolean,
  98. value: true,
  99. },
  100. clear: {
  101. type: Boolean,
  102. value: false,
  103. },
  104. error: {
  105. type: Boolean,
  106. value: false,
  107. },
  108. },
  109. data: {
  110. inputValue: '',
  111. inputFocus: false,
  112. extStyle: '',
  113. },
  114. computed: {
  115. classes() {
  116. const { prefixCls, disabled, inputFocus, error: hasError } = this.data
  117. const wrap = classNames(prefixCls, {
  118. [`${prefixCls}--focus`]: inputFocus,
  119. [`${prefixCls}--disabled`]: disabled,
  120. [`${prefixCls}--error`]: hasError,
  121. })
  122. const label = `${prefixCls}__label`
  123. const control = `${prefixCls}__control`
  124. const item = `${prefixCls}__item`
  125. const clear = `${prefixCls}__clear`
  126. const error = `${prefixCls}__error`
  127. const extra = `${prefixCls}__extra`
  128. return {
  129. wrap,
  130. label,
  131. control,
  132. item,
  133. clear,
  134. error,
  135. extra,
  136. }
  137. },
  138. },
  139. methods: {
  140. updated(inputValue) {
  141. if (this.data.inputValue !== inputValue) {
  142. this.setData({
  143. inputValue,
  144. })
  145. }
  146. },
  147. onChange(e) {
  148. if (!this.data.controlled) {
  149. this.updated(e.detail.value)
  150. }
  151. this.triggerEvent('change', e.detail)
  152. },
  153. onFocus(e) {
  154. this.clearTimer()
  155. this.setData({
  156. inputFocus: true,
  157. })
  158. this.triggerEvent('focus', e.detail)
  159. },
  160. onBlur(e) {
  161. this.setTimer()
  162. this.triggerEvent('blur', e.detail)
  163. },
  164. onConfirm(e) {
  165. this.triggerEvent('confirm', e.detail)
  166. },
  167. onClear() {
  168. const { controlled, inputValue } = this.data
  169. this.setData({
  170. inputValue: controlled ? inputValue : '',
  171. })
  172. this.triggerEvent('clear', { value: '' })
  173. },
  174. onError() {
  175. this.triggerEvent('error', { value: this.data.inputValue })
  176. },
  177. setTimer() {
  178. this.clearTimer()
  179. this.timeout = setTimeout(() => {
  180. this.setData({
  181. inputFocus: false,
  182. })
  183. }, 200)
  184. },
  185. clearTimer() {
  186. if (this.timeout) {
  187. clearTimeout(this.timeout)
  188. this.timeout = null
  189. }
  190. },
  191. },
  192. attached() {
  193. const { defaultValue, value, controlled } = this.data
  194. const inputValue = controlled ? value : defaultValue
  195. this.updated(inputValue)
  196. },
  197. })