@gitlab/ui 42.17.0 → 42.18.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitlab/ui",
3
- "version": "42.17.0",
3
+ "version": "42.18.0",
4
4
  "description": "GitLab UI Components",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -29,3 +29,22 @@ export const objectTokenList = [
29
29
  { id: '11', title: 'whale' },
30
30
  { id: '12', title: 'xenarthra' },
31
31
  ];
32
+
33
+ export const oneTokenList = ['dog'];
34
+
35
+ export const actionsList = [
36
+ {
37
+ label: 'Create',
38
+ fn: () => {
39
+ // eslint-disable-next-line no-alert
40
+ window.alert('Create action');
41
+ },
42
+ },
43
+ {
44
+ label: 'Edit',
45
+ fn: () => {
46
+ // eslint-disable-next-line no-alert
47
+ window.alert('Edit action');
48
+ },
49
+ },
50
+ ];
@@ -4,11 +4,6 @@
4
4
  }
5
5
 
6
6
  .show-dropdown {
7
- @include gl-display-block;
8
7
  max-height: $gl-max-dropdown-max-height;
9
8
  }
10
-
11
- .highlight-dropdown {
12
- @include gl-bg-gray-50;
13
- }
14
9
  }
@@ -1,7 +1,13 @@
1
1
  import { mount } from '@vue/test-utils';
2
2
  import GlDropdownItem from '../../dropdown/dropdown_item.vue';
3
3
  import GlFormInput from '../form_input/form_input.vue';
4
- import { stringTokenList, labelText, objectTokenList } from './constants';
4
+ import {
5
+ stringTokenList,
6
+ labelText,
7
+ objectTokenList,
8
+ oneTokenList,
9
+ actionsList,
10
+ } from './constants';
5
11
  import GlFormCombobox from './form_combobox.vue';
6
12
 
7
13
  const partialToken = 'do';
