Skip to content

Commit e41f68a

Browse files
committed
better and simpler docs
1 parent 63cff07 commit e41f68a

1 file changed

Lines changed: 6 additions & 43 deletions

File tree

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,7 @@
1-
Dropdown options can be replaced at runtime without a page reload. Use this for dependent fields (for example, a region select that determines city options) or for data loaded from Turbo/custom events.
1+
You can set up a dropdown to update its options dynamically based on another input (for example, a region select that drives city options). To achieve this:
2+
- Give the dropdown a unique `id` so it can be targeted by events and linked to a controlling input.
3+
- Use `context_selector` to point at the controlling select’s `id`. When that select’s value changes, the Dropdown reads the new value as its current context.
4+
- Use `options_by_context` to pass a hash of option lists. Keys must match the possible values of the controlling select; each key maps to an array of `{ id, label, value }` options that replace the Dropdown’s options for that context.
5+
- Use `clear_on_context_change` (defaults to `true`) to clear the Dropdown’s current selection whenever the controlling select changes. Set it to `false` if you want to keep a selection that still exists in the new options.
26

3-
## Declarative: `options_by_context`
4-
5-
Similar to Typeahead, pass a hash of option arrays keyed by the value of a controlling input:
6-
7-
- Give the dependent dropdown a unique `id`
8-
- Set `context_selector` to the id of the controlling select (or other input)
9-
- Pass `options_by_context` with keys matching the controller's possible values
10-
- Optionally set `clear_on_context_change` to `false` to keep the current selection when the context changes (defaults to `true`)
11-
12-
Each option uses the standard Dropdown shape: `{ id, label, value }`. If `id` is omitted, `value` is used.
13-
14-
## Imperative: `pb:dropdown:updateOptions`
15-
16-
Dispatch a document-level custom event to replace options for a specific dropdown:
17-
18-
```javascript
19-
document.dispatchEvent(new CustomEvent("pb:dropdown:updateOptions", {
20-
detail: {
21-
dropdownId: "city-dropdown",
22-
options: [
23-
{ id: "nyc", label: "New York", value: "nyc" },
24-
{ id: "la", label: "Los Angeles", value: "la" },
25-
],
26-
clearSelection: true, // optional, defaults to true
27-
},
28-
}));
29-
```
30-
31-
## Custom and Turbo events: `options_event_type`
32-
33-
Set `options_event_type` to a comma-separated list of event names. When any of those events fire with `detail.options` (and optional `detail.dropdownId`), the matching dropdown replaces its options. This works well with Turbo frame loads or app-specific events:
34-
35-
```javascript
36-
document.dispatchEvent(new CustomEvent("cities:loaded", {
37-
detail: {
38-
dropdownId: "city-dropdown",
39-
options: [{ id: "chi", label: "Chicago", value: "chi" }],
40-
},
41-
}));
42-
```
43-
44-
Existing external control events (`pb:dropdown:clear`, `pb:dropdown:select`, and `custom_event_type` for clearing) continue to work alongside option updates.
7+
You can also replace options imperatively by dispatching `pb:dropdown:updateOptions` with `detail: { dropdownId, options }`, or by setting `options_event_type` to a comma-separated list of custom/Turbo event names. When those events fire with `detail.options` (and optional `detail.dropdownId`), the matching Dropdown updates its options.

0 commit comments

Comments
 (0)