Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

formicary

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

formicary - npm Package Compare versions

Comparing version 0.3.0-beta.10 to 0.3.0-beta.11

346

dist/formicary.esm.js

@@ -30,5 +30,8 @@ // src/internal/path.ts

if (target.path.length === 0)
return new Proxy({
path: newPath
}, handler);
return new Proxy(
{
path: newPath
},
handler
);
target.path = newPath;

@@ -46,3 +49,5 @@ return receiver;

if (!result[PATH_RESULT]) {
throw new Error(`You must return a value from the argument in the selector function`);
throw new Error(
`You must return a value from the argument in the selector function`
);
}

@@ -60,3 +65,5 @@ return result[PATH_RESULT];

if (result.some((r) => !r[PATH_RESULT])) {
throw new Error(`You must return a value from the argument in the selector function`);
throw new Error(
`You must return a value from the argument in the selector function`
);
}

@@ -184,6 +191,10 @@ return result.map((r) => r[PATH_RESULT]);

function getFormErrors(formRef) {
return buildObject(Object.fromEntries(Array.from(formRef.controlStates.entries()).map(([key, state]) => [
key,
getControlStateError(state)
])));
return buildObject(
Object.fromEntries(
Array.from(formRef.controlStates.entries()).map(([key, state]) => [
key,
getControlStateError(state)
])
)
);
}

@@ -199,6 +210,10 @@ function getControlStateError(state) {

// src/commands/getFormValue.ts
var getFormValue = (formRef) => buildObject(Object.fromEntries(Array.from(formRef.values.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])));
var getFormValue = (formRef) => buildObject(
Object.fromEntries(
Array.from(formRef.values.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])
)
);

@@ -227,6 +242,9 @@ // src/internal/formRef.ts

isLessThan: (threshold) => "Expected a value less than " + threshold,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
matches: (regex) => "Invalid Value"
};
var setValidatorMessages = (messages) => {
Object.entries(messages).forEach(([key, value]) => validatorMessages[key] = value);
Object.entries(messages).forEach(
([key, value]) => validatorMessages[key] = value
);
};

@@ -313,5 +331,9 @@ var isNumber = (message) => (value) => {

var mergeValidators = (...validators) => (...args) => {
const syncResults = validators.map((validate) => validate(...args));
const syncResults = validators.map(
(validate) => validate(...args)
);
const processResult = (results) => {
const flattenedResults = results.flatMap((result) => result === true ? [] : result);
const flattenedResults = results.flatMap(
(result) => result === true ? [] : result
);
return flattenedResults;

@@ -350,9 +372,15 @@ };

})();
const initialValues = new Map();
Object.entries(getKeyValues(options.initialValue || {})).forEach(([key, value]) => {
initialValues.set(key, new State2(value));
});
const values = new Map(Array.from(initialValues.entries()).map(([key, subject]) => [key, new State2(subject.getValue())]));
const initialValues = /* @__PURE__ */ new Map();
Object.entries(getKeyValues(options.initialValue || {})).forEach(
([key, value]) => {
initialValues.set(key, new State2(value));
}
);
const values = new Map(
Array.from(initialValues.entries()).map(
([key, subject]) => [key, new State2(subject.getValue())]
)
);
const registeredKeys = new State2(new Set(values.keys()));
const controlStates = new Map();
const controlStates = /* @__PURE__ */ new Map();
const getControlValue$ = (key) => getMapValue(key, values);

@@ -364,2 +392,5 @@ const registerControl = ({

}) => {
if (registeredKeys.closed) {
return;
}
const value$ = getControlValue$(key);

@@ -378,3 +409,7 @@ const control$ = getMapValue(key, controlStates);

key,
validator$: control$.pipe(map((control) => control.validator), withDefault(validator), distinctUntilChanged()),
validator$: control$.pipe(
map((control) => control.validator),
withDefault(validator),
distinctUntilChanged()
),
value$,

@@ -453,3 +488,3 @@ manualError$: manualError,

const validationError$ = new Stateless((obs) => {
const dependenciesObserved = new Set();
const dependenciesObserved = /* @__PURE__ */ new Set();
let latestValidator = EMPTY;

@@ -495,3 +530,6 @@ let latestValue = EMPTY;

if (ex instanceof ValueNotThereYetError) {
console.warn(`Setting control ${key} error to pending, as the validation depends on a field that hasn't been registered yet`, ex);
console.warn(
`Setting control ${key} error to pending, as the validation depends on a field that hasn't been registered yet`,
ex
);
} else {

@@ -510,3 +548,7 @@ console.error(ex);

innerUnsub();
innerUnsub = value$.pipe(skipSynchronous(), take(1), map(() => false)).subscribe(obs.next);
innerUnsub = value$.pipe(
skipSynchronous(),
take(1),
map(() => false)
).subscribe(obs.next);
});

@@ -519,17 +561,21 @@ return () => {

const errors = combine({
validatorResult: validationError$.pipe(distinctUntilChanged((a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((v, i) => b[i] === v);
}
return a === b;
})),
validatorResult: validationError$.pipe(
distinctUntilChanged((a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((v, i) => b[i] === v);
}
return a === b;
})
),
manualResult: manualError$
});
return errors.pipe(map(({ validatorResult, manualResult }) => {
if (manualResult === false || manualResult === "pending")
return validatorResult;
if (validatorResult === false || validatorResult === "pending")
return manualResult;
return [...manualResult, ...validatorResult];
}));
return errors.pipe(
map(({ validatorResult, manualResult }) => {
if (manualResult === false || manualResult === "pending")
return validatorResult;
if (validatorResult === false || validatorResult === "pending")
return manualResult;
return [...manualResult, ...validatorResult];
})
);
};

