@homebound/form-state
Advanced tools
Comparing version 2.16.0 to 2.17.0
@@ -23,3 +23,3 @@ import { Fragment, ObjectState } from "./fields/objectField"; | ||
type: "value"; | ||
rules?: Rule<T, V | null | undefined>[]; | ||
rules?: Rule<V | null | undefined>[]; | ||
/** | ||
@@ -52,3 +52,3 @@ * If true, marks this field as the id, which will be used for things like "always include in changedValue". | ||
/** Rules that can run on the full list of children. */ | ||
rules?: Rule<T, readonly ObjectState<U>[]>[]; | ||
rules?: Rule<readonly ObjectState<U>[]>[]; | ||
/** Config for each child's form state, i.e. each book. */ | ||
@@ -55,0 +55,0 @@ config: ObjectConfig<U>; |
@@ -6,3 +6,3 @@ import { ListFieldConfig, ObjectConfig } from "../config"; | ||
/** Form state for list of children, i.e. `U` is a `Book` in a form with a `books: Book[]`. */ | ||
export interface ListFieldState<T, U> extends Omit<FieldState<T, U[]>, "originalValue"> { | ||
export interface ListFieldState<T, U> extends Omit<FieldState<U[]>, "originalValue"> { | ||
readonly rows: ReadonlyArray<ObjectState<U>>; | ||
@@ -12,2 +12,2 @@ add(value: U, index?: number): void; | ||
} | ||
export declare function newListFieldState<T, K extends keyof T, U>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<T, readonly ObjectState<U>[]>[], listConfig: ListFieldConfig<T, U>, config: ObjectConfig<U>, maybeAutoSave: () => void): ListFieldState<T, U>; | ||
export declare function newListFieldState<T, K extends keyof T, U>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<readonly ObjectState<U>[]>[], listConfig: ListFieldConfig<T, U>, config: ObjectConfig<U>, maybeAutoSave: () => void): ListFieldState<T, U>; |
@@ -31,3 +31,3 @@ import { ObjectConfig } from "../config"; | ||
*/ | ||
export declare type ObjectState<T, P = any> = FieldStates<T> & FieldState<P, T> & { | ||
export declare type ObjectState<T, P = any> = FieldStates<T> & FieldState<T> & { | ||
/** Sets the state of fields in `state`. */ | ||
@@ -48,3 +48,3 @@ set(state: Partial<T>, opts?: SetOpts): void; | ||
declare type FieldStates<T> = { | ||
[K in keyof T]-?: T[K] extends Fragment<infer V> ? FragmentField<V> : T[K] extends Array<infer U> | null | undefined ? [U] extends [Builtin] ? FieldState<T, T[K]> : ListFieldState<T, U> : T[K] extends Builtin | null | undefined ? FieldState<T, T[K]> : ObjectState<T[K], T>; | ||
[K in keyof T]-?: T[K] extends Fragment<infer V> ? FragmentField<V> : T[K] extends Array<infer U> | null | undefined ? [U] extends [Builtin] ? FieldState<T[K]> : ListFieldState<T, U> : T[K] extends Builtin | null | undefined ? FieldState<T[K]> : ObjectState<T[K], T>; | ||
}; | ||
@@ -61,3 +61,3 @@ /** | ||
}): ObjectState<T>; | ||
export declare function newObjectState<T, P = any>(config: ObjectConfig<T>, parentState: (() => ObjectState<P>) | undefined, parentListState: FieldState<any, any> | undefined, instance: T, key: keyof T | undefined, maybeAutoSave: () => void): ObjectState<T, P>; | ||
export declare function newObjectState<T, P = any>(config: ObjectConfig<T>, parentState: (() => ObjectState<P>) | undefined, parentListState: FieldState<any> | undefined, instance: T, key: keyof T | undefined, maybeAutoSave: () => void): ObjectState<T, P>; | ||
export {}; |
@@ -13,3 +13,3 @@ import { ObjectState } from "./objectField"; | ||
*/ | ||
export interface FieldState<T, V> { | ||
export interface FieldState<V> { | ||
readonly key: string; | ||
@@ -25,3 +25,3 @@ value: V; | ||
readonly isNewEntity: boolean; | ||
rules: Rule<T, V>[]; | ||
rules: Rule<V>[]; | ||
readonly errors: string[]; | ||
@@ -54,3 +54,3 @@ /** Returns a subset of V with only the changed values. Currently not observable. */ | ||
} | ||
export interface FieldStateInternal<T, V> extends FieldState<T, V> { | ||
export interface FieldStateInternal<T, V> extends FieldState<V> { | ||
set(value: V, opts?: InternalSetOpts): void; | ||
@@ -62,2 +62,2 @@ _isIdKey: boolean; | ||
} | ||
export declare function newValueFieldState<T, K extends keyof T>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<T, T[K] | null | undefined>[], isIdKey: boolean, isDeleteKey: boolean, isReadOnlyKey: boolean, computed: boolean, readOnly: boolean, strictOrder: boolean, maybeAutoSave: () => void): FieldState<T, T[K] | null | undefined>; | ||
export declare function newValueFieldState<T, K extends keyof T>(parentInstance: T, parentState: () => ObjectState<T>, key: K, rules: Rule<T[K] | null | undefined>[], isIdKey: boolean, isDeleteKey: boolean, isReadOnlyKey: boolean, computed: boolean, readOnly: boolean, strictOrder: boolean, maybeAutoSave: () => void): FieldState<T[K] | null | undefined>; |
@@ -944,20 +944,2 @@ "use strict"; | ||
}); | ||
it("lets rules validate against other fields", () => { | ||
const formState = objectField_1.createObjectState({ | ||
firstName: { type: "value", rules: [] }, | ||
lastName: { | ||
type: "value", | ||
rules: [ | ||
({ object }) => { | ||
if (object.firstName.value === object.lastName.value) { | ||
return "Must not match first name"; | ||
} | ||
}, | ||
], | ||
}, | ||
}, {}); | ||
formState.firstName.value = "bob"; | ||
formState.lastName.value = "bob"; | ||
expect(formState.lastName.errors).toEqual(["Must not match first name"]); | ||
}); | ||
it("can return only changed primitive fields", () => { | ||
@@ -964,0 +946,0 @@ // Given an author |
@@ -5,3 +5,3 @@ /// <reference types="react" /> | ||
export declare function TextField(props: { | ||
field: FieldState<any, string | null | undefined>; | ||
field: FieldState<string | null | undefined>; | ||
}): JSX.Element; |
@@ -1,9 +0,6 @@ | ||
import { ObjectState } from "./fields/objectField"; | ||
declare type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N; | ||
/** A validation rule, given the value and name, return the error string if valid, or undefined if valid. */ | ||
export declare type Rule<T, V> = (opts: { | ||
export declare type Rule<V> = (opts: { | ||
value: V; | ||
key: string; | ||
originalValue: V; | ||
object: IfAny<T, any, ObjectState<T>>; | ||
}) => string | undefined; | ||
@@ -14,2 +11,1 @@ /** A rule that validates `value` is not `undefined`, `null`, or empty string. */ | ||
}) => string | undefined; | ||
export {}; |
@@ -264,13 +264,13 @@ "use strict"; | ||
// Given a component | ||
// And it's using a class/mobx proxy as the basis for the data | ||
class AuthorRow { | ||
constructor(firstName, lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
get fullName() { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
function TestComponent() { | ||
// And it's using a class/mobx proxy as the basis for the data | ||
class AuthorRow { | ||
constructor(firstName, lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
get fullName() { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
// And we have two sets of data | ||
@@ -277,0 +277,0 @@ const data1 = { firstName: "f1", lastName: "l1" }; |
@@ -42,3 +42,3 @@ "use strict"; | ||
// Memoize an original for comparing the update against. | ||
const originalState = react_1.useMemo(() => getFormState(apiData), []); | ||
const originalState = react_1.useMemo(() => getFormState(apiData), [getFormState]); | ||
const state = getFormState(apiData); | ||
@@ -128,2 +128,3 @@ return (jsx_runtime_1.jsxs("div", { children: [jsx_runtime_1.jsx("div", Object.assign({ "data-testid": "firstName" }, { children: state.firstName.value }), void 0), | ||
// Memoize an original for comparing the update against. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const originalState = react_1.useMemo(() => getFormState(apiData), []); | ||
@@ -130,0 +131,0 @@ const state = getFormState(apiData); |
{ | ||
"name": "@homebound/form-state", | ||
"version": "2.16.0", | ||
"version": "2.17.0", | ||
"author": "Homebound", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
214884
4277