@@ -22,7 +28,11 @@ const doTimes = (num, fn) => {
22
28
  describe('GlFormCombobox', () => {
23
29
  let wrapper;
24
30
 
25
- const createComponent = ({ tokens = stringTokenList, matchValueToAttr = undefined } = {}) => {
31
+ const createComponent = ({
32
+ tokens = stringTokenList,
33
+ matchValueToAttr = undefined,
34
+ actionList = [],
35
+ } = {}) => {
26
36
  wrapper = mount({
27
37
  data() {
28
38
  return {
@@ -30,6 +40,7 @@ describe('GlFormCombobox', () => {
30
40
  tokens,
31
41
  labelText,
32
42
  matchValueToAttr,
43
+ actionList,
33
44
  };
34
45
  },
35
46
  components: { GlFormCombobox },
@@ -40,6 +51,7 @@ describe('GlFormCombobox', () => {
40
51
  :token-list="tokens"
41
52
  :label-text="labelText"
42
53
  :match-value-to-attr="matchValueToAttr"
54
+ :action-list="actionList"
43
55
  />
44
56
  </div>
45
57
  `,
@@ -54,6 +66,19 @@ describe('GlFormCombobox', () => {
54
66
  const findInputValue = () => findInput().element.value;
55
67
  const setInput = (val) => findInput().setValue(val);
56
68
  const arrowDown = () => findInput().trigger('keydown.down');
69
+ const findFirstAction = () => wrapper.find('[data-testid="combobox-action"]');
70
+
71
+ beforeAll(() => {
72
+ if (!HTMLElement.prototype.scrollIntoView) {
73
+ HTMLElement.prototype.scrollIntoView = jest.fn();
74
+ }
75
+ });
76
+
77
+ afterAll(() => {
78
+ if (HTMLElement.prototype.scrollIntoView.mock) {
79
+ delete HTMLElement.prototype.scrollIntoView;
80
+ }
81
+ });
57
82
 
58
83
  describe.each`
59
84
  valueType | tokens | matchValueToAttr | partialTokenMatch
@@ -216,4 +241,47 @@ describe('GlFormCombobox', () => {
216
241
  });
217
242
  });
218
243
  });
244
+
245
+ describe('with action items', () => {
246
+ let actionSpy;
247
+ const windowAlert = window.alert;
248
+
249
+ beforeEach(() => {
250
+ createComponent({ tokens: oneTokenList, actionList: actionsList });
251
+ actionSpy = jest.spyOn(wrapper.vm.actionList[0], 'fn');
252
+ window.alert = jest.fn();
253
+ });
254
+
255
+ afterEach(() => {
256
+ window.alert = windowAlert;
257
+ });
258
+
259
+ it('click on action item executes its function', async () => {
260
+ await setInput(partialToken);
261
+ expect(findDropdown().isVisible()).toBe(true);
262
+
263
+ await findFirstAction().trigger('click');
264
+
265
+ expect(actionSpy).toHaveBeenCalled();
266
+ expect(findDropdown().isVisible()).toBe(false);
267
+ });
268
+
269
+ it('keyboard navigation and executes function on enter', async () => {
270
+ await setInput('dog');
271
+ findInput().trigger('keydown.down');
272
+ findInput().trigger('keydown.down');
273
+ await findInput().trigger('keydown.enter');
274
+
275
+ expect(actionSpy).toHaveBeenCalled();
276
+ expect(findDropdown().isVisible()).toBe(false);
277
+ });
278
+
279
+ it('displays only action items when no result match input value', async () => {
280
+ await setInput('doNotMatchAnything');
281
+ expect(findDropdown().isVisible()).toBe(true);
282
+
283
+ expect(findFirstAction().exists()).toBe(true);
284
+ expect(findDropdownOptions().length).toBe(2);
285
+ });
286
+ });
219
287
  });
@@ -1,19 +1,27 @@
1
- import { stringTokenList, labelText, objectTokenList } from './constants';
1
+ import { makeContainer } from '../../../../utils/story_decorators/container';
2
+ import { stringTokenList, labelText, objectTokenList, actionsList } from './constants';
2
3
  import readme from './form_combobox.md';
3
4
  import GlFormCombobox from './form_combobox.vue';
4
5
 
5
6
  const template = `
6
7
  <gl-form-combobox
7
8
  v-model="value"
9
+ ref="combobox"
8
10
  :token-list="tokenList"
9
11
  :label-text="labelText"
10
12
  :match-value-to-attr="matchValueToAttr"
13
+ :action-list="actionList"
11
14
  />`;
12
15
 
13
- const generateProps = ({ tokenList = stringTokenList, matchValueToAttr = undefined } = {}) => ({
16
+ const generateProps = ({
17
+ tokenList = stringTokenList,
18
+ matchValueToAttr,
19
+ actionList = undefined,
20
+ } = {}) => ({
14
21
  tokenList,
15
22
  labelText,
16
23
  matchValueToAttr,
24
+ actionList,
17
25
  });
18
26
 
19
27
  const Template = (args) => ({
@@ -34,16 +42,17 @@ export const WithObjectValue = (args, { argTypes }) => ({
34
42
  components: { GlFormCombobox },
35
43
  props: Object.keys(argTypes),
36
44
  mounted() {
37
- document.querySelector('.gl-form-input').focus();
45
+ this.$nextTick(() => this.$refs.combobox.openSuggestions(objectTokenList));
38
46
  },
39
47
  data: () => {
40
48
  return {
41
- value: '',
49
+ value: ' ',
42
50
  };
43
51
  },
44
52
  template: `
45
53
  <gl-form-combobox
46
54
  v-model="value"
55
+ ref="combobox"
47
56
  :token-list="tokenList"
48
57
  :label-text="labelText"
49
58
  :match-value-to-attr="matchValueToAttr"
@@ -58,6 +67,26 @@ export const WithObjectValue = (args, { argTypes }) => ({
58
67
  `,
59
68
  });
60
69
  WithObjectValue.args = generateProps({ tokenList: objectTokenList, matchValueToAttr: 'title' });
70
+ WithObjectValue.decorators = [makeContainer({ height: '370px' })];
71
+
72
+ export const WithActions = (args, { argTypes }) => ({
73
+ components: { GlFormCombobox },
74
+ props: Object.keys(argTypes),
75
+ mounted() {
76
+ this.$nextTick(() => this.$refs.combobox.openSuggestions(['dog']));
77
+ },
78
+ data: () => {
79
+ return {
80
+ value: 'dog',
81
+ };
82
+ },
83
+ template,
84
+ });
85
+ WithActions.args = generateProps({
86
+ tokenList: stringTokenList,
87
+ actionList: actionsList,
88
+ });
89
+ WithActions.decorators = [makeContainer({ height: '180px' })];
61
90
 
62
91
  export default {
63
92
  title: 'base/form/form-combobox',
@@ -2,6 +2,7 @@
2
2
  import { uniqueId } from 'lodash';
3
3
 
4
4
  import GlDropdownItem from '../../dropdown/dropdown_item.vue';
5
+ import GlDropdownDivider from '../../dropdown/dropdown_divider.vue';
5
6
  import GlFormGroup from '../form_group/form_group.vue';
6
7
  import GlFormInput from '../form_input/form_input.vue';
7
8
 
@@ -9,6 +10,7 @@ export default {
9
10
  name: 'GlFormCombobox',
10
11
  components: {
11
12
  GlDropdownItem,
13
+ GlDropdownDivider,
12
14
  GlFormGroup,
13
15
  GlFormInput,
14
16
  },
@@ -30,6 +32,14 @@ export default {
30
32
  type: Array,
31
33
  required: true,
32
34
  },
35
+ /**
36
+ * List of action functions to display at the bottom of the dropdown
37
+ */
38
+ actionList: {
39
+ type: Array,
40
+ required: false,
41
+ default: () => [],
42
+ },
33
43
  value: {
34
44
  type: [String, Object],
35
45
  required: true,
@@ -62,13 +72,37 @@ export default {
62
72
  return this.showSuggestions ? 'off' : 'on';
63
73
  },
64
74
  showSuggestions() {
65
- return this.results.length > 0;
75
+ return this.value.length > 0 && this.allItems.length > 0;
66
76
  },
67
77
  displayedValue() {
68
78
  return this.matchValueToAttr && this.value[this.matchValueToAttr]
69
79
  ? this.value[this.matchValueToAttr]
70
80
  : this.value;
71
81
  },
82
+ resultsLength() {
83
+ return this.results.length;
84
+ },
85
+ allItems() {
86
+ return [...this.results, ...this.actionList];
87
+ },
88
+ },
89
+ watch: {
90
+ tokenList(newList) {
91
+ const filteredTokens = newList.filter((token) => {
92
+ if (this.matchValueToAttr) {
93
+ // For API driven tokens, we don't need extra filtering
94
+ return token;
95
+ }
96
+ return token.toLowerCase().includes(this.value.toLowerCase());
97
+ });
98
+
99
+ if (filteredTokens.length) {
100
+ this.openSuggestions(filteredTokens);
101
+ } else {
102
+ this.results = [];
103
+ this.arrowCounter = -1;
104
+ }
105
+ },
72
106
  },
73
107
  mounted() {
74
108
  document.addEventListener('click', this.handleClickOutside);
@@ -80,6 +114,7 @@ export default {
80
114
  closeSuggestions() {
81
115
  this.results = [];
82
116
  this.arrowCounter = -1;
117
+ this.userDismissedResults = true;
83
118
  },
84
119
  handleClickOutside(event) {
85
120
  if (!this.$el.contains(event.target)) {
@@ -89,33 +124,38 @@ export default {
89
124
  onArrowDown() {
90
125
  const newCount = this.arrowCounter + 1;
91
126
 
92
- if (newCount >= this.results.length) {
127
+ if (newCount >= this.allItems.length) {
93
128
  this.arrowCounter = 0;
94
129
  return;
95
130
  }
96
131
 
97
132
  this.arrowCounter = newCount;
133
+ this.$refs.results[newCount]?.$el.scrollIntoView(false);
98
134
  },
99
135
  onArrowUp() {
100
136
  const newCount = this.arrowCounter - 1;
101
137
 
102
138
  if (newCount < 0) {
103
- this.arrowCounter = this.results.length - 1;
139
+ this.arrowCounter = this.allItems.length - 1;
104
140
  return;
105
141
  }
106
142
 
107
143
  this.arrowCounter = newCount;
144
+ this.$refs.results[newCount]?.$el.scrollIntoView(true);
108
145
  },
109
146
  onEnter() {
110
- const currentToken = this.results[this.arrowCounter] || this.value;
111
- this.selectToken(currentToken);
147
+ const focusedItem = this.allItems[this.arrowCounter] || this.value;
148
+ if (focusedItem.fn) {
149
+ this.selectAction(focusedItem);
150
+ } else {
151
+ this.selectToken(focusedItem);
152
+ }
112
153
  },
113
154
  onEsc() {
114
155
  if (!this.showSuggestions) {
115
156
  this.$emit('input', '');
116
157
  }
117
158
  this.closeSuggestions();
118
- this.userDismissedResults = true;
119
159
  },
120
160
  onEntry(value) {
121
161
  this.$emit('input', value);
@@ -137,7 +177,8 @@ export default {
137
177
  if (filteredTokens.length) {
138
178
  this.openSuggestions(filteredTokens);
139
179
  } else {
140
- this.closeSuggestions();
180
+ this.results = [];
181
+ this.arrowCounter = -1;
141
182
  }
142
183
  },
143
184
  openSuggestions(filteredResults) {
@@ -152,6 +193,11 @@ export default {
152
193
  */
153
194
  this.$emit('value-selected', value);
154
195
  },
196
+ selectAction(value) {
197
+ value.fn();
198
+ this.$emit('input', this.value);
199
+ this.closeSuggestions();
200
+ },
155
201
  },
156
202
  };
157
203
  </script>
@@ -186,20 +232,43 @@ export default {
186
232
  v-show="showSuggestions && !userDismissedResults"
187
233
  :id="suggestionsId"
188
234
  data-testid="combobox-dropdown"
189
- class="dropdown-menu dropdown-full-width gl-list-style-none gl-pl-0 gl-mb-0 gl-overflow-y-auto"
190
- :class="{ 'show-dropdown': showSuggestions }"
235
+ class="dropdown-menu dropdown-full-width show-dropdown gl-list-style-none gl-pl-0 gl-mb-0 gl-display-flex gl-flex-direction-column"
191
236
  >
192
- <gl-dropdown-item
193
- v-for="(result, i) in results"
194
- :key="i"
195
- role="option"
196
- :class="{ 'highlight-dropdown': i === arrowCounter }"
197
- :aria-selected="i === arrowCounter"
198
- tabindex="-1"
199
- @click="selectToken(result)"
200
- >
201
- <slot name="result" :item="result">{{ result }}</slot>
202
- </gl-dropdown-item>
237
+ <li class="gl-overflow-y-auto show-dropdown">
238
+ <ul class="gl-list-style-none gl-pl-0 gl-mb-0">
239
+ <gl-dropdown-item
240
+ v-for="(result, i) in results"
241
+ ref="results"
242
+ :key="i"
243
+ role="option"
244
+ :class="{ 'gl-bg-gray-50': i === arrowCounter }"
245
+ :aria-selected="i === arrowCounter"
246
+ tabindex="-1"
247
+ @click="selectToken(result)"
248
+ >
249
+ <!-- @slot The suggestion result item to display. -->
250
+ <slot name="result" :item="result">{{ result }}</slot>
251
+ </gl-dropdown-item>
252
+ </ul>
253
+ </li>
254
+ <gl-dropdown-divider v-if="resultsLength > 0 && actionList.length > 0" />
255
+ <li>
256
+ <ul class="gl-list-style-none gl-pl-0 gl-mb-0">
257
+ <gl-dropdown-item
258
+ v-for="(action, i) in actionList"
259
+ :key="i + resultsLength"
260
+ role="option"
261
+ :class="{ 'gl-bg-gray-50': i + resultsLength === arrowCounter }"
262
+ :aria-selected="i + resultsLength === arrowCounter"
263
+ tabindex="-1"
264
+ data-testid="combobox-action"
265
+ @click="selectAction(action)"
266
+ >
267
+ <!-- @slot The action item to display. -->
268
+ <slot name="action" :item="action">{{ action.label }}</slot>
269
+ </gl-dropdown-item>
270
+ </ul>
271
+ </li>
203
272
  </ul>
204
273
  </div>
205
274
  </template>