UNPKG

3.28 kBPlain TextView Raw
1<template>
2 <div class="mint-search">
3 <div class="mint-searchbar">
4 <div class="mint-searchbar-inner">
5 <i class="mintui mintui-search"></i>
6 <input
7 ref="input"
8 @click="visible = true"
9 type="search"
10 v-model="currentValue"
11 :placeholder="placeholder"
12 class="mint-searchbar-core">
13 </div>
14 <a
15 class="mint-searchbar-cancel"
16 @click="visible = false, currentValue = ''"
17 v-show="visible"
18 v-text="cancelText">
19 </a>
20 </div>
21 <div class="mint-search-list" v-show="show || currentValue">
22 <div class="mint-search-list-warp">
23 <slot>
24 <x-cell v-for="(item, index) in result" :key="index" :title="item"></x-cell>
25 </slot>
26 </div>
27 </div>
28 </div>
29</template>
30
31<script>
32import XCell from 'mint-ui/packages/cell/index.js';
33if (process.env.NODE_ENV === 'component') {
34 require('mint-ui/packages/cell/style.css');
35}
36
37/**
38 * mt-search
39 * @module components/search
40 * @desc 搜索框
41 * @param {string} value - 绑定值
42 * @param {string} [cancel-text=取消] - 取消按钮文字
43 * @param {string} [placeholder=取消] - 搜索框占位内容
44 * @param {boolean} [autofocus=false] - 自动 focus
45 * @param {boolean} [show=false] - 始终显示列表
46 * @param {string[]} [result] - 结果列表
47 * @param {slot} 结果列表
48 *
49 * @example
50 * <mt-search :value.sync="value" :result.sync="result"></mt-search>
51 * <mt-search :value.sync="value">
52 * <mt-cell v-for="item in result" :title="item"></mt-cell>
53 * </mt-search>
54 */
55export default {
56 name: 'mt-search',
57
58 data() {
59 return {
60 visible: false,
61 currentValue: this.value
62 };
63 },
64
65 components: { XCell },
66
67 watch: {
68 currentValue(val) {
69 this.$emit('input', val);
70 },
71
72 value(val) {
73 this.currentValue = val;
74 }
75 },
76
77 props: {
78 value: String,
79 autofocus: Boolean,
80 show: Boolean,
81 cancelText: {
82 default: '取消'
83 },
84 placeholder: {
85 default: '搜索'
86 },
87 result: Array
88 },
89
90 mounted() {
91 this.autofocus && this.$refs.input.focus();
92 }
93};
94</script>
95
96<style lang="css">
97 @import "../../../src/style/var.css";
98
99 @component-namespace mint {
100 @component search {
101 height: 100%;
102 height: 100vh;
103 overflow: hidden;
104 }
105
106 @component searchbar {
107 position: relative;
108 align-items: center;
109 background-color: $color-grey;
110 box-sizing: border-box;
111 display: flex;
112 padding: 8px 10px;
113 z-index: 1;
114
115 @descendent inner {
116 align-items: center;
117 background-color: $color-white;
118 border-radius: 2px;
119 display: flex;
120 flex: 1;
121 height: 28px;
122 padding: 4px 6px;
123
124 .mintui-search {
125 font-size: 12px;
126 color: $color-grey;
127 }
128 }
129
130 @descendent core {
131 appearance: none;
132 border: 0;
133 box-sizing: border-box;
134 width: 100%;
135 height: 100%;
136 outline: 0;
137 }
138
139 @descendent cancel {
140 color: $color-blue;
141 margin-left: 10px;
142 text-decoration: none;
143 }
144 }
145
146 @component search-list {
147 overflow: auto;
148 padding-top: 44px;
149 position: absolute 0 0 0 0;
150 }
151 }
152</style>