React UI generated from redux state
npm install --save redux-scene
//Dependencies
npm install --save react react-dom redux react-redux
//Related
npm install react-foundation-lib
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import * as MyComponents from 'react-foundation-lib';
import { Builder, reducer } from 'redux-scene';
//Setup Redux
let store = createStore(reducer);
//Setup StageBuilder
function resolveUI(stage) {
return state.UI
}
let MyBuilder = new Builder(MyComponents, resolve, store.dispatch);
//Render Stage
render((
<Provider store={store}>
<MyBuilder.Stage />
</Provider>
),
document.getElementById('root')
);
//Adding components to redux state
import { actions, actionTypes } from 'redux-scene';
store.dispatch(actions.stage.addScene('layout'));
store.dispatch(actions.stage.setRoot('layout'));
store.dispatch(actions.scene.addComponent('layout','button'));
store.dispatch(actions.scene.addComponent('layout','text'));
store.dispatch(actions.scene.addComponent('layout','div1'));
store.dispatch(actions.scene.setRoot('layout', ['button','div1','text']));
store.dispatch(actions.component.setType('layout','button','Button'));
store.dispatch(actions.component.setAttr('layout','button', 'label', 'hi'));
store.dispatch(actions.component.setEvent('layout','button','onClick', {type: "ON_CLICK", attr: 'blah'}));
store.dispatch(actions.component.setType('layout','text','TextField'));
store.dispatch(actions.component.setAttr('layout','text','value', 'first'));
store.dispatch(actions.component.setEvent('layout','text','onChange', {type: actionTypes.component.SET_ATTR, attr: 'value'}));
store.dispatch(actions.component.setType('layout','div1','div'));
store.dispatch(actions.component.setContent('layout','div1',['component','button']));
The Builder class configures the redux-stage
props | description |
---|---|
RComp | object Collection of React Components { name: Component, ...} |
resolve | function resolve(state) user defined function that returns the portion of state containing the Stage data |
dispatch | the redux dispatch funciton |
returns a Stage component with the given props.
<MyBuilder.Stage />
returns a Scene component with the given props.
<MyBuilder.Scene Scene_ID={"scene_a"}/>
returns a Component component with the given props.
<MyBuilder.Component Scene_ID={"scene_a"} Component_ID={"component_a"} />
renders the entire redux-stage specified by the Builder Object
This is the highest level of the state. It contains a collection of Scenes and the name of the current scene to build.
attrs | definition |
---|---|
root | Specifies the root Scene of the Stage |
scenes | A collection of Scene Objects |
{
root: rootScene,
scenes: {
sceneName: Scene,
...
}
}
props | description |
---|---|
Scene_ID | string Scene identifier |
A Scene contains a collection of components and the heirarchy in which to present them. While scenes can be used in other scenes, components can only be directly used in the scene in which they are defined.
attrs | definition |
---|---|
root | string Component identifier or array [string Component identifier, ...] Specifies the highest level components of the Scene |
components | Collection of Components. Each has a unique name within the Scene. Components can only be used within the Scene they are defined |
{
root: componentName or [componentName, ...],
components: {
componentName: Component,
...
}
}
props | description |
---|---|
Scene_ID | string Scene identifier |
Component_ID | string Component identifier or array [ string Component identifier, ...] |
A Component contains all of the information to construct a React Component or an HTML element.
attrs | definition |
---|---|
type | RComp or HTML element name |
attrs | attr: TypeGrammar; defines attributes |
events | event: actionType pairs to dispatch actions. actions contain the actionType and the value from the component |
content | TypeGrammar; content to render inside component |
{
type: typeName,
attrs: {
attr: TypeGrammar,
...
},
events: {
event: actionType,
...
},
content: TypeGrammar
}
TYPE:
VALUE_TYPE
|| COMPONENT_TYPE
|| LIST_TYPE
|| ANY
LIST_TYPE:
["list", LIST<TYPE>]
VALUE_TYPE:
["value", ANY]
COMPONENT_TYPE:
["component", LIST<IDENTIFIER>]
LIST<L_TYPE>:
L_TYPE
|| [L\_TYPE, ...]
ANY:
!undefined
IDENTIFIER:
[a-Z1-9\_-] ___String___
- "A String"
- ["value", "Another String"]
- ["component", "component_ref"]
- ["component", ["several", "components"]]
- ["list", ["A ", "List ", "Of ", "Strings"]]
- ["list", [["component", "a"], "mixed", ["component", "list"]]]