Skip to content
Open
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
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "airbnb"
}
11 changes: 7 additions & 4 deletions lib/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import * as React from "react";
// $FlowFixMe
import { fromJS, is } from "immutable";
import Rx from "rxjs/Rx";
import { Subject } from "rxjs/Rx";

import { map, scan, filter } from "rxjs/operators";

import {
INITIAL_CHANGE_EVENT_TYPE,
VALIDATION_FAILED_EVENT_TYPE,
Expand Down Expand Up @@ -62,13 +65,13 @@ export default function Form({
const WrappedComponentWithDispatcher = StateDispatcher()(WrappedComponent);

class FormContainer extends React.Component<any, any> {
subject: Rx.Subject<FormEvent>;
valueChangeObs: Rx.Subject<FormEvent>;
subject: Subject<FormEvent>;
valueChangeObs: Subject<FormEvent>;
rootDispatcherGetter: ?() => any;

constructor(props) {
super();
this.subject = new Rx.Subject()
this.subject = new Subject()
.scan((acc: AccumulatorEventType, next: FormEvent) => {
switch (next.type) {
case VALUE_CHANGE_EVENT_TYPE:
Expand Down
17 changes: 10 additions & 7 deletions lib/FormElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use babel";
// @flow

import * as React from "react";
Expand All @@ -9,15 +8,19 @@ import {
VALIDATION_FAILED_EVENT_TYPE,
} from "./FormEvents";
import type { FormEvent } from "./FormEvents";
import Rx from "rxjs/Rx";
import { Validation } from "./ValidationHelpers";
import PropTypes from "prop-types";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { filter } from "rxjs/operator/filter";
import { never } from "rxjs/add/observable/never";

const NeverObservable = Rx.Observable.never();
const NeverObservable = Observable.never();

type Props<V> = {
elementName?: string,
valueChangeObs?: Rx.Observable,
valueChangeObs?: Observable<V>,
__debug?: boolean,
value?: V,
validation?: Validation,
Expand All @@ -38,9 +41,9 @@ export default function FormElement({
} = {}) {
return function FormElementHOC(WrappedComponent: React.ComponentType<any>) {
class FormElementContainer extends React.Component<Props<*>, State> {
parentValueChangeObs: Rx.Subject<FormEvent>;
valueChangeObs: Rx.Subject<FormEvent>;
subscription: Rx.Subscription;
parentValueChangeObs: Subject<FormEvent>;
valueChangeObs: Subject<FormEvent>;
subscription: Subscription;
value: ?any;
validation: ?any;
child: React.ElementType;
Expand Down
17 changes: 11 additions & 6 deletions lib/FormWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ import {
INITIAL_CHANGE_EVENT_TYPE,
VALIDATION_FAILED_EVENT_TYPE,
} from "./FormEvents";
import Rx from "rxjs";
import PropTypes from "prop-types";
import { getValue } from "./StateValueHelpers";

const NeverObservable = Rx.Observable.never();
import { Observable } from "rxjs/Observable";
import { never } from "rxjs/add/observable/never";
import "rxjs/add/operator/filter";

import { Subscription } from "rxjs/Subscription";

const NeverObservable = Observable.never();

export default class FormWatcher extends React.Component<Props, State> {
state: State;
props: Props;
watchPath: string;
valueChangeObs: Rx.Observable<*>;
subscription: Rx.Subscription;
valueChangeObs: Observable<*>;
subscription: Subscription;

constructor(props: Props, context: Context) {
super(props);
Expand All @@ -35,7 +40,7 @@ export default class FormWatcher extends React.Component<Props, State> {
this.selectObs(props, context);
}

getAssignedObs(props: Props, context: Context): Rx.Observable<any> {
getAssignedObs(props: Props, context: Context): Observable<any> {
if (context.rootValueChangeObs != null) return context.rootValueChangeObs;
else return NeverObservable;
}
Expand Down Expand Up @@ -147,5 +152,5 @@ type State = {

type Context = {
completeStatePath?: string,
rootValueChangeObs: Rx.Observable<*>,
rootValueChangeObs: Observable<*>,
};
19 changes: 10 additions & 9 deletions lib/FormWatcher.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as React from "react";
import { shallow, mount } from "enzyme";
import { mount } from "enzyme";
import FormWatcher from "./FormWatcher";
import Rx from "rxjs";
import { INITIAL_CHANGE_EVENT_TYPE } from "./FormEvents";
import PropTypes from "prop-types";
import { Observable } from "rxjs/Observable";
import { of } from "rxjs/add/observable/of";

describe("FormWatcher", () => {
class Context extends React.Component {
Expand All @@ -27,7 +28,7 @@ describe("FormWatcher", () => {
};

it('should update when statePath is "infos" and watchPath "infos.name"', done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos",
value: "salut",
});
Expand All @@ -45,7 +46,7 @@ describe("FormWatcher", () => {
});

it("gives nested value in watchedValue", done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos",
value: {
infos: {
Expand All @@ -67,7 +68,7 @@ describe("FormWatcher", () => {
});

it('should update when statePath is "infos.name" and watchPath "infos"', done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos.name",
value: "salut",
});
Expand All @@ -85,7 +86,7 @@ describe("FormWatcher", () => {
});

it('should not update when statePath is "infos.name" and watchPath "name"', done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos.name",
value: "salut",
});
Expand All @@ -103,7 +104,7 @@ describe("FormWatcher", () => {
});

it("should throw if children isn't a function", () => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos.name",
value: "salut",
});
Expand All @@ -119,7 +120,7 @@ describe("FormWatcher", () => {
});

it("should accept a function as watchPath taking current statePath as parameter", done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
statePath: "infos",
value: "salut",
});
Expand All @@ -139,7 +140,7 @@ describe("FormWatcher", () => {
});

it("should handle initial change", done => {
const obs = Rx.Observable.of({
const obs = Observable.of({
value: "salut",
type: INITIAL_CHANGE_EVENT_TYPE,
});
Expand Down
12 changes: 7 additions & 5 deletions lib/StateDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import * as React from "react";
import { fromJS, is, List } from "immutable";
import FormElement from "./FormElement";
import { setValue, getValue } from "./StateValueHelpers";
import Rx from "rxjs/Rx";
import { INITIAL_CHANGE_EVENT_TYPE } from "./FormEvents";
import type { FormEvent, AccumulatorEventType } from "./FormEvents";
import PropTypes from "prop-types";
import { Observable } from "rxjs/Observable";
import { Subscription } from "rxjs/Subscription";
import { never } from "rxjs/add/observable/never";

type Props = {
valueChangeObs?: Rx.Observable<AccumulatorEventType>,
valueChangeObs?: Observable<AccumulatorEventType>,
onChange?: (v: any) => void,
};

Expand All @@ -22,14 +24,14 @@ export default function StateDispatcher(
) {
return function(WrappedComponent: React.ComponentType<any>) {
class Dispatcher extends React.Component<any, any> {
subscription: Rx.Subscription;
subscription: Subscription;
handlers: {
[key: string]: {
get: () => any,
set: (value: any) => void,
},
};
valueChangeObs: Rx.Observable<AccumulatorEventType>;
valueChangeObs: Observable<AccumulatorEventType>;

constructor(props) {
super(props);
Expand Down Expand Up @@ -167,7 +169,7 @@ export default function StateDispatcher(
Dispatcher.defaultProps = Object.assign({}, WrappedComponent.defaultProps, {
onChange() {},
value: {},
valueChangeObs: Rx.Observable.never(),
valueChangeObs: Observable.never(),
});

Dispatcher.propTypes = {
Expand Down
Loading