Skip to content

Commit

Permalink
feat: implement <ShouldUpdate>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 15, 2018
1 parent e71f48a commit b647edc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const MyComponent = mock();
- [`lazy()`](./docs/en/lazy.md), [`delayed()`](./docs/en/delayed.md), and [`viewport()`](./docs/en/viewport.md)
- [Inversion](./docs/en/Inversion.md)
- [`invert()`](./docs/en/invert.md) and [`<Inverted>`](./docs/en/invert.md#inverted)
- `<ShouldUpdate>`
- [`<State>`](./docs/en/State.md) and [`withState()`](./docs/en/State.md#withstate-hoc)
- [`<Toggle>`](./docs/en/Toggle.md), [`withToggle()`](./docs/en/Toggle.md#withtoggle-hoc), and [`@withToggle`](./docs/en/Toggle.md#withtoggle-decorator)
- [`<Flipflop>`](./docs/en/Flipflop.md), [`withFlipflop()`](./docs/en/Flipflop.md#withflipflop-hoc), and [`@withFlipflop`](./docs/en/Flipflop.md#withflipflop-decorator)
Expand Down
18 changes: 18 additions & 0 deletions src/ShouldUpdate/__story__/Example1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Component, createElement as h} from 'react';
import {ShouldUpdate} from '..';

export class Example1 extends Component {
state = {
cnt: 0
};

render () {
return (
<ShouldUpdate when={(props) => props.cnt > 3} props={this.state}>
<div onClick={() => this.setState({cnt: this.state.cnt + 1})}>
Click me ({this.state.cnt})
</div>
</ShouldUpdate>
);
}
}
16 changes: 16 additions & 0 deletions src/ShouldUpdate/__story__/story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Component, createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {linkTo} from '@storybook/addon-links';
import {ShouldUpdate} from '..';
import ShowDocs from '../../../.storybook/ShowDocs'
import {Example1} from './Example1';

storiesOf('Inversion/ShouldUpdate', module)
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ShouldUpdate.md')}))
.add('Renders children', () =>
h(ShouldUpdate, {when: () => false, props: {}},
h('div', {}, 'Hello foobar!')
)
)
.add('When greater than 3', () => <Example1 />)
20 changes: 20 additions & 0 deletions src/ShouldUpdate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Component} from 'react';
import renderProp from '../util/renderProp';

export interface IShouldUpdateProps {
props;
when: (newProps, oldProps) => boolean;
}

export interface IShouldUpdateState {
}

export class ShouldUpdate extends Component<IShouldUpdateProps, IShouldUpdateState> {
shouldComponentUpdate (props) {
return this.props.when(props.props, this.props.props);
}

render () {
return renderProp(this.props, this.props.props);
}
}
6 changes: 3 additions & 3 deletions src/Speak/story.ts → src/Speak/__story__/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {Component, createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {linkTo} from '@storybook/addon-links';
import {Speak} from '.';
import ShowDocs from '../../.storybook/ShowDocs'
import {Speak} from '..';
import ShowDocs from '../../../.storybook/ShowDocs'

class StorySpeakBasic extends Component<any, any> {
state = {
Expand All @@ -26,7 +26,7 @@ class StorySpeakBasic extends Component<any, any> {
}

storiesOf('Generators/Speak', module)
.add('Documentation', () => h(ShowDocs, {md: require('../../docs/en/Speak.md')}))
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Speak.md')}))
.add('Example', () =>
h(StorySpeakBasic)
);

0 comments on commit b647edc

Please sign in to comment.