u-search.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <view class="u-search" :style="{
  3. margin: margin,
  4. }">
  5. <view
  6. class="u-content"
  7. :style="{
  8. backgroundColor: bgColor,
  9. borderRadius: shape == 'round' ? '100rpx' : '10rpx',
  10. border: borderStyle,
  11. height: height + 'rpx'
  12. }"
  13. >
  14. <view class="u-icon-wrap"><u-icon class="u-clear-icon" :size="30" name="search" :color="searchIconColor ? searchIconColor : color"></u-icon></view>
  15. <input
  16. confirm-type="search"
  17. @blur="blur"
  18. :value="value"
  19. @confirm="search"
  20. @input="inputChange"
  21. :disabled="disabled"
  22. @focus="getFocus"
  23. :maxlength="getMaxlength"
  24. :focus="focus"
  25. placeholder-class="u-placeholder-class"
  26. :placeholder="placeholder"
  27. :placeholder-style="`color: ${placeholderColor}`"
  28. class="u-input"
  29. type="text"
  30. :style="[{
  31. textAlign: inputAlign,
  32. color: color
  33. }, inputStyle]"
  34. />
  35. <view class="u-close-wrap" v-if="keyword && clearabled && focused" @touchstart="clear">
  36. <u-icon class="u-clear-icon" name="close-circle-fill" size="34" color="#c0c4cc"></u-icon>
  37. </view>
  38. </view>
  39. <view :style="[actionStyle]" class="u-action"
  40. :class="[showActionBtn || show ? 'u-action-active' : '']"
  41. @touchstart.stop.prevent="custom"
  42. >{{ actionText }}</view>
  43. </view>
  44. </template>
  45. <script>
  46. /**
  47. * search 搜索框
  48. * @description 搜索组件,集成了常见搜索框所需功能,用户可以一键引入,开箱即用。
  49. * @tutorial https://www.uviewui.com/components/search.html
  50. * @property {String} shape 搜索框形状,round-圆形,square-方形(默认round)
  51. * @property {String} bg-color 搜索框背景颜色(默认#f2f2f2)
  52. * @property {String} border-color 边框颜色,配置了颜色,才会有边框
  53. * @property {String} placeholder 占位文字内容(默认“请输入关键字”)
  54. * @property {Boolean} clearabled 是否启用清除控件(默认true)
  55. * @property {Boolean} focus 是否自动获得焦点(默认false)
  56. * @property {Boolean} show-action 是否显示右侧控件(默认true)
  57. * @property {String} action-text 右侧控件文字(默认“搜索”)
  58. * @property {Object} action-style 右侧控件的样式,对象形式
  59. * @property {String} input-align 输入框内容水平对齐方式(默认left)
  60. * @property {Object} input-style 自定义输入框样式,对象形式
  61. * @property {Boolean} disabled 是否启用输入框(默认false)
  62. * @property {String} search-icon-color 搜索图标的颜色,默认同输入框字体颜色
  63. * @property {String} color 输入框字体颜色(默认#606266)
  64. * @property {String} placeholder-color placeholder的颜色(默认#909399)
  65. * @property {String} margin 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30rpx"
  66. * @property {Boolean} animation 是否开启动画,见上方说明(默认false)
  67. * @property {String} value 输入框初始值
  68. * @property {String | Number} maxlength 输入框最大能输入的长度,-1为不限制长度
  69. * @property {Boolean} input-style input输入框的样式,可以定义文字颜色,大小等,对象形式
  70. * @property {String | Number} height 输入框高度,单位rpx(默认64)
  71. * @event {Function} change 输入框内容发生变化时触发
  72. * @event {Function} search 用户确定搜索时触发,用户按回车键,或者手机键盘右下角的"搜索"键时触发
  73. * @event {Function} custom 用户点击右侧控件时触发
  74. * @event {Function} clear 用户点击清除按钮时触发
  75. * @example <u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
  76. */
  77. export default {
  78. name: "u-search",
  79. props: {
  80. // 搜索框形状,round-圆形,square-方形
  81. shape: {
  82. type: String,
  83. default: 'round'
  84. },
  85. // 搜索框背景色,默认值#f2f2f2
  86. bgColor: {
  87. type: String,
  88. default: '#f2f2f2'
  89. },
  90. // 占位提示文字
  91. placeholder: {
  92. type: String,
  93. default: '请输入关键字'
  94. },
  95. // 是否启用清除控件
  96. clearabled: {
  97. type: Boolean,
  98. default: true
  99. },
  100. // 是否自动聚焦
  101. focus: {
  102. type: Boolean,
  103. default: false
  104. },
  105. // 是否在搜索框右侧显示取消按钮
  106. showAction: {
  107. type: Boolean,
  108. default: true
  109. },
  110. // 右边控件的样式
  111. actionStyle: {
  112. type: Object,
  113. default() {
  114. return {};
  115. }
  116. },
  117. // 取消按钮文字
  118. actionText: {
  119. type: String,
  120. default: '搜索'
  121. },
  122. // 输入框内容对齐方式,可选值为 left|center|right
  123. inputAlign: {
  124. type: String,
  125. default: 'left'
  126. },
  127. // 是否启用输入框
  128. disabled: {
  129. type: Boolean,
  130. default: false
  131. },
  132. // 开启showAction时,是否在input获取焦点时才显示
  133. animation: {
  134. type: Boolean,
  135. default: false
  136. },
  137. // 边框颜色,只要配置了颜色,才会有边框
  138. borderColor: {
  139. type: String,
  140. default: 'none'
  141. },
  142. // 输入框的初始化内容
  143. value: {
  144. type: String,
  145. default: ''
  146. },
  147. // 搜索框高度,单位rpx
  148. height: {
  149. type: [Number, String],
  150. default: 64
  151. },
  152. // input输入框的样式,可以定义文字颜色,大小等,对象形式
  153. inputStyle: {
  154. type: Object,
  155. default() {
  156. return {}
  157. }
  158. },
  159. // 输入框最大能输入的长度,-1为不限制长度(来自uniapp文档)
  160. maxlength: {
  161. type: [Number, String],
  162. default: -1
  163. },
  164. // 搜索图标的颜色,默认同输入框字体颜色
  165. searchIconColor: {
  166. type: String,
  167. default: ''
  168. },
  169. // 输入框字体颜色
  170. color: {
  171. type: String,
  172. default: '#606266'
  173. },
  174. // placeholder的颜色
  175. placeholderColor: {
  176. type: String,
  177. default: '#909399'
  178. },
  179. // 组件与其他上下左右元素之间的距离,带单位的字符串形式,如"30rpx"、"30rpx 20rpx"等写法
  180. margin: {
  181. type: String,
  182. default: '0'
  183. }
  184. },
  185. data() {
  186. return {
  187. keyword: '',
  188. showClear: false, // 是否显示右边的清除图标
  189. show: false,
  190. // 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件
  191. focused: this.focus
  192. // 绑定输入框的值
  193. // inputValue: this.value
  194. };
  195. },
  196. watch: {
  197. keyword(nVal) {
  198. // 双向绑定值,让v-model绑定的值双向变化
  199. this.$emit('input', nVal);
  200. // 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择
  201. this.$emit('change', nVal);
  202. },
  203. value: {
  204. immediate: true,
  205. handler(nVal) {
  206. this.keyword = nVal;
  207. }
  208. }
  209. },
  210. computed: {
  211. showActionBtn() {
  212. if (!this.animation && this.showAction) return true;
  213. else return false;
  214. },
  215. // 样式,根据用户传入的颜色值生成,如果不传入,默认为none
  216. borderStyle() {
  217. if (this.borderColor) return `1px solid ${this.borderColor}`;
  218. else return 'none';
  219. },
  220. // 将maxlength转为数值
  221. getMaxlength() {
  222. return Number(this.maxlength);
  223. }
  224. },
  225. methods: {
  226. // 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化
  227. inputChange(e) {
  228. this.keyword = e.detail.value;
  229. },
  230. // 清空输入
  231. // 也可以作为用户通过this.$refs形式调用清空输入框内容
  232. clear() {
  233. this.keyword = '';
  234. this.$emit('clear');
  235. },
  236. // 确定搜索
  237. search() {
  238. this.$emit('search', this.keyword);
  239. // 收起键盘
  240. uni.hideKeyboard();
  241. },
  242. // 点击右边自定义按钮的事件
  243. custom() {
  244. this.$emit('custom', this.keyword);
  245. // 收起键盘
  246. uni.hideKeyboard();
  247. },
  248. // 获取焦点
  249. getFocus() {
  250. this.focused = true;
  251. // 开启右侧搜索按钮展开的动画效果
  252. if (this.animation && this.showAction) this.show = true;
  253. this.$emit('focus', this.keyword);
  254. },
  255. // 失去焦点
  256. blur() {
  257. this.focused = false;
  258. this.show = false;
  259. this.$emit('blur', this.keyword);
  260. }
  261. }
  262. };
  263. </script>
  264. <style lang="scss" scoped>
  265. .u-search {
  266. display: flex;
  267. align-items: center;
  268. flex: 1;
  269. }
  270. .u-content {
  271. display: flex;
  272. align-items: center;
  273. padding: 0 18rpx;
  274. flex: 1;
  275. }
  276. .u-clear-icon {
  277. display: flex;
  278. align-items: center;
  279. }
  280. .u-input {
  281. flex: 1;
  282. font-size: 28rpx;
  283. line-height: 1;
  284. margin: 0 10rpx;
  285. color: $u-tips-color;
  286. }
  287. .u-close-wrap {
  288. width: 40rpx;
  289. height: 100%;
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. border-radius: 50%;
  294. }
  295. .u-placeholder-class {
  296. color: $u-tips-color;
  297. }
  298. .u-action {
  299. font-size: 28rpx;
  300. color: $u-main-color;
  301. width: 0;
  302. overflow: hidden;
  303. transition: all 0.3s;
  304. white-space: nowrap;
  305. text-align: center;
  306. }
  307. .u-action-active {
  308. width: 80rpx;
  309. margin-left: 10rpx;
  310. }
  311. </style>