@bomjan133/react-crud-ui 1.2.5 → 1.2.6
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 +369 -369
- package/bin/cli.js +387 -387
- package/docs/1_GETTING_STARTED.md +333 -333
- package/docs/2_UI_COMPONENTS.md +586 -586
- package/docs/3_CRUD_COMPONENTS.md +974 -974
- package/docs/4_FORM_COMPONENTS.md +778 -778
- package/docs/5_LAYOUT_COMPONENTS.md +402 -402
- package/docs/6_STORE_FACTORY.md +456 -456
- package/docs/7_BOOK_VIEWER.md +365 -365
- package/docs/8_TOAST_NOTIFICATIONS.md +130 -130
- package/docs/9_UTILITIES.md +204 -204
- package/docs/README.md +83 -83
- package/package.json +1 -1
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
# Toast Notifications Guide
|
|
2
|
-
|
|
3
|
-
Complete reference for the toast notification system in @
|
|
4
|
-
|
|
5
|
-
## Table of Contents
|
|
6
|
-
1. [Setup](#setup)
|
|
7
|
-
2. [useToast](#usetoast)
|
|
8
|
-
3. [ToastContainer](#toastcontainer)
|
|
9
|
-
4. [Toast Options](#toast-options)
|
|
10
|
-
5. [Validation Error Example](#validation-error-example)
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
## Setup
|
|
15
|
-
|
|
16
|
-
Toasts are backed by an internal Zustand store, so no provider is required — just mount `ToastContainer` once near the root of your app and call `useToast()` from anywhere.
|
|
17
|
-
|
|
18
|
-
```jsx
|
|
19
|
-
// src/App.jsx
|
|
20
|
-
import { ToastContainer } from '@
|
|
21
|
-
|
|
22
|
-
function App() {
|
|
23
|
-
return (
|
|
24
|
-
<>
|
|
25
|
-
{/* ...routes/layout... */}
|
|
26
|
-
<ToastContainer />
|
|
27
|
-
</>
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
`ToastContainer` renders fixed to the bottom-right of the viewport (`bottom-4 right-4`) and stacks toasts vertically.
|
|
33
|
-
|
|
34
|
-
---
|
|
35
|
-
|
|
36
|
-
## `useToast`
|
|
37
|
-
|
|
38
|
-
```jsx
|
|
39
|
-
import { useToast } from '@
|
|
40
|
-
|
|
41
|
-
function ProductForm() {
|
|
42
|
-
const { success, error, warning, info, toast, dismiss, clearAll } = useToast();
|
|
43
|
-
|
|
44
|
-
const handleCreate = async () => {
|
|
45
|
-
try {
|
|
46
|
-
await createProduct();
|
|
47
|
-
success('Product created successfully!');
|
|
48
|
-
} catch (err) {
|
|
49
|
-
error('Failed to create product', { title: 'Error' });
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return <button onClick={handleCreate}>Create</button>;
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Returned API
|
|
58
|
-
|
|
59
|
-
| Method | Signature | Description |
|
|
60
|
-
|--------|-----------|--------------|
|
|
61
|
-
| `toast` | `(options) => id` | Add a toast with a fully custom `options` object (see below) |
|
|
62
|
-
| `success` | `(message, options?) => id` | Shortcut for `toast({ type: 'success', message, ...options })` |
|
|
63
|
-
| `error` | `(message, options?) => id` | Shortcut for `toast({ type: 'error', message, ...options })` |
|
|
64
|
-
| `warning` | `(message, options?) => id` | Shortcut for `toast({ type: 'warning', message, ...options })` |
|
|
65
|
-
| `info` | `(message, options?) => id` | Shortcut for `toast({ type: 'info', message, ...options })` |
|
|
66
|
-
| `dismiss` | `(id) => void` | Remove a specific toast before its auto-dismiss timer fires |
|
|
67
|
-
| `clearAll` | `() => void` | Remove every active toast |
|
|
68
|
-
| `toasts` | `array` | Current list of active toast objects (used internally by `ToastContainer`) |
|
|
69
|
-
|
|
70
|
-
---
|
|
71
|
-
|
|
72
|
-
## `ToastContainer`
|
|
73
|
-
|
|
74
|
-
Renders every active toast. Mount it once, typically at the top level of your app (it has no props).
|
|
75
|
-
|
|
76
|
-
- Icon and color are derived automatically from `type` (`success`, `error`, `warning`, `info` — defaults to `info` for unknown types).
|
|
77
|
-
- Clicking the `×` button calls `dismiss(id)`.
|
|
78
|
-
- Toasts animate in from the right (`slide-in-from-right-full`).
|
|
79
|
-
|
|
80
|
-
```jsx
|
|
81
|
-
import { ToastContainer } from '@
|
|
82
|
-
|
|
83
|
-
<ToastContainer />
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
---
|
|
87
|
-
|
|
88
|
-
## Toast Options
|
|
89
|
-
|
|
90
|
-
Options accepted by `toast(options)` (and merged in by `success`/`error`/`warning`/`info`):
|
|
91
|
-
|
|
92
|
-
| Option | Type | Default | Description |
|
|
93
|
-
|--------|------|---------|--------------|
|
|
94
|
-
| `type` | `'success' \| 'error' \| 'warning' \| 'info'` | `'info'` | Determines icon and color |
|
|
95
|
-
| `message` | `string` | — | Main toast text |
|
|
96
|
-
| `title` | `string` | — | Optional bold heading shown above the message |
|
|
97
|
-
| `details` | `object` | — | Field-level messages, e.g. API validation errors — see below |
|
|
98
|
-
| `duration` | `number` (ms) | `5000` | Auto-dismiss delay. Pass `0` to disable auto-dismiss (toast stays until manually dismissed) |
|
|
99
|
-
|
|
100
|
-
### Example: with title
|
|
101
|
-
|
|
102
|
-
```jsx
|
|
103
|
-
success('Your changes have been saved.', { title: 'Saved' });
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Example: persistent toast
|
|
107
|
-
|
|
108
|
-
```jsx
|
|
109
|
-
info('Upload in progress — this may take a few minutes.', { duration: 0 });
|
|
110
|
-
// later, once done:
|
|
111
|
-
dismiss(id);
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
---
|
|
115
|
-
|
|
116
|
-
## Validation Error Example
|
|
117
|
-
|
|
118
|
-
`details` renders as a bulleted list of field-level messages below the main text — useful for surfacing API validation errors (each value can be a string or an array of strings):
|
|
119
|
-
|
|
120
|
-
```jsx
|
|
121
|
-
try {
|
|
122
|
-
await api.post('/products', data);
|
|
123
|
-
} catch (err) {
|
|
124
|
-
error('Please fix the errors below', {
|
|
125
|
-
title: 'Validation failed',
|
|
126
|
-
details: err.response?.data?.errors,
|
|
127
|
-
// e.g. { name: ['Name is required'], price: 'Price must be positive' }
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
```
|
|
1
|
+
# Toast Notifications Guide
|
|
2
|
+
|
|
3
|
+
Complete reference for the toast notification system in @bomjan133/react-crud-ui.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
1. [Setup](#setup)
|
|
7
|
+
2. [useToast](#usetoast)
|
|
8
|
+
3. [ToastContainer](#toastcontainer)
|
|
9
|
+
4. [Toast Options](#toast-options)
|
|
10
|
+
5. [Validation Error Example](#validation-error-example)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Toasts are backed by an internal Zustand store, so no provider is required — just mount `ToastContainer` once near the root of your app and call `useToast()` from anywhere.
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
// src/App.jsx
|
|
20
|
+
import { ToastContainer } from '@bomjan133/react-crud-ui';
|
|
21
|
+
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<>
|
|
25
|
+
{/* ...routes/layout... */}
|
|
26
|
+
<ToastContainer />
|
|
27
|
+
</>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`ToastContainer` renders fixed to the bottom-right of the viewport (`bottom-4 right-4`) and stacks toasts vertically.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## `useToast`
|
|
37
|
+
|
|
38
|
+
```jsx
|
|
39
|
+
import { useToast } from '@bomjan133/react-crud-ui';
|
|
40
|
+
|
|
41
|
+
function ProductForm() {
|
|
42
|
+
const { success, error, warning, info, toast, dismiss, clearAll } = useToast();
|
|
43
|
+
|
|
44
|
+
const handleCreate = async () => {
|
|
45
|
+
try {
|
|
46
|
+
await createProduct();
|
|
47
|
+
success('Product created successfully!');
|
|
48
|
+
} catch (err) {
|
|
49
|
+
error('Failed to create product', { title: 'Error' });
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return <button onClick={handleCreate}>Create</button>;
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Returned API
|
|
58
|
+
|
|
59
|
+
| Method | Signature | Description |
|
|
60
|
+
|--------|-----------|--------------|
|
|
61
|
+
| `toast` | `(options) => id` | Add a toast with a fully custom `options` object (see below) |
|
|
62
|
+
| `success` | `(message, options?) => id` | Shortcut for `toast({ type: 'success', message, ...options })` |
|
|
63
|
+
| `error` | `(message, options?) => id` | Shortcut for `toast({ type: 'error', message, ...options })` |
|
|
64
|
+
| `warning` | `(message, options?) => id` | Shortcut for `toast({ type: 'warning', message, ...options })` |
|
|
65
|
+
| `info` | `(message, options?) => id` | Shortcut for `toast({ type: 'info', message, ...options })` |
|
|
66
|
+
| `dismiss` | `(id) => void` | Remove a specific toast before its auto-dismiss timer fires |
|
|
67
|
+
| `clearAll` | `() => void` | Remove every active toast |
|
|
68
|
+
| `toasts` | `array` | Current list of active toast objects (used internally by `ToastContainer`) |
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## `ToastContainer`
|
|
73
|
+
|
|
74
|
+
Renders every active toast. Mount it once, typically at the top level of your app (it has no props).
|
|
75
|
+
|
|
76
|
+
- Icon and color are derived automatically from `type` (`success`, `error`, `warning`, `info` — defaults to `info` for unknown types).
|
|
77
|
+
- Clicking the `×` button calls `dismiss(id)`.
|
|
78
|
+
- Toasts animate in from the right (`slide-in-from-right-full`).
|
|
79
|
+
|
|
80
|
+
```jsx
|
|
81
|
+
import { ToastContainer } from '@bomjan133/react-crud-ui';
|
|
82
|
+
|
|
83
|
+
<ToastContainer />
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Toast Options
|
|
89
|
+
|
|
90
|
+
Options accepted by `toast(options)` (and merged in by `success`/`error`/`warning`/`info`):
|
|
91
|
+
|
|
92
|
+
| Option | Type | Default | Description |
|
|
93
|
+
|--------|------|---------|--------------|
|
|
94
|
+
| `type` | `'success' \| 'error' \| 'warning' \| 'info'` | `'info'` | Determines icon and color |
|
|
95
|
+
| `message` | `string` | — | Main toast text |
|
|
96
|
+
| `title` | `string` | — | Optional bold heading shown above the message |
|
|
97
|
+
| `details` | `object` | — | Field-level messages, e.g. API validation errors — see below |
|
|
98
|
+
| `duration` | `number` (ms) | `5000` | Auto-dismiss delay. Pass `0` to disable auto-dismiss (toast stays until manually dismissed) |
|
|
99
|
+
|
|
100
|
+
### Example: with title
|
|
101
|
+
|
|
102
|
+
```jsx
|
|
103
|
+
success('Your changes have been saved.', { title: 'Saved' });
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Example: persistent toast
|
|
107
|
+
|
|
108
|
+
```jsx
|
|
109
|
+
info('Upload in progress — this may take a few minutes.', { duration: 0 });
|
|
110
|
+
// later, once done:
|
|
111
|
+
dismiss(id);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Validation Error Example
|
|
117
|
+
|
|
118
|
+
`details` renders as a bulleted list of field-level messages below the main text — useful for surfacing API validation errors (each value can be a string or an array of strings):
|
|
119
|
+
|
|
120
|
+
```jsx
|
|
121
|
+
try {
|
|
122
|
+
await api.post('/products', data);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
error('Please fix the errors below', {
|
|
125
|
+
title: 'Validation failed',
|
|
126
|
+
details: err.response?.data?.errors,
|
|
127
|
+
// e.g. { name: ['Name is required'], price: 'Price must be positive' }
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
```
|
package/docs/9_UTILITIES.md
CHANGED
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
# Utility Functions Guide
|
|
2
|
-
|
|
3
|
-
Complete reference for the standalone utility functions in @
|
|
4
|
-
|
|
5
|
-
```jsx
|
|
6
|
-
import {
|
|
7
|
-
cn,
|
|
8
|
-
formatCurrency,
|
|
9
|
-
formatDate,
|
|
10
|
-
formatDateTime,
|
|
11
|
-
formatRelativeTime,
|
|
12
|
-
TimeStampFormat,
|
|
13
|
-
truncate,
|
|
14
|
-
capitalize,
|
|
15
|
-
slugify,
|
|
16
|
-
debounce,
|
|
17
|
-
generateId,
|
|
18
|
-
deepClone,
|
|
19
|
-
isEmpty,
|
|
20
|
-
getInitials,
|
|
21
|
-
parseQueryString,
|
|
22
|
-
buildQueryString,
|
|
23
|
-
} from '@
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Table of Contents
|
|
27
|
-
1. [cn](#cnclassvalue)
|
|
28
|
-
2. [Formatting](#formatting)
|
|
29
|
-
3. [String helpers](#string-helpers)
|
|
30
|
-
4. [Misc helpers](#misc-helpers)
|
|
31
|
-
5. [Query string helpers](#query-string-helpers)
|
|
32
|
-
|
|
33
|
-
---
|
|
34
|
-
|
|
35
|
-
## `cn(...classValue)`
|
|
36
|
-
|
|
37
|
-
Merges Tailwind class names with [`clsx`](https://github.com/lukeed/clsx) + [`tailwind-merge`](https://github.com/dcastil/tailwind-merge), so conflicting utility classes (e.g. `p-2` vs `p-4`) resolve correctly instead of both being applied.
|
|
38
|
-
|
|
39
|
-
```jsx
|
|
40
|
-
cn('px-2 py-1', isActive && 'bg-blue-500', className);
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Used internally by nearly every component in this library to accept a `className` override prop.
|
|
44
|
-
|
|
45
|
-
---
|
|
46
|
-
|
|
47
|
-
## Formatting
|
|
48
|
-
|
|
49
|
-
### `formatCurrency(amount, currency = 'NPR')`
|
|
50
|
-
|
|
51
|
-
Formats a number as currency using `Intl.NumberFormat('en-NP', ...)`.
|
|
52
|
-
|
|
53
|
-
```jsx
|
|
54
|
-
formatCurrency(1500); // "NPR 1,500.00"
|
|
55
|
-
formatCurrency(99.5, 'USD'); // "$99.50"
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### `formatDate(date, options = {})`
|
|
59
|
-
|
|
60
|
-
Formats a date as `Month Day, Year` (locale `en-US`) by default. `options` is merged into and overrides the `Intl.DateTimeFormat` options.
|
|
61
|
-
|
|
62
|
-
```jsx
|
|
63
|
-
formatDate('2026-07-12'); // "July 12, 2026"
|
|
64
|
-
formatDate('2026-07-12', { month: 'short' }); // "Jul 12, 2026"
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### `formatDateTime(date)`
|
|
68
|
-
|
|
69
|
-
Formats a date + time as `Mon Day, Year, HH:MM` (locale `en-US`, 2-digit hour/minute).
|
|
70
|
-
|
|
71
|
-
```jsx
|
|
72
|
-
formatDateTime('2026-07-12T14:30:00'); // "Jul 12, 2026, 02:30 PM"
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### `TimeStampFormat(date)`
|
|
76
|
-
|
|
77
|
-
Formats a date as `YYYY-MM-DD HH:MM:SS` (24-hour, zero-padded). Returns `'-'` for a falsy input — handy for rendering optional timestamp fields in a table without a null check at the call site.
|
|
78
|
-
|
|
79
|
-
```jsx
|
|
80
|
-
TimeStampFormat('2026-07-12T09:05:03'); // "2026-07-12 09:05:03"
|
|
81
|
-
TimeStampFormat(null); // "-"
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### `formatRelativeTime(date)`
|
|
85
|
-
|
|
86
|
-
Formats a date relative to now (`"just now"`, `"5 minutes ago"`, `"3 hours ago"`, `"2 days ago"`). Falls back to `formatDate` once the difference exceeds a week.
|
|
87
|
-
|
|
88
|
-
```jsx
|
|
89
|
-
formatRelativeTime(new Date(Date.now() - 5 * 60 * 1000)); // "5 minutes ago"
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
## String helpers
|
|
95
|
-
|
|
96
|
-
### `truncate(str, length = 100)`
|
|
97
|
-
|
|
98
|
-
Truncates a string to `length` characters, appending `...` if it was cut. Returns `''` for a falsy input.
|
|
99
|
-
|
|
100
|
-
```jsx
|
|
101
|
-
truncate('A very long product description...', 20); // "A very long product ..."
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### `capitalize(str)`
|
|
105
|
-
|
|
106
|
-
Uppercases the first character only. Returns `''` for a falsy input.
|
|
107
|
-
|
|
108
|
-
```jsx
|
|
109
|
-
capitalize('hello world'); // "Hello world"
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### `slugify(str)`
|
|
113
|
-
|
|
114
|
-
Converts a string to a URL-safe slug: lowercases, trims, strips non-word characters, and collapses whitespace/underscores/dashes into single hyphens.
|
|
115
|
-
|
|
116
|
-
```jsx
|
|
117
|
-
slugify(' Hello, World! '); // "hello-world"
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### `getInitials(name)`
|
|
121
|
-
|
|
122
|
-
Returns up to 2 uppercase initials from a space-separated name. Returns `''` for a falsy input.
|
|
123
|
-
|
|
124
|
-
```jsx
|
|
125
|
-
getInitials('Sujan Bomjan'); // "SB"
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
---
|
|
129
|
-
|
|
130
|
-
## Misc helpers
|
|
131
|
-
|
|
132
|
-
### `debounce(func, wait = 300)`
|
|
133
|
-
|
|
134
|
-
Returns a debounced version of `func` that only runs after `wait` ms of inactivity. Commonly used for search inputs.
|
|
135
|
-
|
|
136
|
-
```jsx
|
|
137
|
-
const handleSearch = debounce((value) => {
|
|
138
|
-
setSearchQuery(value);
|
|
139
|
-
}, 300);
|
|
140
|
-
|
|
141
|
-
<input onChange={(e) => handleSearch(e.target.value)} />
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### `generateId()`
|
|
145
|
-
|
|
146
|
-
Returns a short random alphanumeric string (`Math.random().toString(36)`, 7 characters). Suitable for client-only keys (e.g. list item `key`s before a real ID exists) — **not** cryptographically secure and not guaranteed unique across a large dataset.
|
|
147
|
-
|
|
148
|
-
```jsx
|
|
149
|
-
generateId(); // e.g. "f3k2p9a"
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### `deepClone(obj)`
|
|
153
|
-
|
|
154
|
-
Deep-clones a plain JSON-serializable object via `JSON.parse(JSON.stringify(obj))`. Does not preserve `Date`, `Map`, `Set`, `undefined` values, or functions — only use on plain data objects.
|
|
155
|
-
|
|
156
|
-
```jsx
|
|
157
|
-
const copy = deepClone(originalFormValues);
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### `isEmpty(obj)`
|
|
161
|
-
|
|
162
|
-
Returns `true` for `null`/`undefined`, an empty array, or an object with no own keys; `false` otherwise (including for empty strings and `0`, which are returned as-is by the fallback).
|
|
163
|
-
|
|
164
|
-
```jsx
|
|
165
|
-
isEmpty(null); // true
|
|
166
|
-
isEmpty([]); // true
|
|
167
|
-
isEmpty({}); // true
|
|
168
|
-
isEmpty({ a: 1 }); // false
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
---
|
|
172
|
-
|
|
173
|
-
## Query string helpers
|
|
174
|
-
|
|
175
|
-
### `parseQueryString(queryString)`
|
|
176
|
-
|
|
177
|
-
Parses a query string (with or without a leading `?`) into a plain object via `URLSearchParams`.
|
|
178
|
-
|
|
179
|
-
```jsx
|
|
180
|
-
parseQueryString('?page=2&search=shoes'); // { page: '2', search: 'shoes' }
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### `buildQueryString(params)`
|
|
184
|
-
|
|
185
|
-
Builds a query string from an object, skipping keys whose value is `null`, `undefined`, or `''`.
|
|
186
|
-
|
|
187
|
-
```jsx
|
|
188
|
-
buildQueryString({ page: 2, search: 'shoes', category: '' });
|
|
189
|
-
// "page=2&search=shoes"
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
Pairing the two is a common pattern for syncing table filters with the URL:
|
|
193
|
-
|
|
194
|
-
```jsx
|
|
195
|
-
import { useSearchParams } from 'react-router-dom';
|
|
196
|
-
import { buildQueryString, parseQueryString } from '@
|
|
197
|
-
|
|
198
|
-
const [searchParams, setSearchParams] = useSearchParams();
|
|
199
|
-
const filters = parseQueryString(searchParams.toString());
|
|
200
|
-
|
|
201
|
-
const applyFilters = (next) => {
|
|
202
|
-
setSearchParams(buildQueryString({ ...filters, ...next }));
|
|
203
|
-
};
|
|
204
|
-
```
|
|
1
|
+
# Utility Functions Guide
|
|
2
|
+
|
|
3
|
+
Complete reference for the standalone utility functions in @bomjan133/react-crud-ui. All of these are pure functions with no dependency on any store or component — import only what you need.
|
|
4
|
+
|
|
5
|
+
```jsx
|
|
6
|
+
import {
|
|
7
|
+
cn,
|
|
8
|
+
formatCurrency,
|
|
9
|
+
formatDate,
|
|
10
|
+
formatDateTime,
|
|
11
|
+
formatRelativeTime,
|
|
12
|
+
TimeStampFormat,
|
|
13
|
+
truncate,
|
|
14
|
+
capitalize,
|
|
15
|
+
slugify,
|
|
16
|
+
debounce,
|
|
17
|
+
generateId,
|
|
18
|
+
deepClone,
|
|
19
|
+
isEmpty,
|
|
20
|
+
getInitials,
|
|
21
|
+
parseQueryString,
|
|
22
|
+
buildQueryString,
|
|
23
|
+
} from '@bomjan133/react-crud-ui';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Table of Contents
|
|
27
|
+
1. [cn](#cnclassvalue)
|
|
28
|
+
2. [Formatting](#formatting)
|
|
29
|
+
3. [String helpers](#string-helpers)
|
|
30
|
+
4. [Misc helpers](#misc-helpers)
|
|
31
|
+
5. [Query string helpers](#query-string-helpers)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## `cn(...classValue)`
|
|
36
|
+
|
|
37
|
+
Merges Tailwind class names with [`clsx`](https://github.com/lukeed/clsx) + [`tailwind-merge`](https://github.com/dcastil/tailwind-merge), so conflicting utility classes (e.g. `p-2` vs `p-4`) resolve correctly instead of both being applied.
|
|
38
|
+
|
|
39
|
+
```jsx
|
|
40
|
+
cn('px-2 py-1', isActive && 'bg-blue-500', className);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Used internally by nearly every component in this library to accept a `className` override prop.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Formatting
|
|
48
|
+
|
|
49
|
+
### `formatCurrency(amount, currency = 'NPR')`
|
|
50
|
+
|
|
51
|
+
Formats a number as currency using `Intl.NumberFormat('en-NP', ...)`.
|
|
52
|
+
|
|
53
|
+
```jsx
|
|
54
|
+
formatCurrency(1500); // "NPR 1,500.00"
|
|
55
|
+
formatCurrency(99.5, 'USD'); // "$99.50"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `formatDate(date, options = {})`
|
|
59
|
+
|
|
60
|
+
Formats a date as `Month Day, Year` (locale `en-US`) by default. `options` is merged into and overrides the `Intl.DateTimeFormat` options.
|
|
61
|
+
|
|
62
|
+
```jsx
|
|
63
|
+
formatDate('2026-07-12'); // "July 12, 2026"
|
|
64
|
+
formatDate('2026-07-12', { month: 'short' }); // "Jul 12, 2026"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `formatDateTime(date)`
|
|
68
|
+
|
|
69
|
+
Formats a date + time as `Mon Day, Year, HH:MM` (locale `en-US`, 2-digit hour/minute).
|
|
70
|
+
|
|
71
|
+
```jsx
|
|
72
|
+
formatDateTime('2026-07-12T14:30:00'); // "Jul 12, 2026, 02:30 PM"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### `TimeStampFormat(date)`
|
|
76
|
+
|
|
77
|
+
Formats a date as `YYYY-MM-DD HH:MM:SS` (24-hour, zero-padded). Returns `'-'` for a falsy input — handy for rendering optional timestamp fields in a table without a null check at the call site.
|
|
78
|
+
|
|
79
|
+
```jsx
|
|
80
|
+
TimeStampFormat('2026-07-12T09:05:03'); // "2026-07-12 09:05:03"
|
|
81
|
+
TimeStampFormat(null); // "-"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `formatRelativeTime(date)`
|
|
85
|
+
|
|
86
|
+
Formats a date relative to now (`"just now"`, `"5 minutes ago"`, `"3 hours ago"`, `"2 days ago"`). Falls back to `formatDate` once the difference exceeds a week.
|
|
87
|
+
|
|
88
|
+
```jsx
|
|
89
|
+
formatRelativeTime(new Date(Date.now() - 5 * 60 * 1000)); // "5 minutes ago"
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## String helpers
|
|
95
|
+
|
|
96
|
+
### `truncate(str, length = 100)`
|
|
97
|
+
|
|
98
|
+
Truncates a string to `length` characters, appending `...` if it was cut. Returns `''` for a falsy input.
|
|
99
|
+
|
|
100
|
+
```jsx
|
|
101
|
+
truncate('A very long product description...', 20); // "A very long product ..."
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### `capitalize(str)`
|
|
105
|
+
|
|
106
|
+
Uppercases the first character only. Returns `''` for a falsy input.
|
|
107
|
+
|
|
108
|
+
```jsx
|
|
109
|
+
capitalize('hello world'); // "Hello world"
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `slugify(str)`
|
|
113
|
+
|
|
114
|
+
Converts a string to a URL-safe slug: lowercases, trims, strips non-word characters, and collapses whitespace/underscores/dashes into single hyphens.
|
|
115
|
+
|
|
116
|
+
```jsx
|
|
117
|
+
slugify(' Hello, World! '); // "hello-world"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `getInitials(name)`
|
|
121
|
+
|
|
122
|
+
Returns up to 2 uppercase initials from a space-separated name. Returns `''` for a falsy input.
|
|
123
|
+
|
|
124
|
+
```jsx
|
|
125
|
+
getInitials('Sujan Bomjan'); // "SB"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Misc helpers
|
|
131
|
+
|
|
132
|
+
### `debounce(func, wait = 300)`
|
|
133
|
+
|
|
134
|
+
Returns a debounced version of `func` that only runs after `wait` ms of inactivity. Commonly used for search inputs.
|
|
135
|
+
|
|
136
|
+
```jsx
|
|
137
|
+
const handleSearch = debounce((value) => {
|
|
138
|
+
setSearchQuery(value);
|
|
139
|
+
}, 300);
|
|
140
|
+
|
|
141
|
+
<input onChange={(e) => handleSearch(e.target.value)} />
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `generateId()`
|
|
145
|
+
|
|
146
|
+
Returns a short random alphanumeric string (`Math.random().toString(36)`, 7 characters). Suitable for client-only keys (e.g. list item `key`s before a real ID exists) — **not** cryptographically secure and not guaranteed unique across a large dataset.
|
|
147
|
+
|
|
148
|
+
```jsx
|
|
149
|
+
generateId(); // e.g. "f3k2p9a"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `deepClone(obj)`
|
|
153
|
+
|
|
154
|
+
Deep-clones a plain JSON-serializable object via `JSON.parse(JSON.stringify(obj))`. Does not preserve `Date`, `Map`, `Set`, `undefined` values, or functions — only use on plain data objects.
|
|
155
|
+
|
|
156
|
+
```jsx
|
|
157
|
+
const copy = deepClone(originalFormValues);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### `isEmpty(obj)`
|
|
161
|
+
|
|
162
|
+
Returns `true` for `null`/`undefined`, an empty array, or an object with no own keys; `false` otherwise (including for empty strings and `0`, which are returned as-is by the fallback).
|
|
163
|
+
|
|
164
|
+
```jsx
|
|
165
|
+
isEmpty(null); // true
|
|
166
|
+
isEmpty([]); // true
|
|
167
|
+
isEmpty({}); // true
|
|
168
|
+
isEmpty({ a: 1 }); // false
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Query string helpers
|
|
174
|
+
|
|
175
|
+
### `parseQueryString(queryString)`
|
|
176
|
+
|
|
177
|
+
Parses a query string (with or without a leading `?`) into a plain object via `URLSearchParams`.
|
|
178
|
+
|
|
179
|
+
```jsx
|
|
180
|
+
parseQueryString('?page=2&search=shoes'); // { page: '2', search: 'shoes' }
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### `buildQueryString(params)`
|
|
184
|
+
|
|
185
|
+
Builds a query string from an object, skipping keys whose value is `null`, `undefined`, or `''`.
|
|
186
|
+
|
|
187
|
+
```jsx
|
|
188
|
+
buildQueryString({ page: 2, search: 'shoes', category: '' });
|
|
189
|
+
// "page=2&search=shoes"
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Pairing the two is a common pattern for syncing table filters with the URL:
|
|
193
|
+
|
|
194
|
+
```jsx
|
|
195
|
+
import { useSearchParams } from 'react-router-dom';
|
|
196
|
+
import { buildQueryString, parseQueryString } from '@bomjan133/react-crud-ui';
|
|
197
|
+
|
|
198
|
+
const [searchParams, setSearchParams] = useSearchParams();
|
|
199
|
+
const filters = parseQueryString(searchParams.toString());
|
|
200
|
+
|
|
201
|
+
const applyFilters = (next) => {
|
|
202
|
+
setSearchParams(buildQueryString({ ...filters, ...next }));
|
|
203
|
+
};
|
|
204
|
+
```
|