react-hook-form-jsonschema
Advanced tools
Comparing version 0.2.0-beta.3 to 0.2.0-beta.4
@@ -10,2 +10,8 @@ # Changelog | ||
## [0.2.0-beta.4] - 2020-02-05 | ||
### Fixed | ||
- Returning non-filled form inputs in the object passed to onSubmit | ||
## [0.2.0-beta.3] - 2020-01-31 | ||
@@ -12,0 +18,0 @@ |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react-hook-form"),r=require("react"),s=(e=r)&&"object"==typeof e&&"default"in e?e.default:e;const o=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},n=(e,t)=>{const r=Object.keys(t).sort(),s={};for(const n of r){const r=o(n);if(!r)continue;let a=s,i=e;for(let e=0;e<r.length&&void 0!==i;e++)i.type&&"object"===i.type?i=i.properties:!i.type&&i[r[e]]&&"object"===i[r[e]].type&&(i=i[r[e]]),e===r.length-1&&(i[r[e]]&&"integer"===i[r[e]].type?a[r[e]]=parseInt(t[n]):i[r[e]]&&"number"===i[r[e]].type?a[r[e]]=parseFloat(t[n]):i[r[e]]&&"boolean"===i[r[e]].type?a[r[e]]="true"===t[n]:a[r[e]]=t[n]),void 0===a[r[e]]&&(a[r[e]]={}),a=a[r[e]]}return s},a=r.createContext(null);function i(){return r.useContext(a)}var p,u,m;(p=exports.InputTypes||(exports.InputTypes={})).generic="generic",p.radio="radio",p.select="select",p.input="input",p.textArea="textArea",p.checkbox="checkbox",(u=exports.UITypes||(exports.UITypes={})).default="default",u.radio="radio",u.select="select",u.input="input",u.hidden="hidden",u.password="password",u.textArea="textArea",u.checkbox="checkbox",(m=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",m.maxLength="__form_error_maxLenght__",m.minLength="__form_error_minLenght__",m.maxValue="__form_error_maxValue__",m.minValue="__form_error_minValue__",m.pattern="__form_error_pattern__",m.multipleOf="__form_error_multipleOf__",m.undefinedError="__form_error_undefinedError__";const c=e=>{const t={};return e&&(t.required=exports.ErrorTypes.required),t},l=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},d=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},x=e=>{const[t]=d(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},g=e=>{const[t]=d(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r},h=(e,t)=>{const r=x(e),s=g(e),o={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||exports.ErrorTypes.multipleOf)}};return t&&(o.required=exports.ErrorTypes.required),"integer"===e.type?o.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:exports.ErrorTypes.pattern}:o.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:exports.ErrorTypes.pattern},(r||0===r)&&(o.min={value:r,message:exports.ErrorTypes.minValue}),(s||0===s)&&(o.max={value:s,message:exports.ErrorTypes.maxValue}),o},y=(e,t)=>{const r={};return t&&(r.required=exports.ErrorTypes.required),e.minLength&&(r.minLength={value:e.minLength,message:exports.ErrorTypes.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:exports.ErrorTypes.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:exports.ErrorTypes.pattern}),r},b=(e,t)=>e+"/"+t,f=e=>{const t=o(e);let r=i().schema,s=!1,n="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}s=!!(r.required&&r.required.indexOf(t[e])>-1),n=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:s,objectName:n}},v=(e,t,r)=>{const{JSONSchema:s,isRequired:o,objectName:n}=t;let a,i,p;if("number"===s.type||"integer"===s.type){const e=d(s);p=e[0],a=x(s),i=g(s)}return{name:n,path:r,isRequired:o,formContext:e,type:exports.InputTypes.generic,validator:{},getError:()=>((e,t,r,s,o,n,a,i)=>{if("array"===t.type){const e=s.getValues({nest:!0})[o];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:exports.ErrorTypes.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:exports.ErrorTypes.maxLength,expected:t.maxItems}}}if(!e)return;const p={message:exports.ErrorTypes.undefinedError,expected:void 0};switch(e.message){case exports.ErrorTypes.required:p.message=exports.ErrorTypes.required,p.expected=r;break;case exports.ErrorTypes.maxLength:p.message=exports.ErrorTypes.maxLength,p.expected=t.maxLength;break;case exports.ErrorTypes.minLength:p.message=exports.ErrorTypes.minLength,p.expected=t.minLength;break;case exports.ErrorTypes.maxValue:p.message=exports.ErrorTypes.maxValue,p.expected=n;break;case exports.ErrorTypes.minValue:p.message=exports.ErrorTypes.minValue,p.expected=a;break;case exports.ErrorTypes.multipleOf:p.message=exports.ErrorTypes.multipleOf,p.expected=i;break;case exports.ErrorTypes.pattern:p.message=exports.ErrorTypes.pattern,p.expected=t.pattern}return p})(e.errors[r]?e.errors[r]:void 0,s,o,e,r,a,i,p),getObject:()=>s}},O=e=>{const t=i(),r=f(e);return v(t,r,e)},T=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),_=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("array"===t.type){if(t.items.enum)i=t.items.enum;else if("string"===t.items.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}t.uniqueItems&&(i=[...new Set(i)])}else"boolean"===t.type&&(a=c(e.isRequired),i=["true"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const s={key:""};return s.name="array"===t.type?`${e.path}[${r}]`:e.path,s.ref=e.formContext.register(a),s.type="checkbox",s.id=T(e.path,r,i),s.value=i[r],s},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=T(e.path,t,i),r},getItems:()=>i})},I=(e,t)=>e+"-"+t+"-input",q=(e,t)=>{const{register:r}=e.formContext,s=e.getObject();let o,n,a,i,p={};const u={key:""};if("string"===s.type)p=y(s,e.isRequired),u.pattern=s.pattern,u.minLength=s.minLength,u.maxLength=s.maxLength;else if("number"===s.type||"integer"===s.type){const t=d(s);a=t[0],i=t[1],o=x(s),n=g(s),p=h(s,e.isRequired),u.min=`${o}`,u.max=`${n}`,u.step="any"===a?"any":l(a,i||0)}return Object.assign(Object.assign({},e),{validator:p,type:exports.InputTypes.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(p),u.type=t,u.required=e.isRequired,u.id=I(e.path,t),u)})},E=()=>({}),j=e=>Object.assign(Object.assign({},q(e,"hidden")),{isRequired:!1,getLabelProps:E}),k=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return q(e,t)},L=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),R=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("string"===t.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(a),r.type="radio",r.required=e.isRequired,r.id=L(e.path,t,i),r.value=i[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=L(e.path,t,i),r},getItems:()=>i})},S=e=>e+"-select",P=e=>{const t=e.getObject();let r,s,o,n,a={},i=[""];if("string"===t.type)i=i.concat(t.enum?t.enum:[]),a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.select,validator:a,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=S(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(a),t.required=e.isRequired,t.id=S(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,i),r.value=i[t],r},getItems:()=>i})},F=e=>q(e,"password"),V=e=>e+"-textarea-input",C=e=>{const t=e.getObject();let r={};const s={};return"string"===t.type?(r=y(t,e.isRequired),s.minLength=t.minLength,s.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=h(t,e.isRequired)),Object.assign(Object.assign({},e),{type:exports.InputTypes.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=V(e.path),t},getTextAreaProps:()=>(s.name=e.path,s.ref=e.formContext.register(r),s.required=e.isRequired,s.id=V(e.path),s)})};function w(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(P(e)):r.push(k(e));break;case"integer":case"number":r.push(k(e));break;case"array":case"boolean":r.push(_(e))}return r}function M(e,t,r,s){let o=[];const{JSONSchema:n}=t,a=v(e,t,r);if("object"===n.type){return Object.keys(n.properties).reduce(function(e,t,r,s){const o=t.required;return(n,a)=>{const i=!!o&&-1!==o.indexOf(a),p=r&&r.properties?r.properties[a]:void 0,u=b(e,a),m={JSONSchema:t.properties[a],isRequired:i,objectName:a},c=M(s,m,u,p);return n.concat(c)}}(r,n,s,e),[])}if(!s)return o.concat(w(a));switch(s.type){case exports.UITypes.default:o=o.concat(w(a));break;case exports.UITypes.checkbox:o.push(_(a));break;case exports.UITypes.hidden:o.push(j(a));break;case exports.UITypes.input:o.push(k(a));break;case exports.UITypes.password:o.push(F(a));break;case exports.UITypes.radio:o.push(R(a));break;case exports.UITypes.select:o.push(P(a));break;case exports.UITypes.textArea:o.push(C(a))}return o}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:r="onSubmit",revalidateMode:o="onChange",submitFocusError:i=!0}=e,p=t.useForm({mode:r,reValidateMode:o,submitFocusError:i}),u={};return u.onSubmit=p.handleSubmit((t,r)=>{e.onSubmit&&e.onSubmit(n(e.schema,t),r)}),e.noNativeValidate&&(u.noValidate=e.noNativeValidate),s.createElement(a.Provider,{value:Object.assign(Object.assign({},p),{schema:e.schema})},s.createElement("form",Object.assign({},u),e.children))},exports.concatFormPath=b,exports.useCheckbox=e=>_(O(e)),exports.useGenericInput=O,exports.useHidden=e=>j(O(e)),exports.useInput=e=>k(O(e)),exports.useObject=e=>M(i(),f(e.path),e.path,e.UISchema),exports.useObjectFromForm=e=>n(i().schema,e),exports.useObjectFromPath=f,exports.usePassword=e=>F(O(e)),exports.useRadio=e=>R(O(e)),exports.useSelect=e=>P(O(e)),exports.useTextArea=e=>C(O(e)); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react-hook-form"),r=require("react"),s=(e=r)&&"object"==typeof e&&"default"in e?e.default:e;const o=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},n=(e,t)=>{const r=Object.keys(t).sort(),s={};for(const n of r){const r=o(n);if(!r&&t[n])continue;let a=s,i=e;for(let e=0;e<r.length&&void 0!==i;e++){if(i.type&&"object"===i.type&&(i=i.properties,i=i[r[e]]),e===r.length-1){if(!i)break;"integer"===i.type?a[r[e]]=parseInt(t[n]):"number"===i.type?a[r[e]]=parseFloat(t[n]):"boolean"===i.type?a[r[e]]="true"===t[n]:a[r[e]]=t[n]}else void 0===a[r[e]]&&(a[r[e]]={});a=a[r[e]]}}return s},a=r.createContext(null);function i(){return r.useContext(a)}var p,u,m;(p=exports.InputTypes||(exports.InputTypes={})).generic="generic",p.radio="radio",p.select="select",p.input="input",p.textArea="textArea",p.checkbox="checkbox",(u=exports.UITypes||(exports.UITypes={})).default="default",u.radio="radio",u.select="select",u.input="input",u.hidden="hidden",u.password="password",u.textArea="textArea",u.checkbox="checkbox",(m=exports.ErrorTypes||(exports.ErrorTypes={})).required="__form_error_required__",m.maxLength="__form_error_maxLenght__",m.minLength="__form_error_minLenght__",m.maxValue="__form_error_maxValue__",m.minValue="__form_error_minValue__",m.pattern="__form_error_pattern__",m.multipleOf="__form_error_multipleOf__",m.undefinedError="__form_error_undefinedError__";const c=e=>{const t={};return e&&(t.required=exports.ErrorTypes.required),t},l=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},d=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},x=e=>{const[t]=d(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},g=e=>{const[t]=d(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r},h=(e,t)=>{const r=x(e),s=g(e),o={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||exports.ErrorTypes.multipleOf)}};return t&&(o.required=exports.ErrorTypes.required),"integer"===e.type?o.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:exports.ErrorTypes.pattern}:o.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:exports.ErrorTypes.pattern},(r||0===r)&&(o.min={value:r,message:exports.ErrorTypes.minValue}),(s||0===s)&&(o.max={value:s,message:exports.ErrorTypes.maxValue}),o},y=(e,t)=>{const r={};return t&&(r.required=exports.ErrorTypes.required),e.minLength&&(r.minLength={value:e.minLength,message:exports.ErrorTypes.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:exports.ErrorTypes.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:exports.ErrorTypes.pattern}),r},b=(e,t)=>e+"/"+t,f=e=>{const t=o(e);let r=i().schema,s=!1,n="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}s=!!(r.required&&r.required.indexOf(t[e])>-1),n=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:s,objectName:n}},v=(e,t,r)=>{const{JSONSchema:s,isRequired:o,objectName:n}=t;let a,i,p;if("number"===s.type||"integer"===s.type){const e=d(s);p=e[0],a=x(s),i=g(s)}return{name:n,path:r,isRequired:o,formContext:e,type:exports.InputTypes.generic,validator:{},getError:()=>((e,t,r,s,o,n,a,i)=>{if("array"===t.type){const e=s.getValues({nest:!0})[o];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:exports.ErrorTypes.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:exports.ErrorTypes.maxLength,expected:t.maxItems}}}if(!e)return;const p={message:exports.ErrorTypes.undefinedError,expected:void 0};switch(e.message){case exports.ErrorTypes.required:p.message=exports.ErrorTypes.required,p.expected=r;break;case exports.ErrorTypes.maxLength:p.message=exports.ErrorTypes.maxLength,p.expected=t.maxLength;break;case exports.ErrorTypes.minLength:p.message=exports.ErrorTypes.minLength,p.expected=t.minLength;break;case exports.ErrorTypes.maxValue:p.message=exports.ErrorTypes.maxValue,p.expected=n;break;case exports.ErrorTypes.minValue:p.message=exports.ErrorTypes.minValue,p.expected=a;break;case exports.ErrorTypes.multipleOf:p.message=exports.ErrorTypes.multipleOf,p.expected=i;break;case exports.ErrorTypes.pattern:p.message=exports.ErrorTypes.pattern,p.expected=t.pattern}return p})(e.errors[r]?e.errors[r]:void 0,s,o,e,r,a,i,p),getObject:()=>s}},O=e=>{const t=i(),r=f(e);return v(t,r,e)},T=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),_=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("array"===t.type){if(t.items.enum)i=t.items.enum;else if("string"===t.items.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}t.uniqueItems&&(i=[...new Set(i)])}else"boolean"===t.type&&(a=c(e.isRequired),i=["true"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const s={key:""};return s.name="array"===t.type?`${e.path}[${r}]`:e.path,s.ref=e.formContext.register(a),s.type="checkbox",s.id=T(e.path,r,i),s.value=i[r],s},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=T(e.path,t,i),r},getItems:()=>i})},I=(e,t)=>e+"-"+t+"-input",q=(e,t)=>{const{register:r}=e.formContext,s=e.getObject();let o,n,a,i,p={};const u={key:""};if("string"===s.type)p=y(s,e.isRequired),u.pattern=s.pattern,u.minLength=s.minLength,u.maxLength=s.maxLength;else if("number"===s.type||"integer"===s.type){const t=d(s);a=t[0],i=t[1],o=x(s),n=g(s),p=h(s,e.isRequired),u.min=`${o}`,u.max=`${n}`,u.step="any"===a?"any":l(a,i||0)}return Object.assign(Object.assign({},e),{validator:p,type:exports.InputTypes.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(u.name=e.path,u.ref=r(p),u.type=t,u.required=e.isRequired,u.id=I(e.path,t),u)})},E=()=>({}),j=e=>Object.assign(Object.assign({},q(e,"hidden")),{isRequired:!1,getLabelProps:E}),k=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return q(e,t)},L=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),R=e=>{const t=e.getObject();let r,s,o,n,a={},i=[];if("string"===t.type)i=t.enum?t.enum:[],a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{validator:a,type:exports.InputTypes.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(a),r.type="radio",r.required=e.isRequired,r.id=L(e.path,t,i),r.value=i[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,i),r.htmlFor=L(e.path,t,i),r},getItems:()=>i})},S=e=>e+"-select",P=e=>{const t=e.getObject();let r,s,o,n,a={},i=[""];if("string"===t.type)i=i.concat(t.enum?t.enum:[]),a=y(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const p=d(t);if(o=p[0],n=p[1],r=x(t),s=g(t),void 0!==r&&void 0!==s&&"any"!=o){a=h(t,e.isRequired);for(let e=r;e<=s;e+=o)i.push(l(e,n||0))}}else"boolean"===t.type&&(a=c(e.isRequired),i=["true","false"]);return Object.assign(Object.assign({},e),{type:exports.InputTypes.select,validator:a,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=S(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(a),t.required=e.isRequired,t.id=S(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,i),r.value=i[t],r},getItems:()=>i})},F=e=>q(e,"password"),V=e=>e+"-textarea-input",C=e=>{const t=e.getObject();let r={};const s={};return"string"===t.type?(r=y(t,e.isRequired),s.minLength=t.minLength,s.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=h(t,e.isRequired)),Object.assign(Object.assign({},e),{type:exports.InputTypes.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=V(e.path),t},getTextAreaProps:()=>(s.name=e.path,s.ref=e.formContext.register(r),s.required=e.isRequired,s.id=V(e.path),s)})};function w(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(P(e)):r.push(k(e));break;case"integer":case"number":r.push(k(e));break;case"array":case"boolean":r.push(_(e))}return r}function M(e,t,r,s){let o=[];const{JSONSchema:n}=t,a=v(e,t,r);if("object"===n.type){return Object.keys(n.properties).reduce(function(e,t,r,s){const o=t.required;return(n,a)=>{const i=!!o&&-1!==o.indexOf(a),p=r&&r.properties?r.properties[a]:void 0,u=b(e,a),m={JSONSchema:t.properties[a],isRequired:i,objectName:a},c=M(s,m,u,p);return n.concat(c)}}(r,n,s,e),[])}if(!s)return o.concat(w(a));switch(s.type){case exports.UITypes.default:o=o.concat(w(a));break;case exports.UITypes.checkbox:o.push(_(a));break;case exports.UITypes.hidden:o.push(j(a));break;case exports.UITypes.input:o.push(k(a));break;case exports.UITypes.password:o.push(F(a));break;case exports.UITypes.radio:o.push(R(a));break;case exports.UITypes.select:o.push(P(a));break;case exports.UITypes.textArea:o.push(C(a))}return o}Object.defineProperty(exports,"Controller",{enumerable:!0,get:function(){return t.Controller}}),exports.FormContext=e=>{const{validationMode:r="onSubmit",revalidateMode:o="onChange",submitFocusError:i=!0}=e,p=t.useForm({mode:r,reValidateMode:o,submitFocusError:i}),u={};return u.onSubmit=p.handleSubmit((t,r)=>{e.onSubmit&&e.onSubmit(n(e.schema,t),r)}),e.noNativeValidate&&(u.noValidate=e.noNativeValidate),s.createElement(a.Provider,{value:Object.assign(Object.assign({},p),{schema:e.schema})},s.createElement("form",Object.assign({},u),e.children))},exports.concatFormPath=b,exports.useCheckbox=e=>_(O(e)),exports.useGenericInput=O,exports.useHidden=e=>j(O(e)),exports.useInput=e=>k(O(e)),exports.useObject=e=>M(i(),f(e.path),e.path,e.UISchema),exports.useObjectFromForm=e=>n(i().schema,e),exports.useObjectFromPath=f,exports.usePassword=e=>F(O(e)),exports.useRadio=e=>R(O(e)),exports.useSelect=e=>P(O(e)),exports.useTextArea=e=>C(O(e)); |
@@ -1,1 +0,1 @@ | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useContext as n}from"react";const i=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},a=(e,t)=>{const r=Object.keys(t).sort(),n={};for(const a of r){const r=i(a);if(!r)continue;let s=n,o=e;for(let e=0;e<r.length&&void 0!==o;e++)o.type&&"object"===o.type?o=o.properties:!o.type&&o[r[e]]&&"object"===o[r[e]].type&&(o=o[r[e]]),e===r.length-1&&(o[r[e]]&&"integer"===o[r[e]].type?s[r[e]]=parseInt(t[a]):o[r[e]]&&"number"===o[r[e]].type?s[r[e]]=parseFloat(t[a]):o[r[e]]&&"boolean"===o[r[e]].type?s[r[e]]="true"===t[a]:s[r[e]]=t[a]),void 0===s[r[e]]&&(s[r[e]]={}),s=s[r[e]]}return n},s=r(null);function o(){return n(s)}const u=r=>{const{validationMode:n="onSubmit",revalidateMode:i="onChange",submitFocusError:o=!0}=r,u=e({mode:n,reValidateMode:i,submitFocusError:o}),m={};return m.onSubmit=u.handleSubmit((e,t)=>{r.onSubmit&&r.onSubmit(a(r.schema,e),t)}),r.noNativeValidate&&(m.noValidate=r.noNativeValidate),t.createElement(s.Provider,{value:Object.assign(Object.assign({},u),{schema:r.schema})},t.createElement("form",Object.assign({},m),r.children))};var m,c,p;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(m||(m={})),function(e){e.default="default",e.radio="radio",e.select="select",e.input="input",e.hidden="hidden",e.password="password",e.textArea="textArea",e.checkbox="checkbox"}(c||(c={})),function(e){e.required="__form_error_required__",e.maxLength="__form_error_maxLenght__",e.minLength="__form_error_minLenght__",e.maxValue="__form_error_maxValue__",e.minValue="__form_error_minValue__",e.pattern="__form_error_pattern__",e.multipleOf="__form_error_multipleOf__",e.undefinedError="__form_error_undefinedError__"}(p||(p={}));const l=e=>{const t={};return e&&(t.required=p.required),t},d=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},g=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},h=e=>{const[t]=g(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},b=e=>{const[t]=g(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r},f=(e,t)=>{const r=h(e),n=b(e),i={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||p.multipleOf)}};return t&&(i.required=p.required),"integer"===e.type?i.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:p.pattern}:i.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:p.pattern},(r||0===r)&&(i.min={value:r,message:p.minValue}),(n||0===n)&&(i.max={value:n,message:p.maxValue}),i},x=(e,t)=>{const r={};return t&&(r.required=p.required),e.minLength&&(r.minLength={value:e.minLength,message:p.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:p.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:p.pattern}),r},y=(e,t)=>e+"/"+t,v=e=>{const t=i(e);let r=o().schema,n=!1,a="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}n=!!(r.required&&r.required.indexOf(t[e])>-1),a=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:n,objectName:a}},_=e=>a(o().schema,e),O=(e,t,r)=>{const{JSONSchema:n,isRequired:i,objectName:a}=t;let s,o,u;if("number"===n.type||"integer"===n.type){const e=g(n);u=e[0],s=h(n),o=b(n)}return{name:a,path:r,isRequired:i,formContext:e,type:m.generic,validator:{},getError:()=>((e,t,r,n,i,a,s,o)=>{if("array"===t.type){const e=n.getValues({nest:!0})[i];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:p.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:p.maxLength,expected:t.maxItems}}}if(!e)return;const u={message:p.undefinedError,expected:void 0};switch(e.message){case p.required:u.message=p.required,u.expected=r;break;case p.maxLength:u.message=p.maxLength,u.expected=t.maxLength;break;case p.minLength:u.message=p.minLength,u.expected=t.minLength;break;case p.maxValue:u.message=p.maxValue,u.expected=a;break;case p.minValue:u.message=p.minValue,u.expected=s;break;case p.multipleOf:u.message=p.multipleOf,u.expected=o;break;case p.pattern:u.message=p.pattern,u.expected=t.pattern}return u})(e.errors[r]?e.errors[r]:void 0,n,i,e,r,s,o,u),getObject:()=>n}},q=e=>{const t=o(),r=v(e);return O(t,r,e)},k=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),L=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("array"===t.type){if(t.items.enum)o=t.items.enum;else if("string"===t.items.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}t.uniqueItems&&(o=[...new Set(o)])}else"boolean"===t.type&&(s=l(e.isRequired),o=["true"]);return Object.assign(Object.assign({},e),{validator:s,type:m.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const n={key:""};return n.name="array"===t.type?`${e.path}[${r}]`:e.path,n.ref=e.formContext.register(s),n.type="checkbox",n.id=k(e.path,r,o),n.value=o[r],n},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=k(e.path,t,o),r},getItems:()=>o})},j=e=>L(q(e)),I=(e,t)=>e+"-"+t+"-input",R=(e,t)=>{const{register:r}=e.formContext,n=e.getObject();let i,a,s,o,u={};const c={key:""};if("string"===n.type)u=x(n,e.isRequired),c.pattern=n.pattern,c.minLength=n.minLength,c.maxLength=n.maxLength;else if("number"===n.type||"integer"===n.type){const t=g(n);s=t[0],o=t[1],i=h(n),a=b(n),u=f(n,e.isRequired),c.min=`${i}`,c.max=`${a}`,c.step="any"===s?"any":d(s,o||0)}return Object.assign(Object.assign({},e),{validator:u,type:m.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(c.name=e.path,c.ref=r(u),c.type=t,c.required=e.isRequired,c.id=I(e.path,t),c)})},S=()=>({}),V=e=>Object.assign(Object.assign({},R(e,"hidden")),{isRequired:!1,getLabelProps:S}),P=e=>V(q(e)),F=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return R(e,t)},w=e=>F(q(e)),M=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),E=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("string"===t.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{validator:s,type:m.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(s),r.type="radio",r.required=e.isRequired,r.id=M(e.path,t,o),r.value=o[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=M(e.path,t,o),r},getItems:()=>o})},N=e=>E(q(e)),C=e=>e+"-select",A=e=>{const t=e.getObject();let r,n,i,a,s={},o=[""];if("string"===t.type)o=o.concat(t.enum?t.enum:[]),s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{type:m.select,validator:s,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=C(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(s),t.required=e.isRequired,t.id=C(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,o),r.value=o[t],r},getItems:()=>o})},$=e=>A(q(e)),J=e=>R(e,"password"),T=e=>J(q(e)),U=e=>e+"-textarea-input",z=e=>{const t=e.getObject();let r={};const n={};return"string"===t.type?(r=x(t,e.isRequired),n.minLength=t.minLength,n.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=f(t,e.isRequired)),Object.assign(Object.assign({},e),{type:m.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=U(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=e.formContext.register(r),n.required=e.isRequired,n.id=U(e.path),n)})},B=e=>z(q(e));function D(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(A(e)):r.push(F(e));break;case"integer":case"number":r.push(F(e));break;case"array":case"boolean":r.push(L(e))}return r}function G(e,t,r,n){let i=[];const{JSONSchema:a}=t,s=O(e,t,r);if("object"===a.type){return Object.keys(a.properties).reduce(function(e,t,r,n){const i=t.required;return(a,s)=>{const o=!!i&&-1!==i.indexOf(s),u=r&&r.properties?r.properties[s]:void 0,m=y(e,s),c={JSONSchema:t.properties[s],isRequired:o,objectName:s},p=G(n,c,m,u);return a.concat(p)}}(r,a,n,e),[])}if(!n)return i.concat(D(s));switch(n.type){case c.default:i=i.concat(D(s));break;case c.checkbox:i.push(L(s));break;case c.hidden:i.push(V(s));break;case c.input:i.push(F(s));break;case c.password:i.push(J(s));break;case c.radio:i.push(E(s));break;case c.select:i.push(A(s));break;case c.textArea:i.push(z(s))}return i}const H=e=>G(o(),v(e.path),e.path,e.UISchema);export{p as ErrorTypes,u as FormContext,m as InputTypes,c as UITypes,y as concatFormPath,j as useCheckbox,q as useGenericInput,P as useHidden,w as useInput,H as useObject,_ as useObjectFromForm,v as useObjectFromPath,T as usePassword,N as useRadio,$ as useSelect,B as useTextArea}; | ||
import{useForm as e}from"react-hook-form";export{Controller}from"react-hook-form";import t,{createContext as r,useContext as n}from"react";const i=e=>{const t=e.split("/");return"#"===t[0]&&t.shift(),""===t[t.length-1]&&t.pop(),t},a=(e,t)=>{const r=Object.keys(t).sort(),n={};for(const a of r){const r=i(a);if(!r&&t[a])continue;let s=n,o=e;for(let e=0;e<r.length&&void 0!==o;e++){if(o.type&&"object"===o.type&&(o=o.properties,o=o[r[e]]),e===r.length-1){if(!o)break;"integer"===o.type?s[r[e]]=parseInt(t[a]):"number"===o.type?s[r[e]]=parseFloat(t[a]):"boolean"===o.type?s[r[e]]="true"===t[a]:s[r[e]]=t[a]}else void 0===s[r[e]]&&(s[r[e]]={});s=s[r[e]]}}return n},s=r(null);function o(){return n(s)}const u=r=>{const{validationMode:n="onSubmit",revalidateMode:i="onChange",submitFocusError:o=!0}=r,u=e({mode:n,reValidateMode:i,submitFocusError:o}),m={};return m.onSubmit=u.handleSubmit((e,t)=>{r.onSubmit&&r.onSubmit(a(r.schema,e),t)}),r.noNativeValidate&&(m.noValidate=r.noNativeValidate),t.createElement(s.Provider,{value:Object.assign(Object.assign({},u),{schema:r.schema})},t.createElement("form",Object.assign({},m),r.children))};var m,c,p;!function(e){e.generic="generic",e.radio="radio",e.select="select",e.input="input",e.textArea="textArea",e.checkbox="checkbox"}(m||(m={})),function(e){e.default="default",e.radio="radio",e.select="select",e.input="input",e.hidden="hidden",e.password="password",e.textArea="textArea",e.checkbox="checkbox"}(c||(c={})),function(e){e.required="__form_error_required__",e.maxLength="__form_error_maxLenght__",e.minLength="__form_error_minLenght__",e.maxValue="__form_error_maxValue__",e.minValue="__form_error_minValue__",e.pattern="__form_error_pattern__",e.multipleOf="__form_error_multipleOf__",e.undefinedError="__form_error_undefinedError__"}(p||(p={}));const l=e=>{const t={};return e&&(t.required=p.required),t},d=(e,t)=>{const r=Math.pow(10,t||0);return String(Math.round(e*r)/r)},g=e=>{const t=void 0!==e.multipleOf?"integer"===e.type?parseInt(e.multipleOf):parseFloat(e.multipleOf):"integer"===e.type?1:"any";let r=void 0;if(e.multipleOf){const t=e.multipleOf.toString().split(".")[1];r=t?t.length:0}return[t,r]},h=e=>{const[t]=g(e);let r=void 0!==e.exclusiveMinimum?e.exclusiveMinimum:void 0!==e.minimum?e.minimum:void 0;return void 0!==r&&void 0!==e.exclusiveMinimum&&(r+=t&&"any"!=t?t:1e-4),r},b=e=>{const[t]=g(e);let r=void 0!==e.exclusiveMaximum?parseFloat(e.exclusiveMaximum):void 0!==e.maximum?parseFloat(e.maximum):void 0;return void 0!==r&&void 0!==e.exclusiveMaximum&&(r-=t&&"any"!=t?t:1e-4),r},f=(e,t)=>{const r=h(e),n=b(e),i={validate:{multipleOf:t=>"integer"!==e.type||!t||e.multipleOf&&(parseInt(t)%parseInt(e.multipleOf)==0||p.multipleOf)}};return t&&(i.required=p.required),"integer"===e.type?i.pattern={value:/^([+-]?[1-9]\d*|0)$/,message:p.pattern}:i.pattern={value:/^([0-9]+([,.][0-9]+))?$/,message:p.pattern},(r||0===r)&&(i.min={value:r,message:p.minValue}),(n||0===n)&&(i.max={value:n,message:p.maxValue}),i},x=(e,t)=>{const r={};return t&&(r.required=p.required),e.minLength&&(r.minLength={value:e.minLength,message:p.minLength}),e.maxLength&&(r.maxLength={value:e.maxLength,message:p.maxLength}),e.pattern&&(r.pattern={value:new RegExp(e.pattern),message:p.pattern}),r},y=(e,t)=>e+"/"+t,v=e=>{const t=i(e);let r=o().schema,n=!1,a="";for(let e=0;e<t.length;e++){if(!r||!r.type||"object"!==r.type){r={};break}n=!!(r.required&&r.required.indexOf(t[e])>-1),a=t[e],r=r.properties[t[e]]}return{JSONSchema:r,isRequired:n,objectName:a}},_=e=>a(o().schema,e),O=(e,t,r)=>{const{JSONSchema:n,isRequired:i,objectName:a}=t;let s,o,u;if("number"===n.type||"integer"===n.type){const e=g(n);u=e[0],s=h(n),o=b(n)}return{name:a,path:r,isRequired:i,formContext:e,type:m.generic,validator:{},getError:()=>((e,t,r,n,i,a,s,o)=>{if("array"===t.type){const e=n.getValues({nest:!0})[i];if(e){const r=e.filter(e=>!1!==e).length||0;if(t.minItems&&r<t.minItems)return{message:p.minLength,expected:t.minItems};if(t.maxItems&&r>t.maxItems)return{message:p.maxLength,expected:t.maxItems}}}if(!e)return;const u={message:p.undefinedError,expected:void 0};switch(e.message){case p.required:u.message=p.required,u.expected=r;break;case p.maxLength:u.message=p.maxLength,u.expected=t.maxLength;break;case p.minLength:u.message=p.minLength,u.expected=t.minLength;break;case p.maxValue:u.message=p.maxValue,u.expected=a;break;case p.minValue:u.message=p.minValue,u.expected=s;break;case p.multipleOf:u.message=p.multipleOf,u.expected=o;break;case p.pattern:u.message=p.pattern,u.expected=t.pattern}return u})(e.errors[r]?e.errors[r]:void 0,n,i,e,r,s,o,u),getObject:()=>n}},q=e=>{const t=o(),r=v(e);return O(t,r,e)},k=(e,t,r)=>e+"-checkbox-input-"+(r[t]?r[t]:""),L=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("array"===t.type){if(t.items.enum)o=t.items.enum;else if("string"===t.items.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.items.type||"integer"===t.items.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}t.uniqueItems&&(o=[...new Set(o)])}else"boolean"===t.type&&(s=l(e.isRequired),o=["true"]);return Object.assign(Object.assign({},e),{validator:s,type:m.checkbox,isSingle:"boolean"===t.type,getItemInputProps:r=>{const n={key:""};return n.name="array"===t.type?`${e.path}[${r}]`:e.path,n.ref=e.formContext.register(s),n.type="checkbox",n.id=k(e.path,r,o),n.value=o[r],n},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-checkbox-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=k(e.path,t,o),r},getItems:()=>o})},j=e=>L(q(e)),I=(e,t)=>e+"-"+t+"-input",R=(e,t)=>{const{register:r}=e.formContext,n=e.getObject();let i,a,s,o,u={};const c={key:""};if("string"===n.type)u=x(n,e.isRequired),c.pattern=n.pattern,c.minLength=n.minLength,c.maxLength=n.maxLength;else if("number"===n.type||"integer"===n.type){const t=g(n);s=t[0],o=t[1],i=h(n),a=b(n),u=f(n,e.isRequired),c.min=`${i}`,c.max=`${a}`,c.step="any"===s?"any":d(s,o||0)}return Object.assign(Object.assign({},e),{validator:u,type:m.input,getLabelProps:()=>{const r={};return r.id=((e,t)=>e+"-"+t+"-label")(e.path,t),r.htmlFor=I(e.path,t),r},getInputProps:()=>(c.name=e.path,c.ref=r(u),c.type=t,c.required=e.isRequired,c.id=I(e.path,t),c)})},S=()=>({}),V=e=>Object.assign(Object.assign({},R(e,"hidden")),{isRequired:!1,getLabelProps:S}),P=e=>V(q(e)),F=e=>{let t;switch(e.getObject().format){case"date-time":t="datetime-local";break;case"email":t="email";break;case"hostname":case"uri":t="url";break;default:t="text"}return R(e,t)},w=e=>F(q(e)),M=(e,t,r)=>e+"-radio-input-"+(r[t]?r[t]:""),E=e=>{const t=e.getObject();let r,n,i,a,s={},o=[];if("string"===t.type)o=t.enum?t.enum:[],s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{validator:s,type:m.radio,getLabelProps:()=>{const r={};return r.id=e.path+"-label",r.htmlFor=void 0!==t.title?t.title:e.path,r},getItemInputProps:t=>{const r={key:""};return r.name=e.path,r.ref=e.formContext.register(s),r.type="radio",r.required=e.isRequired,r.id=M(e.path,t,o),r.value=o[t],r},getItemLabelProps:t=>{const r={};return r.id=((e,t,r)=>e+"-radio-label-"+(r[t]?r[t]:""))(e.path,t,o),r.htmlFor=M(e.path,t,o),r},getItems:()=>o})},N=e=>E(q(e)),C=e=>e+"-select",A=e=>{const t=e.getObject();let r,n,i,a,s={},o=[""];if("string"===t.type)o=o.concat(t.enum?t.enum:[]),s=x(t,e.isRequired);else if("number"===t.type||"integer"===t.type){const u=g(t);if(i=u[0],a=u[1],r=h(t),n=b(t),void 0!==r&&void 0!==n&&"any"!=i){s=f(t,e.isRequired);for(let e=r;e<=n;e+=i)o.push(d(e,a||0))}}else"boolean"===t.type&&(s=l(e.isRequired),o=["true","false"]);return Object.assign(Object.assign({},e),{type:m.select,validator:s,getLabelProps:()=>{const t={};return t.id=e.path+"-label",t.htmlFor=C(e.path),t},getSelectProps:()=>{const t={};return t.name=e.path,t.ref=e.formContext.register(s),t.required=e.isRequired,t.id=C(e.path),t},getItemOptionProps:t=>{const r={};return r.id=((e,t,r)=>e+"-select-option-"+(r[t]?r[t]:""))(e.path,t,o),r.value=o[t],r},getItems:()=>o})},$=e=>A(q(e)),J=e=>R(e,"password"),T=e=>J(q(e)),U=e=>e+"-textarea-input",z=e=>{const t=e.getObject();let r={};const n={};return"string"===t.type?(r=x(t,e.isRequired),n.minLength=t.minLength,n.maxLength=t.maxLength):"number"!==t.type&&"integer"!==t.type||(r=f(t,e.isRequired)),Object.assign(Object.assign({},e),{type:m.textArea,validator:r,getLabelProps:()=>{const t={};return t.id=e.path+"-textarea-label",t.htmlFor=U(e.path),t},getTextAreaProps:()=>(n.name=e.path,n.ref=e.formContext.register(r),n.required=e.isRequired,n.id=U(e.path),n)})},B=e=>z(q(e));function D(e){const t=e.getObject(),r=[];switch(t.type){case"string":t.enum?r.push(A(e)):r.push(F(e));break;case"integer":case"number":r.push(F(e));break;case"array":case"boolean":r.push(L(e))}return r}function G(e,t,r,n){let i=[];const{JSONSchema:a}=t,s=O(e,t,r);if("object"===a.type){return Object.keys(a.properties).reduce(function(e,t,r,n){const i=t.required;return(a,s)=>{const o=!!i&&-1!==i.indexOf(s),u=r&&r.properties?r.properties[s]:void 0,m=y(e,s),c={JSONSchema:t.properties[s],isRequired:o,objectName:s},p=G(n,c,m,u);return a.concat(p)}}(r,a,n,e),[])}if(!n)return i.concat(D(s));switch(n.type){case c.default:i=i.concat(D(s));break;case c.checkbox:i.push(L(s));break;case c.hidden:i.push(V(s));break;case c.input:i.push(F(s));break;case c.password:i.push(J(s));break;case c.radio:i.push(E(s));break;case c.select:i.push(A(s));break;case c.textArea:i.push(z(s))}return i}const H=e=>G(o(),v(e.path),e.path,e.UISchema);export{p as ErrorTypes,u as FormContext,m as InputTypes,c as UITypes,y as concatFormPath,j as useCheckbox,q as useGenericInput,P as useHidden,w as useInput,H as useObject,_ as useObjectFromForm,v as useObjectFromPath,T as usePassword,N as useRadio,$ as useSelect,B as useTextArea}; |
{ | ||
"name": "react-hook-form-jsonschema", | ||
"version": "0.2.0-beta.3", | ||
"version": "0.2.0-beta.4", | ||
"description": "Wrapper arround react-hook-form to create forms from a JSON schema.", | ||
@@ -5,0 +5,0 @@ "main": "output/index.cjs.js", |
@@ -77,3 +77,3 @@ # react-hook-form-jsonschema | ||
This is the API documentation, `react-hook-form-jsonschema` also re-exports all the [`react-hook-form`](https://github.com/react-hook-form/react-hook-form) functionalities, except for the Components and/or functions with the same name as in the `react-hook-form` API in this case they are just wrappers around the original library ones. | ||
This is the API documentation, `react-hook-form-jsonschema` also re-exports all the [`react-hook-form`](https://github.com/react-hook-form/react-hook-form) types and the `Controller` component. All of the other functionalities are abstracted by this library. | ||
@@ -80,0 +80,0 @@ ## Components API |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60044
437