123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <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="#c0c4cc" :size="fontSize+3" type="search" />
- </slot>
- </view>
- <input v-if="show || searchVal" :focus="showSync" :disabled="readonly" :placeholder="placeholderText" :maxlength="maxlength"
- class="uni-searchbar__box-search-input" confirm-type="search" type="text" v-model="searchVal" :style="{color:textColor,fontSize:fontSize+'px'}"
- @confirm="confirm" @blur="blur" @focus="emitFocus"/>
- <text v-else class="uni-searchbar__text-placeholder" :style="{fontSize:fontSize+'px'}">{{ placeholder }}</text>
- <view v-if="show && (clearButton==='always'||clearButton==='auto'&&searchVal!=='') &&!readonly"
- class="uni-searchbar__box-icon-clear" @click="clear">
- <slot name="clearIcon">
- <uni-icons color="#c0c4cc" size="20" type="clear" />
- </slot>
- </view>
- </view>
- <text @click="cancel" class="uni-searchbar__cancel"
- v-if="cancelButton ==='always' || show && cancelButton ==='auto'" :style="{fontSize:fontSize+'px'}">{{cancelTextI18n}}</text>
- </view>
- </template>
- <script>
- import {
- initVueI18n
- } from '@dcloudio/uni-i18n'
- import messages from './i18n/index.js'
- const {
- t
- } = initVueI18n(messages)
-
- export default {
- name: "UniSearchBar",
- emits: ['input', 'update:modelValue', 'clear', 'cancel', 'confirm', 'blur', 'focus'],
- 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"
- },
- textColor: {
- type: String,
- default: "#000000"
- },
- maxlength: {
- type: [Number, String],
- default: 100
- },
- value: {
- type: [Number, String],
- default: ""
- },
- modelValue: {
- type: [Number, String],
- default: ""
- },
- focus: {
- type: Boolean,
- default: false
- },
- readonly: {
- type: Boolean,
- default: false
- },
- fontSize:{
- type:Number,
- default: 15
- }
- },
- data() {
- return {
- show: false,
- showSync: false,
- searchVal: ''
- }
- },
- computed: {
- cancelTextI18n() {
- return this.cancelText || t("uni-search-bar.cancel")
- },
- placeholderText() {
- return this.placeholder || t("uni-search-bar.placeholder")
- }
- },
- watch: {
-
- value: {
- immediate: true,
- handler(newVal) {
- this.searchVal = newVal
- if (newVal) {
- this.show = true
- }
- }
- },
-
-
- modelValue: {
- immediate: true,
- handler(newVal) {
- this.searchVal = newVal
- if (newVal) {
- this.show = true
- }
- }
- },
-
- focus: {
- immediate: true,
- handler(newVal) {
- if (newVal) {
- if(this.readonly) return
- this.show = true;
- this.$nextTick(() => {
- this.showSync = true
- })
- }
- }
- },
- searchVal(newVal, oldVal) {
- this.$emit("input", newVal)
-
- this.$emit("update:modelValue", newVal)
-
- }
- },
- methods: {
- searchClick() {
- if(this.readonly) return
- if (this.show) {
- return
- }
- this.show = true;
- this.$nextTick(() => {
- this.showSync = true
- })
- },
- clear() {
- this.searchVal = ""
- this.$nextTick(() => {
- this.$emit("clear", { value: "" })
- })
- },
- cancel() {
- if(this.readonly) return
- this.$emit("cancel", {
- value: this.searchVal
- });
- this.searchVal = ""
- this.show = false
- this.showSync = false
-
- uni.hideKeyboard()
-
-
- plus.key.hideSoftKeybord()
-
- },
- confirm() {
-
- uni.hideKeyboard();
-
-
- plus.key.hideSoftKeybord()
-
- this.$emit("confirm", {
- value: this.searchVal
- })
- },
- blur() {
-
- uni.hideKeyboard();
-
-
- plus.key.hideSoftKeybord()
-
- this.$emit("blur", {
- value: this.searchVal
- })
- },
- emitFocus(e) {
- this.$emit("focus", e.detail)
- }
- }
- };
- </script>
- <style lang="scss">
- $uni-searchbar-height: 36px;
- .uni-searchbar {
-
- display: flex;
-
- flex-direction: row;
- position: relative;
- padding: 16px;
-
- }
- .uni-searchbar__box {
-
- display: flex;
- box-sizing: border-box;
- justify-content: left;
-
- overflow: hidden;
- position: relative;
- flex: 1;
- flex-direction: row;
- align-items: center;
- height: $uni-searchbar-height;
- padding: 5px 0px 5px 0px;
- }
- .uni-searchbar__box-icon-search {
-
- display: flex;
-
- flex-direction: row;
-
- padding: 0 8px;
- justify-content: center;
- align-items: center;
- color: #B3B3B3;
- }
- .uni-searchbar__box-search-input {
- flex: 1;
- font-size: 14px;
- color: #333;
- margin-left: 5px;
- margin-top: 1px;
-
- background-color: inherit;
-
- }
- .uni-searchbar__box-icon-clear {
- align-items: center;
- line-height: 24px;
- padding-left: 8px;
-
- cursor: pointer;
-
- }
- .uni-searchbar__text-placeholder {
- font-size: 14px;
- color: #999;
- margin-left: 5px;
- text-align: left;
- }
- .uni-searchbar__cancel {
- padding-left: 10px;
- line-height: $uni-searchbar-height;
- font-size: 14px;
- color: #333333;
-
- cursor: pointer;
-
- }
- </style>
|