-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathTypeField.js
91 lines (73 loc) · 1.81 KB
/
TypeField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
var React = require('react'),
deepSettings = require('./deepSettings'),
objectAssign = require('object-assign')
;
var components = {};
var typeCheckOrder = [];
var TypeField = React.createClass({
components: {},
typeCheckOrder: [],
contextTypes: {
typeDefaults: React.PropTypes.object
},
render: function() {
var Component = this.getComponent(),
settings = objectAssign(
{},
this.context.typeDefaults[ this.props.type ],
this.props.settings
)
;
this.addDeepSettings( settings );
return React.createElement( Component, {
value: this.props.value,
settings: settings,
onUpdated: this.props.onUpdated,
id: this.props.id,
ref: 'field'
});
},
getComponent: function(){
var type = this.props.type;
if( !type )
type = this.guessType( this.props.value );
this.fieldType = type;
return this.components[ type ];
},
guessType: function( value ){
var type = false,
i = 0,
types = this.typeCheckOrder,
component
;
while( !type && i < types.length ){
component = this.components[ types[i] ].prototype;
if( component.isType && component.isType( value ) )
type = types[i++];
else
i++;
}
return type || 'object';
},
getValidationErrors: function( jsonValue ){
return this.refs.field.getValidationErrors( jsonValue );
},
addDeepSettings: function( settings ){
var parentSettings = this.props.parentSettings || {},
deep
;
for( var key in deepSettings ){
deep = deepSettings[ key ]( parentSettings[key], settings[key] );
if( typeof deep != 'undefined' )
settings[ key ] = deep;
}
}
});
TypeField.registerType = function( name, Component, selectable ){
var proto = TypeField.prototype;
proto.components[ name ] = Component;
if( selectable )
proto.typeCheckOrder.unshift( name );
};
module.exports = TypeField;