From db92bae92a0c80decaded2eb9872ff133708368c Mon Sep 17 00:00:00 2001 From: Jake Boone Date: Thu, 26 Nov 2026 13:01:51 -0600 Subject: [PATCH 1/3] Address issue #3 review findings - Fix controlled mode committing on mount (defer controlled-query sync effect) - Fix fan-out: equality-gate per-row memos in RuleGroupBody - Add fine-grained 200-rule test and controlled store-feedback tests - SSR example: radio editor + not-toggle, id uniqueness/label-for check - Docs: peer range, external manager, rule/ruleGroup replacement, rootGroup, path helpers, non-goals - AGENTS.md: remote status, new reactivity rules and gates --- AGENTS.md | 27 ++++-- CHANGELOG.md | 9 ++ README.md | 32 ++++--- docs/customization.md | 44 ++++++++++ examples/ssr/src/App.tsx | 14 +++ examples/ssr/ssr-smoke-test.ts | 24 +++++- .../src/components/fine-grained.test.tsx | 86 +++++++++++++++++++ .../src/internal/RuleGroupBody.tsx | 12 +-- .../src/reactive/createQueryBuilder.test.ts | 62 +++++++++++++ .../src/reactive/createQueryBuilder.ts | 13 ++- .../src/reactive/manager-bridge.ts | 7 +- .../solid-querybuilder/src/types/props.ts | 5 ++ 12 files changed, 309 insertions(+), 26 deletions(-) create mode 100644 packages/solid-querybuilder/src/components/fine-grained.test.tsx diff --git a/AGENTS.md b/AGENTS.md index 38d18fc..5e5d311 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -24,8 +24,8 @@ solid-querybuilder/ └── ssr/ # hand-rolled Vite SSR consumer; the SSR gate ``` -**Target is Solid 2 only.** Peers are `solid-js@^2.0.0-rc.0` **and -`@solidjs/web@^2.0.0-rc.0`** — in Solid 2 the DOM runtime is its own package. There is no +**Target is Solid 2 only.** Peers are `solid-js@^2.0.0` **and `@solidjs/web@^2.0.0`** — in +Solid 2 the DOM runtime is its own package. There is no `^1.9` leg anywhere: in the manifest, in CI, or in the source. A v1-target port, if it ever happens, is a separate repo publishing as `@react-querybuilder/solid1`; do not add compatibility shims or `solid-js@1` code paths here. @@ -165,8 +165,15 @@ type-only module erased by the bundler), and allows `./foo.jsx` under `dist/sour Every write-then-read must `flush()` first. Never paper over this with `setTimeout` or tick counts. `@solidjs/testing-library`'s `render` populates the container synchronously; subsequent updates need `flush()`. -- Return getter objects (not objects of accessors, not memoized fresh objects) from composables +- **Return getter objects** (not objects of accessors, not memoized fresh objects) from composables whose result is read once by a Solid context or passed as a prop. +- ⚠️ **A memo that returns fresh objects notifies every reader.** E.g. `RuleGroupBody`'s + `keyedRules` rebuilds each row's entry on any child change; per-row reads go through + equality-gated memos (`pathsAreEqual` for paths) or one edit re-runs every rule's props. + `fine-grained.test.tsx` (200 rules, one edit → one value-editor recompute) is the gate. +- ⚠️ **The controlled-`query` sync effect is `{ defer: true }`.** Seeding already applied the + mount-time query; a first run compares the raw prop to the _prepared_ one (gained a root `id`), + sees `:id`, and commits — firing `onQueryChange` and an undo entry on mount. - **There is no `onMount`.** Solid 2 does not export one. The post-commit-mount equivalent is `createEffect(() => undefined, () => { … })`: a constant compute phase runs the apply phase once, after render, and never under SSR — which is what React's mount `useEffect` does. @@ -317,6 +324,16 @@ The example gate was proved red twice, independently, and reverted both times: hydration surface comparison red while every markup assertion stayed green. Those two failure modes share no code, which is the point of having both. +The SSR example also renders a radio editor and the not-toggle (`createUniqueId` users). Its id +check (ids unique, every `label[for]` resolves) was proved red: a constant `NotToggle` id → "duplicate +ids". ⚠️ Client-side id **drift** is not detectable here and the gate does not claim it: Solid 2 +hydration adopts server attributes, so a client-only id suffix _and_ an extra client-only +`createUniqueId()` call both left the surface unchanged and hydration error-free. + +New unit gates (#3), each proved red: `fine-grained.test.tsx` (without the `RuleGroupBody` memos, +all 200 value editors recompute) and the controlled-mount / store-feedback tests in +`createQueryBuilder.test.ts` (without `{ defer: true }`, both red). + All five were proven red once and reverted: coverage (threshold to 99 + an injected uncovered function), export-condition **order** (`import` moved first), export-condition **target** (`solid` repointed at `dist/index.js`), the SSR **markup** assertion (component's label @@ -354,5 +371,5 @@ is how CI runs it. ## Repo status -**Remote-less.** The repo is a local git repo with no GitHub remote and nothing pushed. -`.github/workflows/ci.yml` exists so it is in place whenever a remote is added. +GitHub remote: `react-querybuilder/solid-querybuilder` (`origin`). Work lands via PRs against +`main`; CI is `.github/workflows/ci.yml`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 9595103..dd218be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed + +- Controlled mode no longer commits on mount. A `query` without a root `id` (the usual case) was + re-applied by the sync effect's first run against the prepared query, firing `onQueryChange` + and leaving an undo entry even with `enableMountQueryChange={false}`. +- A single edit no longer re-evaluates every rule's props. `RuleGroupBody` rebuilt every row's + entry on any child change; per-row values are now equality-gated memos. Guarded by a 200-rule + fine-grained test. + ### Removed - **Breaking:** `createRuleContext` and `createRuleGroupContext` are no longer exported. Each was a diff --git a/README.md b/README.md index fcfe16c..7c59c3f 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,13 @@ byte-identical DOM output. ## ⚠️ Requires Solid 2.0 -This package targets **Solid 2 only**. Its peer dependencies are `solid-js@^2.0.0-rc.0` **and** -`@solidjs/web@^2.0.0-rc.0` — in Solid 2 the DOM runtime ships as its own package, so both are +This package targets **Solid 2 only**. Its peer dependencies are `solid-js@^2.0.0` **and** +`@solidjs/web@^2.0.0` — in Solid 2 the DOM runtime ships as its own package, so both are required. -Solid 2 is currently in **beta**, and this package tracks it deliberately rather than waiting: the -port is built on Solid 2 idiom (`merge`/`omit`, split effects, `createStore` from `solid-js`) that -has no faithful Solid 1 equivalent. It does not carry `solid-js@1` compatibility shims and it never -will. Should demand warrant a Solid 1.x port, it would be published separately as +The port is built on Solid 2 idiom (`merge`/`omit`, split effects, `createStore` from `solid-js`) +that has no faithful Solid 1 equivalent. It does not carry `solid-js@1` compatibility shims and it +never will. Should demand warrant a Solid 1.x port, it would be published separately as `@react-querybuilder/solid1`. ## Installation @@ -23,9 +22,8 @@ will. Should demand warrant a Solid 1.x port, it would be published separately a npm install solid-querybuilder ``` -`solid-js@^2.0.0-rc.0` and `@solidjs/web@^2.0.0-rc.0` are peer dependencies. Install -`@solidjs/web` from the `next` dist-tag; its `latest` tag is `2.0.0-experimental.0`, an incompatible -line. `@react-querybuilder/core` is a regular dependency and is re-exported in full — `formatQuery`, +`solid-js@^2.0.0` and `@solidjs/web@^2.0.0` are peer dependencies. +`@react-querybuilder/core` is a regular dependency and is re-exported in full — `formatQuery`, `defaultOperators`, `transformQuery`, `QueryManager` and the rest all import from `solid-querybuilder` — so you never need to depend on it directly. @@ -57,6 +55,8 @@ function App() { } ``` +`query={query()}` is a getter, so the prop tracks the signal. ⚠️ Solid 2 defers writes: a read right after `setQuery(...)` still returns the previous value until the next microtask (or `flush()`). See [deferred writes](./docs/differences-from-react-querybuilder.md#4-query-binding). + ## Driving the query | Approach | Use when | @@ -119,6 +119,15 @@ identities) rather than `state.query` (a plain identity signal) whenever you are `state.manager` is the underlying [`QueryManager`](https://react-querybuilder.js.org); everything else is derived from it. +Core's path helpers (`findPath`, `getParentPath`, `pathsAreEqual`, `pathIsDisabled`, `add`, `remove`, `move`, `update`, and the rest) are exported from `solid-querybuilder`. Custom UIs can do path math without depending on core directly: + +```tsx +import { findPath, getParentPath } from 'solid-querybuilder'; + +const node = findPath([1, 0], state.query); // rule or group at path [1, 0] +const parentPath = getParentPath([1, 0]); // [1] +``` + ## Styling Two prebuilt stylesheets ship in `dist`: `query-builder.css` (full) and @@ -130,7 +139,7 @@ Two prebuilt stylesheets ship in `dist`: `query-builder.css` (full) and - [`docs/customization.md`](./docs/customization.md) — translations, `controlElements`, context, writing a replacement component, external control, class names. -- [`docs/differences-from-react-querybuilder.md`](./docs/differences-from-react-querybuilder.md) — +- [`docs/differences-from-react-querybuilder.md`](./docs/differences-from-react-querybuilder.md#4-query-binding).— what is and is not ported, and every intentional divergence. - [`docs/styling.md`](./docs/styling.md) — stylesheets, custom properties, class-name overrides. - [React Query Builder's documentation](https://react-querybuilder.js.org) applies to props, @@ -146,7 +155,8 @@ Two prebuilt stylesheets ship in `dist`: `query-builder.css` (full) and ## Non-goals Drag and drop, UI-framework compatibility packages, `expr`/`datetime` UI, async option lists, -a Redux store, and deprecated-prop fallbacks are all out of scope for v1. +a Redux store (and with it `qbId`), `preserveQueryStateOnUnmount`, and deprecated-prop fallbacks +are all out of scope for v1. ## License diff --git a/docs/customization.md b/docs/customization.md index a069200..0cfe542 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -157,6 +157,48 @@ Two smaller notes: - Build class strings with core's `clsx`, not template interpolation, if you care about matching React Query Builder's output byte for byte. +### Replacing `rule` / `ruleGroup` + +`controlElements.rule` receives `RuleProps`, and `controlElements.ruleGroup` receives `RuleGroupProps`. Both are exported, and they share these members: + +| Prop | Meaning | +| -------------------------------------------- | ---------------------------------------------------------------------- | +| `rule` / `ruleGroup` | The node itself, a store-mirror proxy with a stable identity per `id`. | +| `path` | Its `Path` (`number[]`). Use core's path helpers to do path math. | +| `schema` | Fields, operators, resolvers, flags, and `schema.controls`. | +| `actions` | `onPropChange`, `onRuleAdd`, `onRuleRemove`, `moveRule`, and so on. | +| `translations`, `context` | Same as in React Query Builder. | +| `disabled`, `parentDisabled`, `parentMuted` | Disabled and muted state, both the node's own and inherited. | +| `shiftUpDisabled`, `shiftDownDisabled`, `id` | Positional flags, and the node `id`. | + +The simplest replacement wraps the default: + +```tsx +import { Rule, type RuleProps } from 'solid-querybuilder'; + +const MyRule = (props: RuleProps) => ( +
+ +
+); +``` + +A rule built from scratch writes through `actions` with its own `path`: + +```tsx +const MyRule = (props: RuleProps) => ( + props.actions.onPropChange('value', e.currentTarget.value, props.path)} + /> +); +``` + +`createRuleState(props)` and `createRuleGroupState(props)` are exported too. They are the primitives the default components use (class names, the resolved field and operator, and handlers), so a from-scratch replacement can reuse them instead of rebuilding them. A replacement `ruleGroup` that renders its own children should render each child through `props.schema.controls.rule` / `props.schema.controls.ruleGroup`, so that nested overrides still apply. + +Generic replacements: Solid components have no compile-time prop enumeration. If your component is generic over field or operator types, write it against the default type parameters and widen once at the boundary (`props as unknown as RuleProps`), which is what the default components do. + ## External control Pass a `QueryManager` you own and drive the query from anywhere: @@ -183,6 +225,8 @@ The manager owns every write; the component subscribes. ⚠️ Hand the manager deep-freezes its inputs with Immer, which rejects a Solid store proxy. If a query came out of a Solid store, `snapshot()` it first. +⚠️ **The component never reconfigures an external manager, and reads it once.** Structural props (`fields`, `operators`, `combinators`, and so on) are not pushed into a manager you pass in, so configure it directly (`new QueryManager(query, { fields, … })` / `manager.reconfigure(…)`). The `manager` prop is read at mount only, so swapping in a different instance later is ignored. To switch managers, remount the component. + Between the `manager` prop and the veto callbacks (`onAddRule`, `onAddGroup`, `onRemove`, `onMoveRule`, `onMoveGroup`, `onGroupRule`, `onGroupGroup`, `onUngroup` — each of which can cancel or rewrite the pending change), this replaces everything React Query Builder's Redux store was used for. See diff --git a/examples/ssr/src/App.tsx b/examples/ssr/src/App.tsx index 46e2aa3..d8cd44d 100644 --- a/examples/ssr/src/App.tsx +++ b/examples/ssr/src/App.tsx @@ -5,6 +5,17 @@ export const fields: Field[] = [ { name: 'firstName', label: 'First Name' }, { name: 'lastName', label: 'Last Name' }, { name: 'age', label: 'Age', inputType: 'number' }, + // Radio editor and (via `showNotToggle`) the not-toggle both mint ids with `createUniqueId`; + // the hydration surface compares `id`/`for`, so a server/client id drift fails the gate. + { + name: 'color', + label: 'Color', + valueEditorType: 'radio', + values: [ + { name: 'red', label: 'Red' }, + { name: 'blue', label: 'Blue' }, + ], + }, ]; /** @@ -31,6 +42,8 @@ export const query: RuleGroupTypeIC = { { id: 'r-2', field: 'lastName', operator: '=', value: 'Vai' }, 'and', { id: 'r-3', field: 'age', operator: '<', value: 90 }, + 'and', + { id: 'r-4', field: 'color', operator: '=', value: 'blue' }, ], }, ], @@ -58,6 +71,7 @@ export const App = (props: { query: RuleGroupTypeIC }) => ( ); diff --git a/examples/ssr/ssr-smoke-test.ts b/examples/ssr/ssr-smoke-test.ts index 35f5e24..0f58bec 100644 --- a/examples/ssr/ssr-smoke-test.ts +++ b/examples/ssr/ssr-smoke-test.ts @@ -126,11 +126,13 @@ const fragments = [ 'data-testid="inline-combinator"', // The one control passed through `controlElements`. 'custom-remove-rule', + 'data-testid="not-toggle"', + 'type="radio"', // Server-side `formatQuery(query, 'sql')`, in its HTML-escaped form: Solid's SSR escapes text // content, so `'` is `'` and `>` is `>`. Matching the raw SQL here would be a // false-negative waiting to happen. [Vue hindsight] '(firstName like 'Stev''e%' and age > '28' or ' + - '(lastName = 'Vai' and age < '90'))', + '(lastName = 'Vai' and age < '90' and color = 'blue'))', ]; for (const fragment of fragments) { @@ -152,8 +154,14 @@ const surface = (root: Element): string => el.getAttribute('class') ?? '', el.getAttribute('data-testid') ?? '', el.getAttribute('data-path') ?? '', + // `createUniqueId` output (radio editor, not-toggle): must survive hydration unchanged. + el.getAttribute('id') ?? '', + el.getAttribute('for') ?? '', + el.getAttribute('name') ?? '', ]; - if (el instanceof (el.ownerDocument.defaultView!.HTMLInputElement)) parts.push(el.value); + if (el instanceof (el.ownerDocument.defaultView!.HTMLInputElement)) { + parts.push(el.value, String(el.checked)); + } if (el instanceof (el.ownerDocument.defaultView!.HTMLSelectElement)) parts.push(el.value); if (el instanceof (el.ownerDocument.defaultView!.HTMLTextAreaElement)) parts.push(el.value); return parts.join('|'); @@ -182,6 +190,18 @@ const serverRoot = window.document.querySelector('#root'); check(serverRoot !== null, 'served HTML has no #root element (did the render throw?)'); const serverSurface = serverRoot ? surface(serverRoot) : ''; +// `createUniqueId` (radio editor, not-toggle): ids unique, and every `