|
| 1 | + /* |
| 2 | + * Copyright 2017, GeoSolutions Sas. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +const React = require('react'); |
| 9 | +const {branch} = require('recompose'); |
| 10 | +const { Tooltip, OverlayTrigger} = require('react-bootstrap'); |
| 11 | +const Message = require('../../I18N/Message'); |
| 12 | + |
| 13 | +/** |
| 14 | + * Tooltip enhancer. Enhances an object adding a (localizable) tooltip. |
| 15 | + * It is applied only if props contains `tooltip` or `tooltipId`. It have to be applied to a React (functional) component |
| 16 | + * @type {function} |
| 17 | + * @name tooltip |
| 18 | + * @memberof components.misc.enhancers |
| 19 | + * @prop {string|node} [tooltip] if present will add the tooltip. This is the full tooltip content |
| 20 | + * @prop {string} [tooltipId] if present will show a localized tooltip using the tooltipId as msgId |
| 21 | + * @prop {string} [tooltipPosition="top"] |
| 22 | + * @prop {string} tooltipTrigger see react overlay trigger |
| 23 | + * @example |
| 24 | + * render() { |
| 25 | + * const Cmp = tooltip((props) =><El {...props}></El>); // or simply tooltip(El); |
| 26 | + * return <Cmp tooltipId="Hello" tooltipPosition="left" /> // => render the component with tooltip |
| 27 | + * } |
| 28 | + * |
| 29 | + */ |
| 30 | +module.exports = branch( |
| 31 | + ({tooltip, tooltipId} = {}) => tooltip || tooltipId, |
| 32 | + // TODO return proper HOC |
| 33 | + (Wrapped) => ({tooltip, tooltipId, tooltipPosition = "top", tooltipTrigger, key, ...props} = {}) => (<OverlayTrigger |
| 34 | + trigger={tooltipTrigger} |
| 35 | + key={key} |
| 36 | + placement={tooltipPosition} |
| 37 | + overlay={<Tooltip id={"tooltip-" + {key}}>{tooltipId ? <Message msgId={tooltipId} /> : tooltip}</Tooltip>}><Wrapped {...props}/></OverlayTrigger>)); |
0 commit comments