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