Skip to content

Commit

Permalink
feat(overridePosition): Add "overridePosition" property to handle bor…
Browse files Browse the repository at this point in the history
…der cases and customize position
  • Loading branch information
sonnenhaft committed Jul 15, 2019
1 parent 34fa8cb commit ccb8b58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ className | data-class | String | | extra custom class, can use !importan
getContent | null | Func or Array | (dataTip) => {}, [(dataTip) => {}, Interval] | Generate the tip content dynamically
afterShow | null | Func | (evt) => {} | Function that will be called after tooltip show, with event that triggered show
afterHide | null | Func | (evt) => {} | Function that will be called after tooltip hide, with event that triggered hide
overridePosition | null | Func | ({left:number, top: number}, currentEvent, currentTarget, node, place, desiredPlace, effect, offset) => ({left: number, top: number}) | Function that will replace tooltip position with custom one
disable | data-tip-disable | Bool | true, false | Disable the tooltip behaviour, default is false
scrollHide | data-scroll-hide | Bool | true, false | Hide the tooltip when scrolling, default is true
resizeHide | null | Bool | true, false | Hide the tooltip when resizing the window, default is true
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ReactTooltip extends React.Component {
getContent: PropTypes.any,
afterShow: PropTypes.func,
afterHide: PropTypes.func,
overridePosition: PropTypes.func,
disable: PropTypes.bool,
scrollHide: PropTypes.bool,
resizeHide: PropTypes.bool,
Expand Down Expand Up @@ -309,6 +310,10 @@ class ReactTooltip extends React.Component {
let effect = switchToSolid && 'solid' || this.getEffect(e.currentTarget)
let offset = e.currentTarget.getAttribute('data-offset') || this.props.offset || {}
let result = getPosition(e, e.currentTarget, this.tooltipRef, desiredPlace, desiredPlace, effect, offset)
if (result.position && this.props.overridePosition) {
result.position = this.props.overridePosition(result.position, e.currentTarget, this.tooltipRef, desiredPlace, desiredPlace, effect, offset)
}

let place = result.isNewState ? result.newState.place : desiredPlace

// To prevent previously created timers from triggering
Expand Down Expand Up @@ -480,7 +485,10 @@ class ReactTooltip extends React.Component {
updatePosition () {
const {currentEvent, currentTarget, place, desiredPlace, effect, offset} = this.state
const node = this.tooltipRef
const result = getPosition(currentEvent, currentTarget, node, place, desiredPlace, effect, offset)
let result = getPosition(currentEvent, currentTarget, node, place, desiredPlace, effect, offset)
if (result.position && this.props.overridePosition) {
result.position = this.props.overridePosition(result.position, currentEvent, currentTarget, node, place, desiredPlace, effect, offset)
}

if (result.isNewState) {
// Switch to reverse placement
Expand Down

0 comments on commit ccb8b58

Please sign in to comment.