-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './middleware'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |