index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/libs/classNames'
  3. import styleToCssString from '../helpers/libs/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. onlyShowClearWhenFocus: {
  102. type: Boolean,
  103. value: true,
  104. },
  105. },
  106. data: {
  107. inputValue: '',
  108. inputFocus: false,
  109. shouldShowClear: false,
  110. extStyle: '',
  111. },
  112. observers: {
  113. ['clear, disabled, inputValue, inputFocus, onlyShowClearWhenFocus'](...args) {
  114. const [
  115. clear,
  116. disabled,
  117. inputValue,
  118. inputFocus,
  119. onlyShowClearWhenFocus,
  120. ] = args
  121. this.setClear({
  122. clear,
  123. disabled,
  124. inputValue,
  125. inputFocus,
  126. onlyShowClearWhenFocus,
  127. })
  128. },
  129. },
  130. computed: {
  131. classes: ['prefixCls, disabled, inputFocus', function(prefixCls, disabled, inputFocus) {
  132. const wrap = classNames(prefixCls, {
  133. [`${prefixCls}--focus`]: inputFocus,
  134. [`${prefixCls}--disabled`]: disabled,
  135. })
  136. const form = `${prefixCls}__form`
  137. const box = `${prefixCls}__box`
  138. const search = `${prefixCls}__search`
  139. const input = `${prefixCls}__input`
  140. const clear = `${prefixCls}__clear`
  141. const label = `${prefixCls}__label`
  142. const icon = `${prefixCls}__icon`
  143. const text = `${prefixCls}__text`
  144. const cancel = `${prefixCls}__cancel`
  145. return {
  146. wrap,
  147. form,
  148. box,
  149. search,
  150. input,
  151. clear,
  152. label,
  153. icon,
  154. text,
  155. cancel,
  156. }
  157. }],
  158. },
  159. methods: {
  160. setClear(props) {
  161. const shouldShowClear = (() => {
  162. if (!props.clear || !props.inputValue || props.disabled) return false
  163. if (props.onlyShowClearWhenFocus) {
  164. return props.inputFocus
  165. } else {
  166. return true
  167. }
  168. })()
  169. if (this.data.shouldShowClear !== shouldShowClear) {
  170. this.setData({
  171. shouldShowClear,
  172. })
  173. }
  174. },
  175. updated(inputValue) {
  176. if (this.data.inputValue !== inputValue) {
  177. this.setData({
  178. inputValue,
  179. })
  180. }
  181. },
  182. onChange(e) {
  183. if (!this.data.controlled) {
  184. this.updated(e.detail.value)
  185. }
  186. if (!this.data.inputFocus) {
  187. this.setData({
  188. inputFocus: true,
  189. })
  190. }
  191. this.triggerEvent('change', e.detail)
  192. },
  193. onFocus(e) {
  194. this.setData({
  195. inputFocus: true,
  196. })
  197. this.triggerEvent('focus', e.detail)
  198. },
  199. onBlur(e) {
  200. this.setData({
  201. inputFocus: false,
  202. })
  203. this.triggerEvent('blur', e.detail)
  204. },
  205. onConfirm(e) {
  206. this.triggerEvent('confirm', e.detail)
  207. },
  208. onClear() {
  209. const { controlled, inputValue } = this.data
  210. this.setData({
  211. inputValue: controlled ? inputValue : '',
  212. inputFocus: true,
  213. })
  214. this.triggerEvent('clear', { value: '' })
  215. },
  216. onCancel() {
  217. this.triggerEvent('cancel', { value: this.data.inputValue })
  218. },
  219. onClick() {
  220. if (this.data.disabled) return
  221. this.setData({
  222. inputFocus: true,
  223. })
  224. },
  225. },
  226. attached() {
  227. const { defaultValue, value, controlled } = this.data
  228. const inputValue = controlled ? value : defaultValue
  229. this.updated(inputValue)
  230. this.setClear({ ...this.data, inputValue })
  231. },
  232. })