@mits_pl/use-inertia-filters 1.0.0

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 ADDED
@@ -0,0 +1,217 @@
1
+ # @mits/use-inertia-filters
2
+
3
+ > Headless Vue 3 composable for managing filter state with Inertia.js — debounced, URL-synced, TypeScript-first.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@mits/use-inertia-filters.svg)](https://www.npmjs.com/package/@mits/use-inertia-filters)
6
+ [![license](https://img.shields.io/npm/l/@mits/use-inertia-filters.svg)](./LICENSE)
7
+
8
+ Built and maintained by [MITS](https://mits.pl) — a software house specializing in Laravel + Vue + Inertia.
9
+
10
+ ---
11
+
12
+ ## The problem
13
+
14
+ Every Laravel + Inertia app has the same boilerplate: a list page with a search input, a status dropdown, a per-page selector. Every time you need to:
15
+
16
+ 1. Keep filter state in sync with the URL query string
17
+ 2. Debounce the search input so you don't hammer the server
18
+ 3. Fire immediately for sort/per_page changes
19
+ 4. Preserve Vue component state so the page doesn't flicker
20
+ 5. Reset filters back to defaults
21
+
22
+ This composable does all of that in **one line**.
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @mits/use-inertia-filters
30
+ # or
31
+ yarn add @mits/use-inertia-filters
32
+ # or
33
+ pnpm add @mits/use-inertia-filters
34
+ ```
35
+
36
+ **Peer dependencies:** `vue >= 3.3`, `@inertiajs/vue3 >= 1.0`
37
+
38
+ ---
39
+
40
+ ## Quick start
41
+
42
+ ```vue
43
+ <script setup lang="ts">
44
+ import { useInertiaFilters } from '@mits/use-inertia-filters'
45
+
46
+ const { filters, reset, isDirty } = useInertiaFilters({
47
+ search: '',
48
+ status: null,
49
+ perPage: 15,
50
+ })
51
+ </script>
52
+
53
+ <template>
54
+ <div>
55
+ <input v-model="filters.search" placeholder="Search..." />
56
+
57
+ <select v-model="filters.status">
58
+ <option :value="null">All</option>
59
+ <option value="active">Active</option>
60
+ <option value="inactive">Inactive</option>
61
+ </select>
62
+
63
+ <select v-model="filters.perPage">
64
+ <option :value="15">15</option>
65
+ <option :value="25">25</option>
66
+ <option :value="50">50</option>
67
+ </select>
68
+
69
+ <button v-if="isDirty()" @click="reset">Clear filters</button>
70
+ </div>
71
+ </template>
72
+ ```
73
+
74
+ That's it. Changes are debounced (300ms by default), the URL is updated, and the page re-renders with fresh data from the server.
75
+
76
+ ---
77
+
78
+ ## How it works
79
+
80
+ 1. On mount, the composable reads the current URL query string and hydrates the filter state (with type casting based on initial values).
81
+ 2. When any filter changes, a debounced `router.get()` is called with only the non-default, non-empty values in the query string — keeping the URL clean.
82
+ 3. The visit uses `preserveState: true` and `replace: true` by default, so the page updates without a flicker and without polluting browser history.
83
+
84
+ ---
85
+
86
+ ## API
87
+
88
+ ### `useInertiaFilters(initialFilters, options?)`
89
+
90
+ #### `initialFilters`
91
+
92
+ A plain object defining your filter keys and their default values. Types are inferred from the initial values and used for URL hydration casting.
93
+
94
+ ```ts
95
+ useInertiaFilters({
96
+ search: '', // string
97
+ status: null, // null | string
98
+ page: 1, // number — cast from URL string automatically
99
+ archived: false, // boolean — cast from URL string automatically
100
+ })
101
+ ```
102
+
103
+ #### `options`
104
+
105
+ | Option | Type | Default | Description |
106
+ |---|---|---|---|
107
+ | `debounce` | `number` | `300` | Debounce delay in ms |
108
+ | `preserveState` | `boolean` | `true` | Preserve Vue component state |
109
+ | `preserveScroll` | `boolean` | `true` | Preserve scroll position |
110
+ | `replace` | `boolean` | `true` | Replace history entry (no back-button spam) |
111
+ | `debounceOnly` | `string[]` | `undefined` | Only debounce these keys; all others fire immediately |
112
+ | `onBeforeVisit` | `(filters) => boolean \| void` | — | Return `false` to cancel the visit |
113
+ | `onAfterVisit` | `(filters) => void` | — | Called after each visit |
114
+
115
+ #### Return value
116
+
117
+ | Property | Type | Description |
118
+ |---|---|---|
119
+ | `filters` | `Reactive<T>` | Reactive filters object — bind with `v-model` |
120
+ | `reset()` | `() => void` | Reset all filters to initial values |
121
+ | `resetKeys(...keys)` | `(...keys) => void` | Reset specific keys only |
122
+ | `flush()` | `() => void` | Trigger visit immediately, bypassing debounce |
123
+ | `isDirty()` | `() => boolean` | True if any filter differs from its initial value |
124
+ | `isKeyDirty(key)` | `(key) => boolean` | True if the specific key differs from initial |
125
+
126
+ ---
127
+
128
+ ## Real-world example with `debounceOnly`
129
+
130
+ You want the `search` field to be debounced (wait for the user to stop typing), but `perPage` and `sort` should fire instantly.
131
+
132
+ ```vue
133
+ <script setup lang="ts">
134
+ import { useInertiaFilters } from '@mits/use-inertia-filters'
135
+
136
+ const { filters, reset, isDirty } = useInertiaFilters(
137
+ {
138
+ search: '',
139
+ status: null,
140
+ sort: 'name',
141
+ direction: 'asc',
142
+ perPage: 15,
143
+ },
144
+ {
145
+ debounce: 400,
146
+ debounceOnly: ['search'], // only search is debounced
147
+ },
148
+ )
149
+ </script>
150
+ ```
151
+
152
+ ---
153
+
154
+ ## Laravel controller
155
+
156
+ Nothing special needed on the backend — standard Inertia pattern:
157
+
158
+ ```php
159
+ public function index(Request $request): Response
160
+ {
161
+ $users = User::query()
162
+ ->when($request->search, fn ($q, $v) => $q->where('name', 'like', "%{$v}%"))
163
+ ->when($request->status, fn ($q, $v) => $q->where('status', $v))
164
+ ->orderBy($request->sort ?? 'name', $request->direction ?? 'asc')
165
+ ->paginate($request->perPage ?? 15)
166
+ ->withQueryString();
167
+
168
+ return Inertia::render('Users/Index', [
169
+ 'users' => $users,
170
+ 'filters' => $request->only('search', 'status', 'sort', 'direction', 'perPage'),
171
+ ]);
172
+ }
173
+ ```
174
+
175
+ ---
176
+
177
+ ## Why not use an existing table package?
178
+
179
+ Packages like `inertiajs-tables-laravel-query-builder` are great but opinionated:
180
+
181
+ - Require **Spatie Laravel Query Builder** on the backend
182
+ - Require **Tailwind CSS Forms plugin**
183
+ - Bring their own table UI components
184
+
185
+ `useInertiaFilters` is **headless**. No UI, no backend requirements, no CSS. Use it with any component library — shadcn-vue, PrimeVue, Vuetify, Quasar, or plain HTML.
186
+
187
+ ---
188
+
189
+ ## TypeScript
190
+
191
+ Full type inference out of the box:
192
+
193
+ ```ts
194
+ const { filters, isKeyDirty } = useInertiaFilters({
195
+ search: '',
196
+ perPage: 15,
197
+ })
198
+
199
+ filters.search // string ✓
200
+ filters.perPage // number ✓
201
+ filters.nope // TS error ✓
202
+
203
+ isKeyDirty('search') // ✓
204
+ isKeyDirty('nope') // TS error ✓
205
+ ```
206
+
207
+ ---
208
+
209
+ ## Contributing
210
+
211
+ Issues and PRs welcome at [github.com/mits-software/use-inertia-filters](https://github.com/mits-software/use-inertia-filters).
212
+
213
+ ---
214
+
215
+ ## License
216
+
217
+ MIT © [MITS Sp. z o.o.](https://mits.pl)
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue"),g=require("@inertiajs/vue3");function k(n,t){if(n===null)return t??null;if(typeof t=="number"){const o=Number(n);return isNaN(o)?t:o}return typeof t=="boolean"?n==="true":n}function P(n){if(typeof window>"u")return{...n};const t=new URLSearchParams(window.location.search),o={...n};for(const r in n)t.has(r)&&(o[r]=k(t.get(r),n[r]));return o}function M(n,t){const o={};for(const r in n){const s=n[r],a=t[r];!(s==null||s==="")&&!(s===a)&&(o[r]=s)}return o}function N(n,t={}){const{debounce:o=300,preserveState:r=!0,preserveScroll:s=!0,replace:a=!0,debounceOnly:d,onBeforeVisit:m,onAfterVisit:y}=t,c={...n},f=l.reactive(P(n));function p(){if(typeof window>"u")return;const e=l.toRaw(f),i=M(e,c);m&&m(e)===!1||g.router.get(window.location.pathname,i,{preserveState:r,preserveScroll:s,replace:a,onSuccess:()=>y==null?void 0:y(e)})}let u=null;function h(e){u&&clearTimeout(u),(d?d.includes(e):!0)&&o>0?u=setTimeout(p,o):p()}let b=!1;l.onMounted(()=>{b=!0}),l.onUnmounted(()=>{u&&(clearTimeout(u),u=null)});for(const e in n)l.watch(()=>f[e],()=>{b&&h(e)});function w(){for(const e in c)f[e]=c[e]}function v(...e){for(const i of e)f[i]=c[i]}function D(){u&&(clearTimeout(u),u=null),p()}function S(){return Object.keys(c).some(e=>f[e]!==c[e])}function T(e){return f[e]!==c[e]}return{filters:f,reset:w,resetKeys:v,flush:D,isDirty:S,isKeyDirty:T}}exports.useInertiaFilters=N;
@@ -0,0 +1 @@
1
+ export { useInertiaFilters, type FilterValue, type FilterState, type UseInertiaFiltersOptions, type UseInertiaFiltersReturn, } from './useInertiaFilters';
package/dist/index.js ADDED
@@ -0,0 +1,97 @@
1
+ import { reactive as S, onMounted as v, onUnmounted as N, watch as P, toRaw as U } from "vue";
2
+ import { router as g } from "@inertiajs/vue3";
3
+ function C(n, t) {
4
+ if (n === null) return t ?? null;
5
+ if (typeof t == "number") {
6
+ const o = Number(n);
7
+ return isNaN(o) ? t : o;
8
+ }
9
+ return typeof t == "boolean" ? n === "true" : n;
10
+ }
11
+ function E(n) {
12
+ if (typeof window > "u") return { ...n };
13
+ const t = new URLSearchParams(window.location.search), o = { ...n };
14
+ for (const r in n)
15
+ t.has(r) && (o[r] = C(t.get(r), n[r]));
16
+ return o;
17
+ }
18
+ function K(n, t) {
19
+ const o = {};
20
+ for (const r in n) {
21
+ const s = n[r], l = t[r];
22
+ !(s == null || s === "") && !(s === l) && (o[r] = s);
23
+ }
24
+ return o;
25
+ }
26
+ function j(n, t = {}) {
27
+ const {
28
+ debounce: o = 300,
29
+ preserveState: r = !0,
30
+ preserveScroll: s = !0,
31
+ replace: l = !0,
32
+ debounceOnly: a,
33
+ onBeforeVisit: d,
34
+ onAfterVisit: m
35
+ } = t, c = { ...n }, f = S(E(n));
36
+ function y() {
37
+ if (typeof window > "u") return;
38
+ const e = U(f), i = K(e, c);
39
+ d && d(e) === !1 || g.get(
40
+ window.location.pathname,
41
+ i,
42
+ {
43
+ preserveState: r,
44
+ preserveScroll: s,
45
+ replace: l,
46
+ onSuccess: () => m == null ? void 0 : m(e)
47
+ }
48
+ );
49
+ }
50
+ let u = null;
51
+ function h(e) {
52
+ u && clearTimeout(u), (a ? a.includes(e) : !0) && o > 0 ? u = setTimeout(y, o) : y();
53
+ }
54
+ let p = !1;
55
+ v(() => {
56
+ p = !0;
57
+ }), N(() => {
58
+ u && (clearTimeout(u), u = null);
59
+ });
60
+ for (const e in n)
61
+ P(
62
+ () => f[e],
63
+ () => {
64
+ p && h(e);
65
+ }
66
+ );
67
+ function w() {
68
+ for (const e in c)
69
+ f[e] = c[e];
70
+ }
71
+ function b(...e) {
72
+ for (const i of e)
73
+ f[i] = c[i];
74
+ }
75
+ function D() {
76
+ u && (clearTimeout(u), u = null), y();
77
+ }
78
+ function T() {
79
+ return Object.keys(c).some(
80
+ (e) => f[e] !== c[e]
81
+ );
82
+ }
83
+ function k(e) {
84
+ return f[e] !== c[e];
85
+ }
86
+ return {
87
+ filters: f,
88
+ reset: w,
89
+ resetKeys: b,
90
+ flush: D,
91
+ isDirty: T,
92
+ isKeyDirty: k
93
+ };
94
+ }
95
+ export {
96
+ j as useInertiaFilters
97
+ };
@@ -0,0 +1,52 @@
1
+ export type FilterValue = string | number | boolean | null | undefined;
2
+ export type FilterState = Record<string, FilterValue>;
3
+ export interface UseInertiaFiltersOptions {
4
+ /**
5
+ * Debounce delay in ms before triggering a router visit.
6
+ * Useful for text search inputs. Default: 300
7
+ */
8
+ debounce?: number;
9
+ /**
10
+ * Whether to preserve Vue component state across navigations.
11
+ * Default: true
12
+ */
13
+ preserveState?: boolean;
14
+ /**
15
+ * Whether to preserve scroll position across navigations.
16
+ * Default: true
17
+ */
18
+ preserveScroll?: boolean;
19
+ /**
20
+ * Replace current history entry instead of pushing a new one.
21
+ * Default: true
22
+ */
23
+ replace?: boolean;
24
+ /**
25
+ * Only watch these keys for debounce. Other keys trigger immediately.
26
+ * Useful when you want instant sort/per_page but debounced search.
27
+ */
28
+ debounceOnly?: string[];
29
+ /**
30
+ * Called before each router visit. Return false to cancel.
31
+ */
32
+ onBeforeVisit?: (filters: FilterState) => boolean | void;
33
+ /**
34
+ * Called after each successful router visit.
35
+ */
36
+ onAfterVisit?: (filters: FilterState) => void;
37
+ }
38
+ export interface UseInertiaFiltersReturn<T extends FilterState> {
39
+ /** Reactive filters object — bind directly with v-model */
40
+ filters: T;
41
+ /** Reset all filters to their initial values */
42
+ reset: () => void;
43
+ /** Reset specific keys to their initial values */
44
+ resetKeys: (...keys: (keyof T)[]) => void;
45
+ /** Trigger a visit immediately, bypassing debounce */
46
+ flush: () => void;
47
+ /** Check whether any filter differs from its initial value */
48
+ isDirty: () => boolean;
49
+ /** Check whether a specific key differs from its initial value */
50
+ isKeyDirty: (key: keyof T) => boolean;
51
+ }
52
+ export declare function useInertiaFilters<T extends FilterState>(initialFilters: T, options?: UseInertiaFiltersOptions): UseInertiaFiltersReturn<T>;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@mits_pl/use-inertia-filters",
3
+ "version": "1.0.0",
4
+ "description": "Headless Vue 3 composable for managing filter state with Inertia.js — debounced, URL-synced, TypeScript-first.",
5
+ "keywords": [
6
+ "inertia",
7
+ "inertiajs",
8
+ "vue",
9
+ "vue3",
10
+ "composable",
11
+ "filters",
12
+ "laravel",
13
+ "typescript"
14
+ ],
15
+ "homepage": "https://mits.pl",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/mits-software/use-inertia-filters"
19
+ },
20
+ "author": "MITS Sp. z o.o. <hello@mits.pl> (https://mits.pl)",
21
+ "license": "MIT",
22
+ "type": "module",
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.cjs",
30
+ "types": "./dist/index.d.ts"
31
+ }
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "scripts": {
39
+ "build": "vite build && tsc --emitDeclarationOnly --declaration --declarationDir dist",
40
+ "dev": "vite build --watch",
41
+ "test": "vitest run",
42
+ "test:watch": "vitest",
43
+ "prepublishOnly": "npm run build"
44
+ },
45
+ "peerDependencies": {
46
+ "@inertiajs/vue3": ">=1.0.0",
47
+ "vue": ">=3.3.0"
48
+ },
49
+ "devDependencies": {
50
+ "@inertiajs/vue3": "^2.0.0",
51
+ "@vitejs/plugin-vue": "^5.0.0",
52
+ "jsdom": "^28.1.0",
53
+ "typescript": "^5.4.0",
54
+ "vite": "^5.0.0",
55
+ "vitest": "^1.0.0",
56
+ "vue": "^3.4.0"
57
+ }
58
+ }