@mozaic-ds/vue 0.21.1 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,8 +13,20 @@
13
13
  class="mc-tabs__item"
14
14
  role="presentation"
15
15
  >
16
+ <component
17
+ :is="`${tab.router}`"
18
+ v-if="tab.router"
19
+ v-bind="tab.attrs"
20
+ :to="tab.to"
21
+ class="mc-tabs__link"
22
+ :class="setLinkClass(tab)"
23
+ active-class="mc-tabs__link--selected"
24
+ >
25
+ {{ tab.text }}
26
+ </component>
16
27
  <component
17
28
  :is="tab.href ? (tab.disabled ? 'span' : 'a') : 'button'"
29
+ v-else
18
30
  :id="tab.id"
19
31
  ref="tab"
20
32
  :href="tab.href ? (!tab.disabled ? tab.href : null) : null"
@@ -23,14 +35,9 @@
23
35
  class="mc-tabs__link"
24
36
  role="tab"
25
37
  :aria-selected="tab.active ? 'true' : 'false'"
26
- :class="setActiveClass(tab)"
27
- @click="
28
- manageTabs(
29
- $event.target,
30
- $event,
31
- Object.assign({ index: index }, tab)
32
- )
33
- "
38
+ :class="setLinkClass(tab)"
39
+ v-bind="tab.attrs"
40
+ @click="onTabClicked($event, tab, index)"
34
41
  >
35
42
  {{ tab.text }}
36
43
  </component>
@@ -82,38 +89,41 @@ export default {
82
89
  },
83
90
  activeIndex: {
84
91
  type: Number,
85
- default: 0,
92
+ default: null,
86
93
  },
87
94
  },
88
95
 
89
96
  data() {
90
97
  return {
91
98
  tablist: null,
99
+ localActiveIndex: null,
92
100
  };
93
101
  },
94
102
 
95
103
  computed: {
96
104
  setClasses() {
97
- const classes = [
98
- {
99
- 'mc-tabs--full': this.align === 'full',
100
- 'mc-tabs--full-centered': this.align === 'centered',
101
- 'mc-tabs--dropdown': this.type === 'dropdown',
102
- 'mc-tabs--no-shadow': !this.shadow,
103
- },
104
- ];
105
-
106
- return classes;
105
+ return {
106
+ 'mc-tabs--full': this.align === 'full',
107
+ 'mc-tabs--full-centered': this.align === 'centered',
108
+ 'mc-tabs--dropdown': this.type === 'dropdown',
109
+ 'mc-tabs--no-shadow': !this.shadow,
110
+ };
107
111
  },
108
112
  },
109
113
 
110
114
  watch: {
111
115
  activeIndex(newValue) {
112
- const tab = this.getTabFromIndex(newValue);
113
- if (tab && this.tabs[newValue]) {
114
- this.manageTabs(tab, null, Object.assign({index: newValue}, this.tabs[newValue]));
115
- }
116
- }
116
+ this.localActiveIndex = newValue;
117
+ },
118
+ localActiveIndex: {
119
+ handler: function (newValue) {
120
+ const selectedTab = this.getTabFromIndex(newValue);
121
+ if (selectedTab) {
122
+ this.setTabState(selectedTab);
123
+ }
124
+ },
125
+ immediate: true,
126
+ },
117
127
  },
118
128
 
119
129
  mounted: function () {
@@ -122,17 +132,16 @@ export default {
122
132
  this.tablist = this.$refs.tablist;
123
133
 
124
134
  if (this.activeIndex) {
125
- const tab = this.getTabFromIndex(this.activeIndex);
126
- if (tab) {
127
- this.manageTabs(tab);
128
- }
135
+ this.localActiveIndex = this.activeIndex;
129
136
  } else {
130
- const isActive = this.tabs.some((tab) =>
131
- Object.prototype.hasOwnProperty.call(tab, 'active')
137
+ const isActive = this.tabs.some(
138
+ (tab) =>
139
+ Object.prototype.hasOwnProperty.call(tab, 'active') &&
140
+ tab.active === true
132
141
  );
142
+
133
143
  if (!isActive) {
134
- const firstTab = this.tablist.querySelector('.mc-tabs__link');
135
- this.manageTabs(firstTab);
144
+ this.localActiveIndex = 0;
136
145
  }
137
146
  }
138
147
  }
@@ -140,40 +149,41 @@ export default {
140
149
  },
141
150
 
142
151
  methods: {
143
- setActiveClass: function (tab) {
144
- const tabClasses = [];
145
-
146
- if (tab.active) {
147
- tabClasses.push('mc-tabs__link--selected');
148
- }
149
-
150
- if (tab.disabled) {
151
- tabClasses.push('mc-tabs__link--disabled');
152
- }
153
-
154
- return tabClasses;
152
+ setLinkClass: function (tab) {
153
+ return {
154
+ 'mc-tabs__link--selected': tab.active,
155
+ 'mc-tabs__link--disabled': tab.disabled,
156
+ };
155
157
  },
156
- manageTabs: function (el, e, tab) {
157
- if (tab && tab.disabled) {
158
- return;
159
- }
160
- if (e) {
161
- this.$emit('tab-clicked', e.target, tab);
162
- }
163
-
158
+ setTabState(tab) {
164
159
  this.tablist.querySelectorAll('.mc-tabs__link').forEach((el) => {
165
160
  el.classList.remove('mc-tabs__link--selected');
166
161
  el.setAttribute('aria-selected', 'false');
167
162
  });
168
163
 
169
- el.classList.add('mc-tabs__link--selected');
170
- el.setAttribute('aria-selected', 'true');
164
+ tab.classList.add('mc-tabs__link--selected');
165
+ tab.setAttribute('aria-selected', 'true');
171
166
  },
172
167
  getTabFromIndex: function (index) {
173
- if (this.tablist && this.tablist.children[index] && this.tablist.children[index].children[0]) {
168
+ if (
169
+ this.tablist &&
170
+ this.tablist.children[index] &&
171
+ this.tablist.children[index].children[0]
172
+ ) {
174
173
  return this.tablist.children[index].children[0];
175
174
  }
176
175
  },
176
+ onTabClicked(e, tab, index) {
177
+ if (tab && tab.disabled) {
178
+ return;
179
+ }
180
+
181
+ if (typeof this.activeIndex !== 'number') {
182
+ this.localActiveIndex = index;
183
+ }
184
+
185
+ this.$emit('tab-clicked', e.target, Object.assign({ index: index }, tab));
186
+ },
177
187
  },
178
188
  };
179
189
  </script>
@@ -6,7 +6,6 @@
6
6
  :class="setClasses"
7
7
  :aria-invalid="isInvalid"
8
8
  :value="value"
9
- @input="$emit('input', $event.target.value)"
10
9
  v-on="inputListeners"
11
10
  />
12
11
  </template>