@onsvisual/svelte-components 1.0.31 → 1.0.32

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.
@@ -2,6 +2,8 @@
2
2
  // @ts-nocheck
3
3
 
4
4
  import { onMount, createEventDispatcher } from "svelte";
5
+ import Dropdown from "../Dropdown/Dropdown.svelte";
6
+ import Input from "../Input/Input.svelte";
5
7
 
6
8
  const dispatch = createEventDispatcher();
7
9
  const sleep = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -68,6 +70,11 @@
68
70
  * @type {string|null}
69
71
  */
70
72
  export let groupKey = null;
73
+ /**
74
+ * When using SSR or pre-rendering, this option will render a text input or dropdown before the page is hydrated (allows for progressive enhancement).
75
+ * @type {boolean}
76
+ */
77
+ export let renderFallback = false;
71
78
  /**
72
79
  * Optional: Minimum query length to return results
73
80
  * @type {number}
@@ -92,23 +99,22 @@
92
99
  */
93
100
  export let scriptUrl =
94
101
  "https://cdn.ons.gov.uk/vendor/accessible-autocomplete/3.0.1/accessible-autocomplete.min.js";
95
-
96
- // This clearing method is a bit of a hack, but no better options available at present
97
- // https://github.com/alphagov/accessible-autocomplete/issues/390
98
102
  /**
99
103
  * Call this function externally to clear the input
100
104
  * @type {function}
101
105
  */
102
- export async function clearInput() {
106
+ export let clearInput = async () => {
103
107
  await setInputValue(null);
104
108
  dispatch("clear", null);
105
- }
109
+ };
106
110
  /**
107
111
  * Optional: Set an additional CSS class for the component
108
112
  * @type {string|null}
109
113
  */
110
114
  export let cls = null;
111
115
 
