Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Allow custom CSS styles on Typeahead component #292

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Blazored.Typeahead/BlazoredTypeahead.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@typeparam TItem
@typeparam TValue

<div class="blazored-typeahead @FieldCssClasses">
<div class="blazored-typeahead @_classAttribute @FieldCssClasses" style="@_styleAttribute">

<div class="blazored-typeahead__controls">

Expand Down Expand Up @@ -82,7 +82,7 @@
@onkeyup:preventDefault="@PreventDefault"
@onfocus="HandleInputFocus"
autocomplete="off"
@attributes="AdditionalAttributes"
@attributes="_capturedAttributes"
type="text"
class="blazored-typeahead__input blazored-typeahead__input-multi" />
</div>
Expand Down Expand Up @@ -112,7 +112,7 @@
@onkeyup:preventDefault="@PreventDefault"
@onfocus="HandleInputFocus"
autocomplete="off"
@attributes="AdditionalAttributes"
@attributes="_capturedAttributes"
type="text"
class="blazored-typeahead__input @(IsShowingMask ? "blazored-typeahead__input-hidden" : null)" />
}
Expand Down
28 changes: 27 additions & 1 deletion src/Blazored.Typeahead/BlazoredTypeahead.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public partial class BlazoredTypeahead<TItem, TValue> : ComponentBase, IDisposab
private bool _eventsHookedUp = false;
private ElementReference _searchInput;
private ElementReference _mask;
private IReadOnlyDictionary<string, object>? _capturedAttributes;
private string _classAttribute = string.Empty;
private string _styleAttribute = string.Empty;

[Inject] private IJSRuntime JSRuntime { get; set; }

Expand All @@ -40,7 +43,7 @@ public partial class BlazoredTypeahead<TItem, TValue> : ComponentBase, IDisposab
[Parameter] public RenderFragment HeaderTemplate { get; set; }
[Parameter] public RenderFragment FooterTemplate { get; set; }

[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object> AdditionalAttributes { get; set; }
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
[Parameter] public int MinimumLength { get; set; } = 1;
[Parameter] public int Debounce { get; set; } = 300;
[Parameter] public int MaximumSuggestions { get; set; } = 10;
Expand Down Expand Up @@ -108,6 +111,8 @@ protected override void OnInitialized()
throw new InvalidOperationException($"{GetType()} requires a {nameof(ResultTemplate)} parameter.");
}

_capturedAttributes = CaptureCssAttributes(AdditionalAttributes);

_debounceTimer = new System.Timers.Timer();
_debounceTimer.Interval = Debounce;
_debounceTimer.AutoReset = false;
Expand Down Expand Up @@ -140,6 +145,27 @@ private void Initialize()
IsShowingMask = Value != null;
}

private IReadOnlyDictionary<string, object>? CaptureCssAttributes(IReadOnlyDictionary<string, object>? additionalAttributes)
{
if (additionalAttributes == null)
{
return null;
}

var capturedAttributes = additionalAttributes.ToDictionary(k => k.Key, v => v.Value);
if (capturedAttributes.ContainsKey("class"))
{
_classAttribute = Convert.ToString(capturedAttributes["class"]) ?? string.Empty;
capturedAttributes.Remove("class");
}
if (capturedAttributes.ContainsKey("style"))
{
_styleAttribute = Convert.ToString(capturedAttributes["style"]) ?? string.Empty;
capturedAttributes.Remove("style");
}
return capturedAttributes;
}

private async Task RemoveValue(TValue item)
{
var valueList = Values ?? new List<TValue>();
Expand Down
2 changes: 2 additions & 0 deletions src/Blazored.Typeahead/wwwroot/blazored-typeahead.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
border: none;
padding: .5rem;
border-radius: 5px;
color: inherit;
background-color: inherit;
}

.blazored-typeahead:focus-within {
Expand Down