Skip to content

Commit b0227fb

Browse files
committed
Initial commit
0 parents  commit b0227fb

10 files changed

+349
-0
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015", "stage-0", "react"],
3+
"plugins": ["add-module-exports"]
4+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/lib
3+
/node_modules

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src
2+
test
3+
tools
4+
.npmignore
5+
.idea
6+
.travis.yml

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- stable
5+
cache:
6+
directories:
7+
- node_modules
8+
before_install:
9+
branches:
10+
only:
11+
- master
12+
- /^greenkeeper-.*$/
13+
after_success:
14+
- npm run coverage
15+
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose
16+
- bash <(curl -s https://codecov.io/bash)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Redux Autoform
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# redux-autoform-utils [![Build Status](https://travis-ci.org/redux-autoform/redux-autoform-utils.svg?branch=master)](https://travis-ci.org/redux-autoform/redux-autoform-utils) [![npm version](https://badge.fury.io/js/redux-autoform-utils.svg)](https://badge.fury.io/js/redux-autoform-utils) [![codecov](https://codecov.io/gh/redux-autoform/redux-autoform-utils/branch/master/graph/badge.svg)](https://codecov.io/gh/redux-autoform/redux-autoform-utils) [![Coverage Status](https://coveralls.io/repos/github/redux-autoform/redux-autoform-utils/badge.svg?branch=master)](https://coveralls.io/github/redux-autoform/redux-autoform-utils?branch=master)
2+
3+
[![NPM](https://nodei.co/npm/redux-autoform-utils.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/redux-autoform-utils/) [![NPM](https://nodei.co/npm-dl/redux-autoform-utils.png?months=9&height=3)](https://nodei.co/npm/redux-autoform-utils/)
4+
5+
Common javascript files to all the redux-autoform related projects

package.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "redux-autoform-component-factory",
3+
"version": "0.9",
4+
"description": "Component factory for Redux-Autoform",
5+
"main": "./lib/index.js",
6+
"scripts": {
7+
"build-lib": "babel-node ./tools/build-lib.js",
8+
"test": "mocha ./test --compilers js:babel-register",
9+
"coverage": "babel-node ./node_modules/.bin/isparta cover _mocha"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/redux-autoform/redux-autoform-component-factory.git"
14+
},
15+
"keywords": [
16+
"autoform",
17+
"react",
18+
"redux",
19+
"utils"
20+
],
21+
"author": "",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/redux-autoform/redux-autoform-component-factory/issues"
25+
},
26+
"homepage": "https://github.com/redux-autoform/redux-autoform-utils#readme",
27+
"dependencies": {
28+
"react": "^15.2.1",
29+
"redux-form": "^6.0.1",
30+
"underscore": "^1.8.3"
31+
},
32+
"devDependencies": {
33+
"babel-cli": "^6.6.5",
34+
"babel-core": "^6.7.4",
35+
"babel-loader": "^6.2.4",
36+
"babel-plugin-add-module-exports": "^0.2.1",
37+
"babel-polyfill": "^6.9.1",
38+
"babel-preset-es2015": "^6.6.0",
39+
"babel-preset-react": "^6.5.0",
40+
"babel-preset-stage-0": "^6.5.0",
41+
"chai": "^3.5.0",
42+
"child-process-promise": "^2.0.3",
43+
"colors": "^1.1.2",
44+
"coveralls": "^2.11.11",
45+
"fs-extra-promise": "^0.4.0",
46+
"isparta": "^4.0.0",
47+
"mocha": "^3.0.0",
48+
"path": "^0.12.7",
49+
"rimraf-promise": "^2.0.0"
50+
}
51+
}

src/ComponentFactory.js

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
import React from 'react';
2+
import _ from 'underscore';
3+
import { Field } from 'redux-form';
4+
5+
// component definitions
6+
export default class ComponentFactory {
7+
8+
constructor() {
9+
// this is expected to contain a property for each supported type
10+
// and this property's value is expected to be an array of ComponentBuilder
11+
this.fieldComponentsByType = { };
12+
13+
// this is expected to contain a property for each component definition
14+
// and the value is expected to be the component definition itself
15+
this.fieldComponentsById = { };
16+
17+
// defaultFieldComponents is expected to contain a property for each supported type
18+
// and this property's value is expected to be the component definition id
19+
this.defaultFieldComponents = { };
20+
21+
// this is expected to contain a property for each component definition
22+
// and the value is expected to be the component definition itself
23+
this.groupComponentsById = { };
24+
25+
// The id of the default component for groups
26+
this.defaultGroupComponentId = null;
27+
28+
// This this a list of Root components
29+
this.rootComponentsById = { };
30+
31+
this.currentRoot = null;
32+
33+
}
34+
35+
/**
36+
* Validates the given metadata
37+
* @param metadata
38+
*/
39+
_validateMetadata(metadata) {
40+
if(!metadata)
41+
throw "Metadata should not be null or undefined";
42+
if(!metadata.type)
43+
throw "Metadata should have a type";
44+
if(!metadata.name)
45+
throw "Metadata should have a name";
46+
}
47+
48+
/**
49+
* Registers a component definition
50+
* @param id
51+
* @param types
52+
* @param component
53+
*/
54+
55+
registerFieldComponent(id, types, component) {
56+
// registers the component definition in each given type
57+
for(var i = 0; i < types.length; i++)
58+
{
59+
const type = types[i];
60+
if(!(type in this.fieldComponentsByType))
61+
this.fieldComponentsByType[type] = [];
62+
this.fieldComponentsByType[type].push(component);
63+
}
64+
// registers the component definition
65+
this.fieldComponentsById[id] = component;
66+
}
67+
68+
/**
69+
* @param id The ComponentBuilder id
70+
*/
71+
getFieldComponent(id) {
72+
var component = this.fieldComponentsById[id];
73+
if(!component) {
74+
throw `Could not find the given component. Id: ${id}`;
75+
}
76+
return this.fieldComponentsById[id];
77+
}
78+
79+
/**
80+
* Returns the current component definitions.
81+
* If a type is specified, returns the definitions for that type only
82+
* @returns {{}|*}
83+
*/
84+
getFieldComponents(type) {
85+
if(!type)
86+
return this.fieldComponentsByType;
87+
return this.fieldComponentsByType[type];
88+
}
89+
90+
/**
91+
* Returns the default component definition for the given type
92+
* @param type
93+
*/
94+
getDefaultFieldComponent(type) {
95+
if(!type) throw 'type should have a value';
96+
if(this.defaultFieldComponents[type])
97+
return this.getFieldComponent(this.defaultFieldComponents[type]);
98+
const componentsForType = this.getFieldComponents(type);
99+
const component = _.first(componentsForType);
100+
if(!component)
101+
throw new Error(`Couldn't find any component for the given type. Type: ${type}. Make sure the proper component was registered in the ComponentFactory.`);
102+
return component;
103+
}
104+
105+
/**
106+
* Sets the default component per type.
107+
* @param components - An object that should contain a type as a key and a ComponentBuilder as value
108+
*/
109+
setDefaultFieldComponents(components) {
110+
this.defaultFieldComponents = components;
111+
}
112+
113+
/**
114+
* Gets the appropriate component based on the given metadata
115+
* @param fieldComponentProps
116+
* @returns {*}
117+
*/
118+
buildFieldComponent(fieldComponentProps) {
119+
if(!fieldComponentProps) throw Error('Argument \'props\' should be truthy');
120+
121+
this._validateMetadata(fieldComponentProps);
122+
let componentType;
123+
if(fieldComponentProps.component) {
124+
// if the metadata explicitly specify a component, let's use it
125+
componentType = this.getFieldComponent(fieldComponentProps.component);
126+
}
127+
else
128+
{
129+
// If the metadata doesn't explicitly specify a component, let's return
130+
// the default component for type. If there's no default, let's take the first
131+
// that matches the type
132+
componentType = this.getDefaultFieldComponent(fieldComponentProps.type);
133+
}
134+
if(!componentType)
135+
throw new Error(`Could not resolve the component for the type. Type: ${fieldComponentProps.type}`);
136+
137+
var component = React.createElement(componentType, Object.assign({}, fieldComponentProps, fieldComponentProps.reduxFormProps));
138+
139+
// if there's a 'reduxFormProps' metadata, it should be merged with the
140+
return <Field name={fieldComponentProps.name} component={component} />;
141+
}
142+
143+
/**
144+
* Registers a group component
145+
* @param id
146+
* @param component
147+
*/
148+
registerGroupComponent(id, component) {
149+
this.groupComponentsById[id] = component;
150+
}
151+
152+
getGroupComponent(id) {
153+
let component = this.groupComponentsById[id];
154+
if(!component) {
155+
throw Error(`Could not resolve the group component. Component: ${id}`);
156+
}
157+
return component;
158+
}
159+
160+
/**
161+
* Sets the default group component
162+
* @param id
163+
*/
164+
setDefaultGroupComponent(id) {
165+
this.defaultGroupComponentId = id;
166+
}
167+
168+
/**
169+
* Gets the default group component
170+
* @returns {*}
171+
*/
172+
getDefaultGroupComponent() {
173+
return this.getGroupComponent(this.defaultGroupComponentId);
174+
}
175+
176+
/**
177+
* Gets the appropriate component based on the given metadata
178+
* @param groupComponentProps
179+
* @returns {*}
180+
*/
181+
buildGroupComponent(groupComponentProps) {
182+
if(!groupComponentProps) {
183+
throw Error('The props parameter is required');
184+
}
185+
186+
let componentType;
187+
if(groupComponentProps.component) {
188+
// if the metadata explicitly specify a component, let's use it
189+
componentType = this.getGroupComponent(groupComponentProps.component);
190+
}
191+
else
192+
{
193+
// If the metadata doesn't explicitly specify a component, let's return
194+
// the default component for type. If there's no default, let's take the first
195+
// that matches the type
196+
componentType = this.getDefaultGroupComponent();
197+
}
198+
if(!componentType)
199+
throw new Error(`Could not resolve the component for the group`);
200+
201+
return React.createElement(componentType, groupComponentProps);
202+
}
203+
204+
205+
// Allows to register a new Root component
206+
registerRootComponent(id, component) {
207+
this.rootComponentsById[id] = component;
208+
}
209+
210+
// Allows to define the id of the current Root component that should be used
211+
setCurrentRoot(id) {
212+
this.currentRoot = id;
213+
}
214+
215+
// Return the selected Root component in AutoformInternal
216+
getRoot() {
217+
return this.rootComponentsById[this.currentRoot];
218+
}
219+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Factory */
2+
export default from './ComponentFactory';

tools/build-lib.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'path';
2+
import fsep from 'fs-extra-promise';
3+
import rimraf from 'rimraf-promise';
4+
import colors from 'colors';
5+
import { exec } from 'child-process-promise';
6+
7+
const repoRoot = path.resolve(__dirname, '../');
8+
const lib = path.join(repoRoot, 'lib');
9+
const lessSrc = path.join(repoRoot, '/src/less');
10+
const lessDest = path.join(lib, '/less');
11+
12+
console.log('building lib'.green);
13+
14+
rimraf(lib)
15+
.then(function (error) {
16+
let babelCli = '"./node_modules/.bin/babel" src -d lib';
17+
return exec(babelCli).fail(function (error) {
18+
console.log(colors.red(error))
19+
});
20+
})
21+
.then(() => fsep.copyAsync(lessSrc, lessDest))
22+
.then(() => console.log('lib built'.green));

0 commit comments

Comments
 (0)