1.0.16 • Published 6 years ago

react-storm1er-events v1.0.16

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

React Events

Original repo: https://github.com/nebo15/react-nebo15-events

Greenkeeper badge Build Status

Event Manager for React JS application.

Installation

npm install react-storm1er-events --save

Usage

import React from 'react';
import { EventManager } from 'react-storm1er-events';
import { render } from 'react-dom';

const eventManager = new EventManager();

eventManager.subscribe([
  'Event #1',
  'Event #2',
  'Event #3',
], (name, options) => {
  console.log('event', name, options);
});

render(<EventManagerProvider manager={eventManager}>
  {...application}
</EventManagerProvider>, document.getElementById('root'));


// Some component

class App extends React.Component {
  render() {
    return (
      <EventOptions options={{ component: 'app' }}>
        <EventTrack name="Event #1">
          <button>Click me</button>
        </EventTrack>
      </EventOptions>
    );
  }
}

// Click on button invokes event `Event #1` with options { component: 'app' }.

// You can also use EventManager via context `eventManager`

class MyPopup extends React.Component {
  static contextTypes = {
    eventManager: React.PropTypes.object.isRequired,
  };
  componentDidMount {
    this.eventManager.track('MyPopup Appeared');
  }
  render() {
    return (
      <div>My popup</div>
    );
  }
}

EventManager

Manager of subscriptions for events.

Methods

  • track
  • subscribe
  • register
  • subscribeAll
  • unsubscribeAll

EventManagerProvider

HOC component that defines the context for the children elements in React application. All inherit elements will have the context eventManager

Properties

NameTypeDefault valueDescription
managerEventManagerglobal event managerEventManager that will manage the subscription and handle the events from the children of this EventManagerProvider

Contextes

EventManagerProvider defines the context eventManager to pass the instance of EventManager to the instances of EventTrack and EventOptions.

NameTypeDescription
eventManagerEventManagerEventManager passed as a prop manager in EventManagerProvider or default the global EventManager

EventTrack

Event track component. Using EventTrack, you can define the events in your application. EventTrack add the event listener to your element and fire the event in the EventManager.

Properties

NameTypeDefault valueDescription
namestring-Event name
eventstringonClickName of the event handler. According to this doc: https://facebook.github.io/react/docs/events.html
optionsobject-Options, what will sended to the eventManager then event fires.
extendOptionsbooleantrueExtend or replace the options from parent EventOptions
wrapstring or React.Element-Wrapping element. Can be useful when EventTrack can't set the event handler to the element (eg. you use recompose/pure) or if you have few child components.

EventOptions

EventOptions can be useful if you want to pass the options to all child EventTrack components. eg. You want to know the name of the component there the EventTrack was fired.

<EventOptions options={{ component: 'header' }}>
  <header>
    <EventTrack name="Logo Click">
     <img src="images/logo.png" />
    </EventTrack>
    <Navigation />
  </header>
</EventOptions>

According to this example, when Logo Click will fire, it will be received in the EventManager with options {component: 'header' }. If Navigation has its own EventTracks, they also will be fired with the component: 'header' in the options. (only if EventTrack.extendsOptions is not set to false)

Properties

NameTypeDefault valueDescription
optionsobject-Options will be used during firing the event in the inherit EventTrack components
1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago