Skip to content

Commit

Permalink
Add exit this page component generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Aug 26, 2024
1 parent 00c94ff commit 418b03e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Immutable;
using HtmlTags;

namespace GovUk.Frontend.AspNetCore.ComponentGeneration;

public partial class DefaultComponentGenerator
{
internal const string ExitThisPageElement = "div";

/// <inheritdoc/>
public HtmlTag GenerateExitThisPage(ExitThisPageOptions options)
{
ArgumentNullException.ThrowIfNull(options);
options.Validate();

return new HtmlTag(ExitThisPageElement)
.AddEncodedAttributeIfNotNull("id", options.Id)
.AddClass("govuk-exit-this-page")
.AddClasses(ExplodeClasses(options.Classes))
.UnencodedAttr("data-module", "govuk-exit-this-page")
.MergeEncodedAttributes(options.Attributes)
.AddEncodedAttributeIf(options.ActivatedText.NormalizeEmptyString() is not null, "data-i18n.activated", HtmlEncode(options.ActivatedText))
.AddEncodedAttributeIf(options.TimedOutText.NormalizeEmptyString() is not null, "data-i18n.timed-out", HtmlEncode(options.TimedOutText))
.AddEncodedAttributeIf(options.PressTwoMoreTimesText.NormalizeEmptyString() is not null, "data-i18n.press-two-more-times", HtmlEncode(options.PressTwoMoreTimesText))
.AddEncodedAttributeIf(options.PressOneMoreTimeText.NormalizeEmptyString() is not null, "data-i18n.press-one-more-time", HtmlEncode(options.PressOneMoreTimeText))
.Append(GenerateButton(new ButtonOptions()
{
Html = (options.Html.NormalizeEmptyString() ?? options.Text.NormalizeEmptyString()) is not null ?
options.Html :
new HtmlTag("span")
.AddClass("govuk-visually-hidden")
.Text("Emergency")
.After(new HtmlTag(null).Text("Exit this page"))
.ToHtmlString(),
Text = options.Text.NormalizeEmptyString(),
Classes = "govuk-button--warning govuk-exit-this-page__button govuk-js-exit-this-page-button",
Href = options.RedirectUrl.NormalizeEmptyString() ?? "https://www.bbc.co.uk/weather",
Attributes = ImmutableDictionary<string, string?>.Empty
.Add("rel", "nofollow noreferrer")
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace GovUk.Frontend.AspNetCore.ComponentGeneration;

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public record ExitThisPageOptions
{
public string? Text { get; set; }
public string? Html { get; set; }
public string? RedirectUrl { get; set; }
public string? Id { get; set; }
public string? Classes { get; set; }
public IReadOnlyDictionary<string, string?>? Attributes { get; set; }
public string? ActivatedText { get; set; }
public string? TimedOutText { get; set; }
public string? PressTwoMoreTimesText { get; set; }
public string? PressOneMoreTimeText { get; set; }

internal void Validate()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public interface IComponentGenerator
/// <returns>An <see cref="HtmlTag"/> with the component's HTML.</returns>
HtmlTag GenerateErrorSummary(ErrorSummaryOptions options);

/// <summary>
/// Generates an exit this pagecomponent.
/// </summary>
/// <returns>An <see cref="HtmlTag"/> with the component's HTML.</returns>
HtmlTag GenerateExitThisPage(ExitThisPageOptions options);

/// <summary>
/// Generates a fieldset component.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void ErrorSummary(ComponentTestCaseData<ErrorSummaryOptions> data) =>
data,
(generator, options) => generator.GenerateErrorSummary(options).ToHtmlString());

[Theory]
[ComponentFixtureData("exit-this-page", typeof(ExitThisPageOptions), exclude: "testing")]
public void ExitThisPage(ComponentTestCaseData<ExitThisPageOptions> data) =>
CheckComponentHtmlMatchesExpectedHtml(
data,
(generator, options) => generator.GenerateExitThisPage(options).ToHtmlString());

[Theory]
[ComponentFixtureData("fieldset", typeof(FieldsetOptions))]
public void Fieldset(ComponentTestCaseData<FieldsetOptions> data) =>
Expand Down

0 comments on commit 418b03e

Please sign in to comment.