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(Responsive): add fireOnMount prop #2137

Merged
merged 1 commit into from
Nov 4, 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
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>
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