Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Popup): Multiple event triggering a Popup #1977

Merged
merged 9 commits into from
Aug 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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