@emuanalytics/flow-rdf-ui 4.1.28 → 4.2.1

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.
@@ -38,6 +38,8 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
38
38
  this.mouseDownAt = 0;
39
39
  this.pendingChangeEvent = false;
40
40
  this.pendingInputEvent = false;
41
+ /** @internal When true, the next value change leaves the input text as the user typed it */
42
+ this.pendingKeepInputText = false;
41
43
  this.hasOptions = false;
42
44
  /** @internal True while the user is typing to filter (input text has diverged from the committed value). */
43
45
  this.inputChanged = false;
@@ -206,16 +208,17 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
206
208
  *
207
209
  * @param value - The value to set.
208
210
  * @param emitChangeEvent - A change event will be emitted if true.
211
+ * @param keepInputText - The input text is not re-synced from {@link displayWith} if true.
209
212
  */
210
213
  setValue(value, options) {
211
214
  this.pendingChangeEvent = options?.emitChangeEvent ?? false;
212
215
  this.pendingInputEvent = options?.emitInputEvent ?? false;
216
+ this.pendingKeepInputText = options?.keepInputText ?? false;
213
217
  this.value = value;
214
218
  }
215
219
  updateDebounceDelay() {
216
220
  this.debouncedInput = debounce(
217
- (inputText) => {
218
- this.inputText = inputText;
221
+ () => {
219
222
  this.emit("flow-ui-input");
220
223
  },
221
224
  this.debounceDelay,
@@ -226,10 +229,14 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
226
229
  handleInput(event) {
227
230
  event.stopPropagation();
228
231
  this.inputChanged = true;
232
+ if (this.clearable && isStringFull(this.value)) {
233
+ this.setValue("", { emitChangeEvent: true, keepInputText: true });
234
+ }
229
235
  if (this.open) {
230
236
  this.highlightCurrentOption();
231
237
  }
232
- this.debouncedInput(this.input.value);
238
+ this.inputText = this.input.value;
239
+ this.debouncedInput();
233
240
  }
234
241
  /** @internal Handle the `flow-ui-change` event from the input box */
235
242
  handleInputChange(event) {
@@ -265,11 +272,27 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
265
272
  const target = event.target;
266
273
  const option = target.closest("flow-ui-option");
267
274
  if (option && !option.disabled) {
275
+ await this.commitOption(option);
276
+ }
277
+ }
278
+ /**
279
+ * @internal
280
+ * Commits an option picked by the user and closes the control. Re-picking the already selected
281
+ * option (e.g. after the input text was edited away) commits no new value, so no change event
282
+ * fires; the input text is re-synced from {@link displayWith} instead.
283
+ */
284
+ async commitOption(option) {
285
+ if (option.value !== this.value) {
268
286
  this.setValue(option.value, { emitChangeEvent: true });
269
287
  this.input.focus();
270
288
  await waitForEvent(this, "flow-ui-change");
271
- this.blur();
289
+ } else {
290
+ this.inputChanged = false;
291
+ this.setSelectedOption(option);
292
+ this.input.focus();
293
+ await this.syncDisplayValue();
272
294
  }
295
+ this.blur();
273
296
  }
274
297
  /** @internal */
275
298
  async handleKeyDown(event) {
@@ -306,11 +329,8 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
306
329
  event.preventDefault();
307
330
  event.stopPropagation();
308
331
  if (this.currentOption && !this.currentOption.disabled) {
309
- this.setSelectedOption(this.currentOption);
310
- this.setValue(this.currentOption.value, { emitChangeEvent: true });
332
+ await this.commitOption(this.currentOption);
311
333
  }
312
- await waitForEvent(this, "flow-ui-change");
313
- this.blur();
314
334
  }
315
335
  }
316
336
  /** @internal Add event listeners to the document when the dropdown is opened. */
@@ -370,14 +390,19 @@ class FlowUiAutocomplete extends FlowRdfFormControl {
370
390
  * Syncs the {@link displayValue} by applying {@link displayWith} based on the current {@link value}.
371
391
  */
372
392
  async syncDisplayValue() {
393
+ this.debouncedInput?.cancel();
373
394
  await this.updateComplete.then(async () => {
374
395
  this.inputText = await Promise.resolve(this.displayWith(this.value)) ?? "";
375
396
  });
376
397
  }
377
398
  async handleValueChange(oldValue, value) {
378
399
  if (value !== oldValue) {
379
- this.inputChanged = false;
380
- await this.syncDisplayValue();
400
+ if (this.pendingKeepInputText) {
401
+ this.pendingKeepInputText = false;
402
+ } else {
403
+ this.inputChanged = false;
404
+ await this.syncDisplayValue();
405
+ }
381
406
  const options = this.getAllOptions();
382
407
  this.assignOptionIds(options);
383
408
  this.setSelectedOption(null);