1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

feat(packages/vue): update useCounter composable to include additional functionality

This commit is contained in:
2024-05-31 01:17:35 +07:00
parent d9973af2ed
commit 93065d46ca
2 changed files with 16 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
import { it, expect, describe } from 'vitest';
import { useCounter } from '.';
import { ref } from 'vue';
import { useCounter } from '.';
describe('useCounter', () => {
it('initialize count with the provided initial value', () => {

View File

@@ -1,4 +1,4 @@
import { ref, unref, type MaybeRef } from 'vue';
import { ref, unref, type MaybeRef, type Ref } from 'vue';
import { clamp } from '@robonen/stdlib';
export interface UseCounterOptions {
@@ -6,6 +6,15 @@ export interface UseCounterOptions {
max?: number;
}
export interface UseConterReturn {
count: Ref<number>;
increment: (delta?: number) => void;
decrement: (delta?: number) => void;
set: (value: number) => void;
get: () => number;
reset: (value?: number) => void;
}
/**
* @name useCounter
* @category Utilities
@@ -15,6 +24,7 @@ export interface UseCounterOptions {
* @param {UseCounterOptions} [options={}] The options for the counter
* @param {number} [options.min=Number.MIN_SAFE_INTEGER] The minimum value of the counter
* @param {number} [options.max=Number.MAX_SAFE_INTEGER] The maximum value of the counter
* @returns {UseConterReturn} The counter object
*
* @example
* const { count, increment } = useCounter(0);
@@ -22,7 +32,10 @@ export interface UseCounterOptions {
* @example
* const { count, increment, decrement, set, get, reset } = useCounter(0, { min: 0, max: 10 });
*/
export function useCounter(initialValue: MaybeRef<number> = 0, options: UseCounterOptions = {}) {
export function useCounter(
initialValue: MaybeRef<number> = 0,
options: UseCounterOptions = {},
): UseConterReturn {
let _initialValue = unref(initialValue);
const count = ref(initialValue);