Vueのボタンコンポーネントを作成する

デザイン

BULMAのスタイルを使用したボタンコンポーネントを作成します。
BULMAのスタイルをプロパティで指定できるようにし、ボタンクリックをイベント登録します。

  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
<template>
  <button
      type="button"
      class="button"
      title="buttonName"
      value="buttonName"
      :class="{
      'is-white': isWhite,
      'is-light': isLight,
      'is-dark': isDark,
      'is-black': isBlack,
      'is-text': isText,
      'is-ghost': isGhost,
      'is-primary': isPrimary,
      'is-link': isLink,
      'is-info': isInfo,
      'is-success': isSuccess,
      'is-warning': isWarning,
      'is-danger': isDanger,
      'is-loading': isLoading,
      'is-small': isSmall,
      'is-medium': isMedium,
      'is-large': isLarge,
      'is-responsive': isResponsive,
      'is-fullwidth': isFullwidth,
      'is-inverted': isInverted,
      'is-outlined': isOutlined,
      'is-rounded': isRounded
    }"
      :disabled="isDisabled"
      @click="onClick($event)"
  >
    <slot></slot>
  </button>
</template>

<script>
export default {
  name: "BaseButton",
  props: {
    buttonName: { type: String, required: true },
    buttonId : { type: String, required: true },
    buttonType: { type: String, default: null },
    disabled: { type: Boolean, default: false },
    inverted: { type: Boolean, default: false },
    fullwidth: {type: Boolean, default: false },
    light: { type: Boolean, default: false },
    loading: { type: Boolean, default: false },
    outlined: { type: Boolean, default: false },
    responsive: { type: Boolean, default: true },
    rounded: { type: Boolean, default: false },
    size: { type: String, default: "medium" },
  },
  emits:['click'],
  data() {
    return {
      clickTime: null
    }
  },
  computed: {
    isWhite() {
      return 'white'=== this.buttonType
    },
    isDark() {
      return 'dark'=== this.buttonType
    },
    isBlack() {
      return 'black'=== this.buttonType
    },
    isText() {
      return 'text'=== this.buttonType
    },
    isGhost() {
      return 'ghost'=== this.buttonType
    },
    isPrimary() {
      return 'primary'=== this.buttonType
    },
    isLink() {
      return 'link'=== this.buttonType
    },
    isInfo() {
      return 'info'=== this.buttonType
    },
    isSuccess() {
      return 'success'=== this.buttonType
    },
    isWarning() {
      return 'warning'=== this.buttonType
    },
    isDanger() {
      return 'danger'=== this.buttonType
    },
    isLoading() {
      return !!(this.loading)
    },
    isDisabled() {
      return !!(this.disabled)
    },
    isSmall() {
      return 'small'=== this.size
    },
    isMedium() {
      return 'medium'=== this.size
    },
    isLarge() {
      return 'large'=== this.size
    },
    isResponsive() {
      return !!(this.responsive)
    },
    isFullwidth() {
      return !!(this.fullwidth)
    },
    isInverted() {
      return !!(this.inverted)
    },
    isLight() {
      return !!(this.light)
    },
    isOutlined() {
      return !!(this.outlined)
    },
    isRounded() {
      return !!(this.rounded)
    }
  },
  methods: {
    onClick() {
      if (!this.isDisabled && !this.isLoading) {
        this.$emit('click', this.buttonId)
        this.clickTime = Date.now()
      }
    }
  }
}
</script>

<style scoped>

</style>

感想

コンポーネント化したのでデフォルトのスタイル変更が一箇所の変更で行える。
disableとloadingの処理が減るだけでもコンポーネント化する意味があるかも。

リンク

ボタンコンポーネントの追加

Last Mod: May 8, 2022