From 63a9922250ed9ee884f625009f4bc9a1cfab1fc8 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 15 Nov 2018 06:31:52 +1100 Subject: [PATCH 1/7] Add isVisible and isEnabled props --- index.d.ts | 6 ++- src/Tippy.js | 39 ++++++++++++++++-- test/Tippy.test.js | 100 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index d6fa046..750eda6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ -import React from 'react'; -import { Instance, Props } from 'tippy.js'; +import React from 'react' +import { Instance, Props } from 'tippy.js' type Omit = Pick> @@ -7,6 +7,8 @@ export interface TippyProps extends Omit { content: React.ReactNode | string children: React.ReactNode onCreate?: (tip: Instance) => void + isVisible?: boolean + isEnabled?: boolean } export default class Tippy extends React.Component {} diff --git a/src/Tippy.js b/src/Tippy.js index daf1cb9..e8ce604 100644 --- a/src/Tippy.js +++ b/src/Tippy.js @@ -3,11 +3,14 @@ import ReactDOM from 'react-dom' import PropTypes from 'prop-types' import tippy from 'tippy.js' -// Avoid Babel's large '_objectWithoutProperties' helper function +// These props are not native to `tippy.js` and are specific to React only. +const REACT_ONLY_PROPS = ['children', 'onCreate', 'isVisible', 'isEnabled'] + +// Avoid Babel's large '_objectWithoutProperties' helper function. const getNativeTippyProps = props => { const nativeProps = {} for (const prop in props) { - if (prop !== 'children' && prop !== 'onCreate') { + if (REACT_ONLY_PROPS.indexOf(prop) === -1) { nativeProps[prop] = props[prop] } } @@ -37,14 +40,44 @@ class Tippy extends React.Component { } } + get isManualTrigger() { + return this.props.trigger === 'manual' + } + componentDidMount() { this.setState({ isMounted: true }) + this.tip = tippy.one(ReactDOM.findDOMNode(this), this.options) - this.props.onCreate && this.props.onCreate(this.tip) + + if (this.props.onCreate) { + this.props.onCreate(this.tip) + } + + if (this.props.isEnabled === false) { + this.tip.disable() + } + + if (this.isManualTrigger && this.props.isVisible === true) { + this.tip.show() + } } componentDidUpdate() { this.tip.set(this.options) + + if (this.props.isEnabled === true) { + this.tip.enable() + } else if (this.props.isEnabled === false) { + this.tip.disable() + } + + if (this.isManualTrigger) { + if (this.props.isVisible === true) { + this.tip.show() + } else if (this.props.isVisible === false) { + this.tip.hide() + } + } } componentWillUnmount() { diff --git a/test/Tippy.test.js b/test/Tippy.test.js index cf3b70b..aef5384 100644 --- a/test/Tippy.test.js +++ b/test/Tippy.test.js @@ -127,4 +127,104 @@ describe('', () => { ).includes('
Tooltip
') ).toBe(false) }) + + function initialIsEnabledAs(bool) { + return class extends React.Component { + state = { + isEnabled: bool + } + render() { + return ( + + ') + const reactElementContent = render( + tooltip}> + ') }) test('adds a tippy instance to the child node', () => { - const wrapper = mount( + const { container } = render( - - + render( + + - } - } - const wrapper = mount( -
- -
+ const Child = () =>