Merge pull request #3149 from alphagov/resize-all-textbox-dem

Let textareas resize automatically without having to highlight placeholders
This commit is contained in:
Chris Hill-Scott
2019-11-07 11:13:06 +00:00
committed by GitHub
19 changed files with 156 additions and 85 deletions

View File

@@ -0,0 +1,85 @@
(function(Modules) {
"use strict";
if (
!('oninput' in document.createElement('input'))
) return;
const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g;
Modules.EnhancedTextbox = function() {
this.start = function(textarea) {
let visibleTextbox;
this.highlightPlaceholders = (
typeof textarea.data('highlightPlaceholders') === 'undefined' ||
!!textarea.data('highlightPlaceholders')
);
this.$textbox = $(textarea)
.wrap(`
<div class='textbox-highlight-wrapper' />
`)
.after(this.$background = $(`
<div class="textbox-highlight-background" aria-hidden="true" />
`))
.on("input", this.update);
visibleTextbox = this.$textbox.clone().appendTo("body").css({
position: 'absolute',
visibility: 'hidden',
display: 'block'
});
this.initialHeight = visibleTextbox.height();
this.$background.css({
'border-width': this.$textbox.css('border-width')
});
visibleTextbox.remove();
this.$textbox
.trigger("input");
};
this.resize = () => {
this.$background.width(this.$textbox.outerWidth());
this.$textbox.height(
Math.max(
this.initialHeight,
this.$background.outerHeight()
)
);
if ('stickAtBottomWhenScrolling' in GOVUK) {
GOVUK.stickAtBottomWhenScrolling.recalculate();
}
};
this.contentEscaped = () => $('<div/>').text(this.$textbox.val()).html();
this.contentReplaced = () => this.contentEscaped().replace(
tagPattern, (match, name, separator, value) => value && separator ?
`<span class='placeholder-conditional'>((${name}??</span>${value}))` :
`<span class='placeholder'>((${name}${value}))</span>`
);
this.update = () => {
this.$background.html(
this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped()
);
this.resize();
};
};
})(window.GOVUK.Modules);

View File

@@ -1,64 +0,0 @@
(function(Modules) {
"use strict";
if (
!('oninput' in document.createElement('input'))
) return;
const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g;
Modules.HighlightTags = function() {
this.start = function(textarea) {
this.$textbox = $(textarea)
.wrap(`
<div class='textbox-highlight-wrapper' />
`)
.after(this.$background = $(`
<div class="textbox-highlight-background" aria-hidden="true" />
`))
.on("input", this.update);
this.initialHeight = this.$textbox.height();
this.$background.css({
'width': this.$textbox.outerWidth(),
'border-width': this.$textbox.css('border-width')
});
this.$textbox
.trigger("input");
};
this.resize = () => {
this.$textbox.height(
Math.max(
this.initialHeight,
this.$background.outerHeight()
)
);
if ('stickAtBottomWhenScrolling' in GOVUK) {
GOVUK.stickAtBottomWhenScrolling.recalculate();
}
};
this.escapedMessage = () => $('<div/>').text(this.$textbox.val()).html();
this.replacePlaceholders = () => this.$background.html(
this.escapedMessage().replace(
tagPattern, (match, name, separator, value) => value && separator ?
`<span class='placeholder-conditional'>((${name}??</span>${value}))` :
`<span class='placeholder'>((${name}${value}))</span>`
)
);
this.update = () => this.replacePlaceholders() && this.resize();
};
})(window.GOVUK.Modules);

View File

@@ -2,8 +2,9 @@
field,
label=None,
hint=False,
highlight_tags=False,
highlight_placeholders=False,
autofocus=False,
autosize=False,
colour_preview=False,
help_link=None,
help_link_text=None,
@@ -34,7 +35,7 @@
{% endif %}
</label>
{%
if highlight_tags
if highlight_placeholders or autosize
%}
{% set field_class = 'form-control-{} textbox-highlight-textbox'.format(width) %}
{% else %}
@@ -47,7 +48,8 @@
%}
{{ field(
class=field_class,
data_module='highlight-tags' if highlight_tags else '',
data_module='enhanced-textbox' if highlight_placeholders or autosize else '',
data_highlight_placeholders='true' if highlight_placeholders else 'false',
rows=rows|string,
**kwargs
) }}

