Skip to content

Commit f217437

Browse files
author
daniel.gomez
committed
Added Dummy test to configure dependencies.
TODO, all the Helper/Localizer/Factory should be moved to an Utils repo in order to use them as inter dependencies btw core and ui modules
1 parent 3fa0e51 commit f217437

14 files changed

+189
-7
lines changed

.babelrc

+4
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

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/node_modules
12
/.idea

package.json

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Bootstrap UI implementation for redux-autoform",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "mocha ./test --compilers js:babel-register"
88
},
99
"repository": {
1010
"type": "git",
@@ -21,5 +21,24 @@
2121
"bugs": {
2222
"url": "https://github.com/redux-autoform/redux-autoform-bootstrap-ui/issues"
2323
},
24-
"homepage": "https://github.com/redux-autoform/redux-autoform-bootstrap-ui#readme"
24+
"homepage": "https://github.com/redux-autoform/redux-autoform-bootstrap-ui#readme",
25+
"dependencies": {
26+
"bootstrap": "^3.3.6",
27+
"react": "^15.1.0",
28+
"react-bootstrap": "^0.30.0-rc.1",
29+
"react-select": "^1.0.0-beta14",
30+
"react-widgets": "^3.4.2",
31+
"underscore": "^1.8.3"
32+
},
33+
"devDependencies": {
34+
"babel-cli": "^6.6.5",
35+
"babel-core": "^6.7.4",
36+
"babel-loader": "^6.2.4",
37+
"babel-plugin-add-module-exports": "^0.2.1",
38+
"babel-polyfill": "^6.9.1",
39+
"babel-preset-es2015": "^6.6.0",
40+
"babel-preset-react": "^6.5.0",
41+
"babel-preset-stage-0": "^6.5.0",
42+
"mocha": "^2.5.3"
43+
}
2544
}

src/components/FormGroup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component, PropTypes } from 'react';
2-
import FormGroupInline from './FormGroupInline';
2+
import FormGroupInline from './FormGroupInline.js';
33
import FormGroupStacked from './FormGroupStacked';
44

55
class FormGroup extends Component {

src/components/FormGroupInline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component, PropTypes } from 'react';
22
import { FormGroup as BootstrapFormGroup } from 'react-bootstrap';
33
import FormGroupInlineControlLabel from './FormGroupInlineControlLabel';
44
import FormGroupInlineContent from './FormGroupInlineContent'
5-
import { getDisplayName } from '../../helpers/metadataHelper';
5+
import { getDisplayName } from '../helpers/metadataHelper'; // TODO Move to Util Repo
66

