Skip to content

Commit 8f256b1

Browse files
authored
feat(tag): add new feature check-tag (element-plus#1696)
* feat(tag): add new feature check-tag - Add new component `check-tag` * feat(tag): - add check tag vetur support
1 parent 3ac7c2c commit 8f256b1

File tree

14 files changed

+366
-1
lines changed

14 files changed

+366
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { mount } from '@vue/test-utils'
2+
import CheckTag from '../src/index.vue'
3+
4+
const AXIOM = 'Rem is the best girl'
5+
6+
describe('CheckTag.vue', () => {
7+
test('render test', async () => {
8+
const wrapper = mount(CheckTag, {
9+
slots: {
10+
default: AXIOM,
11+
},
12+
})
13+
expect(wrapper.text()).toEqual(AXIOM)
14+
15+
expect(wrapper.classes()).toContain('el-check-tag')
16+
})
17+
18+
19+
test('functionality', async () => {
20+
const wrapper = mount({
21+
template: `<el-check-tag @change="checked = !checked" :checked="checked">
22+
${AXIOM}
23+
</el-check-tag>`,
24+
components: {
25+
'el-check-tag': CheckTag,
26+
},
27+
data() {
28+
return {
29+
checked: false,
30+
}
31+
},
32+
}, {
33+
slots: {
34+
default: AXIOM,
35+
},
36+
})
37+
expect(wrapper.text()).toEqual(AXIOM)
38+
39+
await wrapper.find('.el-check-tag').trigger('click')
40+
41+
expect(wrapper.vm.checked).toBe(true)
42+
43+
await wrapper.find('.el-check-tag').trigger('click')
44+
45+
expect(wrapper.vm.checked).toBe(false)
46+
47+
})
48+
49+
50+
})

packages/check-tag/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { App } from 'vue'
2+
import CheckTag from './src/index.vue'
3+
import type { SFCWithInstall } from '@element-plus/utils/types'
4+
5+
CheckTag.install = (app: App): void => {
6+
app.component(CheckTag.name, CheckTag)
7+
}
8+
9+
const _CheckTag: SFCWithInstall<typeof CheckTag> = CheckTag
10+
11+
export default _CheckTag

packages/check-tag/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@element-plus/check-tag",
3+
"version": "0.0.0",
4+
"main": "dist/index.js",
5+
"license": "MIT",
6+
"peerDependencies": {
7+
"vue": "^3.0.7"
8+
},
9+
"devDependencies": {
10+
"@vue/test-utils": "^2.0.0-beta.3"
11+
}
12+
}

packages/check-tag/src/index.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<span
3+
:class="{
4+
'el-check-tag': true,
5+
'is-checked': checked,
6+
}"
7+
@click="onChange"
8+
>
9+
<slot></slot>
10+
</span>
11+
</template>
12+
<script lang='ts'>
13+
import { defineComponent } from 'vue'
14+
export default defineComponent({
15+
name: 'ElCheckTag',
16+
props: {
17+
checked: Boolean,
18+
},
19+
emits: ['change'],
20+
setup(props, { emit }) {
21+
22+
const onChange = () => {
23+
emit('change', !props.checked)
24+
}
25+
26+
return {
27+
onChange,
28+
}
29+
},
30+
})
31+
</script>
32+
<style scoped>
33+
</style>

