index.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script >
  2. import LinearText from '../LinearText';
  3. export default {
  4. name: 'ButtonBlock',
  5. components: {
  6. LinearText
  7. },
  8. props: {
  9. items: {
  10. type: Array,
  11. default: () => [
  12. "本日",
  13. "本周",
  14. "本月"
  15. ]
  16. },
  17. valueField: {
  18. type: String,
  19. default: ""
  20. },
  21. defaultIndex: {
  22. type: Number,
  23. default: 0
  24. }
  25. },
  26. data() {
  27. return {
  28. currentItemIndex: 0,
  29. }
  30. },
  31. emits: ["select-item"],
  32. created() {
  33. this.currentItemIndex = this.defaultIndex;
  34. },
  35. methods: {
  36. /**
  37. * 渲染当前节点的颜色
  38. * @param {*} index 当前选项索引
  39. * @param {*} color 选择时的颜色
  40. */
  41. renderCurrentItemColor(index, color) {
  42. return this.currentItemIndex === index ? color : `${color}88`
  43. },
  44. /**
  45. * 渲染文本内容
  46. * @param {*} item 节点数据
  47. */
  48. renderText(item) {
  49. if (this.valueField === '') {
  50. return item
  51. } else {
  52. return item[this.valueField]
  53. }
  54. },
  55. /**
  56. * 点击节点处理器
  57. * @param {*} index 当前点击的选项索引
  58. */
  59. onClickBlockItemHandler(index) {
  60. this.currentItemIndex = index
  61. this.$emit('select-item', index)
  62. }
  63. }
  64. }
  65. </script>
  66. <template >
  67. <div class="button-block">
  68. <div class="button-block_item" v-for="(item,index) in items" :key="index">
  69. <linear-text :text="renderText(item)"
  70. :startColor="renderCurrentItemColor(index, '#95CCFF')"
  71. :endColor="renderCurrentItemColor(index, '#FFFFFF')"
  72. fontSize="16px" :ext-class="{
  73. textShadow: `0 1px 1px ${renderCurrentItemColor(index, '#0057FF')}`
  74. }" @click="onClickBlockItemHandler(index)" />
  75. </div>
  76. </div>
  77. </template>
  78. <style scoped lang='less'>
  79. .button-block {
  80. display: flex;
  81. border-radius: 5px;
  82. box-sizing: border-box;
  83. background: linear-gradient(
  84. 0deg,
  85. #0094FF90 0%,
  86. #0094FF10 100%
  87. );
  88. .button-block_item {
  89. padding: 0px 8.5px 0px 8.5px;
  90. cursor: pointer;
  91. }
  92. }
  93. </style>