react-formless
Advanced tools
Comparing version 0.1.7 to 0.1.8
import WidgetsUtils from '../../utils/widgets.js'; | ||
import Containers from '../form/containers'; | ||
import Containers from './containers'; | ||
import Widgets from './widgets'; | ||
function getDisplayWidget(type) { | ||
let widget; | ||
switch (type) { | ||
case 'tel': | ||
case 'text': | ||
case 'email': | ||
case 'conditionaltext': | ||
widget = Widgets.TextInput; | ||
break; | ||
case 'textarea': | ||
widget = Widgets.TextArea; | ||
break; | ||
case 'heading': | ||
widget = Widgets.Heading; | ||
break; | ||
case 'country': | ||
case 'dropdown': | ||
widget = Widgets.Dropdown; | ||
break; | ||
case 'checkbox_group': | ||
widget = Widgets.CheckboxSingle; | ||
break; | ||
case 'linebreak': | ||
widget = Widgets.Linebreak; | ||
break; | ||
case 'paragraph': | ||
widget = Widgets.Paragraph; | ||
break; | ||
case 'radio_group': | ||
widget = Widgets.RadioGroup; | ||
break; | ||
case 'button_group': | ||
widget = Widgets.ButtonGroup; | ||
break; | ||
case 'date': | ||
widget = Widgets.DateInput; | ||
break; | ||
case 'time': | ||
widget = Widgets.TimeInput; | ||
break; | ||
case 'payment_optional_item': | ||
widget = Widgets.PaymentOptional; | ||
break; | ||
case 'sponsorship': | ||
widget = Widgets.Sponsorship; | ||
break; | ||
case 'donation': | ||
widget = Widgets.Donation; | ||
break; | ||
case 'teams': | ||
widget = Widgets.Team; | ||
break; | ||
case 'cvv': | ||
widget = Widgets.Cvv; | ||
break; | ||
case 'terms': // TODO: special case! | ||
default: | ||
return null; | ||
} | ||
return widget; | ||
} | ||
function validationForWidget(w) { | ||
@@ -34,3 +98,3 @@ const vArray = w.get('Validation'); | ||
const type = w.get('Type'); | ||
const Widget = WidgetsUtils.getDisplayWidget(type); | ||
const Widget = getDisplayWidget(type); | ||
if (!Widget) { | ||
@@ -37,0 +101,0 @@ return null; |
{ | ||
"name": "react-formless", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "react forms", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
value: true | ||
}); | ||
@@ -11,70 +11,134 @@ | ||
var _utilsWidgetsJs = require('../../utils/widgets.js'); | ||
var _containers = require('./containers'); | ||
var _utilsWidgetsJs2 = _interopRequireDefault(_utilsWidgetsJs); | ||
var _containers2 = _interopRequireDefault(_containers); | ||
var _formContainers = require('../form/containers'); | ||
var _widgets = require('./widgets'); | ||
var _formContainers2 = _interopRequireDefault(_formContainers); | ||
var _widgets2 = _interopRequireDefault(_widgets); | ||
function getDisplayWidget(type) { | ||
var widget = undefined; | ||
switch (type) { | ||
case 'tel': | ||
case 'text': | ||
case 'email': | ||
case 'conditionaltext': | ||
widget = _widgets2['default'].TextInput; | ||
break; | ||
case 'textarea': | ||
widget = _widgets2['default'].TextArea; | ||
break; | ||
case 'heading': | ||
widget = _widgets2['default'].Heading; | ||
break; | ||
case 'country': | ||
case 'dropdown': | ||
widget = _widgets2['default'].Dropdown; | ||
break; | ||
case 'checkbox_group': | ||
widget = _widgets2['default'].CheckboxSingle; | ||
break; | ||
case 'linebreak': | ||
widget = _widgets2['default'].Linebreak; | ||
break; | ||
case 'paragraph': | ||
widget = _widgets2['default'].Paragraph; | ||
break; | ||
case 'radio_group': | ||
widget = _widgets2['default'].RadioGroup; | ||
break; | ||
case 'button_group': | ||
widget = _widgets2['default'].ButtonGroup; | ||
break; | ||
case 'date': | ||
widget = _widgets2['default'].DateInput; | ||
break; | ||
case 'time': | ||
widget = _widgets2['default'].TimeInput; | ||
break; | ||
case 'payment_optional_item': | ||
widget = _widgets2['default'].PaymentOptional; | ||
break; | ||
case 'sponsorship': | ||
widget = _widgets2['default'].Sponsorship; | ||
break; | ||
case 'donation': | ||
widget = _widgets2['default'].Donation; | ||
break; | ||
case 'teams': | ||
widget = _widgets2['default'].Team; | ||
break; | ||
case 'cvv': | ||
widget = _widgets2['default'].Cvv; | ||
break; | ||
case 'terms': // TODO: special case! | ||
default: | ||
return null; | ||
} | ||
return widget; | ||
} | ||
function validationForWidget(w) { | ||
var vArray = w.get('Validation'); | ||
if (!vArray) { | ||
return null; | ||
} | ||
var vArray = w.get('Validation'); | ||
if (!vArray) { | ||
return null; | ||
} | ||
return vArray.map(function (v) { | ||
if (v === 'required') { | ||
return { name: 'required' }; | ||
} | ||
return vArray.map(function (v) { | ||
if (v === 'required') { | ||
return { name: 'required' }; | ||
} | ||
if (v === 'email') { | ||
return { name: 'email' }; | ||
} | ||
if (v === 'email') { | ||
return { name: 'email' }; | ||
} | ||
if (/^length/.test(v)) { | ||
var _v$match = v.match(/^length\[(\d+)\,(\d+)\]/); | ||
if (/^length/.test(v)) { | ||
var _v$match = v.match(/^length\[(\d+)\,(\d+)\]/); | ||
var _v$match2 = _slicedToArray(_v$match, 3); | ||
var _v$match2 = _slicedToArray(_v$match, 3); | ||
var min = _v$match2[1]; | ||
var max = _v$match2[2]; | ||
var min = _v$match2[1]; | ||
var max = _v$match2[2]; | ||
return { name: 'basicText', notShorter: min, notLonger: max }; | ||
} | ||
}).filter(function (v) { | ||
return !!v; | ||
}).toJS(); | ||
return { name: 'basicText', notShorter: min, notLonger: max }; | ||
} | ||
}).filter(function (v) { | ||
return !!v; | ||
}).toJS(); | ||
} | ||
function renderWidget(w, opts) { | ||
var name = w.get('Name'); | ||
var type = w.get('Type'); | ||
var Widget = _utilsWidgetsJs2['default'].getDisplayWidget(type); | ||
if (!Widget) { | ||
return null; | ||
} | ||
var name = w.get('Name'); | ||
var type = w.get('Type'); | ||
var Widget = getDisplayWidget(type); | ||
if (!Widget) { | ||
return null; | ||
} | ||
// A unique key for each viewer/widget so that react knows when to redraw. | ||
return React.createElement(Widget, { | ||
key: 'widget_' + name, | ||
type: type, | ||
name: name, | ||
layout: w.get('Layout'), | ||
label: w.get('Label'), | ||
description: w.get('Description'), | ||
placeholder: w.get('Placeholder'), | ||
options: w.get('Children'), | ||
validation: validationForWidget(w), | ||
outerClasses: w.get('OuterClasses'), | ||
innerClasses: w.get('InnerClasses'), | ||
labelClasses: w.get('LabelClasses'), | ||
// A unique key for each viewer/widget so that react knows when to redraw. | ||
return React.createElement(Widget, { | ||
key: 'widget_' + name, | ||
type: type, | ||
name: name, | ||
layout: w.get('Layout'), | ||
label: w.get('Label'), | ||
description: w.get('Description'), | ||
placeholder: w.get('Placeholder'), | ||
options: w.get('Children'), | ||
validation: validationForWidget(w), | ||
outerClasses: w.get('OuterClasses'), | ||
innerClasses: w.get('InnerClasses'), | ||
labelClasses: w.get('LabelClasses'), | ||
viewerIndex: opts.viewerIndex /* only used for the team widget at this stage */, | ||
onChangeFns: opts.onChangeFns, | ||
formController: opts.formController, | ||
onInputUpdate: opts.onInputUpdate, | ||
value: opts.value | ||
viewerIndex: opts.viewerIndex /* only used for the team widget at this stage */, | ||
onChangeFns: opts.onChangeFns, | ||
formController: opts.formController, | ||
onInputUpdate: opts.onInputUpdate, | ||
value: opts.value | ||
}); | ||
}); | ||
} | ||
@@ -88,32 +152,32 @@ // validatorContext = {w.get('ValidationContext')} | ||
function renderWidgets(widgets) { | ||
return widgets.map(function (w) { | ||
if (w.get('Type') === 'group') { | ||
return widgets.map(function (w) { | ||
if (w.get('Type') === 'group') { | ||
// TODO: add default.. | ||
var Container = _formContainers2['default'].getForLayout(w.get('Layout')); | ||
// errorMsg={ this._errorMsgForContianer(w.get('Widgets')) } | ||
// isRequired={ this._isRequiredForContainer(w.get('Widgets')) } | ||
return React.createElement( | ||
Container, | ||
{ | ||
key: w.get('Name'), | ||
label: w.get('Label'), | ||
description: w.get('Description'), | ||
outerClasses: w.get('OuterClasses'), | ||
labelClasses: w.get('LabelClasses') | ||
}, | ||
w.get('Widgets').map(function (wi) { | ||
return renderWidget(wi); | ||
}) | ||
); | ||
} | ||
// TODO: add default.. | ||
var Container = _containers2['default'].getForLayout(w.get('Layout')); | ||
// errorMsg={ this._errorMsgForContianer(w.get('Widgets')) } | ||
// isRequired={ this._isRequiredForContainer(w.get('Widgets')) } | ||
return React.createElement( | ||
Container, | ||
{ | ||
key: w.get('Name'), | ||
label: w.get('Label'), | ||
description: w.get('Description'), | ||
outerClasses: w.get('OuterClasses'), | ||
labelClasses: w.get('LabelClasses') | ||
}, | ||
w.get('Widgets').map(function (wi) { | ||
return renderWidget(wi); | ||
}) | ||
); | ||
} | ||
return renderWidget(w); | ||
}); | ||
return renderWidget(w); | ||
}); | ||
} | ||
exports['default'] = { | ||
renderWidgets: renderWidgets, | ||
renderWidget: renderWidget, | ||
validationForWidget: validationForWidget | ||
renderWidgets: renderWidgets, | ||
renderWidget: renderWidget, | ||
validationForWidget: validationForWidget | ||
}; | ||
@@ -120,0 +184,0 @@ module.exports = exports['default']; |
2160589
33508