uni-icons.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <text :style="styleObj" class="uni-icons" @click="_onClick">{{unicode}}</text>
  3. </template>
  4. <script>
  5. import { fontData } from './uniicons_file_vue.js';
  6. const getVal = (val) => {
  7. const reg = /^[0-9]*$/g
  8. return (typeof val === 'number' || reg.test(val)) ? val + 'px' : val;
  9. }
  10. /**
  11. * Icons 图标
  12. * @description 用于展示 icons 图标
  13. * @tutorial https://ext.dcloud.net.cn/plugin?id=28
  14. * @property {Number} size 图标大小
  15. * @property {String} type 图标图案,参考示例
  16. * @property {String} color 图标颜色
  17. * @property {String} customPrefix 自定义图标
  18. * @event {Function} click 点击 Icon 触发事件
  19. */
  20. export default {
  21. name: 'UniIcons',
  22. emits: ['click'],
  23. props: {
  24. type: {
  25. type: String,
  26. default: ''
  27. },
  28. color: {
  29. type: String,
  30. default: '#333333'
  31. },
  32. size: {
  33. type: [Number, String],
  34. default: 16
  35. },
  36. customPrefix: {
  37. type: String,
  38. default: ''
  39. },
  40. fontFamily: {
  41. type: String,
  42. default: ''
  43. }
  44. },
  45. data() {
  46. return {
  47. icons: fontData
  48. }
  49. },
  50. computed: {
  51. unicode() {
  52. let code = this.icons.find(v => v.font_class === this.type)
  53. if (code) {
  54. return code.unicode
  55. }
  56. return ''
  57. },
  58. iconSize() {
  59. return getVal(this.size)
  60. },
  61. styleObj() {
  62. if (this.fontFamily !== '') {
  63. return `color: ${this.color}; font-size: ${this.iconSize}; font-family: ${this.fontFamily};`
  64. }
  65. return `color: ${this.color}; font-size: ${this.iconSize};`
  66. }
  67. },
  68. methods: {
  69. _onClick(e) {
  70. this.$emit('click', e)
  71. }
  72. }
  73. }
  74. </script>
  75. <style lang="scss">
  76. /* #ifndef APP-NVUE */
  77. @import './uniicons.css';
  78. @font-face {
  79. font-family: uniicons;
  80. src: url('./uniicons.ttf');
  81. }
  82. /* #endif */
  83. .uni-icons {
  84. font-family: uniicons;
  85. text-decoration: none;
  86. text-align: center;
  87. }
  88. </style>