Commit e0fd89fd authored by Dave Pisek's avatar Dave Pisek

Use GlSearchBoxByType in project selector

Replaces the vanilla input field within the project-selector component
with a `<gl-search-box-by-type>` to improve design consistency.
parent 3f734b69
<script> <script>
import _ from 'underscore'; import _ from 'underscore';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon, GlSearchBoxByType } from '@gitlab/ui';
import ProjectListItem from './project_list_item.vue'; import ProjectListItem from './project_list_item.vue';
const SEARCH_INPUT_TIMEOUT_MS = 500; const SEARCH_INPUT_TIMEOUT_MS = 500;
...@@ -9,6 +9,7 @@ export default { ...@@ -9,6 +9,7 @@ export default {
name: 'ProjectSelector', name: 'ProjectSelector',
components: { components: {
GlLoadingIcon, GlLoadingIcon,
GlSearchBoxByType,
ProjectListItem, ProjectListItem,
}, },
props: { props: {
...@@ -53,9 +54,6 @@ export default { ...@@ -53,9 +54,6 @@ export default {
isSelected(project) { isSelected(project) {
return Boolean(_.findWhere(this.selectedProjects, { id: project.id })); return Boolean(_.findWhere(this.selectedProjects, { id: project.id }));
}, },
focusSearchInput() {
this.$refs.searchInput.focus();
},
onInput: _.debounce(function debouncedOnInput() { onInput: _.debounce(function debouncedOnInput() {
this.$emit('searched', this.searchQuery); this.$emit('searched', this.searchQuery);
}, SEARCH_INPUT_TIMEOUT_MS), }, SEARCH_INPUT_TIMEOUT_MS),
...@@ -64,12 +62,11 @@ export default { ...@@ -64,12 +62,11 @@ export default {
</script> </script>
<template> <template>
<div> <div>
<input <gl-search-box-by-type
ref="searchInput"
v-model="searchQuery" v-model="searchQuery"
:placeholder="__('Search your projects')" :placeholder="__('Search your projects')"
type="search" type="search"
class="form-control mb-3 js-project-selector-input" class="mb-3"
autofocus autofocus
@input="onInput" @input="onInput"
/> />
......
---
title: Align project selector search box better with design system
merge_request: 16795
author:
type: changed
...@@ -99,9 +99,6 @@ export default { ...@@ -99,9 +99,6 @@ export default {
addProjects() { addProjects() {
this.addProjectsToDashboard(); this.addProjectsToDashboard();
}, },
onModalShown() {
this.$refs.projectSelector.focusSearchInput();
},
onModalHidden() { onModalHidden() {
this.clearSearchResults(); this.clearSearchResults();
}, },
...@@ -127,7 +124,6 @@ export default { ...@@ -127,7 +124,6 @@ export default {
:ok-title="$options.addProjectsModalSubmit" :ok-title="$options.addProjectsModalSubmit"
:ok-disabled="okDisabled" :ok-disabled="okDisabled"
ok-variant="success" ok-variant="success"
@shown="onModalShown"
@hidden="onModalHidden" @hidden="onModalHidden"
@ok="onOk" @ok="onOk"
> >
......
...@@ -80,9 +80,6 @@ export default { ...@@ -80,9 +80,6 @@ export default {
addProjects() { addProjects() {
this.addProjectsToDashboard(); this.addProjectsToDashboard();
}, },
onModalShown() {
this.$refs.projectSelector.focusSearchInput();
},
onCancel() { onCancel() {
this.clearSearchResults(); this.clearSearchResults();
}, },
...@@ -110,7 +107,6 @@ export default { ...@@ -110,7 +107,6 @@ export default {
:ok-title="s__('OperationsDashboard|Add projects')" :ok-title="s__('OperationsDashboard|Add projects')"
:ok-disabled="okDisabled" :ok-disabled="okDisabled"
ok-variant="success" ok-variant="success"
@shown="onModalShown"
@cancel="onCancel" @cancel="onCancel"
@ok="onOk" @ok="onOk"
> >
......
...@@ -2,7 +2,9 @@ import Vue from 'vue'; ...@@ -2,7 +2,9 @@ import Vue from 'vue';
import _ from 'underscore'; import _ from 'underscore';
import ProjectSelector from '~/vue_shared/components/project_selector/project_selector.vue'; import ProjectSelector from '~/vue_shared/components/project_selector/project_selector.vue';
import ProjectListItem from '~/vue_shared/components/project_selector/project_list_item.vue'; import ProjectListItem from '~/vue_shared/components/project_selector/project_list_item.vue';
import { shallowMount } from '@vue/test-utils';
import { GlSearchBoxByType } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { trimText } from 'spec/helpers/text_helper'; import { trimText } from 'spec/helpers/text_helper';
describe('ProjectSelector component', () => { describe('ProjectSelector component', () => {
...@@ -14,10 +16,12 @@ describe('ProjectSelector component', () => { ...@@ -14,10 +16,12 @@ describe('ProjectSelector component', () => {
let selected = []; let selected = [];
selected = selected.concat(allProjects.slice(0, 3)).concat(allProjects.slice(5, 8)); selected = selected.concat(allProjects.slice(0, 3)).concat(allProjects.slice(5, 8));
const findSearchInput = () => wrapper.find(GlSearchBoxByType).find('input');
beforeEach(() => { beforeEach(() => {
jasmine.clock().install(); jasmine.clock().install();
wrapper = shallowMount(Vue.extend(ProjectSelector), { wrapper = mount(Vue.extend(ProjectSelector), {
propsData: { propsData: {
projectSearchResults: searchResults, projectSearchResults: searchResults,
selectedProjects: selected, selectedProjects: selected,
...@@ -26,6 +30,7 @@ describe('ProjectSelector component', () => { ...@@ -26,6 +30,7 @@ describe('ProjectSelector component', () => {
showLoadingIndicator: false, showLoadingIndicator: false,
showSearchErrorMessage: false, showSearchErrorMessage: false,
}, },
sync: false,
attachToDocument: true, attachToDocument: true,
}); });
...@@ -44,7 +49,8 @@ describe('ProjectSelector component', () => { ...@@ -44,7 +49,8 @@ describe('ProjectSelector component', () => {
it(`triggers a (debounced) search when the search input value changes`, () => { it(`triggers a (debounced) search when the search input value changes`, () => {
spyOn(vm, '$emit'); spyOn(vm, '$emit');
const query = 'my test query!'; const query = 'my test query!';
const searchInput = wrapper.find('.js-project-selector-input'); const searchInput = findSearchInput();
searchInput.setValue(query); searchInput.setValue(query);
searchInput.trigger('input'); searchInput.trigger('input');
...@@ -56,7 +62,7 @@ describe('ProjectSelector component', () => { ...@@ -56,7 +62,7 @@ describe('ProjectSelector component', () => {
it(`debounces the search input`, () => { it(`debounces the search input`, () => {
spyOn(vm, '$emit'); spyOn(vm, '$emit');
const searchInput = wrapper.find('.js-project-selector-input'); const searchInput = findSearchInput();
const updateSearchQuery = (count = 0) => { const updateSearchQuery = (count = 0) => {
if (count === 10) { if (count === 10) {
...@@ -77,9 +83,9 @@ describe('ProjectSelector component', () => { ...@@ -77,9 +83,9 @@ describe('ProjectSelector component', () => {
}); });
it(`includes a placeholder in the search box`, () => { it(`includes a placeholder in the search box`, () => {
expect(wrapper.find('.js-project-selector-input').attributes('placeholder')).toBe( const searchInput = findSearchInput();
'Search your projects',
); expect(searchInput.attributes('placeholder')).toBe('Search your projects');
}); });
it(`triggers a "projectClicked" event when a project is clicked`, () => { it(`triggers a "projectClicked" event when a project is clicked`, () => {
...@@ -92,41 +98,35 @@ describe('ProjectSelector component', () => { ...@@ -92,41 +98,35 @@ describe('ProjectSelector component', () => {
it(`shows a "no results" message if showNoResultsMessage === true`, () => { it(`shows a "no results" message if showNoResultsMessage === true`, () => {
wrapper.setProps({ showNoResultsMessage: true }); wrapper.setProps({ showNoResultsMessage: true });
expect(wrapper.contains('.js-no-results-message')).toBe(true); return vm.$nextTick().then(() => {
const noResultsEl = wrapper.find('.js-no-results-message');
const noResultsEl = wrapper.find('.js-no-results-message'); expect(noResultsEl.exists()).toBe(true);
expect(trimText(noResultsEl.text())).toEqual('Sorry, no projects matched your search');
expect(trimText(noResultsEl.text())).toEqual('Sorry, no projects matched your search'); });
}); });
it(`shows a "minimum search query" message if showMinimumSearchQueryMessage === true`, () => { it(`shows a "minimum search query" message if showMinimumSearchQueryMessage === true`, () => {
wrapper.setProps({ showMinimumSearchQueryMessage: true }); wrapper.setProps({ showMinimumSearchQueryMessage: true });
expect(wrapper.contains('.js-minimum-search-query-message')).toBe(true); return vm.$nextTick().then(() => {
const minimumSearchEl = wrapper.find('.js-minimum-search-query-message');
const minimumSearchEl = wrapper.find('.js-minimum-search-query-message');
expect(trimText(minimumSearchEl.text())).toEqual('Enter at least three characters to search'); expect(minimumSearchEl.exists()).toBe(true);
expect(trimText(minimumSearchEl.text())).toEqual('Enter at least three characters to search');
});
}); });
it(`shows a error message if showSearchErrorMessage === true`, () => { it(`shows a error message if showSearchErrorMessage === true`, () => {
wrapper.setProps({ showSearchErrorMessage: true }); wrapper.setProps({ showSearchErrorMessage: true });
expect(wrapper.contains('.js-search-error-message')).toBe(true); return vm.$nextTick().then(() => {
const errorMessageEl = wrapper.find('.js-search-error-message');
const errorMessageEl = wrapper.find('.js-search-error-message');
expect(trimText(errorMessageEl.text())).toEqual(
'Something went wrong, unable to search projects',
);
});
it(`focuses the input element when the focusSearchInput() method is called`, () => { expect(errorMessageEl.exists()).toBe(true);
const input = wrapper.find('.js-project-selector-input'); expect(trimText(errorMessageEl.text())).toEqual(
'Something went wrong, unable to search projects',
expect(document.activeElement).not.toBe(input.element); );
vm.focusSearchInput(); });
expect(document.activeElement).toBe(input.element);
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment