@shopgate/engage
Version:
Shopgate's ENGAGE library.
31 lines • 3.56 kB
JavaScript
var _excluded=["custom"];function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{logger}from'@shopgate/pwa-core/helpers';import{ELEMENT_TYPE_COUNTRY,ELEMENT_TYPE_PROVINCE}from"../Builder.constants";/** Noop function */var noop=function noop(){};/**
* @typedef {Object} FormElement
* @property {string} id
* @property {boolean} custom
* @property {function} handleChange
* @property {number|null|undefined} sortOrder
* @property {string} label
* @property {string} type
* @property {string|null|undefined} default
* @property {string|null|undefined} placeholder
* @property {boolean|null|undefined} required
* @property {boolean|null|undefined} visible
* @property {FormFieldAction[]|null|undefined} actions
*/ /**
* Takes a list of which elements to render based on the respective element type
*
* @param {Form} formConfig Configuration of which form fields to render
* @param {Function} elementChangeHandler change handler
* @return {FormElement[]}
*/export default(function(formConfig){var elementChangeHandler=arguments.length>1&&arguments[1]!==undefined?arguments[1]:noop;/**
* @type {FormElement[]}
*/var elementList=[];var hasCountryElement=false;var hasProvinceElement=false;/**
* @param {string} id id
* @param {AnyFormField} field field
* @param {boolean} custom custom
*/var addFormElement=function addFormElement(id,field){var custom=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;// The "custom" field is just a placeholder for more fields
if(typeof field.type!=='string'){return;}// Make sure country and province elements are only added once
if(field.type===ELEMENT_TYPE_COUNTRY){if(hasCountryElement){logger.error("Error: Can not add multiple elements of type '".concat(field.type,"'"));return;}hasCountryElement=true;}if(field.type===ELEMENT_TYPE_PROVINCE){if(hasProvinceElement){logger.error("Error: Can not add multiple elements of type '".concat(field.type,"'"));return;}hasProvinceElement=true;}elementList.push(_extends({id:id},field,{custom:custom,handleChange:function handleChange(value){return elementChangeHandler(id,value);}}));};// Extract custom fields, do not mix with normal fields
var _formConfig$fields=formConfig.fields,custom=_formConfig$fields.custom,restFields=_objectWithoutProperties(_formConfig$fields,_excluded);// Add all non-custom attributes and mark them as such
Object.keys(restFields).forEach(function(id){addFormElement(id,formConfig.fields[id]);});// Add custom fields to the element list
if(custom){Object.keys(custom).forEach(function(id){addFormElement(id,formConfig.fields.custom[id],true);});}return elementList;});