fix Search theme control so that it checks if search is enabled for site, and include AllowInput parameter to control input textbox

This commit is contained in:
sbwalker 2024-08-26 10:41:22 -04:00
parent 592255284f
commit aba3110e31

View File

@ -2,28 +2,39 @@
@using System.Net @using System.Net
@using Microsoft.AspNetCore.Http @using Microsoft.AspNetCore.Http
@inherits ThemeControlBase @inherits ThemeControlBase
@inject ISettingService SettingService
@inject IStringLocalizer<Search> Localizer @inject IStringLocalizer<Search> Localizer
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@if (_searchResultsPage != null) @if (_searchResultsPage != null)
{ {
<span class="app-search @CssClass"> @if (AllowInput)
{
<span class="app-search @CssClass">
<form method="post" class="app-form-inline" @formname="@($"SearchForm")" @onsubmit="@PerformSearch" data-enhance>
<input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
<input type="text" name="keywords" maxlength="50"
class="form-control d-inline-block pe-5 shadow-none"
@bind="_keywords"
placeholder="@Localizer["SearchPlaceHolder"]"
aria-label="Search" />
<button type="submit" class="btn btn-search">
<span class="oi oi-magnifying-glass align-middle"></span>
</button>
</form>
</span>
}
else
{
<form method="post" class="app-form-inline" @formname="@($"SearchForm")" @onsubmit="@PerformSearch" data-enhance> <form method="post" class="app-form-inline" @formname="@($"SearchForm")" @onsubmit="@PerformSearch" data-enhance>
<input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" /> <input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
<input type="text" name="keywords" maxlength="50" <button type="submit" class="btn btn-primary">
class="form-control d-inline-block pe-5 shadow-none"
@bind="_keywords"
placeholder="@Localizer["SearchPlaceHolder"]"
aria-label="Search" />
<button type="submit" class="btn btn-search">
<span class="oi oi-magnifying-glass align-middle"></span> <span class="oi oi-magnifying-glass align-middle"></span>
</button> </button>
</form> </form>
</span> }
} }
@code { @code {
private Page _searchResultsPage; private Page _searchResultsPage;
private string _keywords = ""; private string _keywords = "";
@ -32,7 +43,10 @@
public string CssClass { get; set; } public string CssClass { get; set; }
[Parameter] [Parameter]
public string SearchResultPagePath { get; set; } = "search"; public bool AllowInput { get; set; } = true; // setting to false will display only the search icon button - not the textbox
[Parameter]
public string SearchResultPagePath { get; set; } = "search"; // setting to "" will disable search
[CascadingParameter] [CascadingParameter]
HttpContext HttpContext { get; set; } HttpContext HttpContext { get; set; }
@ -42,9 +56,12 @@
protected override void OnInitialized() protected override void OnInitialized()
{ {
if(!string.IsNullOrEmpty(SearchResultPagePath)) if (bool.Parse(SettingService.GetSetting(PageState.Site.Settings, "Search_Enabled", "True")))
{ {
_searchResultsPage = PageState.Pages.FirstOrDefault(i => i.Path == SearchResultPagePath); if (!string.IsNullOrEmpty(SearchResultPagePath))
{
_searchResultsPage = PageState.Pages.FirstOrDefault(i => i.Path == SearchResultPagePath);
}
} }
} }