学员端小程序
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.

303 lines
9.6 KiB

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