From fc72e6efa620ba0ea4a8e30fcdad125fa44a1258 Mon Sep 17 00:00:00 2001 From: Va Da Date: Sat, 9 Jun 2018 21:08:06 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20onChange=20event?= =?UTF-8?q?=20to=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/LocationSensor/__story__/story.tsx | 2 +- src/LocationSensor/index.ts | 13 ++++++++----- src/WidthQuery/__tests__/index.test.tsx | 1 - 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/LocationSensor/__story__/story.tsx b/src/LocationSensor/__story__/story.tsx index d82d4231..5185a6d7 100644 --- a/src/LocationSensor/__story__/story.tsx +++ b/src/LocationSensor/__story__/story.tsx @@ -9,7 +9,7 @@ const Print = (props) => ; const Facc = () => - {(state) => }; + console.log('s', state)}>{(state) => }; const RenderProp = () => } />; diff --git a/src/LocationSensor/index.ts b/src/LocationSensor/index.ts index 7ecd9ffe..d34ee426 100644 --- a/src/LocationSensor/index.ts +++ b/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]; @@ -23,9 +24,8 @@ if (isClient) { patchHistoryMethod('replaceState'); } -export interface ILocationSensorProps { - children?: (ILocationSensorState) => React.ReactElement; - render?: (ILocationSensorState) => React.ReactElement; +export interface ILocationSensorProps extends IUniversalInterfaceProps { + onChange?: (state: ILocationSensorState) => void; } export interface ILocationSensorState { @@ -84,7 +84,10 @@ export class LocationSensor extends Component { - this.setState(this.buildState(trigger)); + const newState = this.buildState(trigger); + + this.setState(newState); + (this.props.onChange || noop)(newState); }; buildState (trigger: string) { diff --git a/src/WidthQuery/__tests__/index.test.tsx b/src/WidthQuery/__tests__/index.test.tsx index 42d41a78..c99489f6 100644 --- a/src/WidthQuery/__tests__/index.test.tsx +++ b/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 '..';