@gitlab/ui 52.6.1 → 52.7.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/CHANGELOG.md +7 -0
- package/dist/components/base/new_dropdowns/listbox/listbox.js +61 -2
- package/package.json +1 -1
- package/src/components/base/new_dropdowns/listbox/listbox.spec.js +126 -0
- package/src/components/base/new_dropdowns/listbox/listbox.stories.js +58 -0
- package/src/components/base/new_dropdowns/listbox/listbox.vue +77 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [52.7.0](https://gitlab.com/gitlab-org/gitlab-ui/compare/v52.6.1...v52.7.0) (2022-12-20)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **GlListbox:** Add infinite scroll to listbox ([d42c9e6](https://gitlab.com/gitlab-org/gitlab-ui/commit/d42c9e64745fdc0ee65e1d612712a1aaf61a2228))
|
|
7
|
+
|
|
1
8
|
## [52.6.1](https://gitlab.com/gitlab-org/gitlab-ui/compare/v52.6.0...v52.6.1) (2022-12-20)
|
|
2
9
|
|
|
3
10
|
|
|
@@ -5,6 +5,7 @@ import { GL_DROPDOWN_SHOWN, GL_DROPDOWN_HIDDEN, HOME, END, ARROW_UP, ARROW_DOWN
|
|
|
5
5
|
import { buttonCategoryOptions, dropdownVariantOptions, buttonSizeOptions } from '../../../../utils/constants';
|
|
6
6
|
import GlButton from '../../button/button';
|
|
7
7
|
import GlLoadingIcon from '../../loading_icon/loading_icon';
|
|
8
|
+
import GlIntersectionObserver from '../../../utilities/intersection_observer/intersection_observer';
|
|
8
9
|
import GlSearchBoxByType from '../../search_box_by_type/search_box_by_type';
|
|
9
10
|
import GlBaseDropdown from '../base_dropdown/base_dropdown';
|
|
10
11
|
import GlListboxItem from './listbox_item';
|
|
@@ -30,7 +31,8 @@ var script = {
|
|
|
30
31
|
GlButton,
|
|
31
32
|
GlSearchBoxByType,
|
|
32
33
|
GlListboxSearchInput,
|
|
33
|
-
GlLoadingIcon
|
|
34
|
+
GlLoadingIcon,
|
|
35
|
+
GlIntersectionObserver
|
|
34
36
|
},
|
|
35
37
|
model: {
|
|
36
38
|
prop: 'selected',
|
|
@@ -203,6 +205,37 @@ var script = {
|
|
|
203
205
|
required: false,
|
|
204
206
|
default: false
|
|
205
207
|
},
|
|
208
|
+
/**
|
|
209
|
+
* Enables infinite scroll.
|
|
210
|
+
* When set to `true`, the `@bottom-reached` event will be fired when
|
|
211
|
+
* the bottom of the listbox is scrolled to.
|
|
212
|
+
* Does not support groups.
|
|
213
|
+
*/
|
|
214
|
+
infiniteScroll: {
|
|
215
|
+
type: Boolean,
|
|
216
|
+
required: false,
|
|
217
|
+
default: false
|
|
218
|
+
},
|
|
219
|
+
/**
|
|
220
|
+
* This prop is used for infinite scroll.
|
|
221
|
+
* It represents the total number of items that exist,
|
|
222
|
+
* even if they have not yet been loaded.
|
|
223
|
+
* Do not set this prop if the total number of items is unknown.
|
|
224
|
+
*/
|
|
225
|
+
totalItems: {
|
|
226
|
+
type: Number,
|
|
227
|
+
required: false,
|
|
228
|
+
default: null
|
|
229
|
+
},
|
|
230
|
+
/**
|
|
231
|
+
* This prop is used for infinite scroll.
|
|
232
|
+
* Set to `true` when more items are being loaded.
|
|
233
|
+
*/
|
|
234
|
+
infiniteScrollLoading: {
|
|
235
|
+
type: Boolean,
|
|
236
|
+
required: false,
|
|
237
|
+
default: false
|
|
238
|
+
},
|
|
206
239
|
/**
|
|
207
240
|
* Message to be displayed when filtering produced no results
|
|
208
241
|
*/
|
|
@@ -291,6 +324,9 @@ var script = {
|
|
|
291
324
|
return this.selected.length > 0;
|
|
292
325
|
}
|
|
293
326
|
return Boolean(this.selected);
|
|
327
|
+
},
|
|
328
|
+
showIntersectionObserver() {
|
|
329
|
+
return this.infiniteScroll && !this.infiniteScrollLoading && !this.loading && !this.searching;
|
|
294
330
|
}
|
|
295
331
|
},
|
|
296
332
|
watch: {
|
|
@@ -316,6 +352,11 @@ var script = {
|
|
|
316
352
|
}
|
|
317
353
|
}
|
|
318
354
|
},
|
|
355
|
+
created() {
|
|
356
|
+
if (process.env.NODE_ENV !== 'production' && this.infiniteScroll && this.items.some(item => !isOption(item))) {
|
|
357
|
+
throw new Error('Infinite scroll does not support groups. Please set the "infiniteScroll" prop to "false"');
|
|
358
|
+
}
|
|
359
|
+
},
|
|
319
360
|
methods: {
|
|
320
361
|
open() {
|
|
321
362
|
this.$refs.baseDropdown.open();
|
|
@@ -457,6 +498,24 @@ var script = {
|
|
|
457
498
|
closeAndFocus() {
|
|
458
499
|
this.$refs.baseDropdown.closeAndFocus();
|
|
459
500
|
},
|
|
501
|
+
onIntersectionObserverAppear() {
|
|
502
|
+
/**
|
|
503
|
+
* Emitted when bottom of listbox has been scrolled to.
|
|
504
|
+
* Used for infinite scroll.
|
|
505
|
+
*
|
|
506
|
+
* @event bottom-reached
|
|
507
|
+
*/
|
|
508
|
+
this.$emit('bottom-reached');
|
|
509
|
+
},
|
|
510
|
+
listboxItemMoreItemsAriaAttributes(index) {
|
|
511
|
+
if (this.totalItems === null) {
|
|
512
|
+
return {};
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
'aria-setsize': this.totalItems,
|
|
516
|
+
'aria-posinset': index + 1
|
|
517
|
+
};
|
|
518
|
+
},
|
|
460
519
|
isOption
|
|
461
520
|
}
|
|
462
521
|
};
|
|
@@ -465,7 +524,7 @@ var script = {
|
|
|
465
524
|
const __vue_script__ = script;
|
|
466
525
|
|
|
467
526
|
/* template */
|
|
468
|
-
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('gl-base-dropdown',{ref:"baseDropdown",attrs:{"aria-haspopup":"listbox","aria-labelledby":_vm.toggleAriaLabelledBy,"toggle-id":_vm.toggleId,"toggle-text":_vm.listboxToggleText,"toggle-class":_vm.toggleClass,"text-sr-only":_vm.textSrOnly,"category":_vm.category,"variant":_vm.variant,"size":_vm.size,"icon":_vm.icon,"disabled":_vm.disabled,"loading":_vm.loading,"no-caret":_vm.noCaret,"right":_vm.right},on:_vm._d({},[_vm.$options.events.GL_DROPDOWN_SHOWN,_vm.onShow,_vm.$options.events.GL_DROPDOWN_HIDDEN,_vm.onHide])},[(_vm.headerText)?_c('div',{staticClass:"gl-display-flex gl-align-items-center gl-p-4! gl-min-h-8",class:_vm.$options.HEADER_ITEMS_BORDER_CLASSES},[_c('div',{staticClass:"gl-flex-grow-1 gl-font-weight-bold gl-font-sm gl-pr-2",attrs:{"id":_vm.headerId,"data-testid":"listbox-header-text"}},[_vm._v("\n "+_vm._s(_vm.headerText)+"\n ")]),_vm._v(" "),(_vm.showResetButton)?_c('gl-button',{staticClass:"gl-focus-inset-border-2-blue-400! gl-flex-shrink-0 gl-font-sm! gl-px-2! gl-py-2!",attrs:{"category":"tertiary","data-testid":"listbox-reset-button"},on:{"click":_vm.onResetButtonClicked}},[_vm._v("\n "+_vm._s(_vm.resetButtonLabel)+"\n ")]):_vm._e()],1):_vm._e(),_vm._v(" "),(_vm.searchable)?_c('div',{class:_vm.$options.HEADER_ITEMS_BORDER_CLASSES},[_c('gl-listbox-search-input',{ref:"searchBox",attrs:{"aria-owns":_vm.listboxId,"data-testid":"listbox-search-input","placeholder":_vm.searchPlaceholder},on:{"input":_vm.search,"keydown":_vm.onKeydown},model:{value:(_vm.searchStr),callback:function ($$v) {_vm.searchStr=$$v;},expression:"searchStr"}}),_vm._v(" "),(_vm.searching)?_c('gl-loading-icon',{staticClass:"gl-my-3",attrs:{"data-testid":"listbox-search-loader","size":"md"}}):_vm._e()],1):_vm._e(),_vm._v(" "),(_vm.showList)?_c(_vm.listboxTag,{ref:"list",tag:"component",staticClass:"gl-dropdown-contents gl-list-style-none gl-pl-0 gl-mb-0",attrs:{"id":"listbox","aria-labelledby":_vm.listAriaLabelledBy || _vm.headerId || _vm.toggleId,"role":"listbox","tabindex":"-1"},on:{"keydown":_vm.onKeydown}},[_vm._l((_vm.items),function(item,index){return [(_vm.isOption(item))?[_c('gl-listbox-item',{key:item.value,attrs:{"is-selected":_vm.isSelected(item),"is-focused":_vm.isFocused(item),"is-check-centered":_vm.isCheckCentered},on:{"select":function($event){return _vm.onSelect(item, $event)}}},[_vm._t("list-item",function(){return [_vm._v("\n "+_vm._s(item.text)+"\n ")]},{"item":item})],2)]:[_c('gl-listbox-group',{key:item.text,class:_vm.groupClasses(index),attrs:{"name":item.text},scopedSlots:_vm._u([(_vm.$scopedSlots['group-label'])?{key:"group-label",fn:function(){return [_vm._t("group-label",null,{"group":item})]},proxy:true}:null],null,true)},[_vm._v(" "),_vm._l((item.options),function(option){return _c('gl-listbox-item',{key:option.value,attrs:{"is-selected":_vm.isSelected(option),"is-focused":_vm.isFocused(option),"is-check-centered":_vm.isCheckCentered},on:{"select":function($event){return _vm.onSelect(option, $event)}}},[_vm._t("list-item",function(){return [_vm._v("\n "+_vm._s(option.text)+"\n ")]},{"item":option})],2)})],2)]]})],2):_vm._e(),_vm._v(" "),(_vm.announceSRSearchResults)?_c('span',{staticClass:"gl-sr-only",attrs:{"data-testid":"listbox-number-of-results","aria-live":"assertive"}},[_vm._t("search-summary-sr-only")],2):(_vm.showNoResultsText)?_c('div',{staticClass:"gl-pl-7 gl-pr-5 gl-pt-3 gl-font-base gl-text-gray-600",attrs:{"aria-live":"assertive","data-testid":"listbox-no-results-text"}},[_vm._v("\n "+_vm._s(_vm.noResultsText)+"\n ")]):_vm._e(),_vm._v(" "),_vm._t("footer")],2)};
|
|
527
|
+
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('gl-base-dropdown',{ref:"baseDropdown",attrs:{"aria-haspopup":"listbox","aria-labelledby":_vm.toggleAriaLabelledBy,"toggle-id":_vm.toggleId,"toggle-text":_vm.listboxToggleText,"toggle-class":_vm.toggleClass,"text-sr-only":_vm.textSrOnly,"category":_vm.category,"variant":_vm.variant,"size":_vm.size,"icon":_vm.icon,"disabled":_vm.disabled,"loading":_vm.loading,"no-caret":_vm.noCaret,"right":_vm.right},on:_vm._d({},[_vm.$options.events.GL_DROPDOWN_SHOWN,_vm.onShow,_vm.$options.events.GL_DROPDOWN_HIDDEN,_vm.onHide])},[(_vm.headerText)?_c('div',{staticClass:"gl-display-flex gl-align-items-center gl-p-4! gl-min-h-8",class:_vm.$options.HEADER_ITEMS_BORDER_CLASSES},[_c('div',{staticClass:"gl-flex-grow-1 gl-font-weight-bold gl-font-sm gl-pr-2",attrs:{"id":_vm.headerId,"data-testid":"listbox-header-text"}},[_vm._v("\n "+_vm._s(_vm.headerText)+"\n ")]),_vm._v(" "),(_vm.showResetButton)?_c('gl-button',{staticClass:"gl-focus-inset-border-2-blue-400! gl-flex-shrink-0 gl-font-sm! gl-px-2! gl-py-2!",attrs:{"category":"tertiary","data-testid":"listbox-reset-button"},on:{"click":_vm.onResetButtonClicked}},[_vm._v("\n "+_vm._s(_vm.resetButtonLabel)+"\n ")]):_vm._e()],1):_vm._e(),_vm._v(" "),(_vm.searchable)?_c('div',{class:_vm.$options.HEADER_ITEMS_BORDER_CLASSES},[_c('gl-listbox-search-input',{ref:"searchBox",attrs:{"aria-owns":_vm.listboxId,"data-testid":"listbox-search-input","placeholder":_vm.searchPlaceholder},on:{"input":_vm.search,"keydown":_vm.onKeydown},model:{value:(_vm.searchStr),callback:function ($$v) {_vm.searchStr=$$v;},expression:"searchStr"}}),_vm._v(" "),(_vm.searching)?_c('gl-loading-icon',{staticClass:"gl-my-3",attrs:{"data-testid":"listbox-search-loader","size":"md"}}):_vm._e()],1):_vm._e(),_vm._v(" "),(_vm.showList)?_c(_vm.listboxTag,{ref:"list",tag:"component",staticClass:"gl-dropdown-contents gl-list-style-none gl-pl-0 gl-mb-0",attrs:{"id":"listbox","aria-labelledby":_vm.listAriaLabelledBy || _vm.headerId || _vm.toggleId,"role":"listbox","tabindex":"-1"},on:{"keydown":_vm.onKeydown}},[_vm._l((_vm.items),function(item,index){return [(_vm.isOption(item))?[_c('gl-listbox-item',_vm._b({key:item.value,attrs:{"is-selected":_vm.isSelected(item),"is-focused":_vm.isFocused(item),"is-check-centered":_vm.isCheckCentered},on:{"select":function($event){return _vm.onSelect(item, $event)}}},'gl-listbox-item',_vm.listboxItemMoreItemsAriaAttributes(index),false),[_vm._t("list-item",function(){return [_vm._v("\n "+_vm._s(item.text)+"\n ")]},{"item":item})],2)]:[_c('gl-listbox-group',{key:item.text,class:_vm.groupClasses(index),attrs:{"name":item.text},scopedSlots:_vm._u([(_vm.$scopedSlots['group-label'])?{key:"group-label",fn:function(){return [_vm._t("group-label",null,{"group":item})]},proxy:true}:null],null,true)},[_vm._v(" "),_vm._l((item.options),function(option){return _c('gl-listbox-item',{key:option.value,attrs:{"is-selected":_vm.isSelected(option),"is-focused":_vm.isFocused(option),"is-check-centered":_vm.isCheckCentered},on:{"select":function($event){return _vm.onSelect(option, $event)}}},[_vm._t("list-item",function(){return [_vm._v("\n "+_vm._s(option.text)+"\n ")]},{"item":option})],2)})],2)]]}),_vm._v(" "),(_vm.showIntersectionObserver)?_c('gl-intersection-observer',{on:{"appear":_vm.onIntersectionObserverAppear}}):_vm._e()],2):_vm._e(),_vm._v(" "),(_vm.infiniteScrollLoading)?_c('gl-loading-icon',{staticClass:"gl-my-3",attrs:{"data-testid":"listbox-infinite-scroll-loader","size":"md"}}):_vm._e(),_vm._v(" "),(_vm.announceSRSearchResults)?_c('span',{staticClass:"gl-sr-only",attrs:{"data-testid":"listbox-number-of-results","aria-live":"assertive"}},[_vm._t("search-summary-sr-only")],2):(_vm.showNoResultsText)?_c('div',{staticClass:"gl-pl-7 gl-pr-5 gl-pt-3 gl-font-base gl-text-gray-600",attrs:{"aria-live":"assertive","data-testid":"listbox-no-results-text"}},[_vm._v("\n "+_vm._s(_vm.noResultsText)+"\n ")]):_vm._e(),_vm._v(" "),_vm._t("footer")],2)};
|
|
469
528
|
var __vue_staticRenderFns__ = [];
|
|
470
529
|
|
|
471
530
|
/* style */
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mount } from '@vue/test-utils';
|
|
2
2
|
import { nextTick } from 'vue';
|
|
3
|
+
import { useMockIntersectionObserver } from '~/utils/use_mock_intersection_observer';
|
|
3
4
|
import GlBaseDropdown from '../base_dropdown/base_dropdown.vue';
|
|
4
5
|
import {
|
|
5
6
|
GL_DROPDOWN_SHOWN,
|
|
@@ -9,6 +10,7 @@ import {
|
|
|
9
10
|
HOME,
|
|
10
11
|
END,
|
|
11
12
|
} from '../constants';
|
|
13
|
+
import GlIntersectionObserver from '../../../utilities/intersection_observer/intersection_observer.vue';
|
|
12
14
|
import GlListbox, { ITEM_SELECTOR } from './listbox.vue';
|
|
13
15
|
import GlListboxItem from './listbox_item.vue';
|
|
14
16
|
import GlListboxGroup from './listbox_group.vue';
|
|
@@ -25,6 +27,8 @@ describe('GlListbox', () => {
|
|
|
25
27
|
});
|
|
26
28
|
};
|
|
27
29
|
|
|
30
|
+
useMockIntersectionObserver();
|
|
31
|
+
|
|
28
32
|
const findBaseDropdown = () => wrapper.findComponent(GlBaseDropdown);
|
|
29
33
|
const findListContainer = () => wrapper.find('[role="listbox"]');
|
|
30
34
|
const findListboxItems = (root = wrapper) => root.findAllComponents(GlListboxItem);
|
|
@@ -36,6 +40,7 @@ describe('GlListbox', () => {
|
|
|
36
40
|
const findLoadingIcon = () => wrapper.find("[data-testid='listbox-search-loader']");
|
|
37
41
|
const findSRNumberOfResultsText = () => wrapper.find("[data-testid='listbox-number-of-results']");
|
|
38
42
|
const findResetButton = () => wrapper.find("[data-testid='listbox-reset-button']");
|
|
43
|
+
const findIntersectionObserver = () => wrapper.findComponent(GlIntersectionObserver);
|
|
39
44
|
|
|
40
45
|
describe('toggle text', () => {
|
|
41
46
|
describe.each`
|
|
@@ -435,4 +440,125 @@ describe('GlListbox', () => {
|
|
|
435
440
|
expect(wrapper.vm.closeAndFocus).toHaveBeenCalled();
|
|
436
441
|
});
|
|
437
442
|
});
|
|
443
|
+
|
|
444
|
+
describe('when `infiniteScroll` prop is `true`', () => {
|
|
445
|
+
it('should throw an error when items are groups', () => {
|
|
446
|
+
expect(() => {
|
|
447
|
+
buildWrapper({
|
|
448
|
+
items: mockGroups,
|
|
449
|
+
infiniteScroll: true,
|
|
450
|
+
});
|
|
451
|
+
}).toThrow(
|
|
452
|
+
'Infinite scroll does not support groups. Please set the "infiniteScroll" prop to "false"'
|
|
453
|
+
);
|
|
454
|
+
expect(wrapper).toHaveLoggedVueErrors();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it('renders `GlIntersectionObserver` component', () => {
|
|
458
|
+
buildWrapper({
|
|
459
|
+
headerText: 'Select assignee',
|
|
460
|
+
items: mockOptions,
|
|
461
|
+
infiniteScroll: true,
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
expect(findIntersectionObserver().exists()).toBe(true);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
describe('when bottom of listbox is reached', () => {
|
|
468
|
+
it('emits `bottom-reached` event', () => {
|
|
469
|
+
buildWrapper({
|
|
470
|
+
items: mockOptions,
|
|
471
|
+
infiniteScroll: true,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
findIntersectionObserver().vm.$emit('appear');
|
|
475
|
+
|
|
476
|
+
expect(wrapper.emitted('bottom-reached')).toEqual([[]]);
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
describe('when `loading` prop is `true`', () => {
|
|
481
|
+
it('does not render `GlIntersectionObserver` component', () => {
|
|
482
|
+
buildWrapper({
|
|
483
|
+
items: mockOptions,
|
|
484
|
+
infiniteScroll: true,
|
|
485
|
+
loading: true,
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
expect(findIntersectionObserver().exists()).toBe(false);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
describe('when `searching` prop is `true`', () => {
|
|
493
|
+
it('does not render `GlIntersectionObserver` component', () => {
|
|
494
|
+
buildWrapper({
|
|
495
|
+
items: mockOptions,
|
|
496
|
+
infiniteScroll: true,
|
|
497
|
+
searching: true,
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
expect(findIntersectionObserver().exists()).toBe(false);
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe('when `infiniteScrollLoading` prop is `true`', () => {
|
|
505
|
+
beforeEach(() => {
|
|
506
|
+
buildWrapper({
|
|
507
|
+
items: mockOptions,
|
|
508
|
+
infiniteScroll: true,
|
|
509
|
+
infiniteScrollLoading: true,
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it('shows loading icon', () => {
|
|
514
|
+
expect(wrapper.find('[data-testid="listbox-infinite-scroll-loader"]').exists()).toBe(true);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it('does not render `GlIntersectionObserver` component', () => {
|
|
518
|
+
expect(findIntersectionObserver().exists()).toBe(false);
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
describe('when `totalItems` prop is set', () => {
|
|
523
|
+
it('adds `aria-setsize` and `aria-posinset` attributes to listbox items', () => {
|
|
524
|
+
const totalItems = mockOptions.length;
|
|
525
|
+
|
|
526
|
+
buildWrapper({
|
|
527
|
+
items: mockOptions,
|
|
528
|
+
infiniteScroll: true,
|
|
529
|
+
totalItems,
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
findListboxItems().wrappers.forEach((listboxItem, index) => {
|
|
533
|
+
expect(listboxItem.attributes('aria-setsize')).toBe(totalItems.toString());
|
|
534
|
+
expect(listboxItem.attributes('aria-posinset')).toBe((index + 1).toString());
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
describe('when `totalItems` prop is not set', () => {
|
|
540
|
+
it('does not add `aria-setsize` and `aria-posinset` attributes to listbox items', () => {
|
|
541
|
+
buildWrapper({
|
|
542
|
+
items: mockOptions,
|
|
543
|
+
infiniteScroll: true,
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
findListboxItems().wrappers.forEach((listboxItem) => {
|
|
547
|
+
expect(listboxItem.attributes('aria-setsize')).toBe(undefined);
|
|
548
|
+
expect(listboxItem.attributes('aria-posinset')).toBe(undefined);
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
describe('when `infiniteScroll` prop is `false`', () => {
|
|
555
|
+
it('does not render `GlIntersectionObserver` component', () => {
|
|
556
|
+
buildWrapper({
|
|
557
|
+
items: mockOptions,
|
|
558
|
+
infiniteScroll: false,
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
expect(findIntersectionObserver().exists()).toBe(false);
|
|
562
|
+
});
|
|
563
|
+
});
|
|
438
564
|
});
|
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
GlAvatar,
|
|
14
14
|
} from '../../../../index';
|
|
15
15
|
import { makeContainer } from '../../../../utils/story_decorators/container';
|
|
16
|
+
import { disableControls } from '../../../../utils/stories_utils';
|
|
17
|
+
import { setStoryTimeout } from '../../../../utils/test_utils';
|
|
16
18
|
import readme from './listbox.md';
|
|
17
19
|
import { mockOptions, mockGroups } from './mock_data';
|
|
18
20
|
import { flattenedOptions } from './utils';
|
|
@@ -28,6 +30,8 @@ const generateProps = ({
|
|
|
28
30
|
loading = defaultValue('loading'),
|
|
29
31
|
searchable = defaultValue('searchable'),
|
|
30
32
|
searching = defaultValue('searching'),
|
|
33
|
+
infiniteScroll = defaultValue('infiniteScroll'),
|
|
34
|
+
infiniteScrollLoading = defaultValue('infiniteScrollLoading'),
|
|
31
35
|
noResultsText = defaultValue('noResultsText'),
|
|
32
36
|
searchPlaceholder = defaultValue('searchPlaceholder'),
|
|
33
37
|
noCaret = defaultValue('noCaret'),
|
|
@@ -51,6 +55,8 @@ const generateProps = ({
|
|
|
51
55
|
loading,
|
|
52
56
|
searchable,
|
|
53
57
|
searching,
|
|
58
|
+
infiniteScroll,
|
|
59
|
+
infiniteScrollLoading,
|
|
54
60
|
noResultsText,
|
|
55
61
|
searchPlaceholder,
|
|
56
62
|
noCaret,
|
|
@@ -77,6 +83,8 @@ const makeBindings = (overrides = {}) =>
|
|
|
77
83
|
':loading': 'loading',
|
|
78
84
|
':searchable': 'searchable',
|
|
79
85
|
':searching': 'searching',
|
|
86
|
+
':infinite-scroll': 'infiniteScroll',
|
|
87
|
+
':infinite-scroll-loading': 'infiniteScrollLoading',
|
|
80
88
|
':no-results-text': 'noResultsText',
|
|
81
89
|
':search-placeholder': 'searchPlaceholder',
|
|
82
90
|
':no-caret': 'noCaret',
|
|
@@ -534,3 +542,53 @@ SearchableGroups.args = generateProps({
|
|
|
534
542
|
items: mockGroups,
|
|
535
543
|
});
|
|
536
544
|
SearchableGroups.decorators = [makeContainer({ height: '370px' })];
|
|
545
|
+
|
|
546
|
+
export const InfiniteScroll = (
|
|
547
|
+
args,
|
|
548
|
+
{ argTypes: { infiniteScroll, infiniteScrollLoading, items, ...argTypes } }
|
|
549
|
+
) => ({
|
|
550
|
+
props: Object.keys(argTypes),
|
|
551
|
+
components: {
|
|
552
|
+
GlListbox,
|
|
553
|
+
},
|
|
554
|
+
data() {
|
|
555
|
+
return {
|
|
556
|
+
selected: mockOptions[1].value,
|
|
557
|
+
items: mockOptions.slice(0, 10),
|
|
558
|
+
infiniteScrollLoading: false,
|
|
559
|
+
infiniteScroll: true,
|
|
560
|
+
};
|
|
561
|
+
},
|
|
562
|
+
mounted() {
|
|
563
|
+
if (this.startOpened) {
|
|
564
|
+
openListbox(this);
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
methods: {
|
|
568
|
+
onBottomReached() {
|
|
569
|
+
this.infiniteScrollLoading = true;
|
|
570
|
+
|
|
571
|
+
setStoryTimeout(() => {
|
|
572
|
+
this.items.push(...mockOptions.slice(10, 12));
|
|
573
|
+
this.infiniteScrollLoading = false;
|
|
574
|
+
this.infiniteScroll = false;
|
|
575
|
+
}, 1000);
|
|
576
|
+
},
|
|
577
|
+
},
|
|
578
|
+
template: template('', {
|
|
579
|
+
label: `<span class="gl-my-0" id="listbox-label">Select a department</span>`,
|
|
580
|
+
bindingOverrides: {
|
|
581
|
+
':items': 'items',
|
|
582
|
+
':infinite-scroll': 'infiniteScroll',
|
|
583
|
+
':infinite-scroll-loading': 'infiniteScrollLoading',
|
|
584
|
+
':total-items': 12,
|
|
585
|
+
'@bottom-reached': 'onBottomReached',
|
|
586
|
+
},
|
|
587
|
+
}),
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
InfiniteScroll.argTypes = {
|
|
591
|
+
...disableControls(['infiniteScroll', 'infiniteScrollLoading', 'items']),
|
|
592
|
+
};
|
|
593
|
+
InfiniteScroll.args = generateProps();
|
|
594
|
+
InfiniteScroll.decorators = [makeContainer({ height: '370px' })];
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from '../../../../utils/constants';
|
|
18
18
|
import GlButton from '../../button/button.vue';
|
|
19
19
|
import GlLoadingIcon from '../../loading_icon/loading_icon.vue';
|
|
20
|
+
import GlIntersectionObserver from '../../../utilities/intersection_observer/intersection_observer.vue';
|
|
20
21
|
import GlSearchBoxByType from '../../search_box_by_type/search_box_by_type.vue';
|
|
21
22
|
import GlBaseDropdown from '../base_dropdown/base_dropdown.vue';
|
|
22
23
|
import GlListboxItem from './listbox_item.vue';
|
|
@@ -43,6 +44,7 @@ export default {
|
|
|
43
44
|
GlSearchBoxByType,
|
|
44
45
|
GlListboxSearchInput,
|
|
45
46
|
GlLoadingIcon,
|
|
47
|
+
GlIntersectionObserver,
|
|
46
48
|
},
|
|
47
49
|
model: {
|
|
48
50
|
prop: 'selected',
|
|
@@ -215,6 +217,37 @@ export default {
|
|
|
215
217
|
required: false,
|
|
216
218
|
default: false,
|
|
217
219
|
},
|
|
220
|
+
/**
|
|
221
|
+
* Enables infinite scroll.
|
|
222
|
+
* When set to `true`, the `@bottom-reached` event will be fired when
|
|
223
|
+
* the bottom of the listbox is scrolled to.
|
|
224
|
+
* Does not support groups.
|
|
225
|
+
*/
|
|
226
|
+
infiniteScroll: {
|
|
227
|
+
type: Boolean,
|
|
228
|
+
required: false,
|
|
229
|
+
default: false,
|
|
230
|
+
},
|
|
231
|
+
/**
|
|
232
|
+
* This prop is used for infinite scroll.
|
|
233
|
+
* It represents the total number of items that exist,
|
|
234
|
+
* even if they have not yet been loaded.
|
|
235
|
+
* Do not set this prop if the total number of items is unknown.
|
|
236
|
+
*/
|
|
237
|
+
totalItems: {
|
|
238
|
+
type: Number,
|
|
239
|
+
required: false,
|
|
240
|
+
default: null,
|
|
241
|
+
},
|
|
242
|
+
/**
|
|
243
|
+
* This prop is used for infinite scroll.
|
|
244
|
+
* Set to `true` when more items are being loaded.
|
|
245
|
+
*/
|
|
246
|
+
infiniteScrollLoading: {
|
|
247
|
+
type: Boolean,
|
|
248
|
+
required: false,
|
|
249
|
+
default: false,
|
|
250
|
+
},
|
|
218
251
|
/**
|
|
219
252
|
* Message to be displayed when filtering produced no results
|
|
220
253
|
*/
|
|
@@ -298,6 +331,9 @@ export default {
|
|
|
298
331
|
}
|
|
299
332
|
return Boolean(this.selected);
|
|
300
333
|
},
|
|
334
|
+
showIntersectionObserver() {
|
|
335
|
+
return this.infiniteScroll && !this.infiniteScrollLoading && !this.loading && !this.searching;
|
|
336
|
+
},
|
|
301
337
|
},
|
|
302
338
|
watch: {
|
|
303
339
|
selected: {
|
|
@@ -324,6 +360,17 @@ export default {
|
|
|
324
360
|
},
|
|
325
361
|
},
|
|
326
362
|
},
|
|
363
|
+
created() {
|
|
364
|
+
if (
|
|
365
|
+
process.env.NODE_ENV !== 'production' &&
|
|
366
|
+
this.infiniteScroll &&
|
|
367
|
+
this.items.some((item) => !isOption(item))
|
|
368
|
+
) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
'Infinite scroll does not support groups. Please set the "infiniteScroll" prop to "false"'
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
},
|
|
327
374
|
methods: {
|
|
328
375
|
open() {
|
|
329
376
|
this.$refs.baseDropdown.open();
|
|
@@ -467,6 +514,25 @@ export default {
|
|
|
467
514
|
closeAndFocus() {
|
|
468
515
|
this.$refs.baseDropdown.closeAndFocus();
|
|
469
516
|
},
|
|
517
|
+
onIntersectionObserverAppear() {
|
|
518
|
+
/**
|
|
519
|
+
* Emitted when bottom of listbox has been scrolled to.
|
|
520
|
+
* Used for infinite scroll.
|
|
521
|
+
*
|
|
522
|
+
* @event bottom-reached
|
|
523
|
+
*/
|
|
524
|
+
this.$emit('bottom-reached');
|
|
525
|
+
},
|
|
526
|
+
listboxItemMoreItemsAriaAttributes(index) {
|
|
527
|
+
if (this.totalItems === null) {
|
|
528
|
+
return {};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return {
|
|
532
|
+
'aria-setsize': this.totalItems,
|
|
533
|
+
'aria-posinset': index + 1,
|
|
534
|
+
};
|
|
535
|
+
},
|
|
470
536
|
isOption,
|
|
471
537
|
},
|
|
472
538
|
};
|
|
@@ -551,6 +617,7 @@ export default {
|
|
|
551
617
|
:is-selected="isSelected(item)"
|
|
552
618
|
:is-focused="isFocused(item)"
|
|
553
619
|
:is-check-centered="isCheckCentered"
|
|
620
|
+
v-bind="listboxItemMoreItemsAriaAttributes(index)"
|
|
554
621
|
@select="onSelect(item, $event)"
|
|
555
622
|
>
|
|
556
623
|
<!-- @slot Custom template of the listbox item -->
|
|
@@ -583,8 +650,17 @@ export default {
|
|
|
583
650
|
</gl-listbox-group>
|
|
584
651
|
</template>
|
|
585
652
|
</template>
|
|
653
|
+
<gl-intersection-observer
|
|
654
|
+
v-if="showIntersectionObserver"
|
|
655
|
+
@appear="onIntersectionObserverAppear"
|
|
656
|
+
/>
|
|
586
657
|
</component>
|
|
587
|
-
|
|
658
|
+
<gl-loading-icon
|
|
659
|
+
v-if="infiniteScrollLoading"
|
|
660
|
+
data-testid="listbox-infinite-scroll-loader"
|
|
661
|
+
size="md"
|
|
662
|
+
class="gl-my-3"
|
|
663
|
+
/>
|
|
588
664
|
<span
|
|
589
665
|
v-if="announceSRSearchResults"
|
|
590
666
|
data-testid="listbox-number-of-results"
|