@@ -585,6 +631,10 @@ var ValueNotThereYetError = class extends Error {

var getInitialValue = (formRef, key) => getMapValue(key, formRef.initialValues).getValue();
var getFormInitialValue = (formRef) => buildObject(Object.fromEntries(Array.from(formRef.initialValues.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])));
var getFormInitialValue = (formRef) => buildObject(
Object.fromEntries(
Array.from(formRef.initialValues.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])
)
);

@@ -653,7 +703,10 @@ // src/commands/touchFields.ts

}, [formRef, options]);
useEffect(() => () => {
if (options.unregisterOnUnmount) {
formRef.unregisterControl(key);
}
}, []);
useEffect(
() => () => {
if (options.unregisterOnUnmount) {
formRef.unregisterControl(key);
}
},
[]
);
return {

@@ -668,11 +721,14 @@ getValue: () => {

const state$ = getControlState(formRef, key);
state$.value.then((value) => {
if (value.touched || state$.closed)
return;
state$.setValue({
...value,
touched: true
});
}, () => {
});
state$.value.then(
(value) => {
if (value.touched || state$.closed)
return;
state$.setValue({
...value,
touched: true
});
},
() => {
}
);
}

@@ -688,3 +744,5 @@ };

});
const [state, setState] = useState(control.getValue);
const [state, setState] = useState(
control.getValue
);
useEffect2(() => subscribe(setState), []);

@@ -711,6 +769,24 @@ return {

const keys$ = keys.length === 0 ? formRef.registeredKeys.pipe(map2((set) => Array.from(set))) : just(getKeys(keys));
return keys$.pipe(switchMap((keys2) => combine2(Object.fromEntries(keys2.map((key) => [
key,
getControlState(formRef, key).pipe(map2((v) => v.touched ? v.error$ : FALSE), withDefault2(FALSE), distinctUntilChanged2(), switchMap((v) => v))
])))), map2((results) => Object.fromEntries(Object.entries(results).filter(([, value]) => value !== false)))).capture();
return keys$.pipe(
switchMap(
(keys2) => combine2(
Object.fromEntries(
keys2.map((key) => [
key,
getControlState(formRef, key).pipe(
map2((v) => v.touched ? v.error$ : FALSE),
withDefault2(FALSE),
distinctUntilChanged2(),
switchMap((v) => v)
)
])
)
)
),
map2(
(results) => Object.fromEntries(
Object.entries(results).filter(([, value]) => value !== false)
)
)
).capture();
}, [formRef, ...keys]);

@@ -761,8 +837,10 @@ const [errors, setErrors] = useState2(() => {

const value$ = getMapValue(getKey(key), formRef.values);
const [value, setValue] = useState3(() => {
if (value$.hasValue()) {
return value$.getValue();
const [value, setValue] = useState3(
() => {
if (value$.hasValue()) {
return value$.getValue();
}
return void 0;
}
return void 0;
});
);
useEffect5(() => value$.subscribe(setValue), [value$]);

@@ -779,7 +857,10 @@ return value;

}
useEffect6(() => () => {
var _a;
(_a = ref.current) == null ? void 0 : _a.dispose();
ref.current = null;
}, []);
useEffect6(
() => () => {
var _a;
(_a = ref.current) == null ? void 0 : _a.dispose();
ref.current = null;
},
[]
);
return ref.current;

@@ -799,7 +880,23 @@ };

const [formRef, mapFn, eqFn] = useHookParams(args);
const valueStream = useMemo2(() => formRef.registeredKeys.pipe(map3((set) => Array.from(set)), switchMap2((keys) => combine3(Object.fromEntries(keys.map((key) => [
key,
formRef.values.get(key).pipe(withDefault3(void 0), distinctUntilChanged3())
])))), map3((formValues) => mapFn(buildObject(formValues))), distinctUntilChanged3(eqFn)).capture(), [formRef]);
const [state, setState] = useState4(() => valueStream.hasValue() ? valueStream.getValue() : {});
const valueStream = useMemo2(
() => formRef.registeredKeys.pipe(
map3((set) => Array.from(set)),
switchMap2(
(keys) => combine3(
Object.fromEntries(
keys.map((key) => [
key,
formRef.values.get(key).pipe(withDefault3(void 0), distinctUntilChanged3())
])
)
)
),
map3((formValues) => mapFn(buildObject(formValues))),
distinctUntilChanged3(eqFn)
).capture(),
[formRef]
);
const [state, setState] = useState4(
() => valueStream.hasValue() ? valueStream.getValue() : {}
);
useEffect7(() => {

@@ -824,7 +921,10 @@ valueStream.subscribe(setState);

const activeListener = useRef3(null);
useEffect8(() => () => {
var _a2;
(_a2 = activeListener.current) == null ? void 0 : _a2.cleanup();
activeListener.current = null;
}, []);
useEffect8(
() => () => {
var _a2;
(_a2 = activeListener.current) == null ? void 0 : _a2.cleanup();
activeListener.current = null;
},
[]
);
const ref = (element) => {

@@ -870,10 +970,22 @@ var _a2, _b, _c;

const [_formRef] = useHookParams([formRef]);
const isPristine$ = useMemo3(() => _formRef.registeredKeys.pipe(switchMap3((keys) => combine4(Array.from(keys).map((key) => {
const initialValue$ = getMapValue(key, _formRef.initialValues);
const value$ = getMapValue(key, _formRef.values);
return combine4({
initialValue: initialValue$,
value: value$
}).pipe(map4(({ initialValue, value }) => initialValue === value));
})).pipe(map4((results) => results.every((pristine) => pristine)))), distinctUntilChanged4()).capture(), [_formRef]);
const isPristine$ = useMemo3(
() => _formRef.registeredKeys.pipe(
switchMap3(
(keys) => combine4(
Array.from(keys).map((key) => {
const initialValue$ = getMapValue(key, _formRef.initialValues);
const value$ = getMapValue(key, _formRef.values);
return combine4({
initialValue: initialValue$,
value: value$
}).pipe(
map4(({ initialValue, value }) => initialValue === value)
);
})
).pipe(map4((results) => results.every((pristine) => pristine)))
),
distinctUntilChanged4()
).capture(),
[_formRef]
);
const [isPristine, setIsPristine] = useState5(() => {

@@ -899,19 +1011,41 @@ if (isPristine$.hasValue()) {

const keys$ = keys.length === 0 ? formRef.registeredKeys.pipe(map5((set) => Array.from(set))) : just2(getKeys(keys));
return keys$.pipe(switchMap4((keys2) => combine5(Object.fromEntries(keys2.map((key) => [
key,
getControlState(formRef, key).pipe(take2(1), switchMap4((v) => v.error$), withDefault4(false))
])))), map5((results) => Object.fromEntries(Object.entries(results).filter(([, value]) => value !== false))));
return keys$.pipe(
switchMap4(
(keys2) => combine5(
Object.fromEntries(
keys2.map((key) => [
key,
getControlState(formRef, key).pipe(
take2(1),
switchMap4((v) => v.error$),
withDefault4(false)
)
])
)
)
),
map5(
(results) => Object.fromEntries(
Object.entries(results).filter(([, value]) => value !== false)
)
)
);
}, [formRef, ...keys]);
const isValid$ = useMemo4(() => error$.pipe(map5((errors) => {
const errorValues = Object.values(errors);
let hasPending = false;
const hasError = errorValues.some((error) => {
if (error === "pending") {
hasPending = true;
return false;
}
return true;
});
return hasError ? false : hasPending ? "pending" : true;
})).capture(), [error$]);
const isValid$ = useMemo4(
() => error$.pipe(
map5((errors) => {
const errorValues = Object.values(errors);
let hasPending = false;
const hasError = errorValues.some((error) => {
if (error === "pending") {
hasPending = true;
return false;
}
return true;
});
return hasError ? false : hasPending ? "pending" : true;
})
).capture(),
[error$]
);
const [isValid, setIsValid] = useState6(() => {

@@ -918,0 +1052,0 @@ if (isValid$.hasValue()) {

@@ -1,27 +0,23 @@

var __create = Object.create;
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return target;
return to;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
__export(exports, {
var src_exports = {};
__export(src_exports, {
FormicaryContext: () => FormicaryContext,

@@ -69,5 +65,6 @@ conditionalValidator: () => conditionalValidator,

});
module.exports = __toCommonJS(src_exports);
// src/internal/path.ts
var import_derive_state = __toModule(require("derive-state"));
var import_derive_state = require("derive-state");

@@ -100,5 +97,8 @@ // src/internal/subfield.ts

if (target.path.length === 0)
return new Proxy({
path: newPath
}, handler);
return new Proxy(
{
path: newPath
},
handler
);
target.path = newPath;

@@ -116,3 +116,5 @@ return receiver;

if (!result[PATH_RESULT]) {
throw new Error(`You must return a value from the argument in the selector function`);
throw new Error(
`You must return a value from the argument in the selector function`
);
}

@@ -130,3 +132,5 @@ return result[PATH_RESULT];

if (result.some((r) => !r[PATH_RESULT])) {
throw new Error(`You must return a value from the argument in the selector function`);
throw new Error(
`You must return a value from the argument in the selector function`
);
}

@@ -254,6 +258,10 @@ return result.map((r) => r[PATH_RESULT]);

function getFormErrors(formRef) {
return buildObject(Object.fromEntries(Array.from(formRef.controlStates.entries()).map(([key, state]) => [
key,
getControlStateError(state)
])));
return buildObject(
Object.fromEntries(
Array.from(formRef.controlStates.entries()).map(([key, state]) => [
key,
getControlStateError(state)
])
)
);
}

@@ -269,9 +277,13 @@ function getControlStateError(state) {

// src/commands/getFormValue.ts
var getFormValue = (formRef) => buildObject(Object.fromEntries(Array.from(formRef.values.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])));
var getFormValue = (formRef) => buildObject(
Object.fromEntries(
Array.from(formRef.values.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])
)
);
// src/internal/formRef.ts
var import_derive_state2 = __toModule(require("derive-state"));
var import_derive_state2 = require("derive-state");

@@ -287,6 +299,9 @@ // src/validators.ts

isLessThan: (threshold) => "Expected a value less than " + threshold,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
matches: (regex) => "Invalid Value"
};
var setValidatorMessages = (messages) => {
Object.entries(messages).forEach(([key, value]) => validatorMessages[key] = value);
Object.entries(messages).forEach(
([key, value]) => validatorMessages[key] = value
);
};

@@ -373,5 +388,9 @@ var isNumber = (message) => (value) => {

var mergeValidators = (...validators) => (...args) => {
const syncResults = validators.map((validate) => validate(...args));
const syncResults = validators.map(
(validate) => validate(...args)
);
const processResult = (results) => {
const flattenedResults = results.flatMap((result) => result === true ? [] : result);
const flattenedResults = results.flatMap(
(result) => result === true ? [] : result
);
return flattenedResults;

@@ -410,9 +429,15 @@ };

})();
const initialValues = new Map();
Object.entries(getKeyValues(options.initialValue || {})).forEach(([key, value]) => {
initialValues.set(key, new import_derive_state2.State(value));
});
const values = new Map(Array.from(initialValues.entries()).map(([key, subject]) => [key, new import_derive_state2.State(subject.getValue())]));
const initialValues = /* @__PURE__ */ new Map();
Object.entries(getKeyValues(options.initialValue || {})).forEach(
([key, value]) => {
initialValues.set(key, new import_derive_state2.State(value));
}
);
const values = new Map(
Array.from(initialValues.entries()).map(
([key, subject]) => [key, new import_derive_state2.State(subject.getValue())]
)
);
const registeredKeys = new import_derive_state2.State(new Set(values.keys()));
const controlStates = new Map();
const controlStates = /* @__PURE__ */ new Map();
const getControlValue$ = (key) => getMapValue(key, values);

@@ -424,2 +449,5 @@ const registerControl = ({

}) => {
if (registeredKeys.closed) {
return;
}
const value$ = getControlValue$(key);

@@ -438,3 +466,7 @@ const control$ = getMapValue(key, controlStates);

key,
validator$: control$.pipe((0, import_derive_state2.map)((control) => control.validator), (0, import_derive_state2.withDefault)(validator), (0, import_derive_state2.distinctUntilChanged)()),
validator$: control$.pipe(
(0, import_derive_state2.map)((control) => control.validator),
(0, import_derive_state2.withDefault)(validator),
(0, import_derive_state2.distinctUntilChanged)()
),
value$,

@@ -513,3 +545,3 @@ manualError$: manualError,

const validationError$ = new import_derive_state2.Stateless((obs) => {
const dependenciesObserved = new Set();
const dependenciesObserved = /* @__PURE__ */ new Set();
let latestValidator = EMPTY;

@@ -555,3 +587,6 @@ let latestValue = EMPTY;

if (ex instanceof ValueNotThereYetError) {
console.warn(`Setting control ${key} error to pending, as the validation depends on a field that hasn't been registered yet`, ex);
console.warn(
`Setting control ${key} error to pending, as the validation depends on a field that hasn't been registered yet`,
ex
);
} else {

@@ -570,3 +605,7 @@ console.error(ex);

innerUnsub();
innerUnsub = value$.pipe((0, import_derive_state2.skipSynchronous)(), (0, import_derive_state2.take)(1), (0, import_derive_state2.map)(() => false)).subscribe(obs.next);
innerUnsub = value$.pipe(
(0, import_derive_state2.skipSynchronous)(),
(0, import_derive_state2.take)(1),
(0, import_derive_state2.map)(() => false)
).subscribe(obs.next);
});

@@ -579,17 +618,21 @@ return () => {

const errors = (0, import_derive_state2.combine)({
validatorResult: validationError$.pipe((0, import_derive_state2.distinctUntilChanged)((a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((v, i) => b[i] === v);
}
return a === b;
})),
validatorResult: validationError$.pipe(
(0, import_derive_state2.distinctUntilChanged)((a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((v, i) => b[i] === v);
}
return a === b;
})
),
manualResult: manualError$
});
return errors.pipe((0, import_derive_state2.map)(({ validatorResult, manualResult }) => {
if (manualResult === false || manualResult === "pending")
return validatorResult;
if (validatorResult === false || validatorResult === "pending")
return manualResult;
return [...manualResult, ...validatorResult];
}));
return errors.pipe(
(0, import_derive_state2.map)(({ validatorResult, manualResult }) => {
if (manualResult === false || manualResult === "pending")
return validatorResult;
if (validatorResult === false || validatorResult === "pending")
return manualResult;
return [...manualResult, ...validatorResult];
})
);
};

@@ -645,6 +688,10 @@ var ValueNotThereYetError = class extends Error {

var getInitialValue = (formRef, key) => getMapValue(key, formRef.initialValues).getValue();
var getFormInitialValue = (formRef) => buildObject(Object.fromEntries(Array.from(formRef.initialValues.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])));
var getFormInitialValue = (formRef) => buildObject(
Object.fromEntries(
Array.from(formRef.initialValues.entries()).map(([key, value]) => [
key,
value.hasValue() ? value.getValue() : void 0
])
)
);

@@ -679,6 +726,6 @@ // src/commands/touchFields.ts

// src/hooks/useControl.ts
var import_react3 = __toModule(require("react"));
var import_react3 = require("react");
// src/context.ts
var import_react = __toModule(require("react"));
var import_react = require("react");
var FormicaryContext = (0, import_react.createContext)(null);

@@ -702,3 +749,3 @@ var useFormRef = () => {

// src/hooks/useControlStateless.ts
var import_react2 = __toModule(require("react"));
var import_react2 = require("react");
function useControlStateless(...args) {

@@ -715,7 +762,10 @@ const [formRef, keySelector, options = {}] = useHookParams(args);

}, [formRef, options]);
(0, import_react2.useEffect)(() => () => {
if (options.unregisterOnUnmount) {
formRef.unregisterControl(key);
}
}, []);
(0, import_react2.useEffect)(
() => () => {
if (options.unregisterOnUnmount) {
formRef.unregisterControl(key);
}
},
[]
);
return {

@@ -730,11 +780,14 @@ getValue: () => {

const state$ = getControlState(formRef, key);
state$.value.then((value) => {
if (value.touched || state$.closed)
return;
state$.setValue({
...value,
touched: true
});
}, () => {
});
state$.value.then(
(value) => {
if (value.touched || state$.closed)
return;
state$.setValue({
...value,
touched: true
});
},
() => {
}
);
}

@@ -750,3 +803,5 @@ };

});
const [state, setState] = (0, import_react3.useState)(control.getValue);
const [state, setState] = (0, import_react3.useState)(
control.getValue
);
(0, import_react3.useEffect)(() => subscribe(setState), []);

@@ -760,4 +815,4 @@ return {

// src/hooks/useErrors.ts
var import_derive_state3 = __toModule(require("derive-state"));
var import_react4 = __toModule(require("react"));
var import_derive_state3 = require("derive-state");
var import_react4 = require("react");
function useErrors(...args) {

@@ -767,6 +822,24 @@ const [formRef, ...keys] = useHookParams(args);

const keys$ = keys.length === 0 ? formRef.registeredKeys.pipe((0, import_derive_state3.map)((set) => Array.from(set))) : (0, import_derive_state3.just)(getKeys(keys));
return keys$.pipe((0, import_derive_state3.switchMap)((keys2) => (0, import_derive_state3.combine)(Object.fromEntries(keys2.map((key) => [
key,
getControlState(formRef, key).pipe((0, import_derive_state3.map)((v) => v.touched ? v.error$ : FALSE), (0, import_derive_state3.withDefault)(FALSE), (0, import_derive_state3.distinctUntilChanged)(), (0, import_derive_state3.switchMap)((v) => v))
])))), (0, import_derive_state3.map)((results) => Object.fromEntries(Object.entries(results).filter(([, value]) => value !== false)))).capture();
return keys$.pipe(
(0, import_derive_state3.switchMap)(
(keys2) => (0, import_derive_state3.combine)(
Object.fromEntries(
keys2.map((key) => [
key,
getControlState(formRef, key).pipe(
(0, import_derive_state3.map)((v) => v.touched ? v.error$ : FALSE),
(0, import_derive_state3.withDefault)(FALSE),
(0, import_derive_state3.distinctUntilChanged)(),
(0, import_derive_state3.switchMap)((v) => v)
)
])
)
)
),
(0, import_derive_state3.map)(
(results) => Object.fromEntries(
Object.entries(results).filter(([, value]) => value !== false)
)
)
).capture();
}, [formRef, ...keys]);

@@ -788,6 +861,6 @@ const [errors, setErrors] = (0, import_react4.useState)(() => {

// src/hooks/useFieldChanges.ts
var import_react6 = __toModule(require("react"));
var import_react6 = require("react");
// src/internal/useLatestRef.ts
var import_react5 = __toModule(require("react"));
var import_react5 = require("react");
function useLatestRef(value) {

@@ -814,12 +887,14 @@ const ref = (0, import_react5.useRef)(value);

// src/hooks/useFieldValue.ts
var import_react7 = __toModule(require("react"));
var import_react7 = require("react");
function useFieldValue(...args) {
const [formRef, key] = useHookParams(args);
const value$ = getMapValue(getKey(key), formRef.values);
const [value, setValue] = (0, import_react7.useState)(() => {
if (value$.hasValue()) {
return value$.getValue();
const [value, setValue] = (0, import_react7.useState)(
() => {
if (value$.hasValue()) {
return value$.getValue();
}
return void 0;
}
return void 0;
});
);
(0, import_react7.useEffect)(() => value$.subscribe(setValue), [value$]);

@@ -830,3 +905,3 @@ return value;

// src/hooks/useForm.ts
var import_react8 = __toModule(require("react"));
var import_react8 = require("react");
var useForm = (options) => {

@@ -837,7 +912,10 @@ const ref = (0, import_react8.useRef)(null);

}
(0, import_react8.useEffect)(() => () => {
var _a;
(_a = ref.current) == null ? void 0 : _a.dispose();
ref.current = null;
}, []);
(0, import_react8.useEffect)(
() => () => {
var _a;
(_a = ref.current) == null ? void 0 : _a.dispose();
ref.current = null;
},
[]
);
return ref.current;

@@ -847,11 +925,27 @@ };

// src/hooks/useFormValue.ts
var import_derive_state4 = __toModule(require("derive-state"));
var import_react9 = __toModule(require("react"));
var import_derive_state4 = require("derive-state");
var import_react9 = require("react");
function useFormValue(...args) {
const [formRef, mapFn, eqFn] = useHookParams(args);
const valueStream = (0, import_react9.useMemo)(() => formRef.registeredKeys.pipe((0, import_derive_state4.map)((set) => Array.from(set)), (0, import_derive_state4.switchMap)((keys) => (0, import_derive_state4.combine)(Object.fromEntries(keys.map((key) => [
key,
formRef.values.get(key).pipe((0, import_derive_state4.withDefault)(void 0), (0, import_derive_state4.distinctUntilChanged)())
])))), (0, import_derive_state4.map)((formValues) => mapFn(buildObject(formValues))), (0, import_derive_state4.distinctUntilChanged)(eqFn)).capture(), [formRef]);
const [state, setState] = (0, import_react9.useState)(() => valueStream.hasValue() ? valueStream.getValue() : {});
const valueStream = (0, import_react9.useMemo)(
() => formRef.registeredKeys.pipe(
(0, import_derive_state4.map)((set) => Array.from(set)),
(0, import_derive_state4.switchMap)(
(keys) => (0, import_derive_state4.combine)(
Object.fromEntries(
keys.map((key) => [
key,
formRef.values.get(key).pipe((0, import_derive_state4.withDefault)(void 0), (0, import_derive_state4.distinctUntilChanged)())
])
)
)
),
(0, import_derive_state4.map)((formValues) => mapFn(buildObject(formValues))),
(0, import_derive_state4.distinctUntilChanged)(eqFn)
).capture(),
[formRef]
);
const [state, setState] = (0, import_react9.useState)(
() => valueStream.hasValue() ? valueStream.getValue() : {}
);
(0, import_react9.useEffect)(() => {

@@ -865,3 +959,3 @@ valueStream.subscribe(setState);

// src/hooks/useInput.ts
var import_react10 = __toModule(require("react"));
var import_react10 = require("react");
function useInput(...args) {

@@ -877,7 +971,10 @@ var _a;

const activeListener = (0, import_react10.useRef)(null);
(0, import_react10.useEffect)(() => () => {
var _a2;
(_a2 = activeListener.current) == null ? void 0 : _a2.cleanup();
activeListener.current = null;
}, []);
(0, import_react10.useEffect)(
() => () => {
var _a2;
(_a2 = activeListener.current) == null ? void 0 : _a2.cleanup();
activeListener.current = null;
},
[]
);
const ref = (element) => {

@@ -919,14 +1016,26 @@ var _a2, _b, _c;

// src/hooks/useIsPristine.ts
var import_react11 = __toModule(require("react"));
var import_derive_state5 = __toModule(require("derive-state"));
var import_react11 = require("react");
var import_derive_state5 = require("derive-state");
function useIsPristine(formRef) {
const [_formRef] = useHookParams([formRef]);
const isPristine$ = (0, import_react11.useMemo)(() => _formRef.registeredKeys.pipe((0, import_derive_state5.switchMap)((keys) => (0, import_derive_state5.combine)(Array.from(keys).map((key) => {
const initialValue$ = getMapValue(key, _formRef.initialValues);
const value$ = getMapValue(key, _formRef.values);
return (0, import_derive_state5.combine)({
initialValue: initialValue$,
value: value$
}).pipe((0, import_derive_state5.map)(({ initialValue, value }) => initialValue === value));
})).pipe((0, import_derive_state5.map)((results) => results.every((pristine) => pristine)))), (0, import_derive_state5.distinctUntilChanged)()).capture(), [_formRef]);
const isPristine$ = (0, import_react11.useMemo)(
() => _formRef.registeredKeys.pipe(
(0, import_derive_state5.switchMap)(
(keys) => (0, import_derive_state5.combine)(
Array.from(keys).map((key) => {
const initialValue$ = getMapValue(key, _formRef.initialValues);
const value$ = getMapValue(key, _formRef.values);
return (0, import_derive_state5.combine)({
initialValue: initialValue$,
value: value$
}).pipe(
(0, import_derive_state5.map)(({ initialValue, value }) => initialValue === value)
);
})
).pipe((0, import_derive_state5.map)((results) => results.every((pristine) => pristine)))
),
(0, import_derive_state5.distinctUntilChanged)()
).capture(),
[_formRef]
);
const [isPristine, setIsPristine] = (0, import_react11.useState)(() => {

@@ -946,4 +1055,4 @@ if (isPristine$.hasValue()) {

// src/hooks/useIsValid.ts
var import_derive_state6 = __toModule(require("derive-state"));
var import_react12 = __toModule(require("react"));
var import_derive_state6 = require("derive-state");
var import_react12 = require("react");
function useIsValid(...args) {

@@ -953,19 +1062,41 @@ const [formRef, defaultValue = false, ...keys] = useHookParams(args);

const keys$ = keys.length === 0 ? formRef.registeredKeys.pipe((0, import_derive_state6.map)((set) => Array.from(set))) : (0, import_derive_state6.just)(getKeys(keys));
return keys$.pipe((0, import_derive_state6.switchMap)((keys2) => (0, import_derive_state6.combine)(Object.fromEntries(keys2.map((key) => [
key,
getControlState(formRef, key).pipe((0, import_derive_state6.take)(1), (0, import_derive_state6.switchMap)((v) => v.error$), (0, import_derive_state6.withDefault)(false))
])))), (0, import_derive_state6.map)((results) => Object.fromEntries(Object.entries(results).filter(([, value]) => value !== false))));
return keys$.pipe(
(0, import_derive_state6.switchMap)(
(keys2) => (0, import_derive_state6.combine)(
Object.fromEntries(
keys2.map((key) => [
key,
getControlState(formRef, key).pipe(
(0, import_derive_state6.take)(1),
(0, import_derive_state6.switchMap)((v) => v.error$),
(0, import_derive_state6.withDefault)(false)
)
])
)
)
),
(0, import_derive_state6.map)(
(results) => Object.fromEntries(
Object.entries(results).filter(([, value]) => value !== false)
)
)
);
}, [formRef, ...keys]);
const isValid$ = (0, import_react12.useMemo)(() => error$.pipe((0, import_derive_state6.map)((errors) => {
const errorValues = Object.values(errors);
let hasPending = false;
const hasError = errorValues.some((error) => {
if (error === "pending") {
hasPending = true;
return false;
}
return true;
});
return hasError ? false : hasPending ? "pending" : true;
})).capture(), [error$]);
const isValid$ = (0, import_react12.useMemo)(
() => error$.pipe(
(0, import_derive_state6.map)((errors) => {
const errorValues = Object.values(errors);
let hasPending = false;
const hasError = errorValues.some((error) => {
if (error === "pending") {
hasPending = true;
return false;
}
return true;
});
return hasError ? false : hasPending ? "pending" : true;
})
).capture(),
[error$]
);
const [isValid, setIsValid] = (0, import_react12.useState)(() => {

@@ -972,0 +1103,0 @@ if (isValid$.hasValue()) {

@@ -10,3 +10,3 @@ import { FormRef } from "../internal/formRef";

}
export declare type Control<T> = Omit<ControlStateless<T>, "subscribe"> & {
export type Control<T> = Omit<ControlStateless<T>, "subscribe"> & {
value: T | undefined;

@@ -13,0 +13,0 @@ };

import { FormRef } from "../internal/formRef";
import { Key, KeysSelector, Paths } from "../internal/path";
declare type Errors<T extends string> = {
type Errors<T extends string> = {
[K in T]: string[] | "pending";

@@ -5,0 +5,0 @@ };

import { FormRef } from "../internal/formRef";
import { Key, KeySelector, Paths, ValueOfPath } from "../internal/path";
declare type OnChange<V> = (value: V, isInitial: boolean) => void;
type OnChange<V> = (value: V, isInitial: boolean) => void;
export declare function useFieldChanges<TValues, P extends Paths<TValues>>(formRef: FormRef<TValues>, path: P, onChange: OnChange<ValueOfPath<TValues, P>>): void;

@@ -5,0 +5,0 @@ export declare function useFieldChanges<TValues, V>(formRef: FormRef<TValues>, keySelector: KeySelector<TValues, V>, onChange: OnChange<V>): void;

import { FormRef } from "../internal/formRef";
import { Key, KeySelector, Paths, ValueOfPath } from "../internal/path";
declare type SomeValue = string | number | boolean | symbol | object;
type SomeValue = string | number | boolean | symbol | object;
export declare function useFieldValue<TValues, P extends Paths<TValues>>(formRef: FormRef<TValues>, path: P): ValueOfPath<TValues, P> | undefined;

@@ -5,0 +5,0 @@ export declare function useFieldValue<TValues, V>(formRef: FormRef<TValues>, keySelector: KeySelector<TValues, V>): V | undefined;

@@ -5,3 +5,3 @@ import { RefCallback } from "react";

import { Validator } from "../validators";
export declare type InputOptions<TValues, V> = {
export type InputOptions<TValues, V> = {
elementProp?: "value" | "checked";

@@ -8,0 +8,0 @@ eventType?: "input" | "onChange";

@@ -23,3 +23,3 @@ import { State, Stateless, StateObservable } from "derive-state";

}
export declare type ErrorResult = string[] | "pending" | false;
export type ErrorResult = string[] | "pending" | false;
export interface ControlState<T> {

@@ -26,0 +26,0 @@ touched: boolean;

import { State } from "derive-state";
declare type MappedKey<K extends string, V> = K | `${K}${V extends Array<infer A> ? `.${number}` | `.${number}.${Paths<A>}` : V extends Record<string, any> ? `.${Paths<V>}` : ""}`;
declare type KeyMap<T> = {
type MappedKey<K extends string, V> = K | `${K}${V extends Array<infer A> ? `.${number}` | `.${number}.${Paths<A>}` : V extends Record<string, any> ? `.${Paths<V>}` : ""}`;
type KeyMap<T> = {
[K in keyof T & string]: MappedKey<K, T[K]>;
};
export declare type Paths<T> = KeyMap<T>[keyof T & string];
declare type NotNil<T> = Exclude<T, undefined | null>;
export declare type ValueOfPath<TValues, Path> = Path extends keyof TValues ? TValues[Path] : Path extends `${infer Prop}.${infer Rest}` ? Prop extends keyof TValues ? ValueOfPath<NotNil<TValues[Prop]>, Rest> : unknown : unknown;
export type Paths<T> = KeyMap<T>[keyof T & string];
type NotNil<T> = Exclude<T, undefined | null>;
export type ValueOfPath<TValues, Path> = Path extends keyof TValues ? TValues[Path] : Path extends `${infer Prop}.${infer Rest}` ? Prop extends keyof TValues ? ValueOfPath<NotNil<TValues[Prop]>, Rest> : unknown : unknown;
declare const internal: unique symbol;
export declare type Key<T, P extends Paths<T>, R> = {
export type Key<T, P extends Paths<T>, R> = {
[internal]: [T, P, R];
};
export declare function createKeyFn<T>(): <P extends Paths<T>>(path: P) => Key<T, P, ValueOfPath<T, P>>;
export declare type KeySelector<TValues, T> = (values: TValues) => T;
export declare type KeysSelector<TValues, R extends ReadonlyArray<any>> = (values: TValues) => R;
export type KeySelector<TValues, T> = (values: TValues) => T;
export type KeysSelector<TValues, R extends ReadonlyArray<any>> = (values: TValues) => R;
export declare const getKey: (keySelector: KeySelector<any, any> | Key<any, any, any> | string) => string;

@@ -17,0 +17,0 @@ export declare const getKeys: (keysSelector: KeysSelector<any, any[]> | [KeysSelector<any, any[]>] | Key<any, any, any>[] | string[]) => string[];

@@ -13,5 +13,5 @@ import { Paths, ValueOfPath } from "./internal/path";

export declare const setValidatorMessages: (messages: Partial<typeof validatorMessages>) => void;
export declare type PureValidator<T> = (value: T) => true | string[] | Promise<true | string[]>;
export declare type Validator<T, TValues = any> = (value: T, getValue: <P extends Paths<TValues>>(key: P) => ValueOfPath<TValues, P>) => true | string[] | Promise<true | string[]>;
declare type ValidatorParam<T> = T | ((getValue: (key: string) => any) => T);
export type PureValidator<T> = (value: T) => true | string[] | Promise<true | string[]>;
export type Validator<T, TValues = any> = (value: T, getValue: <P extends Paths<TValues>>(key: P) => ValueOfPath<TValues, P>) => true | string[] | Promise<true | string[]>;
type ValidatorParam<T> = T | ((getValue: (key: string) => any) => T);
export declare const isNumber: (message?: string) => PureValidator<any>;

@@ -33,3 +33,3 @@ export declare const isInteger: (message?: string) => PureValidator<any>;

export declare const noopValidator: PureValidator<any>;
declare type Nilable<T> = T | undefined | null;
type Nilable<T> = T | undefined | null;
export {};
{
"version": "0.3.0-beta.10",
"version": "0.3.0-beta.11",
"license": "MIT",

@@ -30,27 +30,27 @@ "main": "dist/index.js",

"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^13.5.0",
"@types/babel__core": "^7.1.16",
"@types/jest": "^27.0.2",
"@types/react": "^17.0.34",
"@types/react-dom": "^17.0.11",
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"babel-jest": "^27.3.1",
"esbuild": "^0.13.12",
"eslint": "^8.2.0",
"eslint-plugin-react": "^7.26.1",
"jest": "^27.3.1",
"prettier": "^2.4.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-test-renderer": "^17.0.2",
"tslib": "^2.3.1",
"typescript": "^4.4.4"
"@babel/core": "^7.22.1",
"@babel/preset-env": "^7.22.4",
"@babel/preset-react": "^7.22.3",
"@babel/preset-typescript": "^7.21.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/babel__core": "^7.20.1",
"@types/jest": "^29.5.2",
"@types/react": "^18.2.8",
"@types/react-dom": "^18.2.4",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"babel-jest": "^29.5.0",
"esbuild": "^0.17.19",
"eslint": "^8.42.0",
"eslint-plugin-react": "^7.32.2",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"prettier": "^2.8.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-test-renderer": "^18.2.0",
"tslib": "^2.5.3",
"typescript": "^5.1.3"
},

@@ -63,2 +63,2 @@ "dependencies": {

}
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc