Skip to content

Commit

Permalink
feat(comp:tag-group): add tagGroup component (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenner8023 committed Apr 10, 2022
1 parent 39239be commit b365f3e
Show file tree
Hide file tree
Showing 19 changed files with 448 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/components/config/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ export const defaultConfig: GlobalConfig = {
},
},
tag: {},
tagGroup: {
gap: 8,
wrap: true,
},
textarea: {
autoRows: false,
clearable: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/components/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface GlobalConfig {
stepper: StepperConfig
table: TableConfig
tag: TagConfig
tagGroup: TagGroupConfig
textarea: TextareaConfig
timePicker: TimePickerConfig
timeRangePicker: TimeRangePickerConfig
Expand Down Expand Up @@ -417,6 +418,11 @@ export interface TagConfig {
shape?: TagShape
}

export interface TagGroupConfig {
gap: number | string
wrap: boolean
}

export interface TextareaConfig {
autoRows: boolean | TextareaAutoRows
clearable: boolean
Expand Down
3 changes: 2 additions & 1 deletion packages/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { IxStepper, IxStepperItem } from '@idux/components/stepper'
import { IxSwitch } from '@idux/components/switch'
import { IxTable, IxTableColumn } from '@idux/components/table'
import { IxTab, IxTabs } from '@idux/components/tabs'
import { IxTag } from '@idux/components/tag'
import { IxTag, IxTagGroup } from '@idux/components/tag'
import { IxTextarea } from '@idux/components/textarea'
import { IxTimePicker, IxTimeRangePicker } from '@idux/components/time-picker'
import { IxTimeline, IxTimelineItem } from '@idux/components/timeline'
Expand Down Expand Up @@ -146,6 +146,7 @@ const components = [
IxTab,
IxTabs,
IxTag,
IxTagGroup,
IxTextarea,
IxTimePicker,
IxTimeRangePicker,
Expand Down
1 change: 1 addition & 0 deletions packages/components/style/variable/prefix.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@header-prefix: ~'@{idux-prefix}-header';
@icon-prefix: ~'@{idux-prefix}-icon';
@tag-prefix: ~'@{idux-prefix}-tag';
@tag-group-prefix: ~'@{idux-prefix}-tag-group';
@typography-prefix: ~'@{idux-prefix}-typography';

// Layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 0
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -82,7 +84,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 1
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -108,7 +112,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 2
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -132,7 +138,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 3
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -158,7 +166,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 4
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -184,7 +194,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 5
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -208,7 +220,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 6
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -234,7 +248,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 7
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -260,7 +276,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 8
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand All @@ -284,7 +302,9 @@ exports[`Table > basic work > render work 1`] = `
<!---->London Park no. 9
</td>
<td class=\\"ix-table-align-start\\">
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span></span>
<!----><span class=\\"ix-tag ix-tag-success\\"><!----><!----><span class=\\"ix-tag-content\\">NICE</span>
<!----></span><span class=\\"ix-tag ix-tag-warning\\"><!----><!----><span class=\\"ix-tag-content\\">DEVELOPER</span>
<!----></span>
</td>
<td class=\\"ix-table-align-start\\">
<!----><a style=\\"margin-right: 8px;\\"></a><a></a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Vitest Snapshot v1

exports[`Tag > render work 1`] = `"<span class=\\"ix-tag\\"><!----><!----><span class=\\"ix-tag-content\\">test tag</span></span>"`;
exports[`Tag > render work 1`] = `
"<span class=\\"ix-tag\\"><!----><!----><span class=\\"ix-tag-content\\">test tag</span>
<!----></span>"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Vitest Snapshot v1

exports[`TagGroup > render work 1`] = `
"<div class=\\"ix-tag-group\\" style=\\"gap: 8px 8px;\\"><span class=\\"ix-tag\\"><!----><!----><span class=\\"ix-tag-content\\">A</span>
<!----></span><span class=\\"ix-tag\\"><!----><!----><span class=\\"ix-tag-content\\">B</span>
<!----></span>
</div>"
`;
115 changes: 115 additions & 0 deletions packages/components/tag/__tests__/tagGroup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { type MountingOptions, mount } from '@vue/test-utils'
import { h, ref } from 'vue'

import { renderWork } from '@tests'

import { IxIcon } from '@idux/components/icon'

import IxTagGroup from '../src/TagGroup'
import { TagGroupProps } from '../src/types'

describe('TagGroup', () => {
const TagGroupMount = (options?: MountingOptions<Partial<TagGroupProps>>) => mount(IxTagGroup, { ...options })
const dataSource = [
{ key: 'a', label: 'A' },
{ key: 'b', label: 'B' },
]

renderWork(IxTagGroup, { props: { dataSource } })

test('prop clickable work', async () => {
const clickFn = vi.fn()
const wrapper = TagGroupMount({ props: { dataSource, clickable: false, onClick: clickFn } })
await wrapper.find('.ix-tag').trigger('click')
expect(clickFn).toBeCalledTimes(0)
expect(wrapper.find('.ix-tag-group-clickable').exists()).toBe(false)

await wrapper.setProps({ clickable: true })
await wrapper.find('.ix-tag').trigger('click')
expect(clickFn).toBeCalledTimes(1)
expect(clickFn.mock.calls[0][0]).toBe('a')
expect(wrapper.find('.ix-tag-group-clickable').exists()).toBe(true)
})

test('prop activeKeys work', async () => {
const activeKeys = ref([])
const wrapper = mount({
components: { IxTagGroup },
template: `<IxTagGroup
v-model:activeKeys="activeKeys"
:clickable="true"
:dataSource="dataSource">
</IxTagGroup>`,
setup() {
return { activeKeys, dataSource }
},
})

await wrapper.find('.ix-tag').trigger('click')
expect(activeKeys.value.length).toBe(1)
expect(activeKeys.value[0]).toBe('a')

await wrapper.find('.ix-tag').trigger('click')
expect(activeKeys.value.length).toBe(0)
})

test('prop dataSource work', async () => {
const wrapper = TagGroupMount({ props: { dataSource } })

expect(wrapper.findAll('.ix-tag').length).toBe(2)
expect(wrapper.find('.ix-tag').text()).toBe('A')

await wrapper.setProps({ dataSource: [{ label: 'ABC' }] })
expect(wrapper.findAll('.ix-tag').length).toBe(1)
expect(wrapper.find('.ix-tag').text()).toBe('ABC')
})

test('prop closable work', async () => {
const closeFn = vi.fn()
const wrapper = TagGroupMount({ props: { dataSource, onClose: closeFn } })
expect(wrapper.find('.ix-icon-close').exists()).toBe(false)

await wrapper.setProps({ closable: true })
await wrapper.find('.ix-icon-close').trigger('click')
expect(closeFn).toBeCalledTimes(1)
expect(closeFn.mock.calls[0][0]).toBe('a')
})

test('prop gap work', async () => {
const wrapper = TagGroupMount({ props: { dataSource, gap: 16 } })

expect((wrapper.element as HTMLElement).style.gap).toBe('8px 16px')

await wrapper.setProps({ gap: 15 })
expect((wrapper.element as HTMLElement).style.gap).toBe('8px 15px')
})

test('prop wrap work', async () => {
const wrapper = TagGroupMount({ props: { dataSource, wrap: true } })

expect(wrapper.find('.ix-tag-group-nowrap').exists()).toBe(false)

await wrapper.setProps({ wrap: false })
expect(wrapper.find('.ix-tag-group-nowrap').exists()).toBe(true)
})

test('prop shape work', async () => {
const wrapper = TagGroupMount({ props: { dataSource, shape: 'round' } })
const tags = wrapper.findAll('.ix-tag')

tags.forEach(item => expect(item.classes()).toContain('ix-tag-round'))

await wrapper.setProps({ shape: 'rect' })
tags.forEach(item => expect(item.classes()).toContain('ix-tag-rect'))
})

test('closeIcon slot work', () => {
const wrapper = TagGroupMount({
props: { closable: true, dataSource },
slots: { closeIcon: () => h(IxIcon, { name: 'up' }) },
})

expect(wrapper.find('.ix-icon-close').exists()).not.toBe(true)
expect(wrapper.find('.ix-icon-up').exists()).toBe(true)
})
})
9 changes: 9 additions & 0 deletions packages/components/tag/demo/Clickable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
order: 7
title:
zh: 一组标签
---

## zh

常用于表格的筛选功能,可以通过点击选中。
45 changes: 45 additions & 0 deletions packages/components/tag/demo/Clickable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<template>
<IxTagGroup
v-model:activeKeys="activeKeys"
:dataSource="list"
:clickable="true"
:gap="48"
shape="round"
@click="handleClick"
></IxTagGroup>
</template>

<script lang="ts" setup>
import type { VKey } from '@idux/cdk/utils'
import type { TagData } from '@idux/components/tag'
import { computed, ref } from 'vue'
const activeKeys = ref<VKey[]>([])
const list = computed<TagData[]>(() => {
return [
'斗之气',
'斗者',
'斗师',
'大斗师',
'斗灵',
'斗王',
'斗皇',
'斗宗',
'斗尊',
'斗尊巅峰',
'半圣',
'斗圣',
'斗帝',
].map((label, idx) => ({
key: idx,
label,
color: activeKeys.value.includes(idx) ? 'success' : 'info',
}))
})
const handleClick = (k: VKey, evt: MouseEvent) => {
console.log(k, evt)
}
</script>
9 changes: 9 additions & 0 deletions packages/components/tag/demo/RemovableTag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
order: 8
title:
zh: 可移除的标签
---

## zh

`closable`属性定义一个标签是否可以移除,当标签被移除时会触发`close`事件。

0 comments on commit b365f3e

Please sign in to comment.