searchCom.vue 3.75 KB
<template>
    <div class="search-com" :style="'background-color: ' + defaultSearchs.backgroundColor">
		<div class="box">
			<div class="search-group" :style="'border-color: ' + defaultSearchs.borderColor">
				<div class="left-box">
					<form class="search-form" action="" onsubmit="return false;">
						<div class="search-icon">
							<img src="static/img/icon_search.png" class="search-img" />
						</div>
						<div class="line"></div>
						<div class="input-box">
							<input type="search" ref="searchInput" autofocus :placeholder="defaultSearchs.pl" :maxlength="defaultSearchs.maxlength" v-model="defaultSearchs.keyword" class="keyword" @keyup="confirm(defaultSearchs.keyword, $event)" @blur="onBlur" />
						</div>
					</form>
				</div>
				<div class="right-box">
					<div class="clear-input-box" v-if="defaultSearchs.keyword" @click="clearInput">
						<img src="static/img/icon_clear_input.png" class="clear-input-img" />
					</div>
				</div>
			</div>
			<div class="buttons-box">
				<div class="btn btn-primary" :style="'color: ' + defaultSearchs.borderColor" @click="cancel">{{defaultSearchs.btnText}}</div>
			</div>
		</div>
    </div>
</template>

<script>
    export default {
        data: function () {
            return {
				defaultSearchs: {
					backgroundColor: "transparent",
					keyword: "",
					maxlength: 20,
					borderColor: "#F9F9F9",
					pl: "请输入搜索内容",
					btnText: "取消"
				}
            }
        },
        props: ['searchs'],
		methods: {

			/**
			 * 获取搜索关键字
			 */
			getKeyword() {
				return this.defaultSearchs.keyword;
			},

			/**
			 * 设置搜索关键字
			 * @param {String} keyword [搜索关键字]
			 */
			setKeyword(keyword) {
				this.$set(this.defaultSearchs, "keyword", keyword);
			},

			searchInputBlur() {

				this.$nextTick(() => {
					
					if (typeof this.$refs === 'object' && typeof this.$refs.searchInput === 'object') {
						this.$refs.searchInput.blur();
					}
				});
			},

			/**
			 * 搜索
			 * @param {string} keyword [搜索关键字]
			 * @param {object} e [事件对象]
			 */
			confirm(keyword, e) {

				// 判断按下的按键是不是回车(确认)
				if ((keyword && (e.keyCode === 13 || (typeof e.key === "string" && e.key.toLowerCase() === "enter"))) || (!keyword && (e.keyCode === 8 || (typeof e.key === "string" && e.key.toLowerCase() === "backspace")))) {

					// 判断不是空格字符串
					if (keyword.trim()) {
						this.$emit("confirm", keyword);
					} else {
						this.setKeyword('');
					}
				}
			},

			/**
			 * 清除input信息
			 */
			clearInput() {
				
				// 清除input内容
				this.$set(this.defaultSearchs, "keyword", "");

				// 设置清除input事件
				this.$emit("clearInput");
			},

			/**
			 * 取消按钮
			 */
			cancel() {
				this.$emit("cancel");
			},

			/**
			 * 失去焦点处理
			 */
			onBlur() {

				// 判断是否有onBlur函数
				if (typeof this.$parent.onBlur === "function") {
					this.$parent.onBlur();
				}
			}
		},
		directives: {
		    focus: {
		        inserted: (el, {value}) => {

					// 判断值是否为true
		            if (value) {
		                el.focus();
		            }
		        }
		    }
		},
		mounted() {

			// 合并参数对象
			Object.assign(this.defaultSearchs, this.searchs);

			// 延时执行
			// setTimeout(() => {
				
			// 	this.$nextTick(() => {
					
			// 		if (typeof this.$refs === 'object' && typeof this.$refs.searchInput === 'object') {



			// 			console.log(this.$refs);


			// 			this.$refs.searchInput.focus();
			// 		}
			// 	});
			// }, 1000);
		}
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less" scoped>
    @import './searchCom';
</style>