UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

25 lines (24 loc) 1.19 kB
/** * @name useEnum * @description - Hook that manages state constrained to the values of a given enum object. * Allows cycling through enum values. * * @template T The enum object type. * @param {T} enumObject The enum object itself (e.g., MyEnum). * @param {T[keyof T]} initialValue The initial enum value. * @returns {[T[keyof T], { set: (value: T[keyof T]) => void; next: () => void; prev: () => void; }]} A tuple containing the current enum value and an object with functions to set, go to the next, or go to the previous enum value. * @throws If the initialValue is not a valid value within the enumObject. * * @example * enum Direction { North, East, South, West } * const [direction, { set: setDirection, next, prev }] = useEnum(Direction, Direction.North); * * next(); // direction becomes Direction.East * prev(); // direction becomes Direction.North again * setDirection(Direction.South); // direction becomes Direction.South */ export declare const useEnum: <T extends Record<string | number, string | number>>(enumObject: T, initialValue: T[keyof T]) => [T[keyof T], { set: (value: T[keyof T]) => void; next: () => void; prev: () => void; }];