index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <span>
  3. <el-popover
  4. ref="pop"
  5. v-model="pop"
  6. :placement="placement"
  7. width="300"
  8. trigger="click"
  9. >
  10. <div
  11. v-if="clearable"
  12. class="ibps-clearfix ibps-mb-10"
  13. >
  14. <el-button
  15. type="info"
  16. icon="el-icon-delete"
  17. size="mini"
  18. class="ibps-fr"
  19. @click="selectIcon()"
  20. >
  21. 清空
  22. </el-button>
  23. </div>
  24. <el-input
  25. v-model="searchText"
  26. :clearable="true"
  27. placeholder="搜索 比如 'plus'"
  28. prefix-icon="el-icon-search"
  29. />
  30. <el-row class="ibps-icon-svg-select--group">
  31. <el-col
  32. v-for="(item, index) in iconFilted"
  33. :key="index"
  34. :span="4"
  35. class="ibps-icon-svg-select--item"
  36. @click.native="selectIcon(item)"
  37. >
  38. <ibps-icon-svg :name="item" />
  39. </el-col>
  40. </el-row>
  41. </el-popover>
  42. <!-- 允许用户输入 -->
  43. <el-input
  44. v-if="userInput"
  45. v-model="currentValue"
  46. v-bind="bind"
  47. style="max-width: 240px;"
  48. >
  49. <template v-if="value" slot="prepend">
  50. <ibps-icon-svg
  51. :name="value"
  52. class="ibps-icon-svg-select--input-preview"
  53. />
  54. </template>
  55. <el-button slot="append" v-popover:pop>
  56. <i class="fa fa-list" />
  57. </el-button>
  58. </el-input>
  59. <!-- 不允许用户输入 -->
  60. <el-button v-if="!userInput" v-popover:pop>
  61. <span flex="dir:left main:center cross:center">
  62. <ibps-icon-svg
  63. v-if="value"
  64. :name="value"
  65. class="ibps-icon-svg-select--input-preview ibps-mr-10"
  66. />
  67. <span>{{ value ? value : placeholder }}</span>
  68. </span>
  69. </el-button>
  70. </span>
  71. </template>
  72. <script>
  73. export default {
  74. name: 'ibps-icon-svg-select',
  75. props: {
  76. // 值
  77. value: {
  78. type: String,
  79. required: false,
  80. default: ''
  81. },
  82. // 占位符
  83. placeholder: {
  84. type: String,
  85. required: false,
  86. default: '请选择'
  87. },
  88. // 弹出界面的方向
  89. placement: {
  90. type: String,
  91. required: false,
  92. default: 'right'
  93. },
  94. // 是否可清空
  95. clearable: {
  96. type: Boolean,
  97. required: false,
  98. default: true
  99. },
  100. // 是否允许用户输入
  101. userInput: {
  102. type: Boolean,
  103. required: false,
  104. default: false
  105. },
  106. // 是否在选择后自动关闭
  107. autoClose: {
  108. type: Boolean,
  109. required: false,
  110. default: true
  111. }
  112. },
  113. data() {
  114. return {
  115. // 绑定弹出框
  116. pop: false,
  117. // 组件内输入框的值
  118. currentValue: '',
  119. // 搜索的文字
  120. searchText: ''
  121. }
  122. },
  123. computed: {
  124. // 输入框上绑定的设置
  125. bind() {
  126. return {
  127. placeholder: this.placeholder,
  128. clearable: this.clearable,
  129. ...this.$attrs
  130. }
  131. },
  132. // 是否在搜索
  133. searchMode() {
  134. return !!this.searchText
  135. },
  136. // 过滤后的图标
  137. iconFilted() {
  138. return this.$IconSvg.filter(icon => icon.indexOf(this.searchText) >= 0)
  139. }
  140. },
  141. watch: {
  142. value(value) {
  143. this.currentValue = value
  144. }
  145. },
  146. created() {
  147. this.currentValue = this.value
  148. },
  149. methods: {
  150. selectIcon(iconName = '') {
  151. this.$emit('input', iconName)
  152. if (iconName && this.autoClose) {
  153. this.pop = false
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. @import '~@/assets/styles/public.scss';
  161. .ibps-icon-svg-select--input-preview {
  162. height: 14px;
  163. width: 14px;
  164. display: block;
  165. }
  166. .ibps-icon-svg-select--group {
  167. max-height: 400px;
  168. overflow-x: hidden;
  169. overflow-y: scroll;
  170. margin-top: 10px;
  171. }
  172. .ibps-icon-svg-select--item {
  173. height: 40px;
  174. display: flex;
  175. justify-content: center;
  176. align-items: center;
  177. svg {
  178. height: 20px;
  179. width: 20px;
  180. }
  181. &:hover {
  182. background-color: $color-bg;
  183. border-radius: 4px;
  184. box-shadow: inset 0px 0px 0px 1px $color-border-1;
  185. }
  186. }
  187. </style>