Skip to content

Commit b76c513

Browse files
set state for tabless views, README update
1 parent 1b81791 commit b76c513

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default function App({ tableName, viewName, state, query, rows, user, sta
8585
For tableless react-views, only the following properties are available:
8686

8787
```javascript
88-
export default function App({ viewName, query, user, stateHash })
88+
export default function App({ viewName, query, state, user, stateHash })
8989
```
9090

9191
### Define everything in the view
@@ -553,11 +553,13 @@ export default function App({ viewName, query }) {
553553
}
554554
```
555555
556-
## Controlling embedded views via URL state
556+
## Controlling embedded multi-row views via URL state
557557
558-
A React view can embed and control other Saltcorn views (Feed, List, etc.) by reading and writing URL state. When URL state changes via pjax, Saltcorn re-renders the affected views and passes the updated `state` prop to your React component.
558+
A React view can embed and control other Saltcorn views that display multiple rows by reading and writing URL state. When URL state changes via pjax, Saltcorn re-renders the affected views and passes the updated `state` prop to your React component.
559559
560-
Embedded views (Feed, List, …) use URL params prefixed with a short hash unique to each view instance:
560+
> **Note:** Currently only **List** and **Feed** view templates are supported — they expose the pagination metadata that `useManyView` depends on.
561+
562+
Embedded views use URL params prefixed with a short hash unique to each view instance:
561563
562564
| Param | Effect |
563565
|---|---|
@@ -570,15 +572,15 @@ Use `set_state_field(key, value)` to update a single param, or `set_state_fields
570572
571573
**Full example**
572574
573-
A tableless React controller view that embeds `"persons_feed"` and controls its pagination and sorting. It extracts the server-computed hash from the feed on first load, then keeps the feed in sync with URL state on every pjax navigation.
575+
A tableless React controller view that embeds `"persons_feed"` (a **Feed** view — a **List** view works identically) and controls its pagination and sorting. It extracts the server-computed hash from the embedded view on first load, then keeps it in sync with URL state on every pjax navigation.
574576
575577
```javascript
576578
import React from "react";
577-
import { useEmbeddedView } from "@saltcorn/react-lib/hooks";
578-
import ScView from "@saltcorn/react-lib/components/ScView";
579+
import { useManyView } from "@saltcorn/react-lib/hooks";
580+
import { ScView } from "@saltcorn/react-lib/components";
579581

580582
export default function App({ state }) {
581-
const { hash, html, page, hasNext, ready } = useEmbeddedView("persons_feed", state);
583+
const { hash, page, hasNext, ready, viewProps } = useManyView("persons_feed", state);
582584

583585
if (!ready) return null;
584586

@@ -613,7 +615,7 @@ export default function App({ state }) {
613615
</div>
614616
{/* Hide the feed's built-in paginator — we provide our own above */}
615617
<style>{`.sc-feed-wrapper ul.pagination { display: none !important; }`}</style>
616-
<ScView html={html} className="sc-feed-wrapper" />
618+
<ScView {...viewProps} className="sc-feed-wrapper" />
617619
</div>
618620
);
619621
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"@saltcorn/markup": "^0.2.0",
1616
"@saltcorn/data": "^0.2.0",
17-
"@saltcorn/react-lib": "0.0.1-beta.7",
17+
"@saltcorn/react-lib": "0.0.1-beta.8",
1818
"react": "^19.0.0",
1919
"react-dom": "^19.0.0",
2020
"webpack": "5.97.1",

‎react_view.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const get_state_fields = () => [];
2020
const defaultUserCode = (tableId) => {
2121
return `import React from "react";
2222
23-
export default function App({ viewName, query${
24-
tableId ? ", state, tableName, rows" : " "
23+
export default function App({ viewName, query, state${
24+
tableId ? ", tableName, rows" : ""
2525
} }) {
2626
return <h3>Please write your React code here</h3>;
2727
};
@@ -38,6 +38,7 @@ const run = async (table_id, viewname, { timestamp }, state, extra) => {
3838
query: encodeURIComponent(JSON.stringify(query)),
3939
user: encodeURIComponent(JSON.stringify(req.user || {})),
4040
"state-hash": stateHash,
41+
state: encodeURIComponent(JSON.stringify(state)),
4142
};
4243
if (table_id) {
4344
// with table
@@ -58,7 +59,6 @@ const run = async (table_id, viewname, { timestamp }, state, extra) => {
5859
const totalCount = q.limit ? await table.countRows(where) : undefined;
5960
readState(state, fields, req);
6061
props["table-name"] = table.name;
61-
props.state = encodeURIComponent(JSON.stringify(state));
6262
props.rows = encodeURIComponent(JSON.stringify(rows));
6363
if (totalCount !== undefined) props["total-count"] = String(totalCount);
6464
}

0 commit comments

Comments
 (0)