View File

@@ -20,8 +20,8 @@
<div class="grid-row">
<div class="column-five-sixths">
{{ textbox(form.name, width='1-1', hint='Your recipients will not see this', rows=10) }}
{{ textbox(form.subject, width='1-1', highlight_tags=True, rows=2) }}
{{ textbox(form.template_content, highlight_tags=True, width='1-1', rows=8) }}
{{ textbox(form.subject, width='1-1', highlight_placeholders=True, rows=2) }}
{{ textbox(form.template_content, highlight_placeholders=True, width='1-1', rows=8) }}
{% if current_user.platform_admin %}
{{ radios(form.process_type) }}
{% endif %}

View File

@@ -20,8 +20,8 @@
<div class="grid-row">
<div class="column-five-sixths">
{{ textbox(form.name, width='1-1', hint='Your recipients will not see this', rows=10) }}
{{ textbox(form.subject, width='1-1', highlight_tags=True, rows=2) }}
{{ textbox(form.template_content, highlight_tags=True, width='1-1', rows=8) }}
{{ textbox(form.subject, width='1-1', highlight_placeholders=True, rows=2) }}
{{ textbox(form.template_content, highlight_placeholders=True, width='1-1', rows=8) }}
{{ sticky_page_footer(
'Save'
) }}

View File

@@ -22,7 +22,7 @@
{{ textbox(form.name, width='1-1', hint='Your recipients will not see this') }}
</div>
<div class="column-two-thirds">
{{ textbox(form.template_content, highlight_tags=True, width='1-1', rows=5) }}
{{ textbox(form.template_content, highlight_placeholders=True, width='1-1', rows=5) }}
{% if current_user.platform_admin %}
{{ radios(form.process_type) }}
{% endif %}

View File

@@ -21,7 +21,7 @@
belonging to this organisation requests to go live.
</p>
{% call form_wrapper() %}
{{ textbox(form.request_to_go_live_notes, width='1-1', rows=3) }}
{{ textbox(form.request_to_go_live_notes, width='1-1', rows=3, autosize=True) }}
{{ page_footer('Save') }}
{% endcall %}
</div>

View File

@@ -13,7 +13,7 @@
<div class="column-two-thirds">
<h1 class="heading-large">Submit returned letters</h1>
{% call form_wrapper() %}
{{ textbox(form.references, width='1-1', rows=8) }}
{{ textbox(form.references, width='1-1', rows=8, autosize=True) }}
{{ page_footer("Submit") }}
{% endcall %}
</div>

View File

@@ -45,6 +45,7 @@
form.something_else,
hint='Include links to your brand guidelines or examples of how to use your branding',
width='1-1',
autosize=True,
) }}
{% endcall %}
{% endif %}

View File

@@ -24,7 +24,7 @@
hint='10 lines maximum',
width='1-2',
rows=10,
highlight_tags=True
highlight_placeholders=True
) }}
{% if not first_contact_block %}
<div class="form-group">

View File

@@ -22,7 +22,7 @@
hint='10 lines maximum',
width='1-2',
rows=10,
highlight_tags=True
highlight_placeholders=True
) }}

View File

@@ -22,7 +22,7 @@
hint='10 lines maximum',
width='1-1',
rows=10,
highlight_tags=True
highlight_placeholders=True
) }}
{{ page_footer('Save') }}
{% endcall %}

View File

@@ -181,7 +181,7 @@
<h2 class="heading-large">Textbox</h2>
{{ textbox(form.username) }}
{{ textbox(form.password) }}
{{ textbox(form.message, highlight_tags=True) }}
{{ textbox(form.message, highlight_placeholders=True) }}
{{ textbox(form.code, width='1-8') }}
<h2 class="heading-large">File upload</h2>

View File

@@ -17,7 +17,7 @@
<div class="grid-row">
<div class="column-two-thirds">
{% call form_wrapper() %}
{{ textbox(form.feedback, width='1-1', hint='', rows=10) }}
{{ textbox(form.feedback, width='1-1', hint='', rows=10, autosize=True) }}
{% if not current_user.is_authenticated %}
<h3 class="heading-medium">Do you want a reply?</h3>
<p>Leave your details below if youd like a response.</p>

