Skip to content

Commit

Permalink
docs(Portal): add controlled example (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored Oct 2, 2017
1 parent 9c97ed9 commit b6fb22d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
42 changes: 42 additions & 0 deletions docs/app/Examples/addons/Portal/Types/PortalExampleControlled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { Component } from 'react'
import { Button, Grid, Header, Segment, Portal } from 'semantic-ui-react'

export default class PortalExampleControlled extends Component {
state = {
open: false,
}

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

handleClose = () => this.setState({ open: false })

render() {
const { open } = this.state

return (
<Grid columns={2}>
<Grid.Column>
<Button
content={open ? 'Close Portal' : 'Open Portal'}
negative={open}
positive={!open}
onClick={this.handleClick}
/>

<Portal
closeOnTriggerClick
openOnTriggerClick
onClose={this.handleClose}
open={open}
>
<Segment style={{ left: '40%', position: 'fixed', top: '50%', zIndex: 1000 }}>
<Header>This is a controlled portal</Header>
<p>Portals have tons of great callback functions to hook into.</p>
<p>To close, simply click the close button or click away</p>
</Segment>
</Portal>
</Grid.Column>
</Grid>
)
}
}
35 changes: 24 additions & 11 deletions docs/app/Examples/addons/Portal/Types/PortalExamplePortal.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import React, { Component } from 'react'
import { Button, Grid, Header, Segment, Portal } from 'semantic-ui-react'
import { Button, Grid, Header, Label, Segment, Portal } from 'semantic-ui-react'

export default class ExamplePortal extends Component {
export default class PortalExamplePortal extends Component {
state = {
log: [],
logCount: 0,
open: false,
}

handleOpen = () => {
this.setState({ open: true })
this.writeLog('Portal mounted')
this.writeLog('onOpen()')
}

handleClose = () => {
this.setState({ open: false })
this.writeLog('Portal closed')
this.writeLog('onClose()')
}

writeLog = message => this.setState({ log: [message, ...this.state.log] })
clearLog = () => this.setState({ log: [], logCount: 0 })

writeLog = eventName => this.setState(({
log: [
`${new Date().toLocaleTimeString()}: ${eventName}`,
...this.state.log,
].slice(0, 20),
logCount: this.state.logCount + 1,
}))

render() {
const { log, open } = this.state
const { log, logCount, open } = this.state

return (
<Grid columns={2}>
Expand All @@ -46,11 +55,15 @@ export default class ExamplePortal extends Component {
</Portal>
</Grid.Column>
<Grid.Column>
<Segment>
<pre style={{ height: 200, overflowY: 'scroll' }}>
{log.map((e, i) => <p key={i}>{e}</p>)}
</pre>
</Segment>
<Segment.Group>
<Segment>
<Button compact size='small' floated='right' onClick={this.clearLog}>Clear</Button>
Event Log <Label circular>{logCount}</Label>
</Segment>
<Segment secondary>
<pre>{log.map((e, i) => <div key={i}>{e}</div>)}</pre>
</Segment>
</Segment.Group>
</Grid.Column>
</Grid>
)
Expand Down
5 changes: 5 additions & 0 deletions docs/app/Examples/addons/Portal/Types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const PortalTypesExamples = () => (
description='A basic portal.'
examplePath='addons/Portal/Types/PortalExamplePortal'
/>
<ComponentExample
title='Controlled'
description='A controlled portal.'
examplePath='addons/Portal/Types/PortalExampleControlled'
/>
</ExampleSection>
)

Expand Down

0 comments on commit b6fb22d

Please sign in to comment.