Skip to content

Commit

Permalink
adding typeaheads to prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccar committed Mar 16, 2020
2 parents 7aa4739 + 7fdaaf3 commit 33bda30
Show file tree
Hide file tree
Showing 170 changed files with 763 additions and 463 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const inputClassLimitReached = 'input--limit-reached';
const remainingClassLimitReached = 'input__limit--reached';
const attrCharCheckRef = 'data-char-check-ref';
const attrCharCheckVal = 'data-char-check-num';

class CharCheck {
constructor(context) {
this.context = context;
this.input = this.context.querySelector('input');
this.checkElement = document.getElementById(this.input.getAttribute(attrCharCheckRef));
this.checkVal = this.input.getAttribute(attrCharCheckVal);

this.charLimitSingularMessage = this.checkElement.getAttribute('data-charcount-limit-singular');
this.charLimitPluralMessage = this.checkElement.getAttribute('data-charcount-limit-plural');

this.updateCheckReadout(null, true);
this.input.addEventListener('input', this.updateCheckReadout.bind(this));
}

updateCheckReadout(event, firstRun) {
const value = this.input.value;
const remaining = this.checkVal - value.length;
// Prevent aria live announcement when component initialises
if (!firstRun && event.inputType) {
this.checkElement.setAttribute('aria-live', 'polite');
} else {
this.checkElement.removeAttribute('aria-live');
}

this.checkRemaining(remaining);
this.setCheckClass(remaining, this.input, inputClassLimitReached);
this.setCheckClass(remaining, this.checkElement, remainingClassLimitReached);
}

checkRemaining(remaining) {
let message;
if (remaining === -1) {
message = this.charLimitSingularMessage;
this.checkElement.innerText = message.replace('{x}', Math.abs(remaining));
} else if (remaining < -1) {
message = this.charLimitPluralMessage;
this.checkElement.innerText = message.replace('{x}', Math.abs(remaining));
}
this.setShowMessage(remaining);
}

setShowMessage(remaining) {
this.checkElement.classList[remaining < 0 ? 'remove' : 'add']('u-d-no');
}

setCheckClass(remaining, element, setClass) {
element.classList[remaining < 0 ? 'add' : 'remove'](setClass);
}
}

const checkedWrapper = [...document.querySelectorAll('.js-char-check')];
if (checkedWrapper.length) {
checkedWrapper.forEach(input => new CharCheck(input));
}
5 changes: 3 additions & 2 deletions _prototypes/individual-response--your-household-v15/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import "regenerator-runtime/runtime";
*/
import './assets/modules/typeahead/typeahead';
import './assets/modules/uac/uac';
import './assets/modules/character-check';

