jsl-ui 10.3.0 → 10.4.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.
@@ -13,3 +13,24 @@ const teams = new SelectComponent({
13
13
  - `value` / `getValue()` use keys from `keyColumn` (default `"value"`); `getSelected()` returns the matching data objects. Multiple selection uses arrays; single selection uses a scalar.
14
14
  - Omit `multiple` and use `value: "design"` for single selection with the same data.
15
15
  - `setValue()` takes keys and updates the binding without raising change callbacks. The component owns its dropdown; do not create `SelectDDAreaComponent` separately.
16
+
17
+ ## Search on the server
18
+
19
+ For option sets too large to load, let the server search. The dropdown's own search box stays the only search field.
20
+
21
+ ```ts
22
+ let generation = 0;
23
+ const customers = new SelectComponent({
24
+ ariaLabel: "Customer", data: [],
25
+ onOpen: () => search(""), // optional: load the first batch lazily
26
+ onSearch: term => search(term), // after 300 ms without typing; "" when the list reopens
27
+ onLoadMore: () => loadMore() // list scrolled near its end
28
+ });
29
+ ```
30
+
31
+ - With `onSearch`, typed text is not matched against `data` and `matchSearch` is ignored. `searchDelay` (default 300) debounces typing.
32
+ - Answer with `setData(items, hasMore)` and `setLoading(false)`; call `setLoading(true)` when a request starts. `setData` replaces all options, so for `onLoadMore` pass the items so far plus the new batch. The scroll position is kept.
33
+ - `onLoadMore` only fires while the last `setData` reported `hasMore` and the select is not loading, once per batch. Always finish with `setLoading(false)`, also after an error, or no further batch is requested.
34
+ - The chosen item stays selected and shown even if later results do not list it; `getSelected()` and `setValue()` still find it. Preselect by passing it in `data` with `value`, or by calling `setData` before `setValue`.
35
+ - Discard answers of outdated requests yourself, for example with a counter that grows with every search.
36
+ - `loadingText` replaces the "Loading…" hint.
@@ -65,6 +65,30 @@ const large = new SelectComponent({
65
65
  value: 5000, ariaLabel: "Large list", dropdownHeight: "18rem", placeholder: "Choose an item",
66
66
  onChange: (value, item) => { largeStatus.content = item ? "Selected " + item.text + ", key " + value : "Selection cleared."; }
67
67
  });
68
+ // A pretend server: 500 customers, searched and paged with a delay.
69
+ const customerNames = Array.from({ length: 500 }, (_, i) => ({ value: i + 1, text: "Customer " + ("000" + (i + 1)).slice(-4) + " " + ["Alpine", "Danube", "Linz", "Graz", "Tyrol"][i % 5] }));
70
+ const remoteStatus = status("Open the list, type a name or scroll for more.");
71
+ let remoteGeneration = 0;
72
+ let remoteFound: typeof customerNames = [];
73
+ let remoteTerm = "";
74
+ function remoteLoad(append: boolean) {
75
+ const generation = ++remoteGeneration;
76
+ remote.setLoading(true);
77
+ setTimeout(() => {
78
+ if (generation !== remoteGeneration) { return; }
79
+ const matches = customerNames.filter(item => item.text.toLowerCase().includes(remoteTerm.toLowerCase()));
80
+ remoteFound = matches.slice(0, append ? remoteFound.length + 25 : 25);
81
+ remote.setData(remoteFound, remoteFound.length < matches.length);
82
+ remote.setLoading(false);
83
+ remoteStatus.content = remoteFound.length + " of " + matches.length + " matches loaded.";
84
+ }, 400);
85
+ }
86
+ const remote: SelectComponent = new SelectComponent({
87
+ id: "select-remote", data: [], ariaLabel: "Customer", placeholder: "Choose a customer", searchPlaceholder: "Search customers…",
88
+ onOpen: () => { if (!remoteFound.length) { remoteLoad(false); } },
89
+ onSearch: term => { remoteTerm = term; remoteLoad(false); },
90
+ onLoadMore: () => remoteLoad(true)
91
+ });
68
92
  function card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {
69
93
  return { tag: "article", attr: { id, class: "sample-card" }, children: [
70
94
  group([text("span", number, "sample-number"), text("h3", title)], "card-heading"),
@@ -142,6 +166,10 @@ export const selectShowcase: IJSLVNode = {
142
166
  card("select-scale", "06", "Room for a long list", "Ten thousand options, with only the visible rows rendered. Selection stays in view when you open the list.", [
143
167
  field("select-large", "Item", large), largeStatus,
144
168
  text("p", "Type to search. Arrow keys move through results; Ctrl+Home and Ctrl+End jump to either end.", "inline-note")
169
+ ]),
170
+ card("select-server", "07", "Search on the server", "For lists too long to load. The one search box asks the server, and scrolling to the end loads more.", [
171
+ field("select-remote", "Customer", remote), remoteStatus,
172
+ text("pre", '{ onSearch: term => load(term), onLoadMore: () => loadMore() } // answer with select.setData(items, hasMore)', "sample-code")
145
173
  ])
146
174
  ], "sample-grid select-sample-grid"),
147
175
  { tag: "details", attr: { class: "usage" }, children: [
@@ -34,6 +34,7 @@ export const showcaseSources: Record<string, string> = {
34
34
  "select-custom": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { SelectComponent } from \"../../components/select/SelectComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst status = (content: string): IJSLVNode => ({ tag: \"p\", content, attr: { class: \"sample-status\", role: \"status\" } });\n\nconst field = (id: string, label: string, control: Content) => group([\n { tag: \"label\", content: label, attr: { for: id } }, control\n], \"select-showcase-field\");\n\nconst people = [\n { value: \"maya\", text: \"Maya Chen\", initials: \"MC\", role: \"Product designer\" },\n { value: \"alex\", text: \"Alex Morgan\", initials: \"AM\", role: \"Frontend engineer\" },\n { value: \"sam\", text: \"Sam Rivera\", initials: \"SR\", role: \"Accessibility specialist\" },\n { value: \"jules\", text: \"Jules Martin\", initials: \"JM\", role: \"Product manager\" }\n];\n\nconst memberStatus = status(\"Selected Maya Chen.\");\n\nconst member = new SelectComponent({\n id: \"select-owner\", data: people, value: \"maya\",\n ariaLabel: \"Project owner\", placeholder: \"Assign an owner\", rowHeight: 56,\n matchSearch: (item, term) => (item.text + \" \" + item.role).toLowerCase().includes(term.toLowerCase()),\n onRender: args => args.isCurrent ? text(\"span\", args.data.text) : group([\n { tag: \"span\", content: args.data.initials, attr: { class: \"select-person-avatar\", \"aria-hidden\": \"true\" } },\n group([text(\"strong\", args.data.text), text(\"span\", args.data.role)], \"select-person-copy\")\n ], \"select-person\"),\n onChange: (_value, item) => { memberStatus.content = item ? \"Selected \" + item.text + \".\" : \"No owner selected.\"; }\n});\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"select-custom\", \"04\", \"More than a label\", \"Custom rows add useful details about each person.\", [\n field(\"select-owner\", \"Project owner\", member), memberStatus,\n text(\"p\", \"Search also matches each person's role.\", \"inline-note\")\n ])\n);",
35
35
  "select-states": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { SelectComponent } from \"../../components/select/SelectComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst field = (id: string, label: string, control: Content) => group([\n { tag: \"label\", content: label, attr: { for: id } }, control\n], \"select-showcase-field\");\n\nconst projects = [\n { value: \"website\", text: \"Website refresh\" }, { value: \"library\", text: \"Component library\" },\n { value: \"workspace\", text: \"Team workspace\" }, { value: \"mobile\", text: \"Mobile experience\" },\n { value: \"research\", text: \"Customer research\" }, { value: \"release\", text: \"Autumn release\" }\n];\n\nconst skills = [\n { value: \"design\", text: \"Design\" }, { value: \"typescript\", text: \"TypeScript\" },\n { value: \"accessibility\", text: \"Accessibility\" }, { value: \"testing\", text: \"Testing\" },\n { value: \"research\", text: \"Research\" }, { value: \"documentation\", text: \"Documentation\" }\n];\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"select-states\", \"05\", \"The right level of choice\", \"Search can be omitted for short lists. Required selections and disabled controls share the same design.\", [\n field(\"select-required\", \"Default project\", new SelectComponent({\n id: \"select-required\", data: projects.slice(0, 3), value: \"library\", ariaLabel: \"Default project\",\n enableSearch: false, allowEmpty: false\n })),\n field(\"select-disabled\", \"Unavailable project\", new SelectComponent({\n id: \"select-disabled\", data: projects, value: \"website\", enabled: false, ariaLabel: \"Unavailable project\"\n })),\n field(\"select-disabled-multi\", \"Unavailable skills\", new SelectComponent({\n id: \"select-disabled-multi\", data: skills, multiple: true, value: [\"design\", \"testing\"], enabled: false, ariaLabel: \"Unavailable skills\"\n }))\n ])\n);",
36
36
  "select-scale": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { SelectComponent } from \"../../components/select/SelectComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst status = (content: string): IJSLVNode => ({ tag: \"p\", content, attr: { class: \"sample-status\", role: \"status\" } });\n\nconst field = (id: string, label: string, control: Content) => group([\n { tag: \"label\", content: label, attr: { for: id } }, control\n], \"select-showcase-field\");\n\nconst largeStatus = status(\"Search by name or jump through the list with your keyboard.\");\n\nconst large = new SelectComponent({\n id: \"select-large\", data: Array.from({ length: 10000 }, (_, i) => ({ value: i + 1, text: \"Item \" + (\"00000\" + (i + 1)).slice(-5) })),\n value: 5000, ariaLabel: \"Large list\", dropdownHeight: \"18rem\", placeholder: \"Choose an item\",\n onChange: (value, item) => { largeStatus.content = item ? \"Selected \" + item.text + \", key \" + value : \"Selection cleared.\"; }\n});\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"select-scale\", \"06\", \"Room for a long list\", \"Ten thousand options, with only the visible rows rendered. Selection stays in view when you open the list.\", [\n field(\"select-large\", \"Item\", large), largeStatus,\n text(\"p\", \"Type to search. Arrow keys move through results; Ctrl+Home and Ctrl+End jump to either end.\", \"inline-note\")\n ])\n);",
37
+ "select-server": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { SelectComponent } from \"../../components/select/SelectComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst status = (content: string): IJSLVNode => ({ tag: \"p\", content, attr: { class: \"sample-status\", role: \"status\" } });\n\nconst field = (id: string, label: string, control: Content) => group([\n { tag: \"label\", content: label, attr: { for: id } }, control\n], \"select-showcase-field\");\n\nconst customerNames = Array.from({ length: 500 }, (_, i) => ({ value: i + 1, text: \"Customer \" + (\"000\" + (i + 1)).slice(-4) + \" \" + [\"Alpine\", \"Danube\", \"Linz\", \"Graz\", \"Tyrol\"][i % 5] }));\n\nconst remoteStatus = status(\"Open the list, type a name or scroll for more.\");\n\nlet remoteGeneration = 0;\n\nlet remoteFound: typeof customerNames = [];\n\nlet remoteTerm = \"\";\n\nfunction remoteLoad(append: boolean) {\n const generation = ++remoteGeneration;\n remote.setLoading(true);\n setTimeout(() => {\n if (generation !== remoteGeneration) { return; }\n const matches = customerNames.filter(item => item.text.toLowerCase().includes(remoteTerm.toLowerCase()));\n remoteFound = matches.slice(0, append ? remoteFound.length + 25 : 25);\n remote.setData(remoteFound, remoteFound.length < matches.length);\n remote.setLoading(false);\n remoteStatus.content = remoteFound.length + \" of \" + matches.length + \" matches loaded.\";\n }, 400);\n}\n\nconst remote: SelectComponent = new SelectComponent({\n id: \"select-remote\", data: [], ariaLabel: \"Customer\", placeholder: \"Choose a customer\", searchPlaceholder: \"Search customers…\",\n onOpen: () => { if (!remoteFound.length) { remoteLoad(false); } },\n onSearch: term => { remoteTerm = term; remoteLoad(false); },\n onLoadMore: () => remoteLoad(true)\n});\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"select-server\", \"07\", \"Search on the server\", \"For lists too long to load. The one search box asks the server, and scrolling to the end loads more.\", [\n field(\"select-remote\", \"Customer\", remote), remoteStatus,\n text(\"pre\", '{ onSearch: term => load(term), onLoadMore: () => loadMore() } // answer with select.setData(items, hasMore)', \"sample-code\")\n ])\n);",
37
38
  "checkbox-basic": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { CheckBoxComponent } from \"../../components/checkbox/CheckBoxComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst status = (content: string): IJSLVNode => ({ tag: \"p\", content, attr: { class: \"sample-status\", role: \"status\" } });\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\nconst basicStatus = status(\"Email updates are off.\");\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"checkbox-basic\", \"01\", \"One clear choice\", \"Click the box or its label. Tab focuses the input; Space changes its state.\", [\n new CheckBoxComponent({ id: \"checkbox-basic-input\", label: \"Email me product updates\", onChange: checked => {\n basicStatus.content = checked ? \"Email updates are on.\" : \"Email updates are off.\";\n } }), basicStatus,\n text(\"pre\", '{ label: \"Email me product updates\", onChange: checked => { ... } }', \"sample-code\")\n ])\n);",
38
39
  "checkbox-states": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { CheckBoxComponent } from \"../../components/checkbox/CheckBoxComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"checkbox-states\", \"02\", \"A little more context\", \"Supporting text explains a choice. Disabled controls keep their labels and current state.\", [\n group([\n new CheckBoxComponent({ id: \"checkbox-description\", label: \"Remember this workspace\", description: \"Open this workspace automatically the next time you sign in.\", value: true }),\n new CheckBoxComponent({ id: \"checkbox-disabled\", label: \"Managed by your administrator\", enabled: false }),\n new CheckBoxComponent({ id: \"checkbox-disabled-checked\", label: \"Security notifications\", value: true, enabled: false })\n ], \"checkbox-stack\")\n ])\n);",
39
40
  "checkbox-partial": "import \"../../ui/base.css\";\nimport \"./showcase.css\";\nimport { IJSLComponent, IJSLVNode } from \"jsl-render\";\nimport { CheckBoxComponent } from \"../../components/checkbox/CheckBoxComponent\";\nimport { JSLRender as ExampleRenderer } from \"jsl-render\";\n\ntype Content = IJSLVNode | IJSLComponent;\n\nconst text = (tag: string, content: string, className?: string): IJSLVNode => ({ tag, content, attr: { class: className } });\n\nconst group = (children: Content[], className?: string): IJSLVNode => ({ tag: \"div\", attr: { class: className }, children });\n\nconst status = (content: string): IJSLVNode => ({ tag: \"p\", content, attr: { class: \"sample-status\", role: \"status\" } });\n\nfunction card(id: string, number: string, title: string, description: string, children: Content[]): IJSLVNode {\n return { tag: \"article\", attr: { id, class: \"sample-card\" }, children: [\n group([text(\"span\", number, \"sample-number\"), text(\"h3\", title)], \"card-heading\"),\n text(\"p\", description, \"card-description\"), ...children\n ] };\n}\n\nconst partialStatus = status(\"1 of 3 channels selected.\");\n\nconst channels = [\"Product news\", \"Release notes\", \"Community events\"].map((label, index) => new CheckBoxComponent({\n id: \"checkbox-channel-\" + index, label, value: index === 0,\n onChange: () => syncChannels()\n}));\n\nconst allChannels = new CheckBoxComponent({\n id: \"checkbox-all\", label: \"All updates\", indeterminate: true,\n onChange: checked => { channels.forEach(channel => channel.setValue(checked)); syncChannels(); }\n});\n\nfunction syncChannels() {\n const count = channels.filter(channel => channel.getValue()).length;\n allChannels.setValue(count === channels.length);\n allChannels.indeterminate = count > 0 && count < channels.length;\n partialStatus.content = count + \" of \" + channels.length + \" channels selected.\";\n}\n\n// Add <div id=\"example\"></div> to the page.\nnew ExampleRenderer(document.getElementById(\"example\"), true).render(\n card(\"checkbox-partial\", \"03\", \"Some, all, or none\", \"A mixed parent summarizes its children. Activating it selects or clears the whole group.\", [\n group([allChannels, group(channels, \"checkbox-child-list\")], \"checkbox-stack\"), partialStatus,\n text(\"pre\", \"checkbox.indeterminate = true;\\ncheckbox.setValue(true); // Silent checked update\", \"sample-code\")\n ])\n);",
@@ -39,6 +39,23 @@ export interface ISelectComponentOptions<T = any, TValue = any, TBinding = any>
39
39
  onRender?: (args: ISelectRenderArg<T>) => void | IJSLVNode;
40
40
  onRenderGroup?: (args: ISelectRenderGroupArg) => void | IJSLVNode;
41
41
  matchSearch?: (data: T, term: string) => boolean;
42
+ /**
43
+ * Switches to server-side search: the typed text is no longer matched against `data`. After `searchDelay` ms of
44
+ * typing (and with "" when the list opens with a stale search) this is called with the term; the owner answers
45
+ * with setData() and setLoading(). `matchSearch` is ignored.
46
+ */
47
+ onSearch?: (term: string) => void;
48
+ /** Milliseconds to wait after the last keystroke before onSearch runs. Default 300. */
49
+ searchDelay?: number;
50
+ /** Called whenever the list opens, for example to load the first batch lazily. */
51
+ onOpen?: () => void;
52
+ /**
53
+ * Called when the list is scrolled near its end while setData() reported more items. Start loading and call
54
+ * setLoading(true) at once; finish with setData(all items so far, hasMore) and setLoading(false).
55
+ */
56
+ onLoadMore?: () => void;
57
+ /** Shown at the end of the list while setLoading(true) is active. */
58
+ loadingText?: string;
42
59
  onAppend?: (dom: HTMLDivElement) => void;
43
60
  enableKeyboard?: boolean;
44
61
  multiple?: boolean;
@@ -72,6 +89,11 @@ export declare class SelectComponent<T = any, TValue = any, TBinding = any> impl
72
89
  private dropdownLimit;
73
90
  private disposed;
74
91
  private expandedBeforePress;
92
+ private loading;
93
+ private hasMore;
94
+ private moreRequested;
95
+ private searchedTerm;
96
+ private searchTimer;
75
97
  constructor(o: ISelectComponentOptions<T, TValue, TBinding>);
76
98
  render(): IJSLVNode;
77
99
  onCreate(node: IJSLVNode): void;
@@ -92,6 +114,19 @@ export declare class SelectComponent<T = any, TValue = any, TBinding = any> impl
92
114
  setValue(value: any): void;
93
115
  getSelected(): T | T[];
94
116
  setSelected(item: T | T[]): void;
117
+ /**
118
+ * Replaces the options, for example with the answer to onSearch or with more items for onLoadMore. The current
119
+ * selection is kept even when it is not part of the new data. `hasMore` tells whether onLoadMore may be called.
120
+ */
121
+ setData(data: T[], hasMore?: boolean): void;
122
+ /** Shows or hides the loading hint at the end of the list. Call with false when a search or load has finished. */
123
+ setLoading(loading: boolean): void;
124
+ /** @internal Used by the dropdown. */
125
+ isLoading(): boolean;
126
+ /** @internal Used by the dropdown: the typed text changed. Runs onSearch after searchDelay unless `immediate`. */
127
+ requestSearch(term: string, immediate?: boolean): void;
128
+ /** @internal Used by the dropdown: the end of the list is near. */
129
+ requestMore(): void;
95
130
  focus(): void;
96
131
  hasSelection(): boolean;
97
132
  protected renderSelection(): IJSLVNode;
@@ -13,6 +13,11 @@ export class SelectComponent {
13
13
  this.dropdownLimit = 0;
14
14
  this.disposed = false;
15
15
  this.expandedBeforePress = false;
16
+ // Server-side search state. searchedTerm is the term onSearch last ran with.
17
+ this.loading = false;
18
+ this.hasMore = false;
19
+ this.moreRequested = false;
20
+ this.searchedTerm = "";
16
21
  this.onOutsidePress = (event) => {
17
22
  var _a, _b;
18
23
  if (!((_a = this.dom) === null || _a === void 0 ? void 0 : _a.contains(event.target)) && !((_b = this.dropdownArea) === null || _b === void 0 ? void 0 : _b.contains(event.target))) {
@@ -63,7 +68,7 @@ export class SelectComponent {
63
68
  enableKeyboard: true, rowHeight: 36, allowEmpty: true, enabled: true,
64
69
  openOnEnter: true, focusFirstItem: true, autoShrink: true, bindMode: "toModel",
65
70
  searchPlaceholder: "Search options…", emptyText: "No matching options",
66
- clearButtonLabel: "Clear selection",
71
+ clearButtonLabel: "Clear selection", searchDelay: 300, loadingText: "Loading…",
67
72
  onRenderGroup: args => { var _a; args.node.content = String((_a = args.groupValue) !== null && _a !== void 0 ? _a : ""); },
68
73
  matchSearch: (data, term) => { var _a; return String((_a = data[this.options.textColumn]) !== null && _a !== void 0 ? _a : "").toLowerCase().includes(term.toLowerCase()); },
69
74
  ...o, data: (_a = o.data) !== null && _a !== void 0 ? _a : []
@@ -147,7 +152,7 @@ export class SelectComponent {
147
152
  isExpanded() { return this.expanded; }
148
153
  getListId() { return this.listId; }
149
154
  show(searchTxt) {
150
- var _a, _b, _c;
155
+ var _a, _b, _c, _d, _e;
151
156
  if (!this.options.enabled || this.disposed || !this.dom || this.expanded) {
152
157
  return;
153
158
  }
@@ -174,7 +179,7 @@ export class SelectComponent {
174
179
  }
175
180
  if (this.options.autoShrink) {
176
181
  const contentHeight = (data.length ? data.length * this.options.rowHeight : 64) +
177
- (this.options.enableSearch ? 48 : 0) + 8;
182
+ (this.options.enableSearch ? 48 : 0) + 8 + (this.loading ? 28 : 0);
178
183
  this.dropdownArea.style.height = Math.min(contentHeight, this.dropdownLimit) + "px";
179
184
  }
180
185
  this.positionDropdown();
@@ -184,12 +189,17 @@ export class SelectComponent {
184
189
  this.dropdownRenderer.render(this.dropdownComponent);
185
190
  JSLRender.lastCreatedRenderer = previousRenderer;
186
191
  this.positionDropdown();
192
+ // A fresh list starts with an empty search; make the owner drop results of an earlier search.
193
+ if (this.options.onSearch && !searchTxt) {
194
+ this.requestSearch("", true);
195
+ }
196
+ (_d = (_c = this.options).onOpen) === null || _d === void 0 ? void 0 : _d.call(_c);
187
197
  document.addEventListener("mousedown", this.onOutsidePress, true);
188
198
  document.addEventListener("touchstart", this.onOutsidePress, true);
189
199
  document.addEventListener("focusin", this.onDocumentFocus);
190
200
  window.addEventListener("resize", this.positionDropdown);
191
201
  window.addEventListener("scroll", this.onWindowScroll, true);
192
- (_c = this.renderer) === null || _c === void 0 ? void 0 : _c.refresh();
202
+ (_e = this.renderer) === null || _e === void 0 ? void 0 : _e.refresh();
193
203
  this.updateTriggerAria();
194
204
  this.focusFrame = requestAnimationFrame(() => {
195
205
  var _a;
@@ -212,6 +222,7 @@ export class SelectComponent {
212
222
  cancelAnimationFrame(this.focusFrame);
213
223
  this.focusFrame = undefined;
214
224
  }
225
+ clearTimeout(this.searchTimer);
215
226
  const wasExpanded = this.expanded;
216
227
  this.expanded = false;
217
228
  this.focused = null;
@@ -264,6 +275,60 @@ export class SelectComponent {
264
275
  this.options.onChange.notify(value, this.selected);
265
276
  this.refresh();
266
277
  }
278
+ /**
279
+ * Replaces the options, for example with the answer to onSearch or with more items for onLoadMore. The current
280
+ * selection is kept even when it is not part of the new data. `hasMore` tells whether onLoadMore may be called.
281
+ */
282
+ setData(data, hasMore = false) {
283
+ var _a;
284
+ const key = this.options.keyColumn;
285
+ this.options.data = data !== null && data !== void 0 ? data : [];
286
+ this.hasMore = hasMore;
287
+ this.moreRequested = false;
288
+ // Selection and focus compare by identity: point them at the new objects.
289
+ const current = (item) => { var _a; return (_a = this.options.data.find(candidate => candidate[key] === item[key])) !== null && _a !== void 0 ? _a : item; };
290
+ if (this.selected != null) {
291
+ this.selected = this.options.multiple ? this.selected.map(current) : current(this.selected);
292
+ }
293
+ (_a = this.dropdownComponent) === null || _a === void 0 ? void 0 : _a.dataChanged();
294
+ this.refresh();
295
+ }
296
+ /** Shows or hides the loading hint at the end of the list. Call with false when a search or load has finished. */
297
+ setLoading(loading) {
298
+ this.loading = loading;
299
+ if (!loading) {
300
+ this.moreRequested = false;
301
+ }
302
+ this.refresh();
303
+ }
304
+ /** @internal Used by the dropdown. */
305
+ isLoading() { return this.loading; }
306
+ /** @internal Used by the dropdown: the typed text changed. Runs onSearch after searchDelay unless `immediate`. */
307
+ requestSearch(term, immediate = false) {
308
+ clearTimeout(this.searchTimer);
309
+ if (term === this.searchedTerm) {
310
+ return;
311
+ }
312
+ const run = () => {
313
+ this.searchedTerm = term;
314
+ this.moreRequested = false;
315
+ this.options.onSearch(term);
316
+ };
317
+ if (immediate) {
318
+ run();
319
+ }
320
+ else {
321
+ this.searchTimer = setTimeout(run, this.options.searchDelay);
322
+ }
323
+ }
324
+ /** @internal Used by the dropdown: the end of the list is near. */
325
+ requestMore() {
326
+ if (!this.options.onLoadMore || !this.hasMore || this.loading || this.moreRequested) {
327
+ return;
328
+ }
329
+ this.moreRequested = true;
330
+ this.options.onLoadMore();
331
+ }
267
332
  focus() {
268
333
  var _a;
269
334
  if (this.dom) {
@@ -315,14 +380,23 @@ export class SelectComponent {
315
380
  return node;
316
381
  }
317
382
  selectValue(value) {
318
- var _a;
383
+ var _a, _b;
319
384
  if (value == null) {
320
385
  this.selected = this.options.multiple ? [] : null;
321
386
  return;
322
387
  }
323
- this.selected = this.options.multiple ?
324
- this.options.data.filter(item => Array.isArray(value) && value.indexOf(item[this.options.keyColumn]) >= 0) :
325
- (_a = this.options.data.find(item => item[this.options.keyColumn] === value)) !== null && _a !== void 0 ? _a : null;
388
+ const key = this.options.keyColumn;
389
+ // Items the current data no longer lists (after setData) stay selectable by their key.
390
+ const previous = this.selected == null ? [] : Array.isArray(this.selected) ? this.selected : [this.selected];
391
+ if (this.options.multiple) {
392
+ const keys = Array.isArray(value) ? value : [];
393
+ const listed = this.options.data.filter(item => keys.indexOf(item[key]) >= 0);
394
+ this.selected = [...listed, ...previous.filter(item => keys.indexOf(item[key]) >= 0 &&
395
+ !listed.some(other => other[key] === item[key]))];
396
+ }
397
+ else {
398
+ this.selected = (_b = (_a = this.options.data.find(item => item[key] === value)) !== null && _a !== void 0 ? _a : previous.find(item => item[key] === value)) !== null && _b !== void 0 ? _b : null;
399
+ }
326
400
  }
327
401
  refresh() { var _a, _b; (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.refresh(); (_b = this.dropdownRenderer) === null || _b === void 0 ? void 0 : _b.refresh(); }
328
402
  focusWithoutOpening() {
@@ -19,14 +19,20 @@ export declare class SelectDDAreaComponent implements IJSLComponent {
19
19
  private searchText;
20
20
  private optionPositions;
21
21
  private optionCount;
22
+ private removed;
22
23
  constructor(options: ISelectComponentOptions<any>, component: SelectComponent, args: IDDArguments);
23
24
  onInit(renderer: JSLRender): void;
24
25
  onCreate(node: IJSLVNode): void;
26
+ onRemove(): void;
25
27
  render(): IJSLVNode;
26
28
  navigateDown(): void;
27
29
  navigateUp(): void;
28
30
  search(term: string): void;
31
+ /** The owner replaced the data: keep the focused item if it is still listed, otherwise focus the first. */
32
+ dataChanged(): void;
29
33
  clearSearch(): void;
34
+ private checkLoadMore;
35
+ private checkLoadMoreSoon;
30
36
  private isGroup;
31
37
  private renderRow;
32
38
  private focusFirst;
@@ -9,6 +9,16 @@ export class SelectDDAreaComponent {
9
9
  this.onRender = new EventHandler();
10
10
  this.optionPositions = new Map();
11
11
  this.optionCount = 0;
12
+ this.removed = false;
13
+ this.checkLoadMore = () => {
14
+ const list = this.scrollContainer;
15
+ if (!list || this.removed) {
16
+ return;
17
+ }
18
+ if (list.scrollTop + list.clientHeight >= list.scrollHeight - this.options.rowHeight * 3) {
19
+ this.component.requestMore();
20
+ }
21
+ };
12
22
  this.searchText = (_a = args.searchTxt) !== null && _a !== void 0 ? _a : "";
13
23
  if (this.searchText) {
14
24
  this.search(this.searchText);
@@ -28,6 +38,13 @@ export class SelectDDAreaComponent {
28
38
  this.scrollContainer = this.dom.querySelector(".sel-dd-items");
29
39
  this.scrollToFocus();
30
40
  this.renderer.refresh();
41
+ this.scrollContainer.addEventListener("scroll", this.checkLoadMore);
42
+ this.checkLoadMoreSoon();
43
+ }
44
+ onRemove() {
45
+ var _a;
46
+ (_a = this.scrollContainer) === null || _a === void 0 ? void 0 : _a.removeEventListener("scroll", this.checkLoadMore);
47
+ this.removed = true;
31
48
  }
32
49
  render() {
33
50
  var _a, _b, _c, _d;
@@ -60,6 +77,7 @@ export class SelectDDAreaComponent {
60
77
  this.virtualList.setData(data);
61
78
  this.onRender.notify(data);
62
79
  const label = (_d = (_c = (_b = this.options.ariaLabel) !== null && _b !== void 0 ? _b : this.options.placeholder) !== null && _c !== void 0 ? _c : this.options.placehoder) !== null && _d !== void 0 ? _d : "Options";
80
+ const loading = this.component.isLoading();
63
81
  return {
64
82
  tag: "div", attr: {
65
83
  class: "sel-dd-panel" + (this.options.enableAnimation ? " sel-dd-animate" : ""),
@@ -86,8 +104,12 @@ export class SelectDDAreaComponent {
86
104
  "aria-activedescendant": this.options.enableSearch ? undefined : activeId
87
105
  }, children: [
88
106
  this.virtualList,
89
- { tag: "div", attr: { class: "sel-empty", role: "status", hidden: data.length ? "hidden" : undefined }, content: this.options.emptyText }
107
+ { tag: "div", attr: { class: "sel-empty", role: "status", hidden: data.length || loading ? "hidden" : undefined }, content: this.options.emptyText }
90
108
  ]
109
+ },
110
+ {
111
+ tag: "div", attr: { class: "sel-status", role: "status", hidden: loading ? undefined : "hidden" },
112
+ content: loading ? this.options.loadingText : undefined
91
113
  }
92
114
  ]
93
115
  };
@@ -97,17 +119,41 @@ export class SelectDDAreaComponent {
97
119
  search(term) {
98
120
  var _a;
99
121
  this.searchText = term;
100
- this.filteredData = term ? this.options.data.filter(item => this.options.matchSearch(item, term)) : null;
122
+ // With server-side search the owner replaces the data (dataChanged); nothing is filtered here.
123
+ const remote = this.options.onSearch != null;
124
+ this.filteredData = term && !remote ? this.options.data.filter(item => this.options.matchSearch(item, term)) : null;
101
125
  if (this.scrollContainer) {
102
126
  this.scrollContainer.scrollTop = 0;
103
127
  }
104
128
  if (this.virtualList) {
105
129
  this.virtualList.State.scrollPos = 0;
106
130
  }
107
- this.focusFirst();
131
+ if (remote) {
132
+ this.component.requestSearch(term);
133
+ }
134
+ else {
135
+ this.focusFirst();
136
+ }
137
+ (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.refresh();
138
+ }
139
+ /** The owner replaced the data: keep the focused item if it is still listed, otherwise focus the first. */
140
+ dataChanged() {
141
+ var _a;
142
+ const key = this.options.keyColumn;
143
+ const focus = this.component.getFocus();
144
+ const same = focus == null ? undefined : this.options.data.find(item => item[key] === focus[key]);
145
+ if (same) {
146
+ this.component.setFocus(same);
147
+ }
148
+ else {
149
+ this.focusFirst();
150
+ }
108
151
  (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.refresh();
152
+ this.checkLoadMoreSoon();
109
153
  }
110
154
  clearSearch() { this.search(""); }
155
+ // After a render: a list too short to scroll would otherwise never ask for more.
156
+ checkLoadMoreSoon() { requestAnimationFrame(this.checkLoadMore); }
111
157
  isGroup(item) { return this.options.groupProperty != null && (item === null || item === void 0 ? void 0 : item.__isGroup) === true; }
112
158
  renderRow(item, index) {
113
159
  var _a, _b;
@@ -191,8 +191,10 @@
191
191
  font-weight: 600;
192
192
  cursor: default;
193
193
  }
194
- .sel-empty { padding: 1.25rem .625rem; color: var(--jsl-color-text-muted); font-size: .8125rem; text-align: center; }
194
+ .sel-empty { position: absolute; left: 0; right: 0; top: 0; padding: 1.25rem .625rem; color: var(--jsl-color-text-muted); font-size: .8125rem; text-align: center; }
195
195
  .sel-empty[hidden] { display: none; }
196
+ .sel-status { flex: 0 0 auto; padding: .375rem .625rem; color: var(--jsl-color-text-muted); font-size: .75rem; text-align: center; }
197
+ .sel-status[hidden] { display: none; }
196
198
  .sel-dd-animate { animation: jsl-select-appear .12s ease-out; }
197
199
  @keyframes jsl-select-appear { from { opacity: 0; } to { opacity: 1; } }
198
200
  @media (prefers-reduced-motion: reduce) {
package/lib/index.css CHANGED
@@ -1986,12 +1986,28 @@ input.editor-text, textarea.editor-text {
1986
1986
  text-align: center;
1987
1987
  padding: 1.25rem .625rem;
1988
1988
  font-size: .8125rem;
1989
+ position: absolute;
1990
+ top: 0;
1991
+ left: 0;
1992
+ right: 0;
1989
1993
  }
1990
1994
 
1991
1995
  .sel-empty[hidden] {
1992
1996
  display: none;
1993
1997
  }
1994
1998
 
1999
+ .sel-status {
2000
+ color: var(--jsl-color-text-muted);
2001
+ text-align: center;
2002
+ flex: none;
2003
+ padding: .375rem .625rem;
2004
+ font-size: .75rem;
2005
+ }
2006
+
2007
+ .sel-status[hidden] {
2008
+ display: none;
2009
+ }
2010
+
1995
2011
  .sel-dd-animate {
1996
2012
  animation: .12s ease-out jsl-select-appear;
1997
2013
  }