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() {
  109. const { prefixCls, disabled, inputFocus } = this.data
  110. const wrap = classNames(prefixCls, {
  111. [`${prefixCls}--focus`]: inputFocus,
  112. [`${prefixCls}--disabled`]: disabled,
  113. })
  114. const form = `${prefixCls}__form`
  115. const box = `${prefixCls}__box`
  116. const search = `${prefixCls}__search`
  117. const input = `${prefixCls}__input`
  118. const clear = `${prefixCls}__clear`
  119. const label = `${prefixCls}__label`
  120. const icon = `${prefixCls}__icon`
  121. const text = `${prefixCls}__text`
  122. const cancel = `${prefixCls}__cancel`
  123. return {
  124. wrap,
  125. form,
  126. box,
  127. search,
  128. input,
  129. clear,
  130. label,
  131. icon,
  132. text,
  133. cancel,
  134. }
  135. },
  136. },
  137. methods: {
  138. updated(inputValue) {
  139. if (this.data.inputValue !== inputValue) {
  140. this.setData({
  141. inputValue,
  142. })
  143. }
  144. },
  145. onChange(e) {
  146. if (!this.data.controlled) {
  147. this.updated(e.detail.value)
  148. }
  149. if (!this.data.inputFocus) {
  150. this.setData({
  151. inputFocus: true,
  152. })
  153. }
  154. this.triggerEvent('change', e.detail)
  155. },
  156. onFocus(e) {
  157. this.setData({
  158. inputFocus: true,
  159. })
  160. this.triggerEvent('focus', e.detail)
  161. },
  162. onBlur(e) {
  163. this.setData({
  164. inputFocus: false,
  165. })
  166. this.triggerEvent('blur', e.detail)
  167. },
  168. onConfirm(e) {
  169. this.triggerEvent('confirm', e.detail)
  170. },
  171. onClear() {
  172. const { controlled, inputValue } = this.data
  173. this.setData({
  174. inputValue: controlled ? inputValue : '',
  175. inputFocus: true,
  176. })
  177. this.triggerEvent('clear', { value: '' })
  178. },
  179. onCancel() {
  180. this.triggerEvent('cancel', { value: this.data.inputValue })
  181. },
  182. onClick() {
  183. if (this.data.disabled) return
  184. this.setData({
  185. inputFocus: true,
  186. })
  187. },
  188. },
  189. attached() {
  190. const { defaultValue, value, controlled } = this.data
  191. const inputValue = controlled ? value : defaultValue
  192. this.updated(inputValue)
  193. },
  194. })