Commit d05f4fc2 authored by Martin Wortschack's avatar Martin Wortschack

Apply label color to tokens

- Add background color and
text color to selected
label tokens.
parent 3bb340f2
<script> <script>
import { import {
GlToken,
GlFilteredSearchToken, GlFilteredSearchToken,
GlFilteredSearchSuggestion, GlFilteredSearchSuggestion,
GlDropdownDivider, GlDropdownDivider,
...@@ -11,6 +12,7 @@ import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/cons ...@@ -11,6 +12,7 @@ import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/cons
export default { export default {
components: { components: {
GlToken,
GlFilteredSearchToken, GlFilteredSearchToken,
GlFilteredSearchSuggestion, GlFilteredSearchSuggestion,
GlDropdownDivider, GlDropdownDivider,
...@@ -26,11 +28,40 @@ export default { ...@@ -26,11 +28,40 @@ export default {
type: Object, type: Object,
required: true, required: true,
}, },
active: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
activeLabel: null,
};
}, },
computed: { computed: {
labels() { labels() {
return this.config.labels; return this.config.labels;
}, },
containerStyle() {
if (this.activeLabel) {
const { color, text_color } = this.activeLabel;
return { backgroundColor: color, color: text_color };
}
return {};
},
},
watch: {
active: {
immediate: true,
handler(newValue) {
if (!newValue && this.labels) {
this.activeLabel = this.labels.find(l => l.title === this.value?.data);
}
},
},
}, },
created() { created() {
this.searchLabels(this.value); this.searchLabels(this.value);
...@@ -56,9 +87,17 @@ export default { ...@@ -56,9 +87,17 @@ export default {
v-on="$listeners" v-on="$listeners"
@input="searchLabels" @input="searchLabels"
> >
<template #view="{ inputValue }"> <template #view-token="{ inputValue, cssClasses, listeners }">
<template v-if="config.symbol">{{ config.symbol }}</template <gl-token
>{{ inputValue }} variant="search-value"
:class="cssClasses"
:style="containerStyle"
data-testid="selected-label"
v-on="listeners"
>
<template v-if="config.symbol">{{ config.symbol }}</template>
{{ activeLabel ? activeLabel.title : inputValue }}
</gl-token>
</template> </template>
<template #suggestions> <template #suggestions>
<gl-loading-icon v-if="config.isLoading" /> <gl-loading-icon v-if="config.isLoading" />
......
---
title: Apply label color to shared Analytics label token
merge_request: 36205
author:
type: other
...@@ -16,9 +16,10 @@ describe('LabelToken', () => { ...@@ -16,9 +16,10 @@ describe('LabelToken', () => {
isLoading: false, isLoading: false,
fetchData: jest.fn(), fetchData: jest.fn(),
}; };
const stubs = { const stubs = {
GlFilteredSearchToken: { GlFilteredSearchToken: {
template: `<div><slot name="suggestions"></slot></div>`, template: `<div><slot name="view-token"></slot><slot name="suggestions"></slot></div>`,
}, },
}; };
...@@ -38,6 +39,7 @@ describe('LabelToken', () => { ...@@ -38,6 +39,7 @@ describe('LabelToken', () => {
const findFilteredSearchToken = () => wrapper.find(GlFilteredSearchToken); const findFilteredSearchToken = () => wrapper.find(GlFilteredSearchToken);
const findAllLabelSuggestions = () => wrapper.findAll({ ref: 'labelItem' }); const findAllLabelSuggestions = () => wrapper.findAll({ ref: 'labelItem' });
const findLoadingIcon = () => wrapper.find(GlLoadingIcon); const findLoadingIcon = () => wrapper.find(GlLoadingIcon);
const findSelectedLabelToken = () => wrapper.find('[data-testid="selected-label"]');
it('renders a loading icon', () => { it('renders a loading icon', () => {
createComponent({ config: { ...defaultConfig, isLoading: true }, value: {} }, { stubs }); createComponent({ config: { ...defaultConfig, isLoading: true }, value: {} }, { stubs });
...@@ -45,6 +47,22 @@ describe('LabelToken', () => { ...@@ -45,6 +47,22 @@ describe('LabelToken', () => {
expect(findLoadingIcon().exists()).toBe(true); expect(findLoadingIcon().exists()).toBe(true);
}); });
it('renders the selected label', () => {
const selectedLabel = mockLabels[0];
createComponent({ value: { data: selectedLabel.title } });
expect(findSelectedLabelToken().text()).toContain(selectedLabel.title);
});
it("sets the label's background and text color on the gl-token component", () => {
const selectedLabel = mockLabels[0];
createComponent({ value: { data: selectedLabel.title } });
expect(findSelectedLabelToken().attributes('style')).toEqual(
'background-color: rgb(98, 53, 242); color: rgb(255, 255, 255);',
);
});
describe('suggestions', () => { describe('suggestions', () => {
it('renders a suggestion for each item', () => { it('renders a suggestion for each item', () => {
createComponent(); createComponent();
......
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