Skip to content

Commit

Permalink
feat(Responsive): add fireOnMount prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Fedyashov committed Sep 28, 2017
1 parent 393642d commit 473de78
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { Component } from 'react'
import { Grid, Responsive, Segment } from 'semantic-ui-react'

export default class ResponsiveExampleFireOnMount extends Component {
state = {}

handleOnUpdate = (e, { width }) => this.setState({ width })

render() {
const { width } = this.state
const textAlign = width >= Responsive.onlyComputer.minWidth ? 'right' : 'left'

return (
<Responsive
as={Grid}
columns={1}
fireOnMount
onUpdate={this.handleOnUpdate}
>
<Grid.Column textAlign={textAlign}>
<Segment onUpdate={this.handleOnUpdate}>
This grid has responsive align of the text. It will be right aligned on computer and left aligned on other
breakpoins.
</Segment>
</Grid.Column>
</Responsive>
)
}
}
5 changes: 5 additions & 0 deletions docs/app/Examples/addons/Responsive/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const ResponsiveUsageExamples = () => (
description='Responsive listens for window resize events and calls event handler.'
examplePath='addons/Responsive/Usage/ResponsiveExampleOnUpdate'
/>
<ComponentExample
title='Fire on mount'
description='Responsive can fire callbacks immediately after mount.'
examplePath='addons/Responsive/Usage/ResponsiveExampleFireOnMount'
/>
</ExampleSection>
)

Expand Down
3 changes: 3 additions & 0 deletions src/addons/Responsive/Responsive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface ResponsiveProps {
/** Primary content. */
children?: React.ReactNode;

/** Fires callbacks immediately after mount. */
fireOnMount?: boolean;

/** The maximum width at which content will be displayed. */
maxWidth?: number | string;

Expand Down
6 changes: 6 additions & 0 deletions src/addons/Responsive/Responsive.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default class Responsive extends Component {
/** Primary content. */
children: PropTypes.node,

/** Fires callbacks immediately after mount. */
fireOnMount: PropTypes.bool,

/** The maximum width at which content will be displayed. */
maxWidth: PropTypes.oneOfType([
PropTypes.number,
Expand Down Expand Up @@ -59,7 +62,10 @@ export default class Responsive extends Component {
}

componentDidMount() {
const { fireOnMount } = this.props

eventStack.sub('resize', this.handleResize, { target: 'window' })
if (fireOnMount) this.handleUpdate()
}

componentWillUnmount() {
Expand Down
16 changes: 16 additions & 0 deletions test/specs/addons/Responsive/Responsive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ describe('Responsive', () => {
})
})

describe('fireOnMount', () => {
it('do not fire onUpdate by default', () => {
const onUpdate = sandbox.spy()
mount(<Responsive onUpdate={onUpdate} />)

onUpdate.should.have.not.been.called()
})

it('fires onUpdate after mount when true', () => {
const onUpdate = sandbox.spy()
mount(<Responsive fireOnMount onUpdate={onUpdate} />)

onUpdate.should.have.been.calledOnce()
})
})

describe('maxWidth', () => {
it('renders when fits', () => {
sandbox.stub(window, 'innerWidth').value(Responsive.onlyMobile.maxWidth)
Expand Down

0 comments on commit 473de78

Please sign in to comment.