dbl-components
Version:
Framework based on bootstrap 5
2 lines • 11.7 kB
JavaScript
function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function _slicedToArray(r,e){return _arrayWithHoles(r)||_iterableToArrayLimit(r,e)||_unsupportedIterableToArray(r,e)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArrayLimit(r,l){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var e,n,i,u,a=[],f=!0,o=!1;try{if(i=(t=t.call(r)).next,0===l){if(Object(t)!==t)return;f=!1}else for(;!(f=(e=i.call(t)).done)&&(a.push(e.value),a.length!==l);f=!0);}catch(r){o=!0,n=r}finally{try{if(!f&&null!=t["return"]&&(u=t["return"](),Object(u)!==u))return}finally{if(o)throw n}}return a}}function _arrayWithHoles(r){if(Array.isArray(r))return r}function _toConsumableArray(r){return _arrayWithoutHoles(r)||_iterableToArray(r)||_unsupportedIterableToArray(r)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(r,a){if(r){if("string"==typeof r)return _arrayLikeToArray(r,a);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(r,a):void 0}}function _iterableToArray(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _arrayWithoutHoles(r){if(Array.isArray(r))return _arrayLikeToArray(r)}function _arrayLikeToArray(r,a){(null==a||a>r.length)&&(a=r.length);for(var e=0,n=Array(a);e<a;e++)n[e]=r[e];return n}function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,o)}return t}function _objectSpread(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach(function(r){_defineProperty(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function _classCallCheck(a,n){if(!(a instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var o=r[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function _callSuper(t,o,e){return o=_getPrototypeOf(o),_possibleConstructorReturn(t,_isNativeReflectConstruct()?Reflect.construct(o,e||[],_getPrototypeOf(t).constructor):o.apply(t,e))}function _possibleConstructorReturn(t,e){if(e&&("object"==_typeof(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return _assertThisInitialized(t)}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _isNativeReflectConstruct(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(_isNativeReflectConstruct=function _isNativeReflectConstruct(){return!!t})()}function _superPropGet(t,o,e,r){var p=_get(_getPrototypeOf(1&r?t.prototype:t),o,e);return 2&r&&"function"==typeof p?function(t){return p.apply(e,t)}:p}function _get(){return _get="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(e,t,r){var p=_superPropBase(e,t);if(p){var n=Object.getOwnPropertyDescriptor(p,t);return n.get?n.get.call(arguments.length<3?e:r):n.value}},_get.apply(null,arguments)}function _superPropBase(t,o){for(;!{}.hasOwnProperty.call(t,o)&&null!==(t=_getPrototypeOf(t)););return t}function _getPrototypeOf(t){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},_getPrototypeOf(t)}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&_setPrototypeOf(t,e)}function _setPrototypeOf(t,e){return _setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},_setPrototypeOf(t,e)}function _defineProperty(e,r,t){return(r=_toPropertyKey(r))in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==_typeof(i)?i:i+""}function _toPrimitive(t,r){if("object"!=_typeof(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}import React from"react";import moment from"moment";import{resolveRefs,eventHandler}from"dbl-utils";import JsonRender from"../../json-render";import Field from"./field";var schemaInput={control:{name:["$definitions/name","wrapControl"],tag:"div",classes:"input-group",style:{flexWrap:"nowrap"},content:{from:{name:["$definitions/name","fromControl"],wrapper:false,component:"NoWrapField",controlClasses:"border-end-0",type:"$definitions/type",style:{minWidth:50}},divisor:{name:["$definitions/name","divisor"],tag:"span",classes:"input-group-text border-start-0 border-end-0",content:"$definitions/divisor"},to:{name:["$definitions/name","toControl"],wrapper:false,component:"NoWrapField",controlClasses:"border-start-0",type:"$definitions/type",style:{minWidth:50}}}},definitions:{}};var RangeField=/*#__PURE__*/function(_Field){function RangeField(props){var _this;_classCallCheck(this,RangeField);_this=_callSuper(this,RangeField,[props]);_defineProperty(_this,"events",[]);_this.mutations=_this.mutations.bind(_this);_this.jsonRender=new JsonRender(props,_this.mutations);_this.schemaInput=resolveRefs(schemaInput.control,{definitions:_objectSpread(_objectSpread({},schemaInput.definitions),{},{name:props.name,type:props.type,divisor:props.divisor})});_this.events.push(["".concat(_this.props.name,"-fromControl"),_this.onValuesChange.bind(_this)],["".concat(_this.props.name,"-toControl"),_this.onValuesChange.bind(_this)],["invalid.".concat(_this.props.name,"-fromControl"),_this.onAnyInvalid.bind(_this)],["invalid.".concat(_this.props.name,"-toControl"),_this.onAnyInvalid.bind(_this)],["focus.".concat(_this.props.name,"-fromControl"),_this.onAnyFocus.bind(_this)],["focus.".concat(_this.props.name,"-toControl"),_this.onAnyFocus.bind(_this)]);return _this}_inherits(RangeField,_Field);return _createClass(RangeField,[{key:"componentDidMount",value:function componentDidMount(){_superPropGet(RangeField,"componentDidMount",this,3)([]);this.events.forEach(function(evt){return eventHandler.subscribe.apply(eventHandler,_toConsumableArray(evt))})}},{key:"componentWillUnmount",value:function componentWillUnmount(){_superPropGet(RangeField,"componentWillUnmount",this,3)([]);this.events.forEach(function(_ref){var _ref2=_slicedToArray(_ref,1),evtName=_ref2[0];return eventHandler.unsubscribe(evtName)})}},{key:"onUpdate",value:function onUpdate(){for(var _len=arguments.length,args=new Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key]}_superPropGet(RangeField,"onUpdate",this,3)(args);var value=args[0].value;if(value===undefined)return;var _ref3=value===null||value===""?[null,null]:value,_ref4=_slicedToArray(_ref3,2),from=_ref4[0],to=_ref4[1];this.setState({min:from,max:to});eventHandler.dispatch("update.".concat(this.props.name,"-fromControl"),{value:from});eventHandler.dispatch("update.".concat(this.props.name,"-toControl"),{value:to})}},{key:"inputNode",get:function get(){return this.jsonRender.buildContent(this.schemaInput,0)}},{key:"onValuesChange",value:function onValuesChange(data){var _this2=this;var newState={};if(data["".concat(this.props.name,"-fromControl")]){var min=data["".concat(this.props.name,"-fromControl")];if((this.props.type||this.type)==="number")min=Number(min);newState.min=min}if(data["".concat(this.props.name,"-toControl")]){var max=data["".concat(this.props.name,"-toControl")];if((this.props.type||this.type)==="number")max=Number(max);newState.max=max}this.setState(newState,function(){return _this2.onChange({target:{value:[_this2.state.min,_this2.state.max]}})})}},{key:"isInvalid",value:function isInvalid(value){var _value=_slicedToArray(value,2),min=_value[0],max=_value[1];var isInvalid=true;switch(this.props.type){case"date":case"time":isInvalid=!(max&&min)||moment(min).isAfter(max);break;default:isInvalid=!(typeof max==="number"&&typeof min==="number")||min>max;break}return isInvalid}},{key:"onAnyInvalid",value:function onAnyInvalid(){this.onInvalid()}},{key:"onAnyFocus",value:function onAnyFocus(){this.onFocus()}},{key:"mutations",value:function mutations(name,section){switch(name){case"".concat(this.props.name,"-fromControl"):case"".concat(this.props.name,"-toControl"):{var _this$props=this.props,autoComplete=_this$props.autoComplete,list=_this$props.list,pattern=_this$props.pattern,required=_this$props.required,type=_this$props.type,disabled=_this$props.disabled,min=_this$props.min,max=_this$props.max,step=_this$props.step,noValidate=_this$props.noValidate,readOnly=_this$props.readOnly,dir=_this$props.dir,accept=_this$props.accept,multiple=_this$props.multiple,maxLength=_this$props.maxLength,minLength=_this$props.minLength;var v=this.state.value;var ival=name.endsWith("fromControl")?0:1;var minMax=name.endsWith("fromControl")?"max":"min";var controlClasses=new Set([this.props.controlClasses].flat().map(function(c){return v.split(" ")}).flat());var addDelete;if(readOnly)addDelete="add";else addDelete="delete";controlClasses[addDelete]("form-control-plaintext");return Object.assign({autoComplete:autoComplete,list:list,pattern:pattern,required:required,type:type,disabled:disabled,min:min,max:max,step:step,noValidate:noValidate,readOnly:readOnly,dir:dir,accept:accept,multiple:multiple,maxLength:maxLength,minLength:minLength,value:v[ival]},name.endsWith("fromControl")?this.props.from:this.props.to,_defineProperty(_defineProperty({type:this.props.type},minMax,this.state[minMax]),"controlClasses",Array.from(controlClasses).flat().join(" ")))}case"".concat(this.props.name,"-divisor"):{var classes=new Set(section.classes.split(" "));var _addDelete;if(this.props.readOnly)_addDelete="add";else _addDelete="delete";classes[_addDelete]("border-0");return{classes:Array.from(classes).flat().join(" "),style:{backgroundColor:this.props.disabled?"var(--bs-secondary-bg)":"var(--bs-transparent-bg)"},content:this.props.divisor}}default:break}}}])}(Field);_defineProperty(RangeField,"jsClass","RangeField");_defineProperty(RangeField,"defaultProps",_objectSpread(_objectSpread({},Field.defaultProps),{},{type:"number","default":[],from:{},to:{},divisor:"-",controlClasses:[]}));export{RangeField as default};
//# sourceMappingURL=range-field.js.map