Skip to content

Commit 40ef808

Browse files
committed
fix(tip): support multiple trigger types
1 parent afdc9f3 commit 40ef808

2 files changed

Lines changed: 56 additions & 22 deletions

File tree

src/components/tip/index.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,44 @@ layout: component
123123

124124
通过 `trigger` 属性可以改变触发方式。可选值包括 `hover` `focus` `click`,默认值是 `hover`
125125

126+
一般来说,采用 `['hover', 'focus']` 即能满足多数使用 PC 端场景(使用鼠标操作,或使用 <kbd>tab</kbd> 键操作)。
126127

127128
```html
128-
<c-tip theme="light" position="top" trigger="hover">
129-
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
130-
<c-button primary>hover (default)</c-button>
131-
</c-tip>
129+
<p>
130+
<c-tip theme="light" position="top" trigger="hover">
131+
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
132+
<div>hover</div>
133+
</c-tip>
134+
</p>
132135

133-
<c-tip theme="light" position="top" trigger="focus">
134-
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
135-
<c-button primary>focus</c-button>
136-
</c-tip>
136+
<p>
137+
<c-tip theme="light" position="top" trigger="focus">
138+
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
139+
<a href="javascript:;">focus</span>
140+
</c-tip>
141+
</p>
137142

138-
<c-tip theme="light" position="top" trigger="click">
139-
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
140-
<c-button primary>click</c-button>
141-
</c-tip>
143+
<p>
144+
<c-tip theme="light" position="top" trigger="click">
145+
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
146+
<div>click</div>
147+
</c-tip>
148+
</p>
149+
150+
<p>
151+
<c-tip theme="light" position="top" :trigger="['hover', 'focus']">
152+
<div slot="content">怨春不语,算只有殷勤画檐蛛网,尽日惹飞絮</div>
153+
<a href="javascript;">hover or focus</a>
154+
</c-tip>
155+
</p>
142156
```
143157

144158
## API
145159

146160
| 属性 | 类型 | 说明 | 默认值 |
147161
|-----|-------|------|-------|
148162
| theme | dark \| light | 主题 | dark |
149-
| trigger | hover \| click \| focus | 触发形式 | hover |
163+
| trigger | hover \| click \| focus | 触发形式,可以使用数组 | hover |
150164
| position | top \| right \| bottom \| left | 位置 | bottom |
151165
| disabled | Boolean | 是否禁用 | false |
152166
| content | String | 提示文本 | |

src/components/tip/index.vue

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ export default {
6666
6767
props: {
6868
theme: VueTypes.oneOf(['dark', 'light']).def('dark'),
69-
trigger: VueTypes.oneOf(['hover', 'click', 'focus']).def('hover'),
69+
trigger: {
70+
type: [String, Array],
71+
default: 'hover',
72+
validator (val) {
73+
const validTypes = ['hover', 'click', 'focus']
74+
return [].concat(val).every(v => validTypes.includes(v))
75+
}
76+
},
7077
disabled: VueTypes.bool.def(false),
7178
content: VueTypes.string.def(''),
7279
maxWidth: VueTypes.string.def('300px'),
@@ -87,23 +94,36 @@ export default {
8794
arrowClass () {
8895
const position = OPPOSITE_DIRECTION[this.position]
8996
return `c-tip__arrow--${position}`
97+
},
98+
triggers () {
99+
const { trigger } = this
100+
const triggers = [].concat(trigger)
101+
return triggers
90102
}
91103
},
92104
93105
methods: {
94106
show ({ type = 'click' }) {
95-
if (SHOW_MATCH_MAP[this.trigger] === type) {
96-
this.clearTimeout()
97-
this.visible = true
107+
const { triggers } = this
108+
for (let i = 0; i < triggers.length; i++) {
109+
if (SHOW_MATCH_MAP[triggers[i]] === type) {
110+
this.clearTimeout()
111+
this.visible = true
112+
break
113+
}
98114
}
99115
},
100116
101117
hide ({ type = 'click' } = {}) {
102-
if (HIDE_MATCH_MAP[this.trigger] === type) {
103-
this.clearTimeout()
104-
this.tidOut = setTimeout(() => {
105-
this.visible = false
106-
}, this.hideDelay)
118+
const { triggers } = this
119+
for (let i = 0; i < triggers.length; i++) {
120+
if (HIDE_MATCH_MAP[triggers[i]] === type) {
121+
this.clearTimeout()
122+
this.tidOut = setTimeout(() => {
123+
this.visible = false
124+
}, this.hideDelay)
125+
break
126+
}
107127
}
108128
},
109129

0 commit comments

Comments
 (0)