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): support an object from createRef() as context value #3459

Merged
merged 1 commit into from
Feb 23, 2019
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
18 changes: 9 additions & 9 deletions docs/src/examples/modules/Popup/Usage/PopupExampleContext.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import React from 'react'
import React, { createRef } from 'react'
import { Button, Popup } from 'semantic-ui-react'

class PopupExampleContextControlled extends React.Component {
state = {}

handleRef = node => this.setState({ node })
contextRef = createRef()

render() {
const { node } = this.state
const trigger = <Button content='Trigger Popup' />

return (
<div>
<Popup trigger={trigger} context={node} content='Hello' position='top center' />
<Popup
trigger={<Button content='Trigger Popup' />}
context={this.contextRef}
content='Hello'
position='top center'
/>
---------->
<strong ref={this.handleRef}>here</strong>
<strong ref={this.contextRef}>here</strong>
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import React from 'react'
import React, { createRef, Fragment } from 'react'
import { Button, Popup } from 'semantic-ui-react'

class PopupExampleContextControlled extends React.Component {
state = {}
contextRef = createRef()

toggle = () => this.setState({ open: !this.state.open })

handleRef = node => this.setState({ node })

render() {
const { node, open } = this.state
return (
<div>
<Fragment>
<Button content='Open controlled Popup' onClick={this.toggle} />
<Popup context={node} content='Hello' position='top center' open={open} />
<Popup
context={this.contextRef}
content='Hello'
position='top center'
open={this.state.open}
/>
---------->
<strong ref={this.handleRef}>here</strong>
</div>
<strong ref={this.contextRef}>here</strong>
</Fragment>
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Popup/Popup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface StrictPopupProps extends StrictPortalProps {
content?: SemanticShorthandItem<PopupContentProps>

/** Existing element the pop-up should be bound to. */
context?: object
context?: object | React.RefObject<HTMLElement>

/** A disabled popup only renders its trigger. */
disabled?: boolean
Expand Down
10 changes: 8 additions & 2 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SUI,
useKeyOnly,
useKeyOrValueAndKey,
isRefObject,
} from '../../lib'
import Portal from '../../addons/Portal'
import PopupContent from './PopupContent'
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class Popup extends Component {
content: customPropTypes.itemShorthand,

/** Existing element the pop-up should be bound to. */
context: PropTypes.object,
context: PropTypes.oneOfType([PropTypes.object, customPropTypes.refObject]),

/** A disabled popup only renders its trigger. */
disabled: PropTypes.bool,
Expand Down Expand Up @@ -372,7 +373,12 @@ export default class Popup extends Component {
this.setPopupStyle()
}

getContext = () => this.props.context || this.triggerRef
getContext = () => {
const { context } = this.props
const contextFromProp = isRefObject(context) ? context.current : context

return contextFromProp || this.triggerRef
}

render() {
const {
Expand Down