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.
247 lines
5.8 KiB
247 lines
5.8 KiB
4 years ago
|
<template>
|
||
|
<view class="uni-searchbar">
|
||
|
<view :style="{borderRadius:radius+'px',backgroundColor: bgColor}" class="uni-searchbar__box" @click="searchClick">
|
||
|
<view class="uni-searchbar__box-icon-search">
|
||
|
<slot name="searchIcon">
|
||
|
<uni-icons color="#999999" size="18" type="search" />
|
||
|
</slot>
|
||
|
</view>
|
||
|
<input v-if="show || searchVal" :focus="showSync" :placeholder="placeholder" :maxlength="maxlength" class="uni-searchbar__box-search-input" confirm-type="search" type="text" v-model="searchVal" @confirm="confirm" @blur="blur" @focus="emitFocus" />
|
||
|
<text v-else class="uni-searchbar__text-placeholder">{{ placeholder }}</text>
|
||
|
<view v-if="show && (clearButton==='always'||clearButton==='auto'&&searchVal!=='')" class="uni-searchbar__box-icon-clear" @click="clear">
|
||
|
<slot name="clearIcon">
|
||
|
<uni-icons color="#c0c4cc" size="15" type="clear" />
|
||
|
</slot>
|
||
|
</view>
|
||
|
</view>
|
||
|
<text @click="cancel" class="uni-searchbar__cancel" v-if="cancelButton ==='always' || show && cancelButton ==='auto'">{{cancelText}}</text>
|
||
|
</view>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
|
/**
|
||
|
* SearchBar 搜索栏
|
||
|
* @description 评分组件
|
||
|
* @tutorial https://ext.dcloud.net.cn/plugin?id=866
|
||
|
* @property {Number} radius 搜索栏圆角
|
||
|
* @property {Number} maxlength 输入最大长度
|
||
|
* @property {String} placeholder 搜索栏Placeholder
|
||
|
* @property {String} clearButton = [always|auto|none] 是否显示清除按钮
|
||
|
* @value always 一直显示
|
||
|
* @value auto 输入框不为空时显示
|
||
|
* @value none 一直不显示
|
||
|
* @property {String} cancelButton = [always|auto|none] 是否显示取消按钮
|
||
|
* @value always 一直显示
|
||
|
* @value auto 输入框不为空时显示
|
||
|
* @value none 一直不显示
|
||
|
* @property {String} cancelText 取消按钮的文字
|
||
|
* @property {String} bgColor 输入框背景颜色
|
||
|
* @property {Boolean} focus 是否自动聚焦
|
||
|
* @event {Function} confirm uniSearchBar 的输入框 confirm 事件,返回参数为uniSearchBar的value,e={value:Number}
|
||
|
* @event {Function} input uniSearchBar 的 value 改变时触发事件,返回参数为uniSearchBar的value,e=value
|
||
|
* @event {Function} cancel 点击取消按钮时触发事件,返回参数为uniSearchBar的value,e={value:Number}
|
||
|
* @event {Function} clear 点击清除按钮时触发事件,返回参数为uniSearchBar的value,e={value:Number}
|
||
|
* @event {Function} blur input失去焦点时触发事件,返回参数为uniSearchBar的value,e={value:Number}
|
||
|
*/
|
||
|
|
||
|
export default {
|
||
|
name: "UniSearchBar",
|
||
|
props: {
|
||
|
placeholder: {
|
||
|
type: String,
|
||
|
default: "请输入搜索内容"
|
||
|
},
|
||
|
radius: {
|
||
|
type: [Number, String],
|
||
|
default: 5
|
||
|
},
|
||
|
clearButton: {
|
||
|
type: String,
|
||
|
default: "auto"
|
||
|
},
|
||
|
cancelButton: {
|
||
|
type: String,
|
||
|
default: "auto"
|
||
|
},
|
||
|
cancelText: {
|
||
|
type: String,
|
||
|
default: '取消'
|
||
|
},
|
||
|
bgColor: {
|
||
|
type: String,
|
||
|
default: "#F8F8F8"
|
||
|
},
|
||
|
maxlength: {
|
||
|
type: [Number, String],
|
||
|
default: 100
|
||
|
},
|
||
|
value: {
|
||
|
type: [Number, String],
|
||
|
default: ""
|
||
|
},
|
||
|
focus: {
|
||
|
type: Boolean,
|
||
|
default: false
|
||
|
}
|
||
|
},
|
||
|
data() {
|
||
|
return {
|
||
|
show: false,
|
||
|
showSync: false,
|
||
|
searchVal: ''
|
||
|
}
|
||
|
},
|
||
|
watch: {
|
||
|
value: {
|
||
|
immediate: true,
|
||
|
handler(newVal) {
|
||
|
this.searchVal = newVal
|
||
|
if (newVal) {
|
||
|
this.show = true
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
focus: {
|
||
|
immediate: true,
|
||
|
handler(newVal) {
|
||
|
if (newVal) {
|
||
|
this.show = true;
|
||
|
this.$nextTick(() => {
|
||
|
this.showSync = true
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
searchVal(newVal, oldVal) {
|
||
|
this.$emit("input", newVal)
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
searchClick() {
|
||
|
if (this.show) {
|
||
|
return
|
||
|
}
|
||
|
this.show = true;
|
||
|
this.$nextTick(() => {
|
||
|
this.showSync = true
|
||
|
})
|
||
|
},
|
||
|
clear() {
|
||
|
this.$emit("clear", {
|
||
|
value: this.searchVal
|
||
|
})
|
||
|
this.searchVal = ""
|
||
|
},
|
||
|
cancel() {
|
||
|
this.$emit("cancel", {
|
||
|
value: this.searchVal
|
||
|
});
|
||
|
this.searchVal = ""
|
||
|
this.show = false
|
||
|
this.showSync = false
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.hideKeyboard()
|
||
|
// #endif
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.key.hideSoftKeybord()
|
||
|
// #endif
|
||
|
},
|
||
|
confirm() {
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.hideKeyboard();
|
||
|
// #endif
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.key.hideSoftKeybord()
|
||
|
// #endif
|
||
|
this.$emit("confirm", {
|
||
|
value: this.searchVal
|
||
|
})
|
||
|
},
|
||
|
blur() {
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.hideKeyboard();
|
||
|
// #endif
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.key.hideSoftKeybord()
|
||
|
// #endif
|
||
|
this.$emit("blur", {
|
||
|
value: this.searchVal
|
||
|
})
|
||
|
},
|
||
|
emitFocus(e) {
|
||
|
this.$emit("focus", e.detail)
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
</script>
|
||
|
|
||
|
<style scoped>
|
||
|
.uni-searchbar {
|
||
|
/* #ifndef APP-NVUE */
|
||
|
display: flex;
|
||
|
/* #endif */
|
||
|
flex-direction: row;
|
||
|
position: relative;
|
||
|
padding: 8px;
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__box {
|
||
|
/* #ifndef APP-NVUE */
|
||
|
display: flex;
|
||
|
box-sizing: border-box;
|
||
|
/* #endif */
|
||
|
overflow: hidden;
|
||
|
position: relative;
|
||
|
flex: 1;
|
||
|
justify-content: center;
|
||
|
flex-direction: row;
|
||
|
align-items: center;
|
||
|
height: 36px;
|
||
|
padding: 5px 8px 5px 0px;
|
||
|
border-width: 0.5px;
|
||
|
border-style: solid;
|
||
|
border-color: #e5e5e5;
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__box-icon-search {
|
||
|
/* #ifndef APP-NVUE */
|
||
|
display: flex;
|
||
|
/* #endif */
|
||
|
flex-direction: row;
|
||
|
padding: 0 8px;
|
||
|
justify-content: center;
|
||
|
align-items: center;
|
||
|
color: #808080;
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__box-search-input {
|
||
|
flex: 1;
|
||
|
font-size: 14px;
|
||
|
color: #333;
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__box-icon-clear {
|
||
|
align-items: center;
|
||
|
line-height: 24px;
|
||
|
padding-left: 8px;
|
||
|
/* #ifdef H5 */
|
||
|
cursor: pointer;
|
||
|
/* #endif */
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__text-placeholder {
|
||
|
font-size: 14px;
|
||
|
color: #808080;
|
||
|
margin-left: 5px;
|
||
|
}
|
||
|
|
||
|
.uni-searchbar__cancel {
|
||
|
padding-left: 10px;
|
||
|
line-height: 36px;
|
||
|
font-size: 14px;
|
||
|
color: #333;
|
||
|
/* #ifdef H5 */
|
||
|
cursor: pointer;
|
||
|
/* #endif */
|
||
|
}
|
||
|
</style>
|