dykj-outsource-12123/uni_modules/uview-plus/components/u-search/u-search.vue

332 lines
10 KiB
Vue
Raw Normal View History

2024-06-28 14:18:30 +08:00
<template>
<view
class="u-search"
@tap="clickHandler"
:style="[{
margin: margin,
}, addStyle(customStyle)]"
>
<view
class="u-search__content"
:style="{
backgroundColor: bgColor,
borderRadius: shape == 'round' ? '100px' : '4px',
borderColor: borderColor,
}"
>
<template v-if="$slots.label || label !== null">
<slot name="label">
<text class="u-search__content__label">{{ label }}</text>
</slot>
</template>
<view class="u-search__content__icon">
<u-icon
@tap="clickIcon"
:size="searchIconSize"
:name="searchIcon"
:color="searchIconColor ? searchIconColor : color"
></u-icon>
</view>
<input
confirm-type="search"
@blur="blur"
:value="keyword"
@confirm="search"
@input="inputChange"
:disabled="disabled"
@focus="getFocus"
:focus="focus"
:maxlength="maxlength"
:adjust-position="adjustPosition"
:auto-blur="autoBlur"
placeholder-class="u-search__content__input--placeholder"
:placeholder="placeholder"
:placeholder-style="`color: ${placeholderColor}`"
class="u-search__content__input"
type="text"
:style="[{
textAlign: inputAlign,
color: color,
backgroundColor: bgColor,
height: addUnit(height)
}, inputStyle]"
/>
<view
class="u-search__content__icon u-search__content__close"
v-if="keyword && clearabled && focused"
@click="clear"
>
<u-icon
name="close"
size="11"
color="#ffffff"
customStyle="line-height: 12px"
></u-icon>
</view>
</view>
<text
:style="[actionStyle]"
class="u-search__action"
:class="[(showActionBtn || show) && 'u-search__action--active']"
@tap.stop.prevent="custom"
>{{ actionText }}</text>
</view>
</template>
<script>
import props from './props';
import mpMixin from '../../libs/mixin/mpMixin';
import mixin from '../../libs/mixin/mixin';
import { addUnit, addStyle } from '../../libs/function/index';
/**
* search 搜索框
* @description 搜索组件集成了常见搜索框所需功能用户可以一键引入开箱即用
* @tutorial https://ijry.github.io/uview-plus/components/search.html
* @property {String} shape 搜索框形状round-圆形square-方形默认 'round'
* @property {String} bgColor 搜索框背景颜色默认 '#f2f2f2'
* @property {String} placeholder 占位文字内容默认 '请输入关键字'
* @property {Boolean} clearabled 是否启用清除控件默认 true
* @property {Boolean} focus 是否自动获得焦点默认 false
* @property {Boolean} showAction 是否显示右侧控件默认 true
* @property {Object} actionStyle 右侧控件的样式对象形式
* @property {String} actionText 右侧控件文字默认 '搜索'
* @property {String} inputAlign 输入框内容水平对齐方式 默认 'left'
* @property {Object} inputStyle 自定义输入框样式对象形式
* @property {Boolean} disabled 是否启用输入框默认 false
* @property {String} borderColor 边框颜色配置了颜色才会有边框 (默认 'transparent' )
* @property {String} searchIconColor 搜索图标的颜色默认同输入框字体颜色 (默认 '#909399' )
* @property {Number | String} searchIconSize 搜索图标的字体默认22
* @property {String} color 输入框字体颜色默认 '#606266'
* @property {String} placeholderColor placeholder的颜色默认 '#909399'
* @property {String} searchIcon 输入框左边的图标可以为uView图标名称或图片路径 (默认 'search' )
* @property {String} margin 组件与其他上下左右元素之间的距离带单位的字符串形式"30px" (默认 '0' )
* @property {Boolean} animation 是否开启动画见上方说明默认 false
* @property {String} value 输入框初始值
* @property {String | Number} maxlength 输入框最大能输入的长度-1为不限制长度 (默认 '-1' )
* @property {String | Number} height 输入框高度单位px默认 64
* @property {String | Number} label 搜索框左边显示内容
* @property {Boolean} adjustPosition 键盘弹起时是否自动上推页面
* @property {Boolean} autoBlur 键盘收起时是否自动失去焦点
* @property {Object} customStyle 定义需要用到的外部样式
*
* @event {Function} change 输入框内容发生变化时触发
* @event {Function} search 用户确定搜索时触发用户按回车键或者手机键盘右下角的"搜索"键时触发
* @event {Function} custom 用户点击右侧控件时触发
* @event {Function} clear 用户点击清除按钮时触发
* @example <u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
*/
export default {
name: "u-search",
mixins: [mpMixin, mixin, props],
data() {
return {
keyword: '',
showClear: false, // 是否显示右边的清除图标
show: false,
// 标记input当前状态是否处于聚焦中如果是才会显示右侧的清除控件
focused: this.focus
// 绑定输入框的值
// inputValue: this.value
};
},
watch: {
keyword(nVal) {
// 双向绑定值让v-model绑定的值双向变化
// #ifdef VUE3
this.$emit("update:modelValue", nVal);
// #endif
// #ifdef VUE2
this.$emit('input', nVal);
// #endif
// 触发change事件事件效果和v-model双向绑定的效果一样让用户多一个选择
this.$emit('change', nVal);
},
// #ifdef VUE3
modelValue: {
immediate: true,
handler(nVal) {
this.keyword = nVal;
}
},
// #endif
// #ifdef VUE2
value: {
immediate: true,
handler(nVal) {
this.keyword = nVal;
}
},
// #endif
},
computed: {
showActionBtn() {
return !this.animation && this.showAction
}
},
emits: ['clear', 'search', 'custom', 'focus', 'blur', 'click', 'clickIcon', 'update:modelValue', 'change'],
methods: {
addStyle,
addUnit,
// 目前HX2.6.9 v-model双向绑定无效故监听input事件获取输入框内容的变化
inputChange(e) {
this.keyword = e.detail.value;
},
// 清空输入
// 也可以作为用户通过this.$refs形式调用清空输入框内容
clear() {
this.keyword = '';
// 延后发出事件避免在父组件监听clear事件时value为更新前的值(不为空)
this.$nextTick(() => {
this.$emit('clear');
})
},
// 确定搜索
search(e) {
this.$emit('search', e.detail.value);
try {
// 收起键盘
uni.hideKeyboard();
} catch (e) {}
},
// 点击右边自定义按钮的事件
custom() {
this.$emit('custom', this.keyword);
try {
// 收起键盘
uni.hideKeyboard();
} catch (e) {}
},
// 获取焦点
getFocus() {
this.focused = true;
// 开启右侧搜索按钮展开的动画效果
if (this.animation && this.showAction) this.show = true;
this.$emit('focus', this.keyword);
},
// 失去焦点
blur() {
// 最开始使用的是监听图标@touchstart事件自从hx2.8.4后,此方法在微信小程序出错
// 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件导致图标消失而无法点击这里做一个延时
setTimeout(() => {
this.focused = false;
}, 100)
this.show = false;
this.$emit('blur', this.keyword);
},
// 点击搜索框只有disabled=true时才发出事件因为禁止了输入意味着是想跳转真正的搜索页
clickHandler() {
if (this.disabled) this.$emit('click');
},
// 点击左边图标
clickIcon(e) {
this.$emit('clickIcon', this.keyword);
try {
// 收起键盘
uni.hideKeyboard();
} catch (e) {}
}
}
}
</script>
<style lang="scss" scoped>
@import "../../libs/css/components.scss";
$u-search-content-padding: 0 10px !default;
$u-search-label-color: $u-main-color !default;
$u-search-label-font-size: 14px !default;
$u-search-label-margin: 0 4px !default;
$u-search-close-size: 20px !default;
$u-search-close-radius: 100px !default;
$u-search-close-bgColor: #C6C7CB !default;
$u-search-close-transform: scale(0.82) !default;
$u-search-input-font-size: 14px !default;
$u-search-input-margin: 0 5px !default;
$u-search-input-color: $u-main-color !default;
$u-search-input-placeholder-color: $u-tips-color !default;
$u-search-action-font-size: 14px !default;
$u-search-action-color: $u-main-color !default;
$u-search-action-width: 0 !default;
$u-search-action-active-width: 40px !default;
$u-search-action-margin-left: 5px !default;
/* #ifdef H5 */
// iOS15在H5下hx的某些版本input type=search时会多了一个搜索图标进行移除
[type="search"]::-webkit-search-decoration {
display: none;
}
/* #endif */
.u-search {
@include flex(row);
align-items: center;
flex: 1;
&__content {
@include flex;
align-items: center;
padding: $u-search-content-padding;
flex: 1;
justify-content: space-between;
border-width: 1px;
border-color: transparent;
border-style: solid;
overflow: hidden;
&__icon {
@include flex;
align-items: center;
}
&__label {
color: $u-search-label-color;
font-size: $u-search-label-font-size;
margin: $u-search-label-margin;
}
&__close {
width: $u-search-close-size;
height: $u-search-close-size;
border-top-left-radius: $u-search-close-radius;
border-top-right-radius: $u-search-close-radius;
border-bottom-left-radius: $u-search-close-radius;
border-bottom-right-radius: $u-search-close-radius;
background-color: $u-search-close-bgColor;
@include flex(row);
align-items: center;
justify-content: center;
transform: $u-search-close-transform;
}
&__input {
flex: 1;
font-size: $u-search-input-font-size;
line-height: 1;
margin: $u-search-input-margin;
color: $u-search-input-color;
&--placeholder {
color: $u-search-input-placeholder-color;
}
}
}
&__action {
font-size: $u-search-action-font-size;
color: $u-search-action-color;
width: $u-search-action-width;
overflow: hidden;
transition-property: width;
transition-duration: 0.3s;
/* #ifndef APP-NVUE */
white-space: nowrap;
/* #endif */
text-align: center;
&--active {
width: $u-search-action-active-width;
margin-left: $u-search-action-margin-left;
}
}
}
</style>