116
+ // This method is a bit of a hack, but no better options available at present
117
+ // https://github.com/alphagov/accessible-autocomplete/issues/390
112
118
  async function setInputValue(textValue) {
113
119
  hideMenu = true;
114
120
  inputElement.value = textValue || "";
@@ -204,35 +210,48 @@
204
210
  <script src={scriptUrl} on:load={handleScriptLoad}></script>
205
211
  </svelte:head>
206
212
 
207
- <div class="ons-field {cls}">
208
- {#if label}<label for={id} class="ons-label" class:ons-u-vh={hideLabel}>{label}</label>{/if}
209
- <div class="ons-autocomplete-wrapper">
210
- {#if scriptLoaded}
211
- <div
212
- id="{id}-container"
213
- class="ons-autocomplete"
214
- class:hide-menu={hideMenu}
215
- use:initAutocomplete
216
- ></div>
217
- {#if clearable && !autoClear && value}
218
- <button
219
- type="reset"
220
- title="Clear selection"
221
- aria-label="Clear selection"
222
- on:click={clearInput}
223
- class="ons-autocomplete-clear"
224
- >
225
- <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" viewBox="0 0 14 14" width="18">
226
- <path
227
- fill="currentColor"
228
- d="M13.6 1 l -0.71 -0.71 a 0.5 0.5 0 0 0 -0.71 0 l -5.25 5.25 l -5.25 -5.25 a 0.51 0.51 0 0 0 -0.71 0 l -0.71 0.71 a 0.5 0.5 0 0 0 0 0.71 l 5.25 5.25 l -5.25 5.25 a 0.5 0.5 0 0 0 0 0.71 l 0.71 0.71 a 0.5 0.5 0 0 0 0.71 0 l 5.25 -5.25 l 5.25 5.25 a 0.5 0.5 0 0 0 0.71 0 l 0.71 -0.71 a 0.5 0.5 0 0 0 0 -0.71 l -5.25 -5.25 l 5.25 -5.25 a 0.5 0.5 0 0 0 0 -0.71Z"
229
- ></path>
230
- </svg>
231
- </button>
213
+ {#if renderFallback && !scriptLoaded}
214
+ {#if mode === "search"}
215
+ <Input {id} {label} {hideLabel} value={value?.[labelKey]} />
216
+ {:else}
217
+ <Dropdown {id} {options} {label} {hideLabel} {placeholder} {value} />
218
+ {/if}
219
+ {:else}
220
+ <div class="ons-field {cls}">
221
+ {#if label}<label for={id} class="ons-label" class:ons-u-vh={hideLabel}>{label}</label>{/if}
222
+ <div class="ons-autocomplete-wrapper">
223
+ {#if scriptLoaded}
224
+ <div
225
+ id="{id}-container"
226
+ class="ons-autocomplete"
227
+ class:hide-menu={hideMenu}
228
+ use:initAutocomplete
229
+ ></div>
230
+ {#if clearable && !autoClear && value}
231
+ <button
232
+ type="reset"
233
+ title="Clear selection"
234
+ aria-label="Clear selection"
235
+ on:click={clearInput}
236
+ class="ons-autocomplete-clear"
237
+ >
238
+ <svg
239
+ xmlns="http://www.w3.org/2000/svg"
240
+ aria-hidden="true"
241
+ viewBox="0 0 14 14"
242
+ width="18"
243
+ >
244
+ <path
245
+ fill="currentColor"
246
+ d="M13.6 1 l -0.71 -0.71 a 0.5 0.5 0 0 0 -0.71 0 l -5.25 5.25 l -5.25 -5.25 a 0.51 0.51 0 0 0 -0.71 0 l -0.71 0.71 a 0.5 0.5 0 0 0 0 0.71 l 5.25 5.25 l -5.25 5.25 a 0.5 0.5 0 0 0 0 0.71 l 0.71 0.71 a 0.5 0.5 0 0 0 0.71 0 l 5.25 -5.25 l 5.25 5.25 a 0.5 0.5 0 0 0 0.71 0 l 0.71 -0.71 a 0.5 0.5 0 0 0 0 -0.71 l -5.25 -5.25 l 5.25 -5.25 a 0.5 0.5 0 0 0 0 -0.71Z"
247
+ ></path>
248
+ </svg>
249
+ </button>
250
+ {/if}
232
251
  {/if}
233
- {/if}
252
+ </div>
234
253
  </div>
235
- </div>
254
+ {/if}
236
255
 
237
256
  <style>
238
257
  .ons-autocomplete-wrapper {
@@ -14,17 +14,17 @@ export default class Select extends SvelteComponentTyped<{
14
14
  autoClear?: boolean | undefined;
15
15
  labelKey?: string | undefined;
16
16
  groupKey?: string | null | undefined;
17
+ renderFallback?: boolean | undefined;
17
18
  minLength?: number | undefined;
18
19
  loadOptions?: Function | undefined;
19
20
  scriptUrl?: string | undefined;
20
- clearInput?: (() => Promise<void>) | undefined;
21
+ clearInput?: Function | undefined;
21
22
  }, {
22
23
  clear: CustomEvent<any>;
23
24
  change: CustomEvent<any>;
24
25
  } & {
25
26
  [evt: string]: CustomEvent<any>;
26
27
  }, {}> {
27
- get clearInput(): () => Promise<void>;
28
28
  }
29
29
  export type SelectProps = typeof __propDef.props;
30
30
  export type SelectEvents = typeof __propDef.events;
@@ -44,10 +44,11 @@ declare const __propDef: {
44
44
  autoClear?: boolean | undefined;
45
45
  labelKey?: string | undefined;
46
46
  groupKey?: string | null | undefined;
47
+ renderFallback?: boolean | undefined;
47
48
  minLength?: number | undefined;
48
49
  loadOptions?: Function | undefined;
49
50
  scriptUrl?: string | undefined;
50
- clearInput?: (() => Promise<void>) | undefined;
51
+ clearInput?: Function | undefined;
51
52
  };
52
53
  events: {
53
54
  clear: CustomEvent<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.0.31",
3
+ "version": "1.0.32",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",