@@ -13,14 +13,15 @@ import ObjectField from "./ObjectField";
13
13
import StringField from "./StringField" ;
14
14
import UnsupportedField from "./UnsupportedField" ;
15
15
16
+
16
17
const REQUIRED_FIELD_SYMBOL = "*" ;
17
18
const COMPONENT_TYPES = {
18
- "array" : ArrayField ,
19
- "boolean" : BooleanField ,
20
- "integer" : NumberField ,
21
- "number" : NumberField ,
22
- "object" : ObjectField ,
23
- "string" : StringField ,
19
+ "array" : ArrayField ,
20
+ "boolean" : BooleanField ,
21
+ "integer" : NumberField ,
22
+ "number" : NumberField ,
23
+ "object" : ObjectField ,
24
+ "string" : StringField ,
24
25
} ;
25
26
26
27
function getFieldComponent ( schema , uiSchema , fields ) {
@@ -34,7 +35,8 @@ function getFieldComponent(schema, uiSchema, fields) {
34
35
return COMPONENT_TYPES [ schema . type ] || UnsupportedField ;
35
36
}
36
37
37
- function getLabel ( label , required , id ) {
38
+ function Label ( props ) {
39
+ const { label, required, id} = props ;
38
40
if ( ! label ) {
39
41
return null ;
40
42
}
@@ -45,7 +47,8 @@ function getLabel(label, required, id) {
45
47
) ;
46
48
}
47
49
48
- function renderHelp ( help ) {
50
+ function Help ( props ) {
51
+ const { help} = props ;
49
52
if ( ! help ) {
50
53
return null ;
51
54
}
@@ -55,23 +58,27 @@ function renderHelp(help) {
55
58
return < div className = "help-block" > { help } </ div > ;
56
59
}
57
60
58
- function ErrorList ( { errors} ) {
61
+ function ErrorList ( props ) {
62
+ const { errors = [ ] } = props ;
63
+ if ( errors . length === 0 ) {
64
+ return null ;
65
+ }
59
66
return (
60
67
< div >
61
68
< p />
62
69
< ul className = "error-detail bs-callout bs-callout-info" > {
63
- ( errors || [ ] ) . map ( ( error , index ) => {
70
+ errors . map ( ( error , index ) => {
64
71
return < li className = "text-danger" key = { index } > { error } </ li > ;
65
72
} )
66
73
} </ ul >
67
74
</ div >
68
75
) ;
69
76
}
70
77
71
- function Wrapper ( {
78
+ function DefaultTemplate ( {
72
79
type,
73
80
classNames,
74
- errorSchema ,
81
+ errors ,
75
82
label,
76
83
description,
77
84
hidden,
@@ -80,55 +87,38 @@ function Wrapper({
80
87
displayLabel,
81
88
id,
82
89
children,
83
- DescriptionField,
84
90
} ) {
85
91
if ( hidden ) {
86
92
return children ;
87
93
}
88
- const errors = errorSchema . __errors ;
89
- const isError = errors && errors . length > 0 ;
90
- const classList = [
91
- "form-group" ,
92
- "field" ,
93
- `field-${ type } ` ,
94
- isError ? "field-error has-error" : "" ,
95
- classNames ,
96
- ] . join ( " " ) . trim ( ) ;
97
94
return (
98
- < div className = { classList } >
99
- { displayLabel && label ? getLabel ( label , required , id ) : null }
100
- { displayLabel && description ?
101
- < DescriptionField id = { `${ id } __description` } description = { description } /> : null }
95
+ < div className = { classNames } >
96
+ { displayLabel ? < Label label = { label } required = { required } id = { id } /> : null }
97
+ { displayLabel && description ? description : null }
102
98
{ children }
103
- { isError ? < ErrorList errors = { errors } /> : < div /> }
104
- { renderHelp ( help ) }
99
+ { errors }
100
+ { help }
105
101
</ div >
106
102
) ;
107
103
}
108
104
109
105
if ( process . env . NODE_ENV !== "production" ) {
110
- Wrapper . propTypes = {
106
+ DefaultTemplate . propTypes = {
111
107
type : PropTypes . string . isRequired ,
112
108
id : PropTypes . string ,
113
109
classNames : PropTypes . string ,
114
110
label : PropTypes . string ,
115
- description : PropTypes . oneOfType ( [
116
- PropTypes . string ,
117
- PropTypes . element ,
118
- ] ) ,
119
- help : PropTypes . oneOfType ( [
120
- PropTypes . string ,
121
- PropTypes . element ,
122
- ] ) ,
111
+ description : PropTypes . element ,
112
+ help : PropTypes . element ,
123
113
hidden : PropTypes . bool ,
124
114
required : PropTypes . bool ,
115
+ readonly : PropTypes . bool ,
125
116
displayLabel : PropTypes . bool ,
126
117
children : PropTypes . node . isRequired ,
127
- DescriptionField : PropTypes . func ,
128
118
} ;
129
119
}
130
120
131
- Wrapper . defaultProps = {
121
+ DefaultTemplate . defaultProps = {
132
122
classNames : "" ,
133
123
errorSchema : { errors : [ ] } ,
134
124
hidden : false ,
@@ -138,9 +128,10 @@ Wrapper.defaultProps = {
138
128
139
129
function SchemaField ( props ) {
140
130
const { uiSchema, errorSchema, idSchema, name, required, registry} = props ;
141
- const { definitions, fields} = registry ;
131
+ const { definitions, fields, FieldTemplate = DefaultTemplate } = registry ;
142
132
const schema = retrieveSchema ( props . schema , definitions ) ;
143
133
const FieldComponent = getFieldComponent ( schema , uiSchema , fields ) ;
134
+ const { DescriptionField} = fields ;
144
135
const disabled = Boolean ( props . disabled || uiSchema [ "ui:disabled" ] ) ;
145
136
const readonly = Boolean ( props . readonly || uiSchema [ "ui:readonly" ] ) ;
146
137
@@ -162,25 +153,44 @@ function SchemaField(props) {
162
153
displayLabel = false ;
163
154
}
164
155
165
- return (
166
- < Wrapper
167
- label = { props . schema . title || schema . title || name }
168
- description = { props . schema . description || schema . description }
169
- errorSchema = { errorSchema }
170
- hidden = { uiSchema [ "ui:widget" ] === "hidden" }
171
- help = { uiSchema [ "ui:help" ] }
172
- required = { required }
173
- type = { schema . type }
174
- displayLabel = { displayLabel }
175
- id = { idSchema . $id }
176
- classNames = { uiSchema . classNames }
177
- DescriptionField = { fields . DescriptionField } >
178
- < FieldComponent { ...props }
179
- schema = { schema }
180
- disabled = { disabled }
181
- readonly = { readonly } />
182
- </ Wrapper >
156
+ const field = (
157
+ < FieldComponent { ...props }
158
+ schema = { schema }
159
+ disabled = { disabled }
160
+ readonly = { readonly } />
183
161
) ;
162
+
163
+ const { type} = schema ;
164
+ const id = idSchema . $id ;
165
+ const label = props . schema . title || schema . title || name ;
166
+ const description = props . schema . description || schema . description ;
167
+ const errors = errorSchema . __errors ;
168
+ const isError = errors && errors . length > 0 ;
169
+ const help = uiSchema [ "ui:help" ] ;
170
+ const hidden = uiSchema [ "ui:widget" ] === "hidden" ;
171
+ const classNames = [
172
+ "form-group" ,
173
+ "field" ,
174
+ `field-${ type } ` ,
175
+ isError ? "field-error has-error" : "" ,
176
+ uiSchema . classNames ,
177
+ ] . join ( " " ) . trim ( ) ;
178
+
179
+ const fieldProps = {
180
+ description : < DescriptionField id = { id + "__description" } description = { description } /> ,
181
+ help : < Help help = { help } /> ,
182
+ errors : < ErrorList errors = { errors } /> ,
183
+ type,
184
+ id,
185
+ label,
186
+ hidden,
187
+ required,
188
+ readonly,
189
+ displayLabel,
190
+ classNames,
191
+ } ;
192
+
193
+ return < FieldTemplate { ...fieldProps } > { field } </ FieldTemplate > ;
184
194
}
185
195
186
196
SchemaField . defaultProps = {
@@ -206,6 +216,7 @@ if (process.env.NODE_ENV !== "production") {
206
216
] ) ) . isRequired ,
207
217
fields : PropTypes . objectOf ( PropTypes . func ) . isRequired ,
208
218
definitions : PropTypes . object . isRequired ,
219
+ FieldTemplate : PropTypes . func ,
209
220
} )
210
221
} ;
211
222
}
0 commit comments