@usefy/use-counter 0.0.16 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ This package requires React 18 or 19:
|
|
|
80
80
|
## Quick Start
|
|
81
81
|
|
|
82
82
|
```tsx
|
|
83
|
-
import { useCounter } from
|
|
83
|
+
import { useCounter } from "@usefy/use-counter";
|
|
84
84
|
|
|
85
85
|
function Counter() {
|
|
86
86
|
const { count, increment, decrement, reset } = useCounter(0);
|
|
@@ -106,18 +106,18 @@ A hook that manages counter state with increment, decrement, and reset capabilit
|
|
|
106
106
|
|
|
107
107
|
#### Parameters
|
|
108
108
|
|
|
109
|
-
| Parameter
|
|
110
|
-
|
|
111
|
-
| `initialValue` | `number` | `0`
|
|
109
|
+
| Parameter | Type | Default | Description |
|
|
110
|
+
| -------------- | -------- | ------- | -------------------------------- |
|
|
111
|
+
| `initialValue` | `number` | `0` | The initial value of the counter |
|
|
112
112
|
|
|
113
113
|
#### Returns
|
|
114
114
|
|
|
115
|
-
| Property
|
|
116
|
-
|
|
117
|
-
| `count`
|
|
118
|
-
| `increment` | `() => void` | Increases the counter by 1
|
|
119
|
-
| `decrement` | `() => void` | Decreases the counter by 1
|
|
120
|
-
| `reset`
|
|
115
|
+
| Property | Type | Description |
|
|
116
|
+
| ----------- | ------------ | --------------------------------------- |
|
|
117
|
+
| `count` | `number` | The current counter value |
|
|
118
|
+
| `increment` | `() => void` | Increases the counter by 1 |
|
|
119
|
+
| `decrement` | `() => void` | Decreases the counter by 1 |
|
|
120
|
+
| `reset` | `() => void` | Resets the counter to the initial value |
|
|
121
121
|
|
|
122
122
|
---
|
|
123
123
|
|
|
@@ -126,7 +126,7 @@ A hook that manages counter state with increment, decrement, and reset capabilit
|
|
|
126
126
|
### Basic Counter
|
|
127
127
|
|
|
128
128
|
```tsx
|
|
129
|
-
import { useCounter } from
|
|
129
|
+
import { useCounter } from "@usefy/use-counter";
|
|
130
130
|
|
|
131
131
|
function BasicCounter() {
|
|
132
132
|
const { count, increment, decrement, reset } = useCounter();
|
|
@@ -135,9 +135,15 @@ function BasicCounter() {
|
|
|
135
135
|
<div className="counter">
|
|
136
136
|
<span className="count">{count}</span>
|
|
137
137
|
<div className="controls">
|
|
138
|
-
<button onClick={decrement} aria-label="Decrease"
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
<button onClick={decrement} aria-label="Decrease">
|
|
139
|
+
−
|
|
140
|
+
</button>
|
|
141
|
+
<button onClick={increment} aria-label="Increase">
|
|
142
|
+
+
|
|
143
|
+
</button>
|
|
144
|
+
<button onClick={reset} aria-label="Reset">
|
|
145
|
+
Reset
|
|
146
|
+
</button>
|
|
141
147
|
</div>
|
|
142
148
|
</div>
|
|
143
149
|
);
|
|
@@ -147,7 +153,7 @@ function BasicCounter() {
|
|
|
147
153
|
### Starting with a Custom Value
|
|
148
154
|
|
|
149
155
|
```tsx
|
|
150
|
-
import { useCounter } from
|
|
156
|
+
import { useCounter } from "@usefy/use-counter";
|
|
151
157
|
|
|
152
158
|
function QuantitySelector() {
|
|
153
159
|
const { count, increment, decrement } = useCounter(1);
|
|
@@ -162,10 +168,7 @@ function QuantitySelector() {
|
|
|
162
168
|
−
|
|
163
169
|
</button>
|
|
164
170
|
<span aria-label="Quantity">{count}</span>
|
|
165
|
-
<button
|
|
166
|
-
onClick={increment}
|
|
167
|
-
aria-label="Increase quantity"
|
|
168
|
-
>
|
|
171
|
+
<button onClick={increment} aria-label="Increase quantity">
|
|
169
172
|
+
|
|
170
173
|
</button>
|
|
171
174
|
</div>
|
|
@@ -176,24 +179,25 @@ function QuantitySelector() {
|
|
|
176
179
|
### Pagination Control
|
|
177
180
|
|
|
178
181
|
```tsx
|
|
179
|
-
import { useCounter } from
|
|
182
|
+
import { useCounter } from "@usefy/use-counter";
|
|
180
183
|
|
|
181
184
|
function Pagination({ totalPages }: { totalPages: number }) {
|
|
182
|
-
const {
|
|
185
|
+
const {
|
|
186
|
+
count: currentPage,
|
|
187
|
+
increment: nextPage,
|
|
188
|
+
decrement: prevPage,
|
|
189
|
+
reset,
|
|
190
|
+
} = useCounter(1);
|
|
183
191
|
|
|
184
192
|
return (
|
|
185
193
|
<nav className="pagination" aria-label="Pagination">
|
|
186
|
-
<button
|
|
187
|
-
onClick={prevPage}
|
|
188
|
-
disabled={currentPage <= 1}
|
|
189
|
-
>
|
|
194
|
+
<button onClick={prevPage} disabled={currentPage <= 1}>
|
|
190
195
|
Previous
|
|
191
196
|
</button>
|
|
192
|
-
<span>
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
>
|
|
197
|
+
<span>
|
|
198
|
+
Page {currentPage} of {totalPages}
|
|
199
|
+
</span>
|
|
200
|
+
<button onClick={nextPage} disabled={currentPage >= totalPages}>
|
|
197
201
|
Next
|
|
198
202
|
</button>
|
|
199
203
|
<button onClick={reset}>First Page</button>
|
|
@@ -205,7 +209,7 @@ function Pagination({ totalPages }: { totalPages: number }) {
|
|
|
205
209
|
### Multiple Independent Counters
|
|
206
210
|
|
|
207
211
|
```tsx
|
|
208
|
-
import { useCounter } from
|
|
212
|
+
import { useCounter } from "@usefy/use-counter";
|
|
209
213
|
|
|
210
214
|
function ScoreBoard() {
|
|
211
215
|
const teamA = useCounter(0);
|
|
@@ -237,7 +241,7 @@ function ScoreBoard() {
|
|
|
237
241
|
### With Negative Values
|
|
238
242
|
|
|
239
243
|
```tsx
|
|
240
|
-
import { useCounter } from
|
|
244
|
+
import { useCounter } from "@usefy/use-counter";
|
|
241
245
|
|
|
242
246
|
function TemperatureAdjuster() {
|
|
243
247
|
const { count: temperature, increment, decrement, reset } = useCounter(-10);
|
|
@@ -260,7 +264,7 @@ function TemperatureAdjuster() {
|
|
|
260
264
|
This hook is written in TypeScript and provides full type inference out of the box.
|
|
261
265
|
|
|
262
266
|
```tsx
|
|
263
|
-
import { useCounter } from
|
|
267
|
+
import { useCounter } from "@usefy/use-counter";
|
|
264
268
|
|
|
265
269
|
// Return type is automatically inferred
|
|
266
270
|
const { count, increment, decrement, reset } = useCounter(0);
|
|
@@ -294,12 +298,12 @@ This package maintains comprehensive test coverage to ensure reliability and sta
|
|
|
294
298
|
|
|
295
299
|
### Test Coverage
|
|
296
300
|
|
|
297
|
-
| Category
|
|
298
|
-
|
|
301
|
+
| Category | Coverage |
|
|
302
|
+
| ---------- | ------------ |
|
|
299
303
|
| Statements | 100% (10/10) |
|
|
300
|
-
| Branches
|
|
301
|
-
| Functions
|
|
302
|
-
| Lines
|
|
304
|
+
| Branches | 100% (1/1) |
|
|
305
|
+
| Functions | 100% (6/6) |
|
|
306
|
+
| Lines | 100% (8/8) |
|
|
303
307
|
|
|
304
308
|
### Test Categories
|
|
305
309
|
|
|
@@ -383,14 +387,14 @@ pnpm test --coverage
|
|
|
383
387
|
|
|
384
388
|
Explore other hooks in the **@usefy** collection:
|
|
385
389
|
|
|
386
|
-
| Package
|
|
387
|
-
|
|
388
|
-
| [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle)
|
|
389
|
-
| [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce)
|
|
390
|
-
| [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks
|
|
391
|
-
| [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle)
|
|
392
|
-
| [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks
|
|
393
|
-
| [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where)
|
|
390
|
+
| Package | Description |
|
|
391
|
+
| ------------------------------------------------------------------------------------------ | ------------------------ |
|
|
392
|
+
| [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle) | Boolean state management |
|
|
393
|
+
| [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
|
|
394
|
+
| [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks |
|
|
395
|
+
| [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
|
|
396
|
+
| [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks |
|
|
397
|
+
| [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
|
|
394
398
|
|
|
395
399
|
---
|
|
396
400
|
|