爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
9.6 KiB

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