View File

@@ -24,7 +24,7 @@
</p>
</div>
{% call form_wrapper() %}
{{ textbox(form.feedback, width='1-1', hint='', rows=10) }}
{{ textbox(form.feedback, width='1-1', hint='', rows=10, autosize=True) }}
{% if not current_user.is_authenticated %}
{{ textbox(form.name, width='1-1') }}
{{ textbox(form.email_address, width='1-1') }}

View File

@@ -82,7 +82,7 @@ const javascripts = () => {
paths.src + 'javascripts/detailsPolyfill.js',
paths.src + 'javascripts/apiKey.js',
paths.src + 'javascripts/autofocus.js',
paths.src + 'javascripts/highlightTags.js',
paths.src + 'javascripts/enhancedTextbox.js',
paths.src + 'javascripts/fileUpload.js',
paths.src + 'javascripts/radioSelect.js',
paths.src + 'javascripts/updateContent.js',

View File

@@ -7,6 +7,7 @@
},
"scripts": {
"test": "gulp lint && jest --config tests/javascripts/jest.config.js tests/javascripts",
"test-watch": "jest --watch --config tests/javascripts/jest.config.js tests/javascripts",
"build": "gulp",
"watch": "gulp watch"
},

View File

@@ -346,6 +346,8 @@ def test_should_show_page_for_one_template(
assert "Template &lt;em&gt;content&lt;/em&gt; with &amp; entity" in str(
page.select_one('textarea')
)
assert page.select_one('textarea')['data-module'] == 'enhanced-textbox'
assert page.select_one('textarea')['data-highlight-placeholders'] == 'true'
assert "priority" not in str(page.select_one('main'))
mock_get_service_template.assert_called_with(SERVICE_ONE_ID, template_id, None)

View File

@@ -1,14 +1,14 @@
const helpers = require('./support/helpers.js');
beforeAll(() => {
require('../../app/assets/javascripts/highlightTags.js');
require('../../app/assets/javascripts/enhancedTextbox.js');
});
afterAll(() => {
require('./support/teardown.js');
});
describe('Highlight tags', () => {
describe('Enhanced textbox', () => {
let input;
let textarea;
@@ -38,11 +38,11 @@ describe('Highlight tags', () => {
document.body.innerHTML = `
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control textbox-highlight-textbox" data-module="highlight-tags" type="text" name="subject" id="subject" />
<input class="form-control textbox-highlight-textbox" data-module="enhanced-textbox" type="text" name="subject" id="subject" />
</div>
<div class="form-group">
<label for="template_content">Message</label>
<textarea class="form-control form-control-1-1 textbox-highlight-textbox" data-module="highlight-tags" id="template_content" name="template_content" rows="8">
<textarea class="form-control form-control-1-1 textbox-highlight-textbox" data-module="enhanced-textbox" id="template_content" name="template_content" rows="8">
</textarea>
</div>`;
@@ -125,6 +125,34 @@ describe('Highlight tags', () => {
});
describe("The element's width should match even when the textbox is initially hidden", () => {
beforeEach(() => {
let setDisplayPropertyOfFormGroups = function(property) {
Array.prototype.forEach.call(
document.getElementsByClassName('form-group'),
element => element.style.display = property
);
};
setDisplayPropertyOfFormGroups('none');
window.GOVUK.modules.start();
setDisplayPropertyOfFormGroups('block');
});
test("If the textbox is an <textarea>", () => {
backgroundEl = textarea.nextElementSibling;
expect(backgroundEl.style.width).toEqual('582px');
});
});
test("The element should be hidden from assistive technologies", () => {
expect(backgroundEl.getAttribute('aria-hidden')).toEqual('true');
@@ -167,6 +195,22 @@ describe('Highlight tags', () => {
});
test("Unless a data attribute is set to turn this feature off", () => {
textarea.textContent = "Dear ((title)) ((name))";
textarea.setAttribute('data-highlight-placeholders', 'false')
// start module
window.GOVUK.modules.start();
backgroundEl = textarea.nextElementSibling;
const highlightTags = backgroundEl.querySelectorAll('.placeholder');
expect(highlightTags.length).toEqual(0);
});
});
describe("If there is optional text in the content, its matching text in the element below should be wrapped in a highlight tag", () => {