@felte/react
Advanced tools
Comparing version 1.0.0-next.1 to 1.0.0-next.2
@@ -196,3 +196,3 @@ 'use strict'; | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
const newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
@@ -214,3 +214,2 @@ : path; | ||
function _unset(obj, path) { | ||
var _a; | ||
if (Object(obj) !== obj) | ||
@@ -222,9 +221,15 @@ return; | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
const newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
: path; | ||
(_a = newPath.slice(0, -1).reduce((a, c) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
const foundProp = newPath.slice(0, -1).reduce((a, c) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: undefined, obj)) === null || _a === void 0 ? true : delete _a[newPath[newPath.length - 1]]; | ||
: undefined, obj); | ||
if (Array.isArray(foundProp)) { | ||
foundProp.splice(Number(newPath[newPath.length - 1]), 1); | ||
} | ||
else { | ||
foundProp === null || foundProp === void 0 ? true : delete foundProp[newPath[newPath.length - 1]]; | ||
} | ||
return obj; // Return the top-level object to allow chaining | ||
@@ -238,3 +243,3 @@ } | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = path.toString().match(/[^.[\]]+/g) || []; | ||
const newPath = path.toString().match(/[^.[\]]+/g) || []; | ||
newPath.slice(0, -1).reduce((a, c, i // Iterate all of them except the last one | ||
@@ -610,2 +615,11 @@ ) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
function unsetField(path) { | ||
errors.update(($errors) => { | ||
return _unset($errors, path); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, path); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, path); | ||
}); | ||
data.update(($data) => { | ||
@@ -617,11 +631,2 @@ const newData = _unset($data, path); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, path); | ||
}); | ||
errors.update(($errors) => { | ||
return _unset($errors, path); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, path); | ||
}); | ||
} | ||
@@ -782,43 +787,2 @@ function resetField(path) { | ||
} | ||
function proxyInputs() { | ||
for (const control of Array.from(node.elements).filter(isFormControl)) { | ||
if (shouldIgnore(control) || !control.name) | ||
continue; | ||
let propName = 'value'; | ||
if (isInputElement(control) && | ||
['checkbox', 'radio'].includes(control.type)) { | ||
propName = 'checked'; | ||
} | ||
if (isInputElement(control) && control.type === 'file') { | ||
propName = 'files'; | ||
} | ||
const prop = Object.getOwnPropertyDescriptor(isSelectElement(control) | ||
? HTMLSelectElement.prototype | ||
: isTextAreaElement(control) | ||
? HTMLTextAreaElement.prototype | ||
: HTMLInputElement.prototype, propName); | ||
Object.defineProperty(control, propName, { | ||
configurable: true, | ||
set(newValue) { | ||
var _a; | ||
(_a = prop === null || prop === void 0 ? void 0 : prop.set) === null || _a === void 0 ? void 0 : _a.call(control, newValue); | ||
if (isInputElement(control)) { | ||
if (control.type === 'checkbox') | ||
return setCheckboxValues(control); | ||
if (control.type === 'radio') | ||
return setRadioValues(control); | ||
if (control.type === 'file') | ||
return setFileValue(control); | ||
} | ||
const inputValue = isSelectElement(control) | ||
? control.value | ||
: getInputTextOrNumber(control); | ||
data.update(($data) => { | ||
return _set($data, getPath(control), inputValue); | ||
}); | ||
}, | ||
get: prop === null || prop === void 0 ? void 0 : prop.get, | ||
}); | ||
} | ||
} | ||
_setCurrentExtenders(extender.map(callExtender('MOUNT'))); | ||
@@ -925,12 +889,6 @@ node.noValidate = !!config.validate; | ||
function unsetTaggedForRemove(formControls) { | ||
for (const control of formControls) { | ||
for (const control of formControls.reverse()) { | ||
if (control.hasAttribute('data-felte-keep-on-remove') && | ||
control.dataset.felteKeepOnRemove !== 'false') | ||
continue; | ||
data.update(($data) => { | ||
return _unset($data, getPathFromDataset(control)); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, getPathFromDataset(control)); | ||
}); | ||
errors.update(($errors) => { | ||
@@ -942,2 +900,8 @@ return _unset($errors, getPathFromDataset(control)); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, getPathFromDataset(control)); | ||
}); | ||
data.update(($data) => { | ||
return _unset($data, getPathFromDataset(control)); | ||
}); | ||
} | ||
@@ -950,3 +914,2 @@ } | ||
if (mutation.addedNodes.length > 0) { | ||
proxyInputs(); | ||
const shouldUpdate = Array.from(mutation.addedNodes).some((node) => { | ||
@@ -987,3 +950,2 @@ if (!isElement(node)) | ||
observer.observe(node, mutationOptions); | ||
proxyInputs(); | ||
node.addEventListener('input', handleInput); | ||
@@ -1252,9 +1214,10 @@ node.addEventListener('change', handleChange); | ||
function useAccessor(store) { | ||
const [, setUpdate] = react.useState({}); | ||
const currentValue = react.useRef(get_store_value(store)); | ||
const values = react.useRef({}); | ||
const subscribedRef = react.useRef(false); | ||
const accessor = react.useCallback((selectorOrPath) => { | ||
var _a = react.useState({}), setUpdate = _a[1]; | ||
var currentValue = react.useRef(get_store_value(store)); | ||
var values = react.useRef({}); | ||
var subscribedRef = react.useRef(false); | ||
var accessor = react.useCallback(function (selectorOrPath) { | ||
var _a; | ||
const subscribed = subscribedRef.current; | ||
var _b; | ||
var subscribed = subscribedRef.current; | ||
if (!selectorOrPath) { | ||
@@ -1265,5 +1228,5 @@ subscribedRef.current = true; | ||
if (typeof subscribed === 'boolean') { | ||
subscribedRef.current || (subscribedRef.current = { | ||
[selectorOrPath.toString()]: selectorOrPath, | ||
}); | ||
subscribedRef.current || (subscribedRef.current = (_a = {}, | ||
_a[selectorOrPath.toString()] = selectorOrPath, | ||
_a)); | ||
} | ||
@@ -1273,3 +1236,3 @@ else { | ||
} | ||
return ((_a = values.current[selectorOrPath.toString()]) !== null && _a !== void 0 ? _a : getValue(currentValue.current, selectorOrPath)); | ||
return ((_b = values.current[selectorOrPath.toString()]) !== null && _b !== void 0 ? _b : getValue(currentValue.current, selectorOrPath)); | ||
}, []); | ||
@@ -1281,4 +1244,4 @@ accessor.subscribe = store.subscribe; | ||
} | ||
react.useEffect(() => { | ||
return store.subscribe(($store) => { | ||
react.useEffect(function () { | ||
return store.subscribe(function ($store) { | ||
currentValue.current = $store; | ||
@@ -1289,7 +1252,8 @@ if (!subscribedRef.current) | ||
return setUpdate({}); | ||
let hasChanged = false; | ||
const keys = Object.keys(subscribedRef.current); | ||
for (const key of keys) { | ||
const selector = subscribedRef.current[key]; | ||
const newValue = getValue($store, selector); | ||
var hasChanged = false; | ||
var keys = Object.keys(subscribedRef.current); | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
var selector = subscribedRef.current[key]; | ||
var newValue = getValue($store, selector); | ||
if (typeof values.current[selector.toString()] === 'undefined') { | ||
@@ -1325,2 +1289,13 @@ values.current[selector.toString()] = newValue; | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __rest(s, e) { | ||
@@ -1339,3 +1314,3 @@ var t = {}; | ||
function useConst(setup) { | ||
const ref = react.useRef(); | ||
var ref = react.useRef(); | ||
if (ref.current === undefined) { | ||
@@ -1347,24 +1322,24 @@ ref.current = setup(); | ||
function useForm(config) { | ||
const destroyRef = react.useRef(); | ||
const _a = useConst(() => { | ||
const _a = createForm(config !== null && config !== void 0 ? config : {}, { | ||
storeFactory: writable, | ||
}), { form: coreForm } = _a, rest = __rest(_a, ["form"]); | ||
const form = (node) => { | ||
var destroyRef = react.useRef(); | ||
var _a = useConst(function () { | ||
var _a = createForm(config !== null && config !== void 0 ? config : {}, { | ||
storeFactory: writable | ||
}), coreForm = _a.form, rest = __rest(_a, ["form"]); | ||
var form = function (node) { | ||
if (!node) | ||
return; | ||
const { destroy } = coreForm(node); | ||
var destroy = coreForm(node).destroy; | ||
destroyRef.current = destroy; | ||
}; | ||
return Object.assign({ form }, rest); | ||
}), { cleanup } = _a, rest = __rest(_a, ["cleanup"]); | ||
const data = useAccessor(rest.data); | ||
const errors = useAccessor(rest.errors); | ||
const touched = useAccessor(rest.touched); | ||
const warnings = useAccessor(rest.warnings); | ||
const isSubmitting = useAccessor(rest.isSubmitting); | ||
const isDirty = useAccessor(rest.isDirty); | ||
const isValid = useAccessor(rest.isValid); | ||
react.useEffect(() => { | ||
return () => { | ||
return __assign({ form: form }, rest); | ||
}), cleanup = _a.cleanup, rest = __rest(_a, ["cleanup"]); | ||
var data = useAccessor(rest.data); | ||
var errors = useAccessor(rest.errors); | ||
var touched = useAccessor(rest.touched); | ||
var warnings = useAccessor(rest.warnings); | ||
var isSubmitting = useAccessor(rest.isSubmitting); | ||
var isDirty = useAccessor(rest.isDirty); | ||
var isValid = useAccessor(rest.isValid); | ||
react.useEffect(function () { | ||
return function () { | ||
var _a; | ||
@@ -1375,9 +1350,3 @@ cleanup(); | ||
}, []); | ||
return Object.assign(Object.assign({}, rest), { data, | ||
errors, | ||
warnings, | ||
touched, | ||
isSubmitting, | ||
isDirty, | ||
isValid }); | ||
return __assign(__assign({}, rest), { data: data, errors: errors, warnings: warnings, touched: touched, isSubmitting: isSubmitting, isDirty: isDirty, isValid: isValid }); | ||
} | ||
@@ -1384,0 +1353,0 @@ |
{ | ||
"name": "@felte/react", | ||
"version": "1.0.0-next.1", | ||
"version": "1.0.0-next.2", | ||
"description": "An extensible form library for ReactJS", | ||
@@ -29,3 +29,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@felte/core": "1.0.0-next.1", | ||
"@felte/core": "1.0.0-next.2", | ||
"svelte": "^3.44.3" | ||
@@ -40,3 +40,3 @@ }, | ||
"devDependencies": { | ||
"@felte/common": "1.0.0-next.0", | ||
"@felte/common": "1.0.0-next.1", | ||
"@testing-library/jest-dom": "^5.11.9", | ||
@@ -43,0 +43,0 @@ "@testing-library/react-hooks": "^7.0.2", |
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
484990
2627
+ Added@felte/common@1.0.0-next.1(transitive)
+ Added@felte/core@1.0.0-next.2(transitive)
- Removed@felte/common@1.0.0-next.0(transitive)
- Removed@felte/core@1.0.0-next.1(transitive)
Updated@felte/core@1.0.0-next.2