index.js 5.0 KB

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