-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3: experimenting with CvTag component, including stories #1119
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67cf360
add an initial CvTag component to v3
Fontinalis 63de1f9
update CvTag story
Fontinalis a86f5a4
v3: update CvTag stories
Fontinalis 285825b
v3: clean up CvTag component
Fontinalis 36a6f35
Merge branch 'master' into v3/cv-tag
Fontinalis 162a1ee
add tests for full coverage on CvTag
Fontinalis 5a61f94
Merge branch 'master' into v3/cv-tag
Fontinalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import CvTag from './CvTag'; | ||
import { tagKinds } from './consts.js'; | ||
import { action } from '@storybook/addon-actions'; | ||
|
||
export default { | ||
title: 'Components/CvTag', | ||
component: CvTag, | ||
argTypes: { | ||
kind: { control: { type: 'select', options: tagKinds } }, | ||
}, | ||
}; | ||
|
||
const Template = (args, { argTypes }) => { | ||
return { | ||
props: Object.keys(argTypes), | ||
components: { CvTag }, | ||
template: `<CvTag @remove="onRemove" v-bind="$props" />`, | ||
setup(props) { | ||
return { | ||
onRemove: action('remove'), | ||
}; | ||
}, | ||
}; | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
kind: 'red', | ||
label: 'This is a tag', | ||
filter: false, | ||
}; | ||
Default.parameters = { | ||
docs: { | ||
source: { | ||
code: ` | ||
<CvTag | ||
kind="red" | ||
label="This is a tag" | ||
:filter="false" | ||
/> | ||
`, | ||
}, | ||
}, | ||
}; | ||
|
||
export const Filter = Template.bind({}); | ||
Filter.args = { | ||
kind: 'teal', | ||
label: 'This is a tag', | ||
filter: true, | ||
}; | ||
Filter.parameters = { | ||
docs: { | ||
source: { | ||
code: ` | ||
<CvTag | ||
kind="teal" | ||
label="This is a tag" | ||
:filter="true" | ||
/> | ||
`, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<template> | ||
<span :class="tagClasses" role="listitem" :title="title"> | ||
<span :class="`${carbonPrefix}--tag__label`"> | ||
{{ label }} | ||
</span> | ||
<button | ||
v-if="filter" | ||
:class="`${carbonPrefix}--tag__close-icon`" | ||
:aria-label="clearAriaLabel" | ||
@click="onRemove" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a good reason not to use |
||
:disabled="disabled" | ||
> | ||
<Close16 /> | ||
</button> | ||
</span> | ||
</template> | ||
|
||
<script> | ||
import { computed } from 'vue'; | ||
import { carbonPrefix } from '../../global/settings'; | ||
import { tagKinds } from './consts'; | ||
import Close16 from '@carbon/icons-vue/es/close/16'; | ||
|
||
export default { | ||
name: 'CvTag', | ||
components: { Close16 }, | ||
props: { | ||
clearAriaLabel: { type: String, default: 'Clear filter' }, | ||
/** | ||
* disabled by property or if skeleton | ||
*/ | ||
disabled: Boolean, | ||
/** | ||
* label to be used in the CvTag component | ||
*/ | ||
label: { type: String, required: true }, | ||
/** | ||
* kind of the CvTag | ||
*/ | ||
kind: { | ||
type: String, | ||
default: 'red', | ||
validator(val) { | ||
return tagKinds.includes(val); | ||
}, | ||
}, | ||
/** | ||
* If filter is true, the CvTag will include a remove button on the right side, which on a click, will emit the 'remove' event. | ||
*/ | ||
filter: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
emits: [ | ||
/** | ||
* Triggers when the clear button is pressed on a filter CvTag | ||
*/ | ||
'remove', | ||
], | ||
setup(props, { emit }) { | ||
const title = computed(() => { | ||
return props.filter ? props.clearAriaLabel : null; | ||
}); | ||
|
||
const tagClasses = computed(() => { | ||
const classes = [ | ||
`${carbonPrefix}--tag`, | ||
`${carbonPrefix}--tag--${props.kind}`, | ||
]; | ||
|
||
if (props.filter) { | ||
classes.push(`${carbonPrefix}--tag--filter`); | ||
} | ||
|
||
if (props.disabled) { | ||
classes.push(`${carbonPrefix}--tag--disabled`); | ||
} | ||
return classes; | ||
}); | ||
|
||
const onRemove = () => { | ||
if (!props.disabled) { | ||
emit('remove'); | ||
} | ||
}; | ||
|
||
return { | ||
carbonPrefix, | ||
title, | ||
tagClasses, | ||
onRemove, | ||
}; | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { shallowMount } from '@vue/test-utils'; | ||
import { carbonPrefix } from '../../../global/settings'; | ||
|
||
import { CvTag } from '..'; | ||
|
||
describe('CvTag', () => { | ||
it('CvTag - default', () => { | ||
const tagKind = 'red'; | ||
const tagLabel = 'test tag label'; | ||
const wrapper = shallowMount(CvTag, { | ||
props: { | ||
kind: tagKind, | ||
label: tagLabel, | ||
filter: false, | ||
}, | ||
}); | ||
|
||
// check if the root span exists | ||
// - verify classes on the root span | ||
const tagSpan = wrapper.find('span'); | ||
expect(tagSpan.classes()).toContain(`${carbonPrefix}--tag`); | ||
expect(tagSpan.classes()).toContain(`${carbonPrefix}--tag--${tagKind}`); | ||
|
||
// check if the label exists | ||
// - verify the tag label class | ||
// - verify if the label's content is equal to the given label text | ||
const labelSpan = tagSpan.find('span'); | ||
expect(labelSpan.classes()).toContain(`${carbonPrefix}--tag__label`); | ||
expect(labelSpan.element.innerText).toEqual(); | ||
|
||
// check if the remove button exists | ||
// - it should not, becuase the filter is set to false | ||
expect(tagSpan.find('button').exists()).toBe(false); | ||
}); | ||
|
||
it('CvTag - filter', () => { | ||
const tagKind = 'green'; | ||
const tagLabel = 'test tag with filter'; | ||
const wrapper = shallowMount(CvTag, { | ||
props: { | ||
kind: tagKind, | ||
label: tagLabel, | ||
filter: true, | ||
}, | ||
}); | ||
|
||
// check if the root span exists | ||
// - verify classes on the root span | ||
const tagSpan = wrapper.find('span'); | ||
expect(tagSpan.classes()).toContain(`${carbonPrefix}--tag`); | ||
expect(tagSpan.classes()).toContain(`${carbonPrefix}--tag--${tagKind}`); | ||
|
||
// check if the label exists | ||
// - verify the tag label class | ||
// - verify if the label's content is equal to the given label text | ||
const labelSpan = tagSpan.find('span'); | ||
expect(labelSpan.classes()).toContain(`${carbonPrefix}--tag__label`); | ||
expect(labelSpan.element.innerText).toEqual(); | ||
|
||
// check if the remove button exists | ||
// - it should not, becuase the filter is set to false | ||
const removeButton = tagSpan.find('button'); | ||
expect(removeButton.exists()).toBe(true); | ||
|
||
// click on remove button | ||
removeButton.element.click(); | ||
|
||
// check if it emitted remove | ||
expect(wrapper.emitted().remove).toBeTruthy(); | ||
}); | ||
|
||
it('CvTag - disabled', () => { | ||
const tagKind = 'red'; | ||
const tagLabel = 'test tag label'; | ||
const wrapper = shallowMount(CvTag, { | ||
props: { | ||
kind: tagKind, | ||
label: tagLabel, | ||
filter: false, | ||
disabled: true, | ||
}, | ||
}); | ||
|
||
// check if the root span exists | ||
// - verify tag disabled class on span | ||
const tagSpan = wrapper.find('span'); | ||
expect(tagSpan.classes()).toContain(`${carbonPrefix}--tag--disabled`); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A direct call to onRemove when the tag is disabled catches the else path. // Call onRemove directly to test disabled path
wrapper.vm.onRemove(); |
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const tagKinds = [ | ||
'red', | ||
'magenta', | ||
'purple', | ||
'blue', | ||
'cyan', | ||
'teal', | ||
'green', | ||
'gray', | ||
'cool-gray', | ||
'warm-gray', | ||
'high-contrast', | ||
]; | ||
const CvTagConsts = { tagKinds }; | ||
export default CvTagConsts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import CvTag from './CvTag'; | ||
import CvTagConsts from './consts'; | ||
|
||
export { CvTag, CvTagConsts }; | ||
export default CvTag; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice will both borrow and raise an issue with Storybook regarding the docs output.