Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor date and duration components to work in jinja2 environments #3080

Merged
merged 33 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5591811
formatting
rmccar Mar 8, 2024
96ff762
standardise date and duration and remove shared logic
rmccar Mar 8, 2024
3f90313
add multiple input fields macro
rmccar Mar 8, 2024
16f48be
more standardising
rmccar Mar 8, 2024
7985261
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 11, 2024
4ef0c33
fix tests
rmccar Mar 11, 2024
65654f1
fix tests
rmccar Mar 11, 2024
7f098e4
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 11, 2024
9f5bf89
fix tests
rmccar Mar 11, 2024
089fd64
fix tests
rmccar Mar 11, 2024
b7dc7dd
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 11, 2024
2847e06
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 11, 2024
e6f6406
formatting
rmccar Mar 12, 2024
ea35411
Merge branch 'fix-date-duration-inputs-in-jinja' of github.com:ONSdig…
rmccar Mar 12, 2024
5dcc0ab
add error tests to duration
rmccar Mar 12, 2024
fb5e721
fix test
rmccar Mar 12, 2024
13e59b8
fix test
rmccar Mar 12, 2024
0f29f96
remove whitespace
rmccar Mar 12, 2024
e0c5b5d
remove whitespace
rmccar Mar 12, 2024
7d65e25
fix test
rmccar Mar 12, 2024
c0e7f52
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 12, 2024
c23d012
fix single input issue
rmccar Mar 12, 2024
941e61b
Merge branch 'fix-date-duration-inputs-in-jinja' of github.com:ONSdig…
rmccar Mar 12, 2024
31d3f17
add test coverage for single inputs
rmccar Mar 12, 2024
e935232
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 12, 2024
d4c8733
update new examples
rmccar Mar 13, 2024
54d13dc
Merge branch 'fix-date-duration-inputs-in-jinja' of github.com:ONSdig…
rmccar Mar 13, 2024
650d2bc
update snapshots
rmccar Mar 13, 2024
10fe3dc
update snapshots
rmccar Mar 13, 2024
3f6966c
Merge branch 'main' into fix-date-duration-inputs-in-jinja
rmccar Mar 13, 2024
0d302c2
update snapshots
rmccar Mar 13, 2024
fc8e5c8
remove old snapshots
rmccar Mar 13, 2024
ba7a3cb
Merge branch 'fix-date-duration-inputs-in-jinja' of github.com:ONSdig…
rmccar Mar 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ yarn build

- "render whitespace" set to "all"
- "insert spaces when pressing tab" checked
- "trim trailing whitespace" checked
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 0 additions & 86 deletions src/components/date-field-input/_macro.njk

This file was deleted.

140 changes: 71 additions & 69 deletions src/components/date-input/_macro.njk
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% from "components/date-field-input/_macro.njk" import onsDateFieldInput %}
{% from "components/multiple-input-fields/_macro.njk" import onsMultipleInputFields %}
{% from "components/input/_macro.njk" import onsInput %}

{% macro onsDateInput(params) %}
{% set exclusiveClass = " ons-js-exclusive-group-item" if params.mutuallyExclusive else "" %}
{%- set numberOfFields = 0 -%}
{%- set numberOfFields = 0 -%}

{%- if params.day -%}
{%- set numberOfFields = numberOfFields + 1 -%}
Expand All @@ -16,82 +17,83 @@
{%- set numberOfFields = numberOfFields + 1 -%}
{%- endif -%}

