react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
120 lines • 3.29 kB
TypeScript
import type { Post } from '../http/Post';
import type { Action } from '../action/Action';
/**
* Value entry tracked by Formula
*/
export interface FormulaValue<T = unknown> {
/** Current value */
value: T;
/** Reset callback to clear the field */
reset?: () => void;
}
/**
* Callbacks for Formula lifecycle
*/
export interface FormulaCallbacks<R = unknown> {
/** Called when submission completes successfully */
onComplete?: (result: R) => void;
/** Called when submission fails */
onError?: (error: Error) => void;
/** Called after submission finishes (success or error) */
onFinally?: () => void;
}
/**
* Formula - Form value collection and submission manager
*
* Collects values from form controls registered via `value()` method,
* and submits them via a Post model when `submit()` is called.
*
* @typeParam T - Type of the collected form data
* @typeParam R - Type of the response from Post
*
* @example
* ```typescript
* const formula = new Formula<UserInput, User>(
* new Post<UserInput, User>()
* .target('/api/users')
* .onThen((user) => console.log('Created:', user.id))
* );
*
* // Register values from form controls
* formula.value('name', 'John');
* formula.value('email', 'john@example.com');
*
* // Get all collected values
* const data = formula.params(); // { name: 'John', email: 'john@example.com' }
*
* // Submit the form
* await formula.submit();
* ```
*/
export declare class Formula<T extends Record<string, unknown> = Record<string, unknown>, R = unknown> {
private _values;
private _post;
private _action;
private _callbacks;
private _isSubmitting;
constructor(post: Post<T, R>);
/**
* Register or update a value in the formula
* @param key - Field key
* @param value - Field value
* @param reset - Optional reset callback
*/
value<K extends keyof T>(key: K, value: T[K], reset?: () => void): this;
/**
* Get a single value by key
*/
get<K extends keyof T>(key: K): T[K] | undefined;
/**
* Check if a value is registered
*/
has(key: keyof T): boolean;
/**
* Remove a value from the formula
*/
remove(key: keyof T): this;
/**
* Get all collected values as an object
*/
params(): Partial<T>;
/**
* Reset all registered values
*/
reset(): this;
/**
* Link an action to this formula
*/
action(action: Action): this;
/**
* Get the linked action
*/
getAction(): Action | null;
/**
* Get the Post model
*/
getPost(): Post<T, R>;
/**
* Set callback for successful completion
*/
onComplete(callback: (result: R) => void): this;
/**
* Set callback for errors
*/
onError(callback: (error: Error) => void): this;
/**
* Set callback for finally (always runs)
*/
onFinally(callback: () => void): this;
/**
* Check if formula is currently submitting
*/
isSubmitting(): boolean;
/**
* Submit the formula via Post
* @param additionalData - Additional data to merge with form values
*/
submit(additionalData?: Partial<T>): Promise<R | undefined>;
}
export default Formula;
//# sourceMappingURL=Formula.d.ts.map