tracked-instance
Version:
Build large forms and track all changes
28 lines (27 loc) • 1.46 kB
TypeScript
import { ComputedRef, Ref } from 'vue';
import { DeepPartial } from './utils';
export interface TrackedInstance<Data> {
/** Reactive reference to the current (possibly modified) data. */
data: Ref<Data>;
/** True when at least one field differs from the value at the last loadData() call. */
isDirty: ComputedRef<boolean>;
/** Partial object containing only the fields that have changed since the last loadData(). */
changedData: ComputedRef<DeepPartial<Data>>;
/** Replaces the current data and clears the dirty state. The new value becomes the new baseline. */
loadData: (newData: Data) => void;
/** Reverts all changes, restoring data to the state at the last loadData() call. */
reset: () => void;
}
export interface TrackedInstanceOptions {
/**
* Custom equality function for comparing primitive values.
* When provided, replaces the default strict equality (===) check.
* Called only for primitive leaf values (strings, numbers, booleans, null, undefined).
*
* @example treat null and empty string as equal
* equals: (a, b) => (a ?? '') === (b ?? '')
*/
equals?: (a: unknown, b: unknown) => boolean;
}
export declare function useTrackedInstance<Data = any>(value?: undefined, options?: TrackedInstanceOptions): TrackedInstance<Data | undefined>;
export declare function useTrackedInstance<Data>(value: Data, options?: TrackedInstanceOptions): TrackedInstance<Data>;