Skip to content

Commit ba7fe97

Browse files
authored
Tabbed paragraph changes (#283)
* Tabbed paragraph changes - Make the intro rich text - Allow external links in the buttons - Update validation * Update the mobile version of the button * Ensure links are correctly underlined in tabbed paragraph block text
1 parent 25bab41 commit ba7fe97

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

tbx/core/blocks.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,56 @@ class Meta:
605605
group = "Calls to action"
606606

607607

608+
class TabbedParagraphSectionsListBlock(blocks.ListBlock):
609+
def clean(self, value):
610+
result = super().clean(value)
611+
errors = {}
612+
613+
for i in range(0, len(result)):
614+
button_values = {
615+
"button_link": result[i]["button_link"],
616+
"button_text": result[i]["button_text"],
617+
"button_url": result[i]["button_url"],
618+
}
619+
620+
item_errors = ErrorList()
621+
622+
if button_values.get("button_link") and button_values.get("button_url"):
623+
item_errors.append(
624+
ValidationError(
625+
"You must specify either a page link or a URL, not both."
626+
)
627+
)
628+
629+
if button_values.get("button_text") and (
630+
not button_values.get("button_link")
631+
and not button_values.get("button_url")
632+
):
633+
item_errors.append(
634+
"You must specify a button link or URL for the button text you have provided."
635+
)
636+
637+
if item_errors:
638+
errors[i] = item_errors
639+
640+
if errors:
641+
raise blocks.ListBlockValidationError(block_errors=errors)
642+
643+
return result
644+
645+
608646
class TabbedParagraphBlock(blocks.StructBlock):
609647
title = blocks.CharBlock(max_length=255, required=False)
610-
intro = blocks.TextBlock(label="Introduction", required=False)
611-
tabbed_paragraph_sections = blocks.ListBlock(
648+
intro = blocks.RichTextBlock(label="Introduction", required=False)
649+
tabbed_paragraph_sections = TabbedParagraphSectionsListBlock(
612650
blocks.StructBlock(
613651
[
614652
("name", blocks.CharBlock()),
615653
("summary", blocks.TextBlock()),
616654
("text", blocks.RichTextBlock()),
617655
("button_text", blocks.CharBlock(required=False)),
618656
("button_link", blocks.PageChooserBlock(required=False)),
657+
("button_url", blocks.URLBlock(required=False)),
619658
],
620659
help_text="Add a tabbed paragraph, with a name, summary, text and an optional page link & button text",
621660
icon="breadcrumb-expand",
@@ -627,30 +666,13 @@ class TabbedParagraphBlock(blocks.StructBlock):
627666
def clean(self, value):
628667
value = super().clean(value)
629668
errors = defaultdict(ErrorList)
630-
non_block_errors = ErrorList()
631669

632670
if value["intro"] and not value["title"]:
633671
message = "You cannot add an intro without also adding a title"
634672
errors["title"].append(ValidationError(message))
635673

636-
for tabbed_paragraph_section in value["tabbed_paragraph_sections"]:
637-
button_values = {
638-
"button_link": tabbed_paragraph_section["button_link"],
639-
"button_text": tabbed_paragraph_section["button_text"],
640-
}
641-
642-
if any(button_values.values()) and not all(button_values.values()):
643-
message = "There must be a value for both button link and text, if one has a value."
644-
645-
for key, value in button_values.items():
646-
if not value:
647-
errors[key].append(message)
648-
non_block_errors.append(ValidationError(message))
649-
650-
if errors or non_block_errors:
651-
raise blocks.StructBlockValidationError(
652-
block_errors=errors, non_block_errors=non_block_errors
653-
)
674+
if errors:
675+
raise blocks.StructBlockValidationError(block_errors=errors)
654676

655677
return value
656678

tbx/project_styleguide/templates/patterns/molecules/streamfield/blocks/tabbed_paragraph_block.html

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{% endif %}
1010

1111
{% if value.intro %}
12-
<p class="tabbed-paragraph__intro">{{ value.intro }}</p>
12+
<div class="tabbed-paragraph__intro rich-text">{{ value.intro }}</div>
1313
{% endif %}
1414

1515
{# desktop - tabs #}
@@ -40,11 +40,19 @@
4040
<div class="tabs__panel-summary">
4141
{{ item.summary }}
4242
</div>
43-
<div class="tabs__panel-text">
43+
<div class="tabs__panel-text rich-text">
4444
{{ item.text|richtext }}
4545
</div>
46-
{% if item.button_text and item.button_link %}
47-
<a href="{% pageurl item.button_link %}" class="button tabs__panel-button">{{ item.button_text }}</a>
46+
{% if item.button_text%}
47+
{% if item.button_link or item.button_url %}
48+
{% if item.button_link %}
49+
<a href="{% pageurl item.button_link %}" class="button tabs__panel-button">
50+
{% else %}
51+
<a href="{{ item.button_url }}" class="button tabs__panel-button">
52+
{% endif %}
53+
{{ item.button_text }}
54+
</a>
55+
{% endif %}
4856
{% endif %}
4957
</div>
5058
{% endfor %}
@@ -67,11 +75,19 @@
6775
<div class="tabs-mobile__summary-text">
6876
{{ tab.summary }}
6977
</div>
70-
<div class="tabs-mobile__text">
78+
<div class="tabs-mobile__text rich-text">
7179
{{ tab.text|richtext }}
7280
</div>
73-
{% if tab.button_text and tab.button_link %}
74-
<a href="{% pageurl tab.button_link %}" class="button tabs-mobile__button">{{ tab.button_text }}</a>
81+
{% if tab.button_text %}
82+
{% if tab.button_link or tab.button_url %}
83+
{% if tab.button_link %}
84+
<a href="{% pageurl tab.button_link %}" class="button tabs-mobile__button">
85+
{% else %}
86+
<a href="{{ tab.button_url }}" class="button tabs-mobile__button">
87+
{% endif %}
88+
{{ tab.button_text }}
89+
</a>
90+
{% endif %}
7591
{% endif %}
7692
</div>
7793
</details>

tbx/project_styleguide/templates/patterns/molecules/streamfield/blocks/tabbed_paragraph_block.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
context:
22
value:
33
title: Putting people at the heart of our process
4-
intro: Cover the full spectrum of SEO, so whether you need a quick site health check, some training and support or a full SEO strategy, we can help.
4+
intro: <p>Cover the full <a href="#">spectrum of SEO</a>, so whether you need a quick site health check, some training and support or a full SEO strategy, we can help.</p>
55
tabbed_paragraph_sections:
66
- name: Strategy and consultancy
77
summary: Gathering insights into people’s needs and behaviours to inform and guide design solutions.

0 commit comments

Comments
 (0)