1 | # rc-trigger
|
2 | ---
|
3 |
|
4 | React Trigger Component
|
5 |
|
6 | [![NPM version][npm-image]][npm-url]
|
7 | [![build status][travis-image]][travis-url]
|
8 | [![Test coverage][coveralls-image]][coveralls-url]
|
9 | [![Dependencies][david-image]][david-url]
|
10 | [![DevDependencies][david-dev-image]][david-dev-url]
|
11 | [![npm download][download-image]][download-url]
|
12 | [![bundle size][bundlephobia-image]][bundlephobia-url]
|
13 |
|
14 | [npm-image]: http://img.shields.io/npm/v/rc-trigger.svg?style=flat-square
|
15 | [npm-url]: http://npmjs.org/package/rc-trigger
|
16 | [travis-image]: https://img.shields.io/travis/react-component/trigger/master?style=flat-square
|
17 | [travis-url]: https://travis-ci.org/react-component/trigger
|
18 | [circleci-image]: https://img.shields.io/circleci/react-component/trigger/master?style=flat-square
|
19 | [circleci-url]: https://circleci.com/gh/react-component/trigger
|
20 | [coveralls-image]: https://img.shields.io/coveralls/react-component/trigger.svg?style=flat-square
|
21 | [coveralls-url]: https://coveralls.io/r/react-component/trigger?branch=master
|
22 | [david-url]: https://david-dm.org/react-component/trigger
|
23 | [david-image]: https://david-dm.org/react-component/trigger/status.svg?style=flat-square
|
24 | [david-dev-url]: https://david-dm.org/react-component/trigger?type=dev
|
25 | [david-dev-image]: https://david-dm.org/react-component/trigger/dev-status.svg?style=flat-square
|
26 | [download-image]: https://img.shields.io/npm/dm/rc-trigger.svg?style=flat-square
|
27 | [download-url]: https://npmjs.org/package/rc-trigger
|
28 | [bundlephobia-url]: https://bundlephobia.com/result?p=rc-trigger
|
29 | [bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-trigger
|
30 |
|
31 | ## Install
|
32 |
|
33 | [![rc-trigger](https://nodei.co/npm/rc-trigger.png)](https://npmjs.org/package/rc-trigger)
|
34 |
|
35 | ## Usage
|
36 |
|
37 | Include the default [styling](https://github.com/react-component/trigger/blob/master/assets/index.less#L4:L11) and then:
|
38 |
|
39 | ```js
|
40 | import React from 'react';
|
41 | import ReactDOM from 'react-dom';
|
42 | import Trigger from 'rc-trigger';
|
43 |
|
44 | ReactDOM.render((
|
45 | <Trigger
|
46 | action={['click']}
|
47 | popup={<span>popup</span>}
|
48 | popupAlign={{
|
49 | points: ['tl', 'bl'],
|
50 | offset: [0, 3]
|
51 | }}
|
52 | >
|
53 | <a href='#'>hover</a>
|
54 | </Trigger>
|
55 | ), container);
|
56 | ```
|
57 |
|
58 | ## Compatibility
|
59 |
|
60 | | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Electron |
|
61 | | --- | --- | --- | --- | --- |
|
62 | | IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
|
63 |
|
64 | ## Example
|
65 |
|
66 | http://localhost:9001
|
67 |
|
68 | online example: http://react-component.github.io/trigger/
|
69 |
|
70 | ## Development
|
71 |
|
72 | ```
|
73 | npm install
|
74 | npm start
|
75 | ```
|
76 |
|
77 | ## API
|
78 |
|
79 | ### props
|
80 |
|
81 | <table class="table table-bordered table-striped">
|
82 | <thead>
|
83 | <tr>
|
84 | <th style="width: 100px;">name</th>
|
85 | <th style="width: 50px;">type</th>
|
86 | <th style="width: 50px;">default</th>
|
87 | <th>description</th>
|
88 | </tr>
|
89 | </thead>
|
90 | <tbody>
|
91 | <tr>
|
92 | <td>alignPoint</td>
|
93 | <td>bool</td>
|
94 | <td>false</td>
|
95 | <td>Popup will align with mouse position (support action of 'click', 'hover' and 'contextMenu')</td>
|
96 | </tr>
|
97 | <tr>
|
98 | <td>popupClassName</td>
|
99 | <td>string</td>
|
100 | <td></td>
|
101 | <td>additional className added to popup</td>
|
102 | </tr>
|
103 | <tr>
|
104 | <td>forceRender</td>
|
105 | <td>boolean</td>
|
106 | <td>false</td>
|
107 | <td>whether render popup before first show</td>
|
108 | </tr>
|
109 | <tr>
|
110 | <td>destroyPopupOnHide</td>
|
111 | <td>boolean</td>
|
112 | <td>false</td>
|
113 | <td>whether destroy popup when hide</td>
|
114 | </tr>
|
115 | <tr>
|
116 | <td>getPopupClassNameFromAlign</td>
|
117 | <td>getPopupClassNameFromAlign(align: Object):String</td>
|
118 | <td></td>
|
119 | <td>additional className added to popup according to align</td>
|
120 | </tr>
|
121 | <tr>
|
122 | <td>action</td>
|
123 | <td>string[]</td>
|
124 | <td>['hover']</td>
|
125 | <td>which actions cause popup shown. enum of 'hover','click','focus','contextMenu'</td>
|
126 | </tr>
|
127 | <tr>
|
128 | <td>mouseEnterDelay</td>
|
129 | <td>number</td>
|
130 | <td>0</td>
|
131 | <td>delay time to show when mouse enter. unit: s.</td>
|
132 | </tr>
|
133 | <tr>
|
134 | <td>mouseLeaveDelay</td>
|
135 | <td>number</td>
|
136 | <td>0.1</td>
|
137 | <td>delay time to hide when mouse leave. unit: s.</td>
|
138 | </tr>
|
139 | <tr>
|
140 | <td>popupStyle</td>
|
141 | <td>Object</td>
|
142 | <td></td>
|
143 | <td>additional style of popup</td>
|
144 | </tr>
|
145 | <tr>
|
146 | <td>prefixCls</td>
|
147 | <td>String</td>
|
148 | <td>rc-trigger-popup</td>
|
149 | <td>prefix class name</td>
|
150 | </tr>
|
151 | <tr>
|
152 | <td>popupTransitionName</td>
|
153 | <td>String|Object</td>
|
154 | <td></td>
|
155 | <td>https://github.com/react-component/animate</td>
|
156 | </tr>
|
157 | <tr>
|
158 | <td>maskTransitionName</td>
|
159 | <td>String|Object</td>
|
160 | <td></td>
|
161 | <td>https://github.com/react-component/animate</td>
|
162 | </tr>
|
163 | <tr>
|
164 | <td>onPopupVisibleChange</td>
|
165 | <td>Function</td>
|
166 | <td></td>
|
167 | <td>call when popup visible is changed</td>
|
168 | </tr>
|
169 | <tr>
|
170 | <td>mask</td>
|
171 | <td>boolean</td>
|
172 | <td>false</td>
|
173 | <td>whether to support mask</td>
|
174 | </tr>
|
175 | <tr>
|
176 | <td>maskClosable</td>
|
177 | <td>boolean</td>
|
178 | <td>true</td>
|
179 | <td>whether to support click mask to hide</td>
|
180 | </tr>
|
181 | <tr>
|
182 | <td>popupVisible</td>
|
183 | <td>boolean</td>
|
184 | <td></td>
|
185 | <td>whether popup is visible</td>
|
186 | </tr>
|
187 | <tr>
|
188 | <td>zIndex</td>
|
189 | <td>number</td>
|
190 | <td></td>
|
191 | <td>popup's zIndex</td>
|
192 | </tr>
|
193 | <tr>
|
194 | <td>defaultPopupVisible</td>
|
195 | <td>boolean</td>
|
196 | <td></td>
|
197 | <td>whether popup is visible initially</td>
|
198 | </tr>
|
199 | <tr>
|
200 | <td>popupAlign</td>
|
201 | <td>Object: alignConfig of [dom-align](https://github.com/yiminghe/dom-align)</td>
|
202 | <td></td>
|
203 | <td>popup 's align config</td>
|
204 | </tr>
|
205 | <tr>
|
206 | <td>onPopupAlign</td>
|
207 | <td>function(popupDomNode, align)</td>
|
208 | <td></td>
|
209 | <td>callback when popup node is aligned</td>
|
210 | </tr>
|
211 | <tr>
|
212 | <td>popup</td>
|
213 | <td>React.Element | function() => React.Element</td>
|
214 | <td></td>
|
215 | <td>popup content</td>
|
216 | </tr>
|
217 | <tr>
|
218 | <td>getPopupContainer</td>
|
219 | <td>getPopupContainer(): HTMLElement</td>
|
220 | <td></td>
|
221 | <td>function returning html node which will act as popup container</td>
|
222 | </tr>
|
223 | <tr>
|
224 | <td>getDocument</td>
|
225 | <td>getDocument(): HTMLElement</td>
|
226 | <td></td>
|
227 | <td>function returning document node which will be attached click event to close trigger</td>
|
228 | </tr>
|
229 | <tr>
|
230 | <td>popupPlacement</td>
|
231 | <td>string</td>
|
232 | <td></td>
|
233 | <td>use preset popup align config from builtinPlacements, can be merged by popupAlign prop</td>
|
234 | </tr>
|
235 | <tr>
|
236 | <td>builtinPlacements</td>
|
237 | <td>object</td>
|
238 | <td></td>
|
239 | <td>builtin placement align map. used by placement prop</td>
|
240 | </tr>
|
241 | <tr>
|
242 | <td>stretch</td>
|
243 | <td>string</td>
|
244 | <td></td>
|
245 | <td>Let popup div stretch with trigger element. enums of 'width', 'minWidth', 'height', 'minHeight'. (You can also mixed with 'height minWidth')</td>
|
246 | </tr>
|
247 | </tbody>
|
248 | </table>
|
249 |
|
250 |
|
251 | ## Test Case
|
252 |
|
253 | ```
|
254 | npm test
|
255 | npm run chrome-test
|
256 | ```
|
257 |
|
258 | ## Coverage
|
259 |
|
260 | ```
|
261 | npm run coverage
|
262 | ```
|
263 |
|
264 | open coverage/ dir
|
265 |
|
266 | ## React 16 Note
|
267 |
|
268 | After React 16, you won't access popup element's ref in parent component's componentDidMount, which means following code won't work.
|
269 |
|
270 | ```javascript
|
271 | class App extends React.Component {
|
272 | componentDidMount() {
|
273 | this.input.focus(); // error, this.input is undefined.
|
274 | }
|
275 |
|
276 | render() {
|
277 | return (
|
278 | <Trigger
|
279 | action={['click']}
|
280 | popup={<div><input ref={node => this.input = node} type="text" /></div>}
|
281 | >
|
282 | <button>click</button>
|
283 | </Trigger>
|
284 | )
|
285 | }
|
286 | }
|
287 | ```
|
288 |
|
289 | Consider wrap your popup element to a separate component:
|
290 |
|
291 | ```javascript
|
292 | class InputPopup extends React.Component {
|
293 | componentDidMount() {
|
294 | this.props.onMount();
|
295 | }
|
296 |
|
297 | render() {
|
298 | return (
|
299 | <div>
|
300 | <input ref={this.props.inputRef} type="text" />
|
301 | </div>
|
302 | );
|
303 | }
|
304 | }
|
305 |
|
306 | class App extends React.Component {
|
307 | handlePopupMount() {
|
308 | this.input.focus(); // error, this.input is undefined.
|
309 | }
|
310 |
|
311 | render() {
|
312 | return (
|
313 | <Trigger
|
314 | action={['click']}
|
315 | popup={<InputPopup inputRef={node => this.input = node} onMount={this.handlePopupMount} />}
|
316 | >
|
317 | <button>click</button>
|
318 | </Trigger>
|
319 | )
|
320 | }
|
321 | }
|
322 | ```
|
323 |
|
324 | ## License
|
325 |
|
326 | rc-trigger is released under the MIT license.
|