陶杰
2024-08-22 4aa0bd47801606b4d0e0a6a2ed8fe92a7e5f2444
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<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="18" 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"
                @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!=='') &&!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'">{{cancelTextI18n}}</text>
    </view>
</template>
 
<script>
    import {
        initVueI18n
    } from '@dcloudio/uni-i18n'
    import messages from './i18n/index.js'
    const {
        t
    } = initVueI18n(messages)
 
    /**
     * 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 是否自动聚焦
     * @property {Boolean} readonly 组件只读,不能有任何操作,只做展示
     * @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",
        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"
            },
            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
            }
        },
        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: {
            // #ifndef VUE3
            value: {
                immediate: true,
                handler(newVal) {
                    this.searchVal = newVal
                    if (newVal) {
                        this.show = true
                    }
                }
            },
            // #endif
            // #ifdef VUE3
            modelValue: {
                immediate: true,
                handler(newVal) {
                    this.searchVal = newVal
                    if (newVal) {
                        this.show = true
                    }
                }
            },
            // #endif
            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)
                // #ifdef VUE3
                this.$emit("update:modelValue", newVal)
                // #endif
            }
        },
        methods: {
            searchClick() {
                if(this.readonly) return
                if (this.show) {
                    return
                }
                this.show = true;
                this.$nextTick(() => {
                    this.showSync = true
                })
            },
            clear() {
                this.$emit("clear", {
                    value: this.searchVal
                })
                this.searchVal = ""
            },
            cancel() {
                if(this.readonly) return
                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 lang="scss">
    $uni-searchbar-height: 36px;
 
    .uni-searchbar {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        position: relative;
        padding: 10px;
        // background-color: #fff;
    }
 
    .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: $uni-searchbar-height;
        padding: 5px 8px 5px 0px;
    }
 
    .uni-searchbar__box-icon-search {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        // width: 32px;
        padding: 0 8px;
        justify-content: center;
        align-items: center;
        color: #B3B3B3;
    }
 
    .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: #B3B3B3;
        margin-left: 5px;
    }
 
    .uni-searchbar__cancel {
        padding-left: 10px;
        line-height: $uni-searchbar-height;
        font-size: 14px;
        color: #333333;
        /* #ifdef H5 */
        cursor: pointer;
        /* #endif */
    }
</style>