import {
RELATIONSHIPS_STORAGE_KEY,
Expand Down Expand Up @@ -512,7 +513,7 @@ function fieldItemDisplayHack() {
$('.field__item').after('<br />');
}

function validateInputs() {
function validateInputs(testFails) {
var inputs = Array.from(document.querySelectorAll('.input'));
inputs
.filter(function(input) { return input.required })
Expand All @@ -523,7 +524,7 @@ function validateInputs() {
field = input.closest('.field'),
errorMsg = input.getAttribute('data-error-msg');

if (input.value === '') {
if (input.value === testFails || testFails === true) {
hasErrors = true;
if (!listItem.classList.contains('js-visible')) {
errorBox.classList.remove('u-d-no');
Expand Down
116 changes: 116 additions & 0 deletions _prototypes/individual-response--your-household-v15/complete copy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
title: IR - Household prototype v15
project: Individual Response
globalcss: false
layout: eq-default-extras
assetPrefixUrl: https://sdc-global-design-patterns.netlify.com
hideSaveLater: true
---

<style type="text/css">

.page_line {
margin-top: 0.5em;
border: 0.02em solid #999;
width: 100%;
margin-bottom: 1.6em;
}

.title {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 1em;
}

.person-count {
font-size: 3em;
font-weight: bold;
margin-bottom: -0.2em;
margin-top: -0.3em;
}

.smalltext {
font-size: 0.4em;
font-weight: normal;
margin-left: -0.2em;
}

.tick-spacing {
margin-left: 1.3em;
margin-bottom: 0.8em;
}

</style>
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v5.0"></script>
<link rel="stylesheet" href="../style.css"/>
<div class="page__subheader">
<div class="container">
</div>
</div>

<div class="page__container container">
<div class="grid grid--reverse">
<div class="grid__col col-4@m">
</div>
<div class="grid__col col-8@m">
<main role="main" id="main" class="page__main">
<div class="panel panel--simple panel--success u-mb-m">
<div class="panel__body">
<h1 class="u-mb-xs">
Your census has been submitted
</h1>
<p>Thank you for completing the census.
We have received your answers and you do not need to do anything else.</p>
<h2 class="u-fs-r--b u-mb-no">Your confirmation number</h2>
<code class="u-fs-xl">1098 5379 24</code>
</div>
</div>
<p class="lock u-mb-l">Your personal information is protected by law and will be kept confidential.</p>
<h2>Send this confirmation to an email address</h2>
<p>If you would like confirmation of your completed census please provide a valid email address.</p>
<form action="../complete--confirmation-sent/">
<div class="field">
<label class="label" for="email-address">
Email address
</label>
<input autocomplete="new-password" class="input input--text js-email-address"
id="email-address"
value="" />
</div>
<button type="submit" class="btn btn--small u-mb-l u-mt-s qa-btn-submit">
<span class="btn__inner">Send confirmation</span>
</button>
</form>
<hr class="u-mb-m u-mt-m">
<h2 class="u-fs-m u-mb-xs">Share</h2>
<p class="u-mb-xs">Let others know you've completed the census</p>

<div class="share-buttons">
<div class="fb-share-button u-mt-no u-pt-no u-mr-s u-fl" data-href="https://census.gov.uk/about-the-census/about-census-2021/" data-layout="button" data-size="small"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fcensus.gov.uk%2F&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>
<div style="display:inline-block; margin-top:.4rem; margin-right:1rem;">
<a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-text="I&#39;ve just completed my Census 2021 questionnaire! Start yours at:" data-url="https://census.gov.uk/" data-hashtags="census2021" data-dnt="true" data-show-count="false">Tweet</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
</div>
</main>
</div>
</div>
</div>

<script src='/js/compiled/individual-response--your-household-v15/bundle.js'></script>

<script>
$('.qa-btn-submit').on('click', function (e) {
e.preventDefault();
if( !validateEmail($('#email-address').val()) || !$('#email-address').val()) {
window.location.href = '../complete--confirmation-email-failed/?email=' + encodeURI($('#email-address').val());
} else {
window.location.href = '../complete--confirmation-sent/?email=' + encodeURI($('#email-address').val());
}
});

function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: IR - Household prototype v15
project: Individual Response
globalcss: false
layout: eq-default-extras
assetPrefixUrl: https://sdc-global-design-patterns.netlify.com
hideSaveLater: true
---

<style type="text/css">

.page_line {
margin-top: 0.5em;
border: 0.02em solid #999;
width: 100%;
margin-bottom: 1.6em;
}

.title {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 1em;
}

.person-count {
font-size: 3em;
font-weight: bold;
margin-bottom: -0.2em;
margin-top: -0.3em;
}

.smalltext {
font-size: 0.4em;
font-weight: normal;
margin-left: -0.2em;
}

.tick-spacing {
margin-left: 1.3em;
margin-bottom: 0.8em;
}

</style>
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v5.0"></script>
<link rel="stylesheet" href="../style.css"/>
<div class="page__subheader">
<div class="container">
</div>
</div>

<div class="page__container container">
<div class="grid grid--reverse">
<div class="grid__col col-4@m">
</div>
<div class="grid__col col-8@m">
<main role="main" id="main" class="page__main">
<div class="panel panel--error js-error-box u-mb-m u-d-no">
<div class="panel__header">
<div class="panel__title u-fs-r--b">This page has an error</div>
</div>
<div class="panel__body">
<p class="u-fs-r">This must be corrected to continue</p>
<ul class="list list--bare">
<li class="list__item u-fs-r">
1. <a class="js-inpagelink" href="#panel-error">Enter a valid email address, such as [email protected]</a>
</li>
</ul>
</div>
</div>
<h2>Send another confirmation email</h2>

<form action="../complete--confirmation-sent/">
<div id="email-field" class="field">
<label class="label" for="email-address">
Email address
</label>
<input autocomplete="new-password" class="input input--text js-email-address"
id="email-address"
value="" />
</div>
<button type="submit" class="btn btn--small u-mb-l u-mt-s qa-btn-submit">
<span class="btn__inner">Send confirmation</span>
</button>
</form>
<!--
<hr class="u-mb-m u-mt-m">
<h2 class="u-fs-m u-mb-xs">Share</h2>
<p class="u-mb-xs">Let others know you've completed the census</p>
<div class="share-buttons">
<div class="fb-share-button u-mt-no u-pt-no u-mr-s u-fl" data-href="https://census.gov.uk/about-the-census/about-census-2021/" data-layout="button" data-size="small"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fcensus.gov.uk%2F&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>
<div style="display:inline-block; margin-top:.4rem; margin-right:1rem;">
<a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-text="I&#39;ve just completed my Census 2021 questionnaire! Start yours at:" data-url="https://census.gov.uk/" data-hashtags="census2021" data-dnt="true" data-show-count="false">Tweet</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
</div>
-->
</main>
</div>
</div>
</div>

<script src='/js/compiled/individual-response--your-household-v15/bundle.js'></script>

<script>
var queryString = window.location.search;
var searchParams = new URLSearchParams(queryString);


$('.qa-btn-submit').on('click', function (e) {
e.preventDefault();
if( !validateEmail($('#email-address').val()) || !$('#email-address').val()) {
window.location.href = '../complete--confirmation-resend/?invalid=true';
} else {
window.location.href = '../complete--confirmation-sent/?email=' + encodeURI($('#email-address').val());
}
});

function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}

function checkErrors() {
if(searchParams.get('invalid')) {
let param = searchParams.get('true');
$('.js-error-box').removeClass('u-d-no');
$('#email-field').wrap( '<div class="panel panel--simple panel--error"><div class="panel__body"></div></div>' );
$('#email-field').before('<p id="panel-error" class="panel__error u-fs-r--b">Enter a valid email address, such as [email protected]</p>');
};
}

$(checkErrors);
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,14 @@
<main role="main" id="main" class="page__main">
<div class="panel panel--simple panel--success u-mb-m">
<div class="panel__body">
<h1 class="u-mb-xs u-fs-l ">
Confirmation email sent
<h1 class="u-mb-xs u-fs-l js-piping">
A confirmation email has been sent to {x}
</h1>
<p class="js-piping">An email confirming you have completed your census has been sent to&nbsp;<strong>{x}</strong></p>
</div>
</div>
<h2 class="u-fs-m u-mb-xs">Share</h2>
<p class="u-mb-xs">Let others know you've done your census.</p>
<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<!-- Your share button code -->
<div class="share-buttons">
<div class="fb-share-button u-mt-no u-pt-no u-mr-s u-fl"
data-href="https://census.gov.uk/about-the-census/"
data-text="I&#39;ve just completed my Census 2021 questionnaire! Start yours at:"
data-layout="button">
</div>
<div style="display:inline-block; margin-top:.4rem; margin-right:1rem;">
<a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-text="I&#39;ve just completed my Census 2021 questionnaire! Start yours at:" data-url="https://census.gov.uk/" data-hashtags="census2021" data-dnt="true" data-show-count="false">Tweet</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
</div>
<h2 class="u-fs-r--b u-mb-xs">Didn't receive an email?</h2>
<p class="u-mb-m">It can take a few minutes for the email to arrive. If it doesn't arrive, you can <a href="../complete--confirmation-resend">send another confirmation email</a>.</p>

</main>
</div>
</div>
Expand Down
Loading

0 comments on commit 33bda30

Please sign in to comment.