Commit 75ec1e33 authored by 王博文's avatar 王博文

修复右侧文字提示不生效

parent 6b9cfd96
<template> <template>
<view class="u-search"> <view class="u-search">
<view class="u-content" :style="{ <view
class="u-content"
:style="{
backgroundColor: bgColor, backgroundColor: bgColor,
borderRadius: shape == 'round' ? '100rpx' : '10rpx', borderRadius: shape == 'round' ? '100rpx' : '10rpx',
border: borderStyle, border: borderStyle,
height: height + 'rpx' height: height + 'rpx'
}"> }"
<view class="u-icon-wrap"> >
<u-icon class="u-clear-icon" :size="30" name="search" color="#909399"></u-icon> <view class="u-icon-wrap">
</view> <u-icon class="u-clear-icon" :size="30" name="search" color="#909399"></u-icon>
<input confirm-type="search" @blur="blur" :value="value" @confirm="search" @input="inputChange" :disabled="disabled" @focus="getFocus" :focus="focus" </view>
placeholder-class="u-placeholder-class" :placeholder="placeholder" class="u-input" type="text" :style="{ <input
confirm-type="search"
@blur="blur"
:value="value"
@confirm="search"
@input="inputChange"
:disabled="disabled"
@focus="getFocus"
:focus="focus"
placeholder-class="u-placeholder-class"
:placeholder="placeholder"
class="u-input"
type="text"
:style="{
textAlign: inputAlign textAlign: inputAlign
}" /> }"
<view class="u-close-wrap" v-if="keyword && clearabled && focused" @touchstart="clear"> />
<u-icon class="u-clear-icon" name="close" :size="16" color="#fff" @touchstart="clear"></u-icon> <view class="u-close-wrap" v-if="keyword && clearabled && focused" @touchstart="clear">
</view> <u-icon class="u-clear-icon" name="close" :size="16" color="#fff" @touchstart="clear"></u-icon>
</view> </view>
<view :style="[actionStyle]" class="u-action" :class="[showActionBtn || show ? 'u-action-active' : '']" @tap="custom"> </view>
{{actiontext}} <view
</view> :style="[actionStyle]"
</view> class="u-action"
:class="[showActionBtn || show ? 'u-action-active' : '']"
@tap="custom"
>{{actionText}}</view>
</view>
</template> </template>
<script> <script>
export default { export default {
props: { props: {
// 搜索框形状,round-圆形,square-方形 // 搜索框形状,round-圆形,square-方形
shape: { shape: {
type: String, type: String,
default: 'round' default: "round"
}, },
// 搜索框背景色,默认值#f2f2f2 // 搜索框背景色,默认值#f2f2f2
bgColor: { bgColor: {
type: String, type: String,
default: '#f2f2f2' default: "#f2f2f2"
}, },
// 占位提示文字 // 占位提示文字
placeholder: { placeholder: {
type: String, type: String,
default: '请输入关键字' default: "请输入关键字"
}, },
// 是否启用清除控件 // 是否启用清除控件
clearabled: { clearabled: {
type: Boolean, type: Boolean,
default: true default: true
}, },
// 是否自动聚焦 // 是否自动聚焦
focus: { focus: {
type: Boolean, type: Boolean,
default: false default: false
}, },
// 是否在搜索框右侧显示取消按钮 // 是否在搜索框右侧显示取消按钮
showAction: { showAction: {
type: Boolean, type: Boolean,
default: true default: true
}, },
// 右边控件的样式 // 右边控件的样式
actionStyle: { actionStyle: {
type: Object, type: Object,
default() { default() {
return {} return {};
} }
}, },
// 取消按钮文字 // 取消按钮文字
actiontext: { actionText: {
type: String, type: String,
default: '搜索' default: "搜索"
}, },
// 输入框内容对齐方式,可选值为 left|center|right // 输入框内容对齐方式,可选值为 left|center|right
inputAlign: { inputAlign: {
type: String, type: String,
default: 'left' default: "left"
}, },
// 是否启用输入框 // 是否启用输入框
disabled: { disabled: {
type: Boolean, type: Boolean,
default: false default: false
}, },
// 开启showAction时,是否在input获取焦点时才显示 // 开启showAction时,是否在input获取焦点时才显示
animation: { animation: {
type: Boolean, type: Boolean,
default: false default: false
}, },
// 边框颜色,只要配置了颜色,才会有边框 // 边框颜色,只要配置了颜色,才会有边框
borderColor: { borderColor: {
type: String, type: String,
default: 'none' default: "none"
}, },
// 输入框的初始化内容 // 输入框的初始化内容
value: { value: {
type: String, type: String,
default: '' default: ""
}, },
// 搜索框高度,单位rpx // 搜索框高度,单位rpx
height: { height: {
type: [Number, String], type: [Number, String],
default: 64 default: 64
} }
}, },
data() { data() {
return { return {
keyword: '', keyword: "",
showClear: false, // 是否显示右边的清除图标 showClear: false, // 是否显示右边的清除图标
show: false, show: false,
// 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件 // 标记input当前状态是否处于聚焦中,如果是,才会显示右侧的清除控件
focused: this.focus, focused: this.focus
// 绑定输入框的值 // 绑定输入框的值
// inputValue: this.value // inputValue: this.value
} };
}, },
watch: { watch: {
keyword(nVal) { keyword(nVal) {
// 双向绑定值,让v-model绑定的值双向变化 // 双向绑定值,让v-model绑定的值双向变化
this.$emit('input', nVal) this.$emit("input", nVal);
// 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择 // 触发change事件,事件效果和v-model双向绑定的效果一样,让用户多一个选择
this.$emit('change', nVal) this.$emit("change", nVal);
}, },
value: { value: {
immediate: true, immediate: true,
handler(nVal) { handler(nVal) {
this.keyword = nVal; this.keyword = nVal;
} }
} }
}, },
computed: { computed: {
showActionBtn() { showActionBtn() {
if (!this.animation && this.showAction) return true; if (!this.animation && this.showAction) return true;
else return false; else return false;
}, },
// 样式,根据用户传入的颜色值生成,如果不传入,默认为none // 样式,根据用户传入的颜色值生成,如果不传入,默认为none
borderStyle() { borderStyle() {
if(this.borderColor) return `1px solid ${this.borderColor}`; if (this.borderColor) return `1px solid ${this.borderColor}`;
else return 'none'; else return "none";
} }
}, },
methods: { methods: {
// 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化 // 目前HX2.6.9 v-model双向绑定无效,故监听input事件获取输入框内容的变化
inputChange(e) { inputChange(e) {
this.keyword = e.detail.value; this.keyword = e.detail.value;
}, },
// 清空输入 // 清空输入
// 也可以作为用户通过this.$refs形式调用清空输入框内容 // 也可以作为用户通过this.$refs形式调用清空输入框内容
clear() { clear() {
this.keyword = ''; this.keyword = "";
}, },
// 确定搜索 // 确定搜索
search() { search() {
this.$emit('search', this.keyword); this.$emit("search", this.keyword);
// 收起键盘 // 收起键盘
uni.hideKeyboard(); uni.hideKeyboard();
}, },
// 点击右边自定义按钮的事件 // 点击右边自定义按钮的事件
custom() { custom() {
this.$emit('custom', this.keyword); this.$emit("custom", this.keyword);
// 收起键盘 // 收起键盘
uni.hideKeyboard(); uni.hideKeyboard();
}, },
// 获取焦点 // 获取焦点
getFocus() { getFocus() {
this.focused = true; this.focused = true;
// 开启右侧搜索按钮展开的动画效果 // 开启右侧搜索按钮展开的动画效果
if (this.animation && this.showAction) this.show = true; if (this.animation && this.showAction) this.show = true;
}, },
// 失去焦点 // 失去焦点
blur() { blur() {
this.focused = false; this.focused = false;
this.show = false; this.show = false;
} }
} }
} };
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.u-search { .u-search {
display: flex; display: flex;
align-items: center; align-items: center;
flex: 1; flex: 1;
} }
.u-content { .u-content {
display: flex; display: flex;
align-items: center; align-items: center;
padding: 0 18rpx; padding: 0 18rpx;
flex: 1; flex: 1;
} }
.u-clear-icon {
display: flex;
align-items: center;
}
.u-input { .u-clear-icon {
flex: 1; display: flex;
font-size: 28rpx; align-items: center;
line-height: 1; }
margin: 0 10rpx;
color: $u-tips-color;
}
.u-close-wrap { .u-input {
width: 34rpx; flex: 1;
height: 34rpx; font-size: 28rpx;
display: flex; line-height: 1;
align-items: center; margin: 0 10rpx;
justify-content: center; color: $u-tips-color;
background-color: rgb(200, 203, 204); }
border-radius: 50%;
}
.u-placeholder-class { .u-close-wrap {
color: $u-tips-color; width: 34rpx;
} height: 34rpx;
display: flex;
align-items: center;
justify-content: center;
background-color: rgb(200, 203, 204);
border-radius: 50%;
}
.u-action { .u-placeholder-class {
font-size: 28rpx; color: $u-tips-color;
color: $u-main-color; }
width: 0;
overflow: hidden; .u-action {
transition: all 0.3s; font-size: 28rpx;
white-space: nowrap; color: $u-main-color;
text-align: center; width: 0;
} overflow: hidden;
transition: all 0.3s;
.u-action-active { white-space: nowrap;
width: 80rpx; text-align: center;
margin-left: 10rpx; }
}
.u-action-active {
width: 80rpx;
margin-left: 10rpx;
}
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment