Comparing version 0.1.2 to 0.1.3
@@ -462,5 +462,7 @@ 'use strict'; | ||
registeredKeys.next(keys); | ||
var manualError = new rxjs.Subject(); | ||
control$.next({ | ||
touched: false, | ||
validator: validator, | ||
manualError: manualError, | ||
error$: createError$({ | ||
@@ -470,2 +472,3 @@ key: key, | ||
value$: value$, | ||
manualError$: manualError.asObservable(), | ||
getControlValue$: function getControlValue$(key) { | ||
@@ -521,3 +524,3 @@ return navigateDeepSubject(key, values$); | ||
var dependency$ = new rxjs.Subject(); | ||
return rxjs.combineLatest([value$, dependency$.pipe(filterSeenValues(), operators.mergeMap(function (subject) { | ||
var validatorError$ = rxjs.combineLatest([value$, dependency$.pipe(filterSeenValues(), operators.mergeMap(function (subject) { | ||
return subject.pipe(skipSynchronous()); | ||
@@ -565,3 +568,13 @@ }), deferredStartWith(null))]).pipe(operators.map(function (_ref2) { | ||
} | ||
}), operators.shareReplay(1) // TODO doesnt work with shareLatest | ||
})); | ||
var manualError$ = params.manualError$.pipe(operators.switchMap(function (v) { | ||
return rxjs.concat(rxjs.of(v), value$.pipe(skipSynchronous(), operators.mapTo(false))); | ||
}), operators.startWith(false)); | ||
return rxjs.combineLatest([validatorError$, manualError$]).pipe(operators.map(function (_ref4) { | ||
var validatorResult = _ref4[0], | ||
manualResult = _ref4[1]; | ||
if (manualResult === false || manualResult === 'pending') return validatorResult; | ||
if (validatorResult === false || validatorResult === 'pending') return manualResult; | ||
return [].concat(manualResult, validatorResult); | ||
}), operators.shareReplay(1) // TODO doesnt work with shareLatest - It receives many unsubscriptions+resubscriptions | ||
); | ||
@@ -668,3 +681,15 @@ }; | ||
var setFieldValue = function setFieldValue(formRef, keySelector, value) { | ||
return navigateDeepSubject(keySelector, formRef.values$).next(value); | ||
var value$ = navigateDeepSubject(keySelector, formRef.values$); | ||
if (value$ === formRef.values$ && typeof value === 'object' && value !== null) { | ||
var keys = value$.getKeys(); | ||
var autofill = {}; | ||
keys.forEach(function (key) { | ||
if (value$.getChild(key).hasValue()) autofill[key] = value$.getChild(key).getValue(); | ||
}); | ||
value$.next(_extends({}, autofill, value)); | ||
return; | ||
} | ||
value$.next(value); | ||
}; | ||
@@ -676,3 +701,22 @@ | ||
var useControl = function useControl(formRef, options) { | ||
var touchFields = function touchFields(formRef, keysSelector, touch) { | ||
if (touch === void 0) { | ||
touch = true; | ||
} | ||
var keys = keysSelector ? getKeys(keysSelector) : formRef.registeredKeys.getValue(); | ||
keys.forEach(function (key) { | ||
return getControlState(formRef, key).getChild('touched').next(touch); | ||
}); | ||
}; | ||
var setFieldError = function setFieldError(formRef, keySelector, error) { | ||
try { | ||
getControlState(formRef, keySelector).getChild('manualError').getValue().next(error); | ||
} catch (ex) { | ||
console.warn("Can't set error: Field not registered", ex); | ||
} | ||
}; | ||
var useControlSubscription = function useControlSubscription(formRef, options) { | ||
var key = getKey(options.key); | ||
@@ -698,6 +742,6 @@ react.useEffect(function () { | ||
var useControlState = function useControlState(formRef, options) { | ||
var _useControl = useControl(formRef, options), | ||
subscribe = _useControl.subscribe, | ||
control = _objectWithoutPropertiesLoose(_useControl, ["subscribe"]); | ||
var useControl = function useControl(formRef, options) { | ||
var _useControlSubscripti = useControlSubscription(formRef, options), | ||
subscribe = _useControlSubscripti.subscribe, | ||
control = _objectWithoutPropertiesLoose(_useControlSubscripti, ["subscribe"]); | ||
@@ -973,9 +1017,2 @@ var _useState = react.useState(options.initialValue), | ||
var touchFields = function touchFields(formRef, keysSelector) { | ||
var keys = keysSelector ? getKeys(keysSelector) : formRef.registeredKeys.getValue(); | ||
keys.forEach(function (key) { | ||
return getControlState(formRef, key).getChild('touched').next(true); | ||
}); | ||
}; | ||
exports.isAtLeast = isAtLeast; | ||
@@ -993,2 +1030,3 @@ exports.isAtMost = isAtMost; | ||
exports.resetForm = resetForm; | ||
exports.setFieldError = setFieldError; | ||
exports.setFieldValue = setFieldValue; | ||
@@ -998,3 +1036,3 @@ exports.setInitialValue = setInitialValue; | ||
exports.useControl = useControl; | ||
exports.useControlState = useControlState; | ||
exports.useControlSubscription = useControlSubscription; | ||
exports.useErrors = useErrors; | ||
@@ -1001,0 +1039,0 @@ exports.useForm = useForm; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("rxjs"),t=require("rxjs-deep-subject"),r=require("rxjs/operators"),n=require("react");function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function o(e){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function i(e,t){return(i=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function a(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function c(e,t,r){return(c=a()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var u=new(Function.bind.apply(e,n));return r&&i(u,r.prototype),u}).apply(null,arguments)}function s(e){var t="function"==typeof Map?new Map:void 0;return(s=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return c(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),i(r,e)})(e)}function f(e,t){if(null==e)return{};var r,n,u={},o=Object.keys(e);for(n=0;n<o.length;n++)t.indexOf(r=o[n])>=0||(u[r]=e[r]);return u}function l(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:String(t)}var p=Symbol("path"),v=function(e){if("string"==typeof e)return e;var t=new Proxy({path:""},y(!1)),r=e(t);if(r!==t)throw new Error("You must return a value from the argument in the selector function");return r[p]},d=function(e,t){if("string"==typeof e){var r=e.split("."),n=t;return r.forEach((function(e){return n=n.getChild(e)})),n}var u=new Proxy({subject:t},{get:function(e,t,r){return t===p?e.subject:(e.subject=e.subject.getChild(t),r)}}),o=e(u);if(o!==u)throw new Error("You must return a value from the argument in the selector function");return o[p]},h=function(e){return"object"==typeof e?e:e(new Proxy({path:""},y(!0))).map((function(e){return e[p]}))},y=function(e){var t={get:function(r,n,u){if(n===p)return r.path;if("symbol"==typeof n)throw new Error("Can't serialize symbols to keys");var o="number"!=typeof n&&isNaN(n)?r.path.length?r.path+"."+n:n:r.path+"["+n+"]";return e&&0===r.path.length?new Proxy({path:o},t):(r.path=o,u)}};return t},b=function(e,t){return"function"==typeof e?e(t):"string"==typeof e?Number(t(e)):e},m=function e(t,r){return function(){for(var n=arguments.length,u=new Array(n),o=0;o<n;o++)u[o]=arguments[o];if(r>=t.length)return!0;var i=t[r].apply(t,u),a=function(n){return!0!==n?n:e(t,r+1).apply(void 0,u)};return x(i)?i.then(a):a(i)}},g=function(){return!0},x=function(e){return"object"==typeof e&&!Array.isArray(e)},w=Symbol("empty"),V=function(e,t){return d(t,e.controlStates$)},E=function(t){var n=t.key,u=t.validator$,o=t.value$,i=t.getControlValue$,a=new e.Subject;return e.combineLatest([o,a.pipe((function(e){return e.pipe(r.scan((function(e,t){return e.seen.has(t)?{seen:e.seen,lastValue:w}:(e.seen.add(t),{seen:e.seen,lastValue:t})}),{seen:new Set,lastValue:w}),r.filter((function(e){return e.lastValue!==w})),r.map((function(e){return e.lastValue})))}),r.mergeMap((function(e){return e.pipe($())})),S(null))]).pipe(r.map((function(e){return e[0]})),r.withLatestFrom(u),r.switchMap((function(t){var r=t[0],u=t[1];try{var o=u(r,(function(e){var t=v(e),r=i(t);if(a.next(r),!r.hasValue())throw new j(t);return r.getValue()}));return"boolean"==typeof o?e.of(!0!==o&&[]):Array.isArray(o)?e.of(o):e.concat(e.of("pending"),o.then((function(e){return!0!==e&&(!1===e?[]:e)})))}catch(t){return t instanceof j?console.warn("Setting control "+n+" error to pending, as the validation depends on a field that hasn't been registered yet",t):console.error(t),e.of("pending")}})),r.shareReplay(1))},j=function(e){var t,r;function n(t){var r;return(r=e.call(this,"Control "+t+" doesn't have any value yet")||this).name="ValueNotThereYetError",Object.setPrototypeOf(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(r),n.prototype),r}return r=e,(t=n).prototype=Object.create(r.prototype),t.prototype.constructor=t,t.__proto__=r,n}(s(Error)),S=function(t){return function(r){return new e.Observable((function(e){var n={value:!1},u=[],o=r.subscribe({next:function(t){n.value?e.next(t):u.push(t)},error:function(t){return e.error(t)},complete:function(){return e.complete()}});if(n.value=!0,!e.closed){e.next(t);for(var i=0;i<u.length&&!e.closed;i++)e.next(u[i])}return o}))}},$=function(){return function(t){return new e.Observable((function(e){var r={skip:!0},n=t.subscribe({next:function(t){return r.skip?void 0:e.next(t)},error:function(t){return e.error(t)},complete:function(){return e.complete()}});return r.skip=!1,n}))}},O=function(e,t){var r=v(t.key);return n.useEffect((function(){e.registerControl(t)}),[e,t]),{setValue:function(t){return d(r,e.values$).next(t)},subscribe:function(t){var n=d(r,e.values$).subscribe(t);return function(){n.unsubscribe()}},touch:function(){return V(e,r).getChild("touched").next(!0)}}},C={},k={};exports.isAtLeast=function(e){return function(t,r){var n=b(e,r);return!(Number(t)<n)||["Expected a value of at least "+n]}},exports.isAtMost=function(e){return function(t,r){var n=b(e,r);return!(Number(t)>n)||["Expected a value of at most "+n]}},exports.isGreaterThan=function(e){return function(t,r){var n=b(e,r);return!(Number(t)<=n)||["Expected a value greater than "+n]}},exports.isInteger=function(e){return parseInt(e)===parseFloat(e)||["Expected an integer"]},exports.isLessThan=function(e){return function(t,r){var n=b(e,r);return!(Number(t)>=n)||["Expected a value less than "+n]}},exports.isNumber=function(e){return!isNaN(e)||["Expected a number"]},exports.isRequired=function(e){return null!=e&&""!==e||["required"]},exports.matches=function(e,t){return function(r){return!!e.test(r)||[null!=t?t:"Invalid value"]}},exports.mergeValidators=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var u=t.map((function(e){return e.apply(void 0,r)})),o=function(e){var t=e.flatMap((function(e){return"boolean"==typeof e?[]:e}));return 0===t.length?!e.some((function(e){return!1===e})):t};return u.some(x)?Promise.all(u).then(o):o(u)}},exports.pipeValidators=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return m(t,0)},exports.readForm=function(e){try{return e.values$.getValue()}catch(e){return{}}},exports.resetForm=function(e,t){var r=e.values$,n=e.initialValues$,u=e.registeredKeys;if(!t)return r.next(n.getValue()),void u.getValue().forEach((function(t){V(e,t).getChild("touched").next(!1)}));h(t).forEach((function(t){var u=d(t,r),o=d(t,n);u.next(o.getValue()),V(e,t).getChild("touched").next(!1)}))},exports.setFieldValue=function(e,t,r){return d(t,e.values$).next(r)},exports.setInitialValue=function(e,t,r){return d(t,e.initialValues$).next(r)},exports.touchFields=function(e,t){(t?h(t):e.registeredKeys.getValue()).forEach((function(t){return V(e,t).getChild("touched").next(!0)}))},exports.useControl=O,exports.useControlState=function(e,t){var r=O(e,t),o=r.subscribe,i=f(r,["subscribe"]),a=n.useState(t.initialValue),c=a[0],s=a[1];return n.useEffect((function(){return o(s)}),[]),u({},i,{value:c})},exports.useErrors=function(t,o){var i=o?h(o):[C],a=n.useState({}),c=a[0],s=a[1],p=n.useMemo((function(){return(i[0]===C?t.registeredKeys.pipe(r.map((function(e){return Array.from(e)}))):e.of(i)).pipe(r.switchMap((function(n){return e.merge.apply(void 0,n.map((function(n){return V(t,n).pipe(r.switchMap((function(t){return t.touched?t.error$.pipe(r.map((function(e){return{key:n,payload:e}}))):e.of({key:n,payload:!1})})))}))).pipe(r.scan((function(e,t){var r,n=t.key,o=t.payload;return!1===o?n in e?f(e,[n].map(l)):e:e[n]===o?e:u({},e,((r={})[n]=o,r))}),{}),r.distinctUntilChanged())})))}),[t].concat(i));return n.useEffect((function(){var e=p.subscribe(s);return function(){return e.unsubscribe()}}),[p]),c},exports.useForm=function(r){var u=n.useRef(null);return u.current||(u.current=function(r){void 0===r&&(r={});var n=new e.BehaviorSubject(new Set),u=t.deepSubject(r.initialValue),o=t.deepSubject(r.initialValue),i=t.deepSubject();return{registeredKeys:n,registerControl:function(e){var t=e.initialValue,r=e.validator,a=void 0===r?g:r,c=v(e.key),s=d(c,i),f=d(c,o);if(!s.hasValue()){var l=n.getValue();l.add(c),n.next(l),s.next({touched:!1,validator:a,error$:E({key:c,validator$:s.getChild("validator"),value$:f,getControlValue$:function(e){return d(e,o)}})})}var p=s.getChild("validator");a!==p.getValue()&&p.next(a);var h=d(c,u);h.hasValue()&&h.getValue()===t||h.next(t),f.hasValue()||f.next(t)},initialValues$:u,controlStates$:i,values$:o,dispose:function(){u.complete(),o.complete(),i.complete()}}}(r)),n.useEffect((function(){return function(){return u.current.dispose()}}),[]),u.current},exports.useInput=function(e,t){void 0===t&&(t={});var r=t.eventType,u=void 0===r?"input":r,o=t.elementProp,i=void 0===o?"value":o,a=n.useRef(null);return n.useEffect((function(){var r=a.current;if(r){var n=t.initialValue,o=void 0===n?"":n,c=t.validator,s=t.key?v(t.key):r.name;if(s){e.registerControl({initialValue:o,key:s,validator:c});var f=d(s,e.values$),l=f.subscribe((function(e){r[i]!==e&&(r[i]=e)})),p=function(){return V(e,s).getChild("touched").next(!0)};r.addEventListener("blur",p);var h=function(e){return f.next(e.target[i])};return r.addEventListener(u,h),function(){l.unsubscribe(),r.removeEventListener("blur",p),r.removeEventListener(u,h)}}console.error("An input is missing its key. Either supply it through useInput `key` option or through the input's name",r,{options:t})}})),a},exports.useIsPristine=function(t){var u=n.useState(!0),o=u[0],i=u[1],a=n.useMemo((function(){return t.registeredKeys.pipe(r.switchMap((function(n){return e.combineLatest(Array.from(n).map((function(n){var u=d(n,t.initialValues$),o=d(n,t.values$);return e.combineLatest([u,o]).pipe(r.map((function(e){return e[0]===e[1]})))}))).pipe(r.map((function(e){return e.every((function(e){return e}))})))})),r.distinctUntilChanged())}),[t]);return n.useEffect((function(){var e=a.subscribe(i);return function(){return e.unsubscribe()}}),[a]),o},exports.useIsValid=function(t,o,i){void 0===o&&(o=!1);var a=i?h(i):[k],c=n.useMemo((function(){return(a[0]===k?t.registeredKeys.pipe(r.map((function(e){return Array.from(e)}))):e.of(a)).pipe(r.switchMap((function(n){return e.merge.apply(void 0,n.map((function(e){return V(t,e).pipe(r.switchMap((function(t){return t.error$.pipe(r.map((function(t){return{key:e,payload:t}})))})))}))).pipe(r.scan((function(e,t){var r,n=t.key,o=t.payload;return!1===o?n in e?f(e,[n].map(l)):e:e[n]===o?e:u({},e,((r={})[n]=o,r))}),{}),r.distinctUntilChanged())})))}),[t].concat(a)),s=n.useMemo((function(){return c.pipe(r.map((function(e){var t=Object.values(e),r=!1;return!t.some((function(e){return"pending"!==e||(r=!0,!1)}))&&(!r||"pending")})))}),[c]),p=n.useState((function(){var e=o;return s.subscribe((function(t){return e=t})).unsubscribe(),e})),v=p[0],d=p[1];return n.useEffect((function(){var e=s.subscribe(d);return function(){return e.unsubscribe()}}),[s]),v},exports.useWatch=function(e,t){var r=n.useState(void 0),u=r[0],o=r[1],i=d(t,e.values$);return n.useEffect((function(){var e=i.subscribe(o);return function(){return e.unsubscribe()}}),[i]),u}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("rxjs"),t=require("rxjs-deep-subject"),r=require("rxjs/operators"),n=require("react");function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function o(e){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function i(e,t){return(i=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function a(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function c(e,t,r){return(c=a()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var u=new(Function.bind.apply(e,n));return r&&i(u,r.prototype),u}).apply(null,arguments)}function s(e){var t="function"==typeof Map?new Map:void 0;return(s=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return c(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),i(r,e)})(e)}function f(e,t){if(null==e)return{};var r,n,u={},o=Object.keys(e);for(n=0;n<o.length;n++)t.indexOf(r=o[n])>=0||(u[r]=e[r]);return u}function l(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:String(t)}var p=Symbol("path"),v=function(e){if("string"==typeof e)return e;var t=new Proxy({path:""},y(!1)),r=e(t);if(r!==t)throw new Error("You must return a value from the argument in the selector function");return r[p]},d=function(e,t){if("string"==typeof e){var r=e.split("."),n=t;return r.forEach((function(e){return n=n.getChild(e)})),n}var u=new Proxy({subject:t},{get:function(e,t,r){return t===p?e.subject:(e.subject=e.subject.getChild(t),r)}}),o=e(u);if(o!==u)throw new Error("You must return a value from the argument in the selector function");return o[p]},h=function(e){return"object"==typeof e?e:e(new Proxy({path:""},y(!0))).map((function(e){return e[p]}))},y=function(e){var t={get:function(r,n,u){if(n===p)return r.path;if("symbol"==typeof n)throw new Error("Can't serialize symbols to keys");var o="number"!=typeof n&&isNaN(n)?r.path.length?r.path+"."+n:n:r.path+"["+n+"]";return e&&0===r.path.length?new Proxy({path:o},t):(r.path=o,u)}};return t},b=function(e,t){return"function"==typeof e?e(t):"string"==typeof e?Number(t(e)):e},m=function e(t,r){return function(){for(var n=arguments.length,u=new Array(n),o=0;o<n;o++)u[o]=arguments[o];if(r>=t.length)return!0;var i=t[r].apply(t,u),a=function(n){return!0!==n?n:e(t,r+1).apply(void 0,u)};return x(i)?i.then(a):a(i)}},g=function(){return!0},x=function(e){return"object"==typeof e&&!Array.isArray(e)},w=Symbol("empty"),V=function(e,t){return d(t,e.controlStates$)},E=function(t){var n=t.key,u=t.validator$,o=t.value$,i=t.getControlValue$,a=new e.Subject,c=e.combineLatest([o,a.pipe((function(e){return e.pipe(r.scan((function(e,t){return e.seen.has(t)?{seen:e.seen,lastValue:w}:(e.seen.add(t),{seen:e.seen,lastValue:t})}),{seen:new Set,lastValue:w}),r.filter((function(e){return e.lastValue!==w})),r.map((function(e){return e.lastValue})))}),r.mergeMap((function(e){return e.pipe(C())})),$(null))]).pipe(r.map((function(e){return e[0]})),r.withLatestFrom(u),r.switchMap((function(t){var r=t[0],u=t[1];try{var o=u(r,(function(e){var t=v(e),r=i(t);if(a.next(r),!r.hasValue())throw new j(t);return r.getValue()}));return"boolean"==typeof o?e.of(!0!==o&&[]):Array.isArray(o)?e.of(o):e.concat(e.of("pending"),o.then((function(e){return!0!==e&&(!1===e?[]:e)})))}catch(t){return t instanceof j?console.warn("Setting control "+n+" error to pending, as the validation depends on a field that hasn't been registered yet",t):console.error(t),e.of("pending")}}))),s=t.manualError$.pipe(r.switchMap((function(t){return e.concat(e.of(t),o.pipe(C(),r.mapTo(!1)))})),r.startWith(!1));return e.combineLatest([c,s]).pipe(r.map((function(e){var t=e[0],r=e[1];return!1===r||"pending"===r?t:!1===t||"pending"===t?r:[].concat(r,t)})),r.shareReplay(1))},j=function(e){var t,r;function n(t){var r;return(r=e.call(this,"Control "+t+" doesn't have any value yet")||this).name="ValueNotThereYetError",Object.setPrototypeOf(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(r),n.prototype),r}return r=e,(t=n).prototype=Object.create(r.prototype),t.prototype.constructor=t,t.__proto__=r,n}(s(Error)),$=function(t){return function(r){return new e.Observable((function(e){var n={value:!1},u=[],o=r.subscribe({next:function(t){n.value?e.next(t):u.push(t)},error:function(t){return e.error(t)},complete:function(){return e.complete()}});if(n.value=!0,!e.closed){e.next(t);for(var i=0;i<u.length&&!e.closed;i++)e.next(u[i])}return o}))}},C=function(){return function(t){return new e.Observable((function(e){var r={skip:!0},n=t.subscribe({next:function(t){return r.skip?void 0:e.next(t)},error:function(t){return e.error(t)},complete:function(){return e.complete()}});return r.skip=!1,n}))}},S=function(e,t){var r=v(t.key);return n.useEffect((function(){e.registerControl(t)}),[e,t]),{setValue:function(t){return d(r,e.values$).next(t)},subscribe:function(t){var n=d(r,e.values$).subscribe(t);return function(){n.unsubscribe()}},touch:function(){return V(e,r).getChild("touched").next(!0)}}},O={},k={};exports.isAtLeast=function(e){return function(t,r){var n=b(e,r);return!(Number(t)<n)||["Expected a value of at least "+n]}},exports.isAtMost=function(e){return function(t,r){var n=b(e,r);return!(Number(t)>n)||["Expected a value of at most "+n]}},exports.isGreaterThan=function(e){return function(t,r){var n=b(e,r);return!(Number(t)<=n)||["Expected a value greater than "+n]}},exports.isInteger=function(e){return parseInt(e)===parseFloat(e)||["Expected an integer"]},exports.isLessThan=function(e){return function(t,r){var n=b(e,r);return!(Number(t)>=n)||["Expected a value less than "+n]}},exports.isNumber=function(e){return!isNaN(e)||["Expected a number"]},exports.isRequired=function(e){return null!=e&&""!==e||["required"]},exports.matches=function(e,t){return function(r){return!!e.test(r)||[null!=t?t:"Invalid value"]}},exports.mergeValidators=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var u=t.map((function(e){return e.apply(void 0,r)})),o=function(e){var t=e.flatMap((function(e){return"boolean"==typeof e?[]:e}));return 0===t.length?!e.some((function(e){return!1===e})):t};return u.some(x)?Promise.all(u).then(o):o(u)}},exports.pipeValidators=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return m(t,0)},exports.readForm=function(e){try{return e.values$.getValue()}catch(e){return{}}},exports.resetForm=function(e,t){var r=e.values$,n=e.initialValues$,u=e.registeredKeys;if(!t)return r.next(n.getValue()),void u.getValue().forEach((function(t){V(e,t).getChild("touched").next(!1)}));h(t).forEach((function(t){var u=d(t,r),o=d(t,n);u.next(o.getValue()),V(e,t).getChild("touched").next(!1)}))},exports.setFieldError=function(e,t,r){try{V(e,t).getChild("manualError").getValue().next(r)}catch(e){console.warn("Can't set error: Field not registered",e)}},exports.setFieldValue=function(e,t,r){var n=d(t,e.values$);if(n===e.values$&&"object"==typeof r&&null!==r){var o=n.getKeys(),i={};return o.forEach((function(e){n.getChild(e).hasValue()&&(i[e]=n.getChild(e).getValue())})),void n.next(u({},i,r))}n.next(r)},exports.setInitialValue=function(e,t,r){return d(t,e.initialValues$).next(r)},exports.touchFields=function(e,t,r){void 0===r&&(r=!0),(t?h(t):e.registeredKeys.getValue()).forEach((function(t){return V(e,t).getChild("touched").next(r)}))},exports.useControl=function(e,t){var r=S(e,t),o=r.subscribe,i=f(r,["subscribe"]),a=n.useState(t.initialValue),c=a[0],s=a[1];return n.useEffect((function(){return o(s)}),[]),u({},i,{value:c})},exports.useControlSubscription=S,exports.useErrors=function(t,o){var i=o?h(o):[O],a=n.useState({}),c=a[0],s=a[1],p=n.useMemo((function(){return(i[0]===O?t.registeredKeys.pipe(r.map((function(e){return Array.from(e)}))):e.of(i)).pipe(r.switchMap((function(n){return e.merge.apply(void 0,n.map((function(n){return V(t,n).pipe(r.switchMap((function(t){return t.touched?t.error$.pipe(r.map((function(e){return{key:n,payload:e}}))):e.of({key:n,payload:!1})})))}))).pipe(r.scan((function(e,t){var r,n=t.key,o=t.payload;return!1===o?n in e?f(e,[n].map(l)):e:e[n]===o?e:u({},e,((r={})[n]=o,r))}),{}),r.distinctUntilChanged())})))}),[t].concat(i));return n.useEffect((function(){var e=p.subscribe(s);return function(){return e.unsubscribe()}}),[p]),c},exports.useForm=function(r){var u=n.useRef(null);return u.current||(u.current=function(r){void 0===r&&(r={});var n=new e.BehaviorSubject(new Set),u=t.deepSubject(r.initialValue),o=t.deepSubject(r.initialValue),i=t.deepSubject();return{registeredKeys:n,registerControl:function(t){var r=t.initialValue,a=t.validator,c=void 0===a?g:a,s=v(t.key),f=d(s,i),l=d(s,o);if(!f.hasValue()){var p=n.getValue();p.add(s),n.next(p);var h=new e.Subject;f.next({touched:!1,validator:c,manualError:h,error$:E({key:s,validator$:f.getChild("validator"),value$:l,manualError$:h.asObservable(),getControlValue$:function(e){return d(e,o)}})})}var y=f.getChild("validator");c!==y.getValue()&&y.next(c);var b=d(s,u);b.hasValue()&&b.getValue()===r||b.next(r),l.hasValue()||l.next(r)},initialValues$:u,controlStates$:i,values$:o,dispose:function(){u.complete(),o.complete(),i.complete()}}}(r)),n.useEffect((function(){return function(){return u.current.dispose()}}),[]),u.current},exports.useInput=function(e,t){void 0===t&&(t={});var r=t.eventType,u=void 0===r?"input":r,o=t.elementProp,i=void 0===o?"value":o,a=n.useRef(null);return n.useEffect((function(){var r=a.current;if(r){var n=t.initialValue,o=void 0===n?"":n,c=t.validator,s=t.key?v(t.key):r.name;if(s){e.registerControl({initialValue:o,key:s,validator:c});var f=d(s,e.values$),l=f.subscribe((function(e){r[i]!==e&&(r[i]=e)})),p=function(){return V(e,s).getChild("touched").next(!0)};r.addEventListener("blur",p);var h=function(e){return f.next(e.target[i])};return r.addEventListener(u,h),function(){l.unsubscribe(),r.removeEventListener("blur",p),r.removeEventListener(u,h)}}console.error("An input is missing its key. Either supply it through useInput `key` option or through the input's name",r,{options:t})}})),a},exports.useIsPristine=function(t){var u=n.useState(!0),o=u[0],i=u[1],a=n.useMemo((function(){return t.registeredKeys.pipe(r.switchMap((function(n){return e.combineLatest(Array.from(n).map((function(n){var u=d(n,t.initialValues$),o=d(n,t.values$);return e.combineLatest([u,o]).pipe(r.map((function(e){return e[0]===e[1]})))}))).pipe(r.map((function(e){return e.every((function(e){return e}))})))})),r.distinctUntilChanged())}),[t]);return n.useEffect((function(){var e=a.subscribe(i);return function(){return e.unsubscribe()}}),[a]),o},exports.useIsValid=function(t,o,i){void 0===o&&(o=!1);var a=i?h(i):[k],c=n.useMemo((function(){return(a[0]===k?t.registeredKeys.pipe(r.map((function(e){return Array.from(e)}))):e.of(a)).pipe(r.switchMap((function(n){return e.merge.apply(void 0,n.map((function(e){return V(t,e).pipe(r.switchMap((function(t){return t.error$.pipe(r.map((function(t){return{key:e,payload:t}})))})))}))).pipe(r.scan((function(e,t){var r,n=t.key,o=t.payload;return!1===o?n in e?f(e,[n].map(l)):e:e[n]===o?e:u({},e,((r={})[n]=o,r))}),{}),r.distinctUntilChanged())})))}),[t].concat(a)),s=n.useMemo((function(){return c.pipe(r.map((function(e){var t=Object.values(e),r=!1;return!t.some((function(e){return"pending"!==e||(r=!0,!1)}))&&(!r||"pending")})))}),[c]),p=n.useState((function(){var e=o;return s.subscribe((function(t){return e=t})).unsubscribe(),e})),v=p[0],d=p[1];return n.useEffect((function(){var e=s.subscribe(d);return function(){return e.unsubscribe()}}),[s]),v},exports.useWatch=function(e,t){var r=n.useState(void 0),u=r[0],o=r[1],i=d(t,e.values$);return n.useEffect((function(){var e=i.subscribe(o);return function(){return e.unsubscribe()}}),[i]),u}; | ||
//# sourceMappingURL=formicary.cjs.production.min.js.map |
@@ -1,4 +0,4 @@ | ||
import { BehaviorSubject, combineLatest, Observable, of, concat, Subject, merge } from 'rxjs'; | ||
import { BehaviorSubject, Subject, combineLatest, Observable, of, concat, merge } from 'rxjs'; | ||
import { deepSubject } from 'rxjs-deep-subject'; | ||
import { scan, filter, map, mergeMap, withLatestFrom, switchMap, shareReplay, distinctUntilChanged } from 'rxjs/operators'; | ||
import { scan, filter, map, shareReplay, mergeMap, withLatestFrom, switchMap, mapTo, startWith, distinctUntilChanged } from 'rxjs/operators'; | ||
import { useEffect, useState, useMemo, useRef } from 'react'; | ||
@@ -458,5 +458,7 @@ | ||
registeredKeys.next(keys); | ||
var manualError = new Subject(); | ||
control$.next({ | ||
touched: false, | ||
validator: validator, | ||
manualError: manualError, | ||
error$: createError$({ | ||
@@ -466,2 +468,3 @@ key: key, | ||
value$: value$, | ||
manualError$: manualError.asObservable(), | ||
getControlValue$: function getControlValue$(key) { | ||
@@ -517,3 +520,3 @@ return navigateDeepSubject(key, values$); | ||
var dependency$ = new Subject(); | ||
return combineLatest([value$, dependency$.pipe(filterSeenValues(), mergeMap(function (subject) { | ||
var validatorError$ = combineLatest([value$, dependency$.pipe(filterSeenValues(), mergeMap(function (subject) { | ||
return subject.pipe(skipSynchronous()); | ||
@@ -561,3 +564,13 @@ }), deferredStartWith(null))]).pipe(map(function (_ref2) { | ||
} | ||
}), shareReplay(1) // TODO doesnt work with shareLatest | ||
})); | ||
var manualError$ = params.manualError$.pipe(switchMap(function (v) { | ||
return concat(of(v), value$.pipe(skipSynchronous(), mapTo(false))); | ||
}), startWith(false)); | ||
return combineLatest([validatorError$, manualError$]).pipe(map(function (_ref4) { | ||
var validatorResult = _ref4[0], | ||
manualResult = _ref4[1]; | ||
if (manualResult === false || manualResult === 'pending') return validatorResult; | ||
if (validatorResult === false || validatorResult === 'pending') return manualResult; | ||
return [].concat(manualResult, validatorResult); | ||
}), shareReplay(1) // TODO doesnt work with shareLatest - It receives many unsubscriptions+resubscriptions | ||
); | ||
@@ -664,3 +677,15 @@ }; | ||
var setFieldValue = function setFieldValue(formRef, keySelector, value) { | ||
return navigateDeepSubject(keySelector, formRef.values$).next(value); | ||
var value$ = navigateDeepSubject(keySelector, formRef.values$); | ||
if (value$ === formRef.values$ && typeof value === 'object' && value !== null) { | ||
var keys = value$.getKeys(); | ||
var autofill = {}; | ||
keys.forEach(function (key) { | ||
if (value$.getChild(key).hasValue()) autofill[key] = value$.getChild(key).getValue(); | ||
}); | ||
value$.next(_extends({}, autofill, value)); | ||
return; | ||
} | ||
value$.next(value); | ||
}; | ||
@@ -672,3 +697,22 @@ | ||
var useControl = function useControl(formRef, options) { | ||
var touchFields = function touchFields(formRef, keysSelector, touch) { | ||
if (touch === void 0) { | ||
touch = true; | ||
} | ||
var keys = keysSelector ? getKeys(keysSelector) : formRef.registeredKeys.getValue(); | ||
keys.forEach(function (key) { | ||
return getControlState(formRef, key).getChild('touched').next(touch); | ||
}); | ||
}; | ||
var setFieldError = function setFieldError(formRef, keySelector, error) { | ||
try { | ||
getControlState(formRef, keySelector).getChild('manualError').getValue().next(error); | ||
} catch (ex) { | ||
console.warn("Can't set error: Field not registered", ex); | ||
} | ||
}; | ||
var useControlSubscription = function useControlSubscription(formRef, options) { | ||
var key = getKey(options.key); | ||
@@ -694,6 +738,6 @@ useEffect(function () { | ||
var useControlState = function useControlState(formRef, options) { | ||
var _useControl = useControl(formRef, options), | ||
subscribe = _useControl.subscribe, | ||
control = _objectWithoutPropertiesLoose(_useControl, ["subscribe"]); | ||
var useControl = function useControl(formRef, options) { | ||
var _useControlSubscripti = useControlSubscription(formRef, options), | ||
subscribe = _useControlSubscripti.subscribe, | ||
control = _objectWithoutPropertiesLoose(_useControlSubscripti, ["subscribe"]); | ||
@@ -969,10 +1013,3 @@ var _useState = useState(options.initialValue), | ||
var touchFields = function touchFields(formRef, keysSelector) { | ||
var keys = keysSelector ? getKeys(keysSelector) : formRef.registeredKeys.getValue(); | ||
keys.forEach(function (key) { | ||
return getControlState(formRef, key).getChild('touched').next(true); | ||
}); | ||
}; | ||
export { isAtLeast, isAtMost, isGreaterThan, isInteger, isLessThan, isNumber, isRequired, matches, mergeValidators, pipeValidators, readForm, resetForm, setFieldValue, setInitialValue, touchFields, useControl, useControlState, useErrors, useForm, useInput, useIsPristine, useIsValid, useWatch }; | ||
export { isAtLeast, isAtMost, isGreaterThan, isInteger, isLessThan, isNumber, isRequired, matches, mergeValidators, pipeValidators, readForm, resetForm, setFieldError, setFieldValue, setInitialValue, touchFields, useControl, useControlSubscription, useErrors, useForm, useInput, useIsPristine, useIsValid, useWatch }; | ||
//# sourceMappingURL=formicary.esm.js.map |
@@ -1,15 +0,16 @@ | ||
export { readForm } from './readForm'; | ||
export { resetForm } from './resetForm'; | ||
export { setFieldValue } from './setFieldValue'; | ||
export { setInitialValue } from './setInitialValue'; | ||
export { useControl } from './useControl'; | ||
export { useControlState } from './useControlState'; | ||
export { useErrors } from './useErrors'; | ||
export { useForm } from './useForm'; | ||
export { useInput } from './useInput'; | ||
export { useIsPristine } from './useIsPristine'; | ||
export { useIsValid } from './useIsValid'; | ||
export { useWatch } from './useWatch'; | ||
export { touchFields } from './touchFields'; | ||
export { readForm } from './commands/readForm'; | ||
export { resetForm } from './commands/resetForm'; | ||
export { setFieldValue } from './commands/setFieldValue'; | ||
export { setInitialValue } from './commands/setInitialValue'; | ||
export { touchFields } from './commands/touchFields'; | ||
export { setFieldError } from './commands/setFieldError'; | ||
export { useControlSubscription } from './hooks/useControlSubscription'; | ||
export { useControl } from './hooks/useControl'; | ||
export { useErrors } from './hooks/useErrors'; | ||
export { useForm } from './hooks/useForm'; | ||
export { useInput } from './hooks/useInput'; | ||
export { useIsPristine } from './hooks/useIsPristine'; | ||
export { useIsValid } from './hooks/useIsValid'; | ||
export { useWatch } from './hooks/useWatch'; | ||
export { FieldValidator as Validator, isAtLeast, isGreaterThan, isInteger, isNumber, isAtMost, isLessThan, isRequired, mergeValidators, pipeValidators, matches, } from './validators'; | ||
export { FormRef } from './internal/formRef'; |
@@ -1,4 +0,4 @@ | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
import { DeepSubject } from 'rxjs-deep-subject'; | ||
import { KeySelector } from '../path'; | ||
import { KeySelector } from './path'; | ||
import { FieldValidator } from '../validators'; | ||
@@ -32,2 +32,3 @@ export interface ControlOptions<TValues, T> { | ||
validator: FieldValidator<T>; | ||
manualError: Subject<ErrorResult>; | ||
error$: Observable<ErrorResult>; | ||
@@ -34,0 +35,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { KeySelector } from './path'; | ||
import { KeySelector } from './internal/path'; | ||
export declare type PureValidator<T> = (value: T) => boolean | string[] | Promise<boolean | string[]>; | ||
@@ -3,0 +3,0 @@ export declare type FieldValidator<T, TValues = any> = (value: T, getValue: (key: KeySelector<TValues, T>) => any) => boolean | string[] | Promise<boolean | string[]>; |
{ | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"license": "MIT", | ||
@@ -70,4 +70,4 @@ "main": "dist/index.js", | ||
"rxjs": "^6.6.3", | ||
"rxjs-deep-subject": "^0.1.4" | ||
"rxjs-deep-subject": "^0.1.5" | ||
} | ||
} | ||
} |
@@ -1,14 +0,15 @@ | ||
export { readForm } from './readForm'; | ||
export { resetForm } from './resetForm'; | ||
export { setFieldValue } from './setFieldValue'; | ||
export { setInitialValue } from './setInitialValue'; | ||
export { useControl } from './useControl'; | ||
export { useControlState } from './useControlState'; | ||
export { useErrors } from './useErrors'; | ||
export { useForm } from './useForm'; | ||
export { useInput } from './useInput'; | ||
export { useIsPristine } from './useIsPristine'; | ||
export { useIsValid } from './useIsValid'; | ||
export { useWatch } from './useWatch'; | ||
export { touchFields } from './touchFields'; | ||
export { readForm } from './commands/readForm'; | ||
export { resetForm } from './commands/resetForm'; | ||
export { setFieldValue } from './commands/setFieldValue'; | ||
export { setInitialValue } from './commands/setInitialValue'; | ||
export { touchFields } from './commands/touchFields'; | ||
export { setFieldError } from './commands/setFieldError'; | ||
export { useControlSubscription } from './hooks/useControlSubscription'; | ||
export { useControl } from './hooks/useControl'; | ||
export { useErrors } from './hooks/useErrors'; | ||
export { useForm } from './hooks/useForm'; | ||
export { useInput } from './hooks/useInput'; | ||
export { useIsPristine } from './hooks/useIsPristine'; | ||
export { useIsValid } from './hooks/useIsValid'; | ||
export { useWatch } from './hooks/useWatch'; | ||
export { | ||
@@ -15,0 +16,0 @@ FieldValidator as Validator, |
@@ -12,8 +12,11 @@ import { | ||
map, | ||
mapTo, | ||
mergeMap, | ||
shareReplay, | ||
skip, | ||
startWith, | ||
switchMap, | ||
withLatestFrom, | ||
} from 'rxjs/operators'; | ||
import { getKey, KeySelector, navigateDeepSubject } from '../path'; | ||
import { getKey, KeySelector, navigateDeepSubject } from './path'; | ||
import { FieldValidator, noopValidator } from '../validators'; | ||
@@ -52,2 +55,3 @@ import { filterSeenValues } from './util'; | ||
validator: FieldValidator<T>; | ||
manualError: Subject<ErrorResult>; | ||
error$: Observable<ErrorResult>; | ||
@@ -77,3 +81,5 @@ } | ||
const key = getKey(keySelector); | ||
const control$ = navigateDeepSubject(key, controlStates$); | ||
const control$ = navigateDeepSubject(key, controlStates$) as DeepSubject< | ||
ControlState<any> | ||
>; | ||
const value$ = navigateDeepSubject(key, values$); | ||
@@ -84,5 +90,7 @@ if (!control$.hasValue()) { | ||
registeredKeys.next(keys); | ||
const manualError = new Subject<ErrorResult>(); | ||
control$.next({ | ||
touched: false, | ||
validator, | ||
manualError, | ||
error$: createError$({ | ||
@@ -92,2 +100,3 @@ key, | ||
value$, | ||
manualError$: manualError.asObservable(), | ||
getControlValue$: key => navigateDeepSubject(key, values$), | ||
@@ -143,2 +152,3 @@ }), | ||
value$: Observable<T>; | ||
manualError$: Observable<ErrorResult>; // Assuming hot observable | ||
getControlValue$: (key: string) => DeepSubject<any>; | ||
@@ -149,3 +159,3 @@ }): Observable<ErrorResult> => { | ||
return combineLatest([ | ||
const validatorError$ = combineLatest([ | ||
value$, | ||
@@ -195,5 +205,24 @@ dependency$.pipe( | ||
} | ||
}), | ||
shareReplay(1) // TODO doesnt work with shareLatest | ||
}) | ||
); | ||
const manualError$ = params.manualError$.pipe( | ||
switchMap(v => | ||
concat(of(v), value$.pipe(skipSynchronous(), mapTo(false as const))) | ||
), | ||
startWith(false as const) | ||
); | ||
return combineLatest([validatorError$, manualError$]).pipe( | ||
map( | ||
([validatorResult, manualResult]): ErrorResult => { | ||
if (manualResult === false || manualResult === 'pending') | ||
return validatorResult; | ||
if (validatorResult === false || validatorResult === 'pending') | ||
return manualResult; | ||
return [...manualResult, ...validatorResult]; | ||
} | ||
), | ||
shareReplay(1) // TODO doesnt work with shareLatest - It receives many unsubscriptions+resubscriptions | ||
); | ||
}; | ||
@@ -200,0 +229,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { KeySelector } from './path'; | ||
import { KeySelector } from './internal/path'; | ||
@@ -3,0 +3,0 @@ export type PureValidator<T> = ( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
287629
50
3104
Updatedrxjs-deep-subject@^0.1.5