77
class FormGroupInline extends Component {
88
static propTypes = {

src/components/fieldComponents/CheckBox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { getDisplayName } from '../../../helpers/metadataHelper';
2+
import { getDisplayName } from '../../helpers/metadataHelper'; // TODO Move to Util Repo
33
import { Checkbox as BootstrapCheckbox } from 'react-bootstrap';
44
import Col from 'react-bootstrap/lib/Col';
55

src/components/fieldComponents/DateTimePicker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component, PropTypes } from 'react';
22
import {DateTimePicker as ReactWidgetsDateTimePicker} from 'react-widgets';
3-
import { getDateLocalizer } from '../../../localization/dateLocalizer';
3+
import { getDateLocalizer } from '../../localization/dateLocalizer'; // TODO Move to Util Repo
44
import FormGroup from '../FormGroup';
55

66
class DateTimePicker extends Component {

src/factory/BootstrapFactory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ComponentFactory from './ComponentFactory';
1+
import ComponentFactory from './ComponentFactory'; //TODO Move to Util repo
22
import { registerBaseComponents } from './helper/BootstrapComponentFactoryHelper';
33

44
class BootstrapFactory extends ComponentFactory {

src/helpers/metadataHelper.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function getDisplayName(displayName, name) {
2+
if(displayName == null) return null;
3+
else if(displayName == undefined || displayName == '') return name;
4+
return displayName;
5+
}

src/localization/dateLocalizer.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var _dateLocalizer;
2+
3+
/**
4+
* Sets the date localizer
5+
* @param localizer
6+
*/
7+
export function setDateLocalizer(localizer) {
8+
if (!localizer) throw Error('\'localizer\' should be truthy');
9+
_dateLocalizer = localizer;
10+
}
11+
12+
/**
13+
* Gets the date localizer
14+
* @returns {*}
15+
*/
16+
export function getDateLocalizer() {
17+
if(!_dateLocalizer)
18+
throw Error('Localizer has not yet been set');
19+
return _dateLocalizer;
20+
}
21+

src/localization/momentLocalizer.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { setDateLocalizer } from './dateLocalizer'
2+
3+
export default function(moment) {
4+
if (!moment) throw Error('\'moment\' should be truthy');
5+
6+
if (typeof moment !== 'function')
7+
throw new TypeError('You must provide a valid moment object');
8+
9+
let localField = typeof moment().locale === 'function' ? 'locale' : 'lang';
10+
11+
if (!moment.localeData)
12+
throw new TypeError('The Moment localizer depends on the `localeData` api, please provide a moment object v2.2.0 or higher');
13+
14+
let getMoment = (culture, value, format) => culture ? moment(value, format)[localField](culture) : moment(value, format);
15+
16+
let endOfDecade = (date) => moment(date).add(10, 'year').add(-1, 'millisecond').toDate();
17+
18+
let endOfCentury = (date) => moment(date).add(100, 'year').add(-1, 'millisecond').toDate();
19+
20+
let localizer = {
21+
formats: {
22+
date: 'L',
23+
time: 'LT',
24+
default: 'lll',
25+
header: 'MMMM YYYY',
26+
footer: 'LL',
27+
weekday: 'dd',
28+
dayOfMonth: 'DD',
29+
month: 'MMM',
30+
year: 'YYYY',
31+
32+
decade(date, culture, localizer) {
33+
return localizer.format(date, 'YYYY', culture)
34+
+ ' - ' + localizer.format(endOfDecade(date), 'YYYY', culture)
35+
},
36+
37+
century(date, culture, localizer) {
38+
return localizer.format(date, 'YYYY', culture)
39+
+ ' - ' + localizer.format(endOfCentury(date), 'YYYY', culture)
40+
}
41+
},
42+
43+
firstOfWeek(culture) {
44+
return moment.localeData(culture).firstDayOfWeek()
45+
},
46+
47+
parse(value, format, culture) {
48+
if (!value) return undefined; // localizers should return undefined for empty inputs
49+
const m = getMoment(culture, value, format);
50+
if (m.isValid()) return m.toDate();
51+
return null; // localizers should return nul for invalid inputs
52+
},
53+
54+
format(value, format, culture) {
55+
return getMoment(culture, value).format(format)
56+
}
57+
};
58+
59+
setDateLocalizer(localizer);
60+
61+
return localizer;
62+
}

src/localization/numberLocalizer.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var _numberLocalizer;
2+
3+
/**
4+
* Sets the number localizer
5+
* @param localizer
6+
*/
7+
export function setNumberLocalizer(localizer) {
8+
if (!localizer) throw Error('\'localizer\' should be truthy');
9+
_numberLocalizer = localizer;
10+
}
11+
12+
/**
13+
* Gets the number localizer
14+
* @returns {*}
15+
*/
16+
export function getNumberLocalizer() {
17+
if(!_numberLocalizer)
18+
throw Error('Localizer has not yet been set');
19+
return _numberLocalizer;
20+
}
21+

src/localization/numbroLocalizer.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { setNumberLocalizer } from './numberLocalizer'
2+
3+
export default function(numbro) {
4+
5+
if (!numbro) throw Error('\'numbro\' should be truthy');
6+
7+
if (typeof numbro !== 'function')
8+
throw new TypeError('You must provide a valid numbro object');
9+
10+
let localizer = {
11+
12+
cultureData: numbro.cultureData(),
13+
14+
parse(value, format, culture) {
15+
if(format) throw Error('numbro localizer\'s parse method does not support passing the format');
16+
if(culture) throw Error('numbro localizer\'s parse method does not support passing the culture');
17+
18+
if (value == null || value == undefined || value == '') return undefined; // localizers should return undefined for empty inputs and null for invalid inputs
19+
20+
// numbro's unformat is very permissive, so we need to check for a valid number
21+
if(/^[+-]?[0-9,\.]+$/.test(value)) { // ToDo: improve this regular expression
22+
let result = numbro().unformat(value);
23+
return isNaN(result) ? null : result; // numbro returns NaN sometimes. We need to make sure that the localizers always return null for invalid inputs
24+
}
25+
return null; // localizers should return null for invalid inputs
26+
},
27+
28+
format(value, format, culture) {
29+
if(culture) throw Error('numbro localizer\'s format method does not support passing the culture');
30+
return numbro(value).format(format);
31+
}
32+
};
33+
34+
setNumberLocalizer(localizer);
35+
36+
return localizer;
37+
}

test/createFactory.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { BootstrapFactory } from '../src/index';
2+
3+
4+
describe("Factory creation", function() {
5+
it("User requests an edit factory", function(done) {
6+
done()
7+
})
8+
9+
it("User requests an details factory", function(done) {
10+
done()
11+
})
12+
});

0 commit comments

Comments
 (0)