{% set fields = [] %}
{% if params.day %}
{% set dayField = {
"id": params.id + "-day",
"label": {
"text": params.day.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.day.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-day-label"
},
"name": params.day.name,
"width": "2",
"min": 1,
"max": 31,
"maxLength": 2,
"attributes": params.day.attributes,
"value": params.day.value,
"classes": (" ons-input--error" if (params.error and params.day.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default("")
} %}
{% set fields = (fields.push(dayField), fields) %}
{% endif %}
{% if params.month %}
{% set monthField = {
"id": params.id + "-month",
"label": {
"text": params.month.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.month.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-month-label"
},
"name": params.month.name,
"width": "2",
"min": 1,
"max": 12,
"maxLength": 2,
"attributes": params.month.attributes,
"value": params.month.value,
"classes": (" ons-input--error" if (params.error and params.month.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default("")
} %}
{% set fields = (fields.push(monthField), fields) %}
{% endif %}
{% if params.year %}
{% set yearField = {
"id": params.id + "-year",
"label": {
"text": params.year.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.year.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-year-label"
},
"name": params.year.name,
"width": "4",
"min": 1000,
"max": 3000,
"maxLength": 4,
"attributes": params.year.attributes,
"value": params.year.value,
"classes": (" ons-input--error" if (params.error and params.year.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default("")
} %}
{% set fields = (fields.push(yearField), fields) %}
{% endif %}
{% set fields %}
{% if params.day %}
{{ onsInput({
"id": params.id + "-day",
"type": "number",
"name": params.day.name,
"classes": (" ons-input--error" if (params.error and params.day.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default(""),
"width": "2",
"min": 1,
"max": 31,
"maxLength": 2,
"attributes": params.day.attributes,
"label": {
"text": params.day.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.day.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-day-label"
},
"value": params.day.value,
"error": params.error if numberOfFields < 2
}) }}
{% endif %}

{{ onsDateFieldInput({
{% if params.month %}
{{ onsInput({
"id": params.id + "-month",
"type": "number",
"name": params.month.name,
"classes": (" ons-input--error" if (params.error and params.month.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default(""),
"width": "2",
"min": 1,
"max": 12,
"maxLength": 2,
"attributes": params.month.attributes,
"label": {
"text": params.month.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.month.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-month-label"
},
"value": params.month.value,
"error": params.error if numberOfFields < 2
}) }}
{% endif %}

{% if params.year %}
{{ onsInput({
"id": params.id + "-year",
"type": "number",
"name": params.year.name,
"classes": (" ons-input--error" if (params.error and params.year.error) or (params.error and not params.day.error and not params.month.error and not params.year.error) else "") + exclusiveClass | default(""),
"width": "4",
"min": 1000,
"max": 3000,
"maxLength": 4,
"attributes": params.year.attributes,
"label": {
"text": params.year.label.text if numberOfFields > 1 else params.legendOrLabel,
"description": params.year.label.description if numberOfFields > 1 else params.description,
"id": params.id + "-year-label"
},
"value": params.year.value,
"error": params.error if numberOfFields < 2
}) }}
{% endif %}
{% endset %}

{{ onsMultipleInputFields({
"id": params.id,
"legendOrLabel": params.legendOrLabel,
"description": params.description,
"mutuallyExclusive": params.mutuallyExclusive,
"fields": fields,
"numberOfFields": numberOfFields,
"fields": fields,
"error": params.error,
"legend": params.legendOrLabel,
"dontWrap": params.dontWrap,
"legendClasses": params.legendClasses,
"legendIsQuestionTitle": params.legendIsQuestionTitle,
"classes": params.classes,
"exclusiveOptions": params.mutuallyExclusive.exclusiveOptions,
"or": params.mutuallyExclusive.or,
"deselectMessage": params.mutuallyExclusive.deselectMessage,
"deselectGroupAdjective": params.mutuallyExclusive.deselectGroupAdjective,
"deselectExclusiveOptionAdjective": params.mutuallyExclusive.deselectExclusiveOptionAdjective
"legendClasses": params.legendClasses,
"legendIsQuestionTitle": params.legendIsQuestionTitle
}) }}
{% endmacro %}
25 changes: 20 additions & 5 deletions src/components/date-input/_macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const EXAMPLE_YEAR_FIELD_WITH_ERROR = {
},
};

const EXAMPLE_YEAR_FIELD_WITH_NO_ERROR = {
const EXAMPLE_YEAR_FIELD_WITH_ERROR_FALSE = {
year: {
label: {
text: 'Year',
Expand Down Expand Up @@ -148,9 +148,9 @@ const EXAMPLE_DATE_MULTIPLE_FIELDS_WITH_ERROR = {
...EXAMPLE_YEAR_FIELD_WITH_ERROR,
};

const EXAMPLE_DATE_MULTIPLE_FIELDS_WITH_NO_ERROR = {
const EXAMPLE_DATE_SINGLE_FIELD_WITH_ERROR_FALSE = {
...EXAMPLE_DATE_INPUT_BASE,
...EXAMPLE_YEAR_FIELD_WITH_NO_ERROR,
...EXAMPLE_YEAR_FIELD_WITH_ERROR_FALSE,
};

describe('macro: date input', () => {
Expand All @@ -177,6 +177,7 @@ describe('macro: date input', () => {
max: 31,
maxLength: 2,
classes: '',
error: '',
label: {
text: 'Day',
description: 'The day',
Expand Down Expand Up @@ -204,6 +205,7 @@ describe('macro: date input', () => {
max: 12,
maxLength: 2,
classes: '',
error: '',
label: {
text: 'Month',
description: 'The month',
Expand Down Expand Up @@ -231,6 +233,7 @@ describe('macro: date input', () => {
max: 3000,
maxLength: 4,
classes: '',
error: '',
label: {
text: 'Year',
description: 'The year',
Expand All @@ -249,6 +252,12 @@ describe('macro: date input', () => {
expect($(div).hasClass('ons-field-group')).toBe(true);
});

it('has the correct number of inputs', () => {
const $ = cheerio.load(renderComponent('date-input', EXAMPLE_DATE_MULTIPLE_FIELDS));
const $inputs = $('.ons-input');
expect($inputs.length).toBe(3);
});

it('has the expected `fieldset` output', () => {
const faker = templateFaker();
const fieldsetSpy = faker.spy('fieldset');
Expand Down Expand Up @@ -356,6 +365,12 @@ describe('macro: date input', () => {
});
});

it('has the correct number of inputs', () => {
const $ = cheerio.load(renderComponent('date-input', EXAMPLE_DATE_SINGLE_FIELD));
const $inputs = $('.ons-input');
expect($inputs.length).toBe(1);
});

it('has the expected `error` output', () => {
const faker = templateFaker();
const errorSpy = faker.spy('error');
Expand All @@ -371,7 +386,7 @@ describe('macro: date input', () => {
});
});

describe('mode: multiple fields with single field error', () => {
describe('mode: multiple fields with errors', () => {
it('passes jest-axe checks', async () => {
const $ = cheerio.load(renderComponent('date-input', EXAMPLE_DATE_MULTIPLE_FIELDS_WITH_SINGLE_ERROR));

Expand All @@ -394,7 +409,7 @@ describe('macro: date input', () => {
});

it('does not provide error class when error parameter set to false', async () => {
const $ = cheerio.load(renderComponent('date-input', EXAMPLE_DATE_MULTIPLE_FIELDS_WITH_NO_ERROR));
const $ = cheerio.load(renderComponent('date-input', EXAMPLE_DATE_SINGLE_FIELD_WITH_ERROR_FALSE));
const $errorContent = $('.ons-input--error');

expect($errorContent.length).toBe(0);
Expand Down
Loading
Loading