Skip to content

Commit

Permalink
feat: 🎸 add onChange event to <LocationSensor>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 9, 2018
1 parent f142450 commit fc72e6e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/LocationSensor/__story__/story.tsx
Expand Up @@ -9,7 +9,7 @@ const Print = (props) =>
</pre>;

const Facc = () =>
<LocationSensor>{(state) => <Print {...state} />}</LocationSensor>;
<LocationSensor onChange={(state) => console.log('s', state)}>{(state) => <Print {...state} />}</LocationSensor>;

const RenderProp = () =>
<LocationSensor render={(state) => <Print {...state} />} />;
Expand Down
13 changes: 8 additions & 5 deletions src/LocationSensor/index.ts
@@ -1,7 +1,8 @@
import {Component} from 'react';
import {on, off, isClient} from '../util';
import {on, off, isClient, noop} from '../util';
import faccToHoc from '../util/faccToHoc';
import renderProp from '../util/renderProp';
import {IUniversalInterfaceProps} from '../typing';

const patchHistoryMethod = (method) => {
const original = history[method];
Expand All @@ -23,9 +24,8 @@ if (isClient) {
patchHistoryMethod('replaceState');
}

export interface ILocationSensorProps {
children?: (ILocationSensorState) => React.ReactElement<any>;
render?: (ILocationSensorState) => React.ReactElement<any>;
export interface ILocationSensorProps extends IUniversalInterfaceProps<ILocationSensorState> {
onChange?: (state: ILocationSensorState) => void;
}

export interface ILocationSensorState {
Expand Down Expand Up @@ -84,7 +84,10 @@ export class LocationSensor extends Component<ILocationSensorProps, ILocationSen
};

onChange = (trigger: string) => {
this.setState(this.buildState(trigger));
const newState = this.buildState(trigger);

this.setState(newState);
(this.props.onChange || noop)(newState);
};

buildState (trigger: string) {
Expand Down
1 change: 0 additions & 1 deletion src/WidthQuery/__tests__/index.test.tsx
@@ -1,5 +1,4 @@
import {createElement as h} from 'react';
import {render} from 'react-dom';
import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';
import {WidthQuery} from '..';
Expand Down

0 comments on commit fc72e6e

Please sign in to comment.