@kform/react
Version:
React integration for KForm.
35 lines (34 loc) • 2.58 kB
TypeScript
import { Path } from "@kform/core";
import { Numeric } from "../utils/numericUtils";
import { DistributedOmit } from "../utils/typeUtils";
import { Controller } from "./useController";
import { FormatterControllerState } from "./useFormatter";
import { InputOptions, InputOwnController } from "./useInput";
/** Options available to the {@link useNumericInput} hook. */
export type NumericInputOptions<T extends Numeric | null = Numeric | null, TFormatted = string, TInput = HTMLInputElement> = DistributedOmit<InputOptions<T, TFormatted, TInput>, "format" | "parse"> & NumericInputOwnOptions<T, TFormatted, TInput>;
/** Own options available to the {@link useNumericInput} hook. */
export interface NumericInputOwnOptions<T extends Numeric | null = Numeric | null, TFormatted = string, TInput = HTMLInputElement> {
formatFromString?: (stringValue: string, state: FormatterControllerState<T, TFormatted>, input: TInput | null) => TFormatted;
parseToString?: (formattedValue: TFormatted, state: FormatterControllerState<T, TFormatted>, input: TInput | null) => string;
}
/**
* Controller used to read and control the form value associated with the form
* input, exposing properties that should be set on the input itself.
*/
export type NumericInputController<T extends Numeric | null = Numeric | null, TFormatted = string, TInput = HTMLInputElement> = Controller<T> & NumericInputOwnController<TFormatted, TInput>;
/** Own numeric input controller. */
export type NumericInputOwnController<TFormatted = string, TInput = HTMLInputElement> = InputOwnController<TFormatted, TInput>;
/**
* Hook controlling the integration between a numeric value of the form manager
* and an input of the form.
*
* @param path Path of the numeric form value to access, relative to the current
* path.
* @param options Available options.
* @returns A controller used to read and control the numeric form value
* associated with the form input, exposing properties that should be set on
* the input itself.
* @throws {InvalidPathError} When the schema at {@link path} is not numeric.
*/
export declare function useNumericInput<T extends Numeric | null = Numeric | null, TFormatted = string, TInput = HTMLInputElement>(path?: Path | string, options?: undefined): NumericInputController<T, TFormatted, TInput>;
export declare function useNumericInput<T extends Numeric | null = Numeric | null, TFormatted = string, TInput = HTMLInputElement>(path: Path | string | undefined, options: NumericInputOptions<T, TFormatted, TInput>): NumericInputController<T, TFormatted, TInput>;