Skip to content

Commit

Permalink
CCMSPUI-466 | add classes attribute to details component
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar461 committed Dec 16, 2024
1 parent 65ec298 commit d404077
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,28 @@ protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
Map<String, String> attributes = ProcessorUtils.parseAttributes(context, tag);
String summaryText = attributes.getOrDefault("summaryText", "");
String text = attributes.getOrDefault("text", "");
String classNames = buildClassNames(attributes);

// Build the HTML structure
String detailsHtml = buildDetailsHtml(summaryText, text);
String detailsHtml = buildDetailsHtml(summaryText, text, classNames);

// Create the model and replace the tag
final IModelFactory modelFactory = context.getModelFactory();
final IModel model = modelFactory.parse(context.getTemplateData(), detailsHtml);
structureHandler.replaceWith(model, false);
}

private String buildDetailsHtml(String summaryText, String text) {
private String buildClassNames(Map<String, String> attributes) {
String classNames = "govuk-details";
if (attributes.containsKey("classes")) {
classNames += " " + attributes.get("classes");
}
return classNames;
}

private String buildDetailsHtml(String summaryText, String text, String classNames) {
return new StringBuilder()
.append("<details class=\"").append("govuk-details").append("\">")
.append("<details class=\"").append(classNames).append("\">")
.append("<summary class=\"").append("govuk-details__summary").append("\">")
.append("<span class=\"").append("govuk-details__summary-text").append("\">")
.append(summaryText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ void shouldRenderGovukButton() {

Context context = new Context();
String renderedHtml = templateEngine.process("test-details", context);
System.out.println(renderedHtml);
assertThat(renderedHtml)
.contains(
"<details class=\"govuk-details\"><summary class=\"govuk-details__summary\">" +
"<span class=\"govuk-details__summary-text\">Help with nationality</span>" +
"</summary><div class=\"govuk-details__text\">We need to know your nationality " +
"so we can work out which elections you're entitled to vote in.</div></details>");
"so we can work out which elections you're entitled to vote in.</div></details>")
.contains(
"<details class=\"govuk-details govuk-!-margin-top-2\"><summary " +
"class=\"govuk-details__summary\"><span class=\"govuk-details__summary-text\">" +
"Help with nationality</span></summary><div class=\"govuk-details__text\">" +
"We need to know your nationality so we can work out which elections you're " +
"entitled to vote in.</div></details>");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<govuk:details summaryText="Help with nationality"
text="We need to know your nationality so we can work out which elections you're entitled to vote in."/>

<govuk:details classes="govuk-!-margin-top-2" summaryText="Help with nationality"
text="We need to know your nationality so we can work out which elections you're entitled to vote in."/>

</body>
</html>

0 comments on commit d404077

Please sign in to comment.