Skip to content

Commit

Permalink
Fix #3349 by adding script function that attaches datalist to the con…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
vnbaaij committed Feb 13, 2025
1 parent f60a549 commit 6792c29
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/Demo/Shared/Pages/Lab/IssueTester.razor
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@


<datalist id="colors">
<option value="red" />
<option value="green" />
<option value="blue" />
<option value="yellow" />
</datalist>
<FluentSearch DataList="colors" />


10 changes: 10 additions & 0 deletions src/Core/Components/Search/FluentSearch.razor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// ------------------------------------------------------------------------
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Extensions;
Expand Down Expand Up @@ -93,6 +97,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
await Module.InvokeVoidAsync("setControlAttribute", Id, "autocomplete", AutoComplete);
}
}

if (DataList != null && !string.IsNullOrEmpty(Id))
{
Module ??= await JSRuntime.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE.FormatCollocatedUrl(LibraryConfiguration));
await Module.InvokeVoidAsync("setDataList", Id, DataList);
}
}

protected override bool TryParseValueFromString(string? value, out string? result, [NotNullWhen(false)] out string? validationErrorMessage)
Expand Down
12 changes: 12 additions & 0 deletions src/Core/Components/Search/FluentSearch.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function setControlAttribute(id, attrName, value) {
fieldElement?.setAttribute(attrName, value);
}
}

export function setDataList(id, datalistid) {
const fieldElement = document.getElementById(id);
const dataList = document.getElementById(datalistid)?.cloneNode(true);

const shadowRoot = fieldElement.shadowRoot;
const shadowDataList = shadowRoot.getElementById(datalistid);
if (shadowDataList) {
shadowRoot.removeChild(shadowDataList);
}
shadowRoot.appendChild(dataList);
}

0 comments on commit 6792c29

Please sign in to comment.