packages/element-plus/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import ElVirtualList from '@element-plus/virtual-list'
8989
import ElSpace from '@element-plus/space'
9090
import ElSkeleton from '@element-plus/skeleton'
9191
import ElSkeletonItem from '@element-plus/skeleton-item'
92+
import ElCheckTag from '@element-plus/check-tag'
93+
9294
import { use, i18n } from '@element-plus/locale'
9395
// if you encountered problems alike "Can't resolve './version'"
9496
// please run `yarn bootstrap` first
@@ -141,6 +143,7 @@ const components = [
141143
ElCheckbox,
142144
ElCheckboxButton,
143145
ElCheckboxGroup,
146+
ElCheckTag,
144147
ElCol,
145148
ElCollapse,
146149
ElCollapseItem,
@@ -255,6 +258,7 @@ export {
255258
ElCheckbox,
256259
ElCheckboxButton,
257260
ElCheckboxGroup,
261+
ElCheckTag,
258262
ElCol,
259263
ElCollapse,
260264
ElCollapseItem,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@import "mixins/mixins";
2+
@import "common/var";
3+
4+
@include b(check-tag) {
5+
background-color: $--background-color-base;
6+
border-radius: $--border-radius-base;
7+
color: $--color-info;
8+
cursor: pointer;
9+
display: inline-block;
10+
font-size: $--font-size-base;
11+
line-height: $--font-size-base;
12+
padding: 7px 15px;
13+
transition: $--all-transition;
14+
font-weight: bold;
15+
&:hover {
16+
background-color: rgb(220, 223, 230);
17+
}
18+
19+
@include when(checked) {
20+
background-color: #DEEDFC;
21+
color: $--color-primary-light-1;
22+
&:hover {
23+
background-color: $--color-primary-light-7;
24+
}
25+
}
26+
}

packages/theme-chalk/src/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@
8686
@import "./skeleton-item.scss";
8787
@import "./empty.scss";
8888
@import "./affix.scss";
89+
@import "./check-tag.scss";

vetur/tags.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@
185185
"defaults": ["type"],
186186
"description": "Used for marking and selection."
187187
},
188+
"el-check-tag": {
189+
"attributes": ["checked"],
190+
"description": "Light weighted checkbox with tag appearance"
191+
},
188192
"el-progress": {
189193
"attributes": ["percentage", "type", "stroke-width", "text-inside", "status", "color", "width", "show-text", "stroke-linecap", "format"],
190194
"defaults": ["percentage"],

vetur/web-types.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4334,6 +4334,28 @@
43344334
}
43354335
]
43364336
},
4337+
{
4338+
"name": "el-check-tag",
4339+
"description": "Light weighted checkbox with tag appearance",
4340+
"doc-url": "https://element-plus.org/#/en-US/component/tag",
4341+
"attributes": [
4342+
{
4343+
"name": "checked",
4344+
"default": "-",
4345+
"description": "whether the tag is selected",
4346+
"value": {
4347+
"type": "boolean",
4348+
"kind": "expression"
4349+
}
4350+
}
4351+
],
4352+
"events": [
4353+
{
4354+
"name": "change",
4355+
"description": "triggers when check tag checked state change"
4356+
}
4357+
]
4358+
},
43374359
{
43384360
"name": "el-progress",
43394361
"description": "Progress is used to show the progress of current operation, and inform the user the current status.",

website/docs/en-US/tag.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,36 @@ Tag provide three different themes: `dark`、`light` and `plain`
185185
```
186186
:::
187187

188+
### Checkable tag
189+
190+
Sometimes because of the business needs, we might need checkbox like tag, but **button like checkbox** cannot meet our needs, here comes `check-tag`
191+
192+
:::demo basic check-tag usage, the API is rather simple.
193+
```html
194+
195+
<div>
196+
<el-check-tag checked style="margin-right: 8px;">Checked</el-check-tag>
197+
<el-check-tag @change="onChange" :checked="checked">Toggle me</el-check-tag>
198+
</div>
199+
200+
<script>
201+
export default {
202+
data() {
203+
return {
204+
checked: false,
205+
}
206+
},
207+
methods: {
208+
onChange(checked) {
209+
this.checked = checked;
210+
}
211+
}
212+
}
213+
</script>
214+
215+
```
216+
:::
217+
188218
### Attributes
189219
| Attribute | Description | Type | Accepted Values | Default |
190220
|---------- |-------------- |---------- |-------------------------------- |-------- |
@@ -202,3 +232,13 @@ Tag provide three different themes: `dark`、`light` and `plain`
202232
|---------- |-------- |---------- |
203233
| click | triggers when Tag is clicked ||
204234
| close | triggers when Tag is removed ||
235+
236+
### CheckTag Attributes
237+
| Attribute | Description | Type | Accepted | Default |
238+
|---------- |-------------- |---------- |-------------------------------- |-------- |
239+
| checked | is checked | boolean | true/false ||
240+
241+
### CheckTag Events
242+
| Event Name | Description | Parameters |
243+
|---------- |-------- |---------- |
244+
| change | triggers when Check Tag is clicked | checked |

0 commit comments

Comments
 (0)