Skip to content

Commit

Permalink
Add basics.
Browse files Browse the repository at this point in the history
  • Loading branch information
zebulonj committed Apr 17, 2017
1 parent a9d73be commit ea96635
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Map } from 'immutable';

export const FORMS_OUTSIDE_CLICK = 'FORMS_OUTSIDE_CLICK';
export const FORMS_CANCEL_OUTSIDE_CLICK = 'FORMS_CANCEL_OUTSIDE_CLICK';

export const FORMS_INITIALIZE = 'FORMS_INITIALIZE';
export const FORMS_UPDATE_VALUE = 'FORMS_UPDATE_VALUE';
export const FORMS_TOGGLE_SELECT = 'FORMS_TOGGLE_SELECT';

export function initializeForm( form, initialValues = Map() ) {
return {
type: FORMS_INITIALIZE,
form,
initialValues
};
}

export function updateFormValue( form, field, value ) {
return {
type: FORMS_UPDATE_VALUE,
form,
field,
value
};
}

export function toggleFormSelect( form, field ) {
return {
type: FORMS_TOGGLE_SELECT,
form,
field
};
}

export function cancelOutsideClick() {
return {
type: FORMS_CANCEL_OUTSIDE_CLICK
};
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './middleware';
33 changes: 33 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import {
FORMS_OUTSIDE_CLICK,
FORMS_CANCEL_OUTSIDE_CLICK,
FORMS_TOGGLE_SELECT
} from './actions';

const defaultOptions = {
outsideAction: FORMS_OUTSIDE_CLICK
};

export const middleware = options => store => {
options = Object.assign( {}, defaultOptions, options );

const { outsideAction } = options;
let cancel;

if ( document ) {
document.body.addEventListener( 'click', () => {
cancel = false;

setTimeout( () => !cancel && store.dispatch({ type: outsideAction }), 0 );
}, true );
}

return next => action => {
if ( [FORMS_CANCEL_OUTSIDE_CLICK, FORMS_TOGGLE_SELECT].indexOf( action.type ) !== -1 ) {
cancel = true;
}

return next( action );
};
}
25 changes: 25 additions & 0 deletions src/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Map } from 'immutable';

import {
FORMS_OUTSIDE_CLICK,
FORMS_INITIALIZE,
FORMS_UPDATE_VALUE,
FORMS_TOGGLE_SELECT
} from './actions';

const NEW_FORM = Map({});

export function $forms( state = Map(), action ) {
switch ( action.type ) {
case FORMS_OUTSIDE_CLICK:
return state.map( form => form.set( '$toggles', Map() ) );
case FORMS_INITIALIZE:
return state.set( action.form, NEW_FORM.merge({ $values: action.initialValues }) );
case FORMS_UPDATE_VALUE:
return state.update( action.form, Map(), form => form.setIn( ['$values', action.field], action.value ) );
case FORMS_TOGGLE_SELECT:
return state.updateIn( [action.form, '$toggles', action.field], value => !value );
default:
return state;
}
}

0 comments on commit ea96635

Please sign in to comment.