Skip to content

Commit

Permalink
feat(Popup): Multiple event triggering a Popup (#1977)
Browse files Browse the repository at this point in the history
* fix(Popup): change position(right/left) for small width components

The position of the Popup on elements with smaller width(<25px) not aligned.

* Review changes updated

* feat(Popup): Multiple event triggering a Popup

Popup triggering by multiple events(hover, focus or click)

* test case for proptypes

* Requested changes updated

* Proptypes validation for Popup

* Review changes updated

* Test case for multiple event trigger Popup

* Rectifying indentation
  • Loading branch information
vipul-21 authored and levithomason committed Aug 20, 2017
1 parent af1f05d commit 87bae70
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
13 changes: 13 additions & 0 deletions docs/app/Examples/modules/Popup/Usage/PopupExampleMultiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { Button, Popup } from 'semantic-ui-react'

const PopupExampleMultiple = () => (
<Popup
trigger={<Button icon>Click me or Hover me</Button>}
header='Movie Search'
content='Multiple events can trigger a popup'
on={['hover', 'click']}
/>
)

export default PopupExampleMultiple
5 changes: 5 additions & 0 deletions docs/app/Examples/modules/Popup/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const PopupUsageExamples = () => (
description='A popup can be nested inside another.'
examplePath='modules/Popup/Usage/PopupExampleNested'
/>
<ComponentExample
title='Multiple'
description='A popup can be triggered on multiple events.'
examplePath='modules/Popup/Usage/PopupExampleMultiple'
/>
<ComponentExample
title='Controlled'
description='A popup can have its visibility controlled from outside.'
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Popup/Popup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export interface PopupProps extends PortalProps {
/** Horizontal offset in pixels to be applied to the popup. */
offset?: number;

/** Event triggering the popup. */
on?: 'hover' | 'click' | 'focus';
/** Events triggering the popup. */
on?: 'hover' | 'click' | 'focus' | Array<'hover' | 'click' | 'focus'>;

/**
* Called when a close event happens.
Expand Down
17 changes: 11 additions & 6 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ export default class Popup extends Component {
/** Horizontal offset in pixels to be applied to the Popup. */
offset: PropTypes.number,

/** Event triggering the popup. */
on: PropTypes.oneOf(['hover', 'click', 'focus']),
/** Events triggering the popup. */
on: PropTypes.oneOfType([
PropTypes.oneOf(['hover', 'click', 'focus']),
PropTypes.arrayOf(PropTypes.oneOf(['hover', 'click', 'focus'])),
]),

/**
* Called when a close event happens.
Expand Down Expand Up @@ -245,20 +248,22 @@ export default class Popup extends Component {
const portalProps = {}

const { on, hoverable } = this.props
const normalizedOn = _.isArray(on) ? on : [on]

if (hoverable) {
portalProps.closeOnPortalMouseLeave = true
portalProps.mouseLeaveDelay = 300
}

if (on === 'click') {
if (_.includes(normalizedOn, 'click')) {
portalProps.openOnTriggerClick = true
portalProps.closeOnTriggerClick = true
portalProps.closeOnDocumentClick = true
} else if (on === 'focus') {
}
if (_.includes(normalizedOn, 'focus')) {
portalProps.openOnTriggerFocus = true
portalProps.closeOnTriggerBlur = true
} else if (on === 'hover') {
}
if (_.includes(normalizedOn, 'hover')) {
portalProps.openOnTriggerMouseEnter = true
portalProps.closeOnTriggerMouseLeave = true
// Taken from SUI: https://git.io/vPmCm
Expand Down
17 changes: 17 additions & 0 deletions test/specs/modules/Popup/Popup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ describe('Popup', () => {
wrapper.find('input').simulate('focus')
assertInBody('.ui.popup.visible')
})

it('it appears on multiple', (done) => {
const trigger = <button>foo</button>
const button = wrapperMount(<Popup on={['click', 'hover']} content='foo' header='bar' trigger={trigger} />)
.find('button')

button.simulate('click')
assertInBody('.ui.popup.visible')

domEvent.click('body')

button.simulate('mouseenter')
setTimeout(() => {
assertInBody('.ui.popup.visible')
done()
}, 51)
})
})

describe('open', () => {
Expand Down

0 comments on commit 87bae70

